snippetMinor
How to add a named constraint in Oracle SQL?
Viewed 0 times
howsqlnamedconstraintoracleadd
Problem
I have a table. I need to add a
This produces
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
Add a NOT NULL constraint
Optional
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).
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
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 CAddendum
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 identifierALTER 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 alteredCode 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 identifiercreate 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 alteredContext
StackExchange Database Administrators Q#208659, answer score: 8
Revisions (0)
No revisions yet.