1

I know that it is possible and even encouraged to add primary key constraints and foreign key constraints after the creation of the table so that the programmer has control over the names of the constraints. If the programmer adds these constraints when the table is created (without specific add constraint syntax) then the Database Engine gives a name to these constraints that is comprised of random characters. The name that the database engine gives to these 'non-explicit' constraints depends on the implementation.

However my question is: Does ANY kind of constraint have a specific name in the database engine? Even the NOT NULL constraints ? Can those simple constraints like NOT NULL be referenced at a later time in an "ALTER TABLE" command?

Jack Douglas
40.6k16 gold badges106 silver badges179 bronze badges
asked Mar 26, 2017 at 21:31
1
  • Please ask any follow-up or tangentially related questions separately Commented Mar 27, 2017 at 14:36

2 Answers 2

3

I know that it is possible and even encouraged to add primary key constraints and foreign key constraints after the creation of the table so that the programmer has control over the names of the constraints. If the programmer adds these constraints when the table is created (without specific add constraint syntax) then the Database Engine gives a name to these constraints that is comprised of random characters.

Are you saying constraint names can't be given at table creation time? That isn't correct:

create table foo(id integer primary key);
create table bar(id integer constraint pk_bar primary key);
select table_name, constraint_name from user_constraints;
TABLE_NAME | CONSTRAINT_NAME
:--------- | :--------------
FOO | SYS_C007856 
BAR | PK_BAR 

dbfiddle here

However my question is: Does ANY kind of constraint have a specific name in the database engine? Even the NOT NULL constraints ? Can those simple constraints like NOT NULL be referenced at a later time in an "ALTER TABLE" command?

It's exactly the same for NOT NULL constraints:

create table foo(id integer not null);
create table bar(id integer constraint nn_bar_id not null);
select table_name, constraint_name from user_constraints;
TABLE_NAME | CONSTRAINT_NAME
:--------- | :--------------
FOO | SYS_C007860 
BAR | NN_BAR_ID 

dbfiddle here

Can those simple constraints like NOT NULL be referenced at a later time in an "ALTER TABLE" command?

Of course, just like any other constraint:

alter table bar drop constraint nn_bar_id;

dbfiddle here

answered Mar 27, 2017 at 14:24
2

Apart from using named constraints, you can add/remove a NOT NULL constraint in a column without any name.

Let's imagine we have the following table (Oracle):

 CREATE TABLE t
 (
 a numeric PRIMARY KEY,
 b numeric
 ) ;
 INSERT INTO t (a,b) VALUES (1, 2);

If you want to change the table and force column b to hold only NOT NULL values, you can do it by using the following statement (that doesn't require to name the constraint):

 ALTER TABLE t 
 MODIFY (b NOT NULL) ;

If you were to try this when there's already a NULL value on column b, you'd get the error:

ORA-02296: cannot enable (xxxxxxxxxx) - null values found

If you would like to change back the column to allow NULL values, you can do it with the following DDL, which doesn't require that you know any constraint name:

 ALTER TABLE t
 MODIFY (b NULL) ;

dbfiddle here

answered Mar 26, 2017 at 21:48
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.