HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

How to add a named constraint in Oracle SQL?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
howsqlnamedconstraintoracleadd

Problem

I have a table. I need to add a NOT NULL constraint to one of the columns. This is my best attempt at a solution:

alter table clookup add CONSTRAINT lookup_9 NOT NULL (clookup_col)

This produces ERROR at line 1: ORA-00904: : invalid identifier on the NOT NULL part. Why?

Solution

Oracle 12c:

Table

create table clookup ( clookup_col varchar2( 64 ) );


Add a NOT NULL constraint

alter table clookup
modify ( clookup_col constraint lookup_9 not null ) ;


Optional

select 
  table_name
, constraint_name
, constraint_type
from user_constraints
where table_name = 'CLOOKUP' ;

-- result
TABLE_NAME  CONSTRAINT_NAME  CONSTRAINT_TYPE  
CLOOKUP     LOOKUP_9         C


Addendum

It may not always be necessary to use the ALTER TABLE ... MODIFY ... technique. There is a "special rule" for defining NOT NULL constraints (see documentation):


You can define constraints syntactically in two ways:


As part of the definition of an individual column or attribute. This
is called inline specification.


As part of the table definition. This is called out-of-line
specification.


NOT NULL constraints must be declared inline. All other constraints
can be declared either inline or out of line.

Also (here):


Use the ALTER TABLE statement to alter the definition of a
nonpartitioned table, a partitioned table, a table partition, or a
table subpartition.

The following examples show what does NOT work: just adding a NOT NULL constraint (not a column) to a table, via ALTER TABLE ... ADD ... (out-of-line).

-- Oracle 12c
create table T ( name varchar2( 32 ) ) ;

alter table T 
add constraint name_nn not null ( name ) ;
-- ORA-00904: : invalid identifier

alter table T
add ( 
  n number
, constraint name_nn not null ( name )  -- out-of-line
) ;
-- ORA-00904: : invalid identifier


ALTER TABLE ... ADD ... can be used for other constraints eg UNIQUE or PRIMARY KEY "out-of-line" (as in the examples above), though. It also works when we want to add NOT NULL constraints "inline" ie when specifying a column and a constraint eg

create table T ( name varchar2( 32 ) ) ;

alter table T
add n number not null ;
-- Table T altered

-- or
alter table T
add n number constraint number_nn not null ;
-- Table T altered

Code Snippets

create table clookup ( clookup_col varchar2( 64 ) );
alter table clookup
modify ( clookup_col constraint lookup_9 not null ) ;
select 
  table_name
, constraint_name
, constraint_type
from user_constraints
where table_name = 'CLOOKUP' ;

-- result
TABLE_NAME  CONSTRAINT_NAME  CONSTRAINT_TYPE  
CLOOKUP     LOOKUP_9         C
-- Oracle 12c
create table T ( name varchar2( 32 ) ) ;

alter table T 
add constraint name_nn not null ( name ) ;
-- ORA-00904: : invalid identifier

alter table T
add ( 
  n number
, constraint name_nn not null ( name )  -- out-of-line
) ;
-- ORA-00904: : invalid identifier
create table T ( name varchar2( 32 ) ) ;

alter table T
add n number not null ;
-- Table T altered

-- or
alter table T
add n number constraint number_nn not null ;
-- Table T altered

Context

StackExchange Database Administrators Q#208659, answer score: 8

Revisions (0)

No revisions yet.