SQL> desc t
Name Null? Type
----------------------------------------- -------- -----------
A NUMBER(38)
B NUMBER(38)
C NUMBER(38)
D NUMBER(38)
SQL> alter table t add constraint pk_t primary key (a,b);
Table altered.
SQL> desc t
Name Null? Type
----------------------------------------- -------- ----------------
A NOT NULL NUMBER(38)
B NOT NULL NUMBER(38)
C NUMBER(38)
D NUMBER(38)
可以看到A、B两个列都自动改为了NOT NULL
SQL> alter table t modify (a int null);
alter table t modify (a int null)
*
ERROR at line 1:
ORA-01451: column to be modified to NULL cannot be modified to NULL
可以看到,列A不允许改为 NULL
SQL> alter table t drop constraint pk_t;
Table altered.
SQL> alter table t add constraint uk_t_1 unique (a,b);
Table altered.
SQL> desc t
Name Null? Type
----------------------------------------- -------- -----------
A NUMBER(38)
B NUMBER(38)
C NUMBER(38)
D NUMBER(38)
SQL> desc t
Name Null? Type
----------------------------------------- -------- ----------
A NUMBER(38)
B NOT NULL NUMBER(38)
C NUMBER(38)
D NUMBER(38)
再做如下的实验:
SQL> drop table t;
Table dropped.
SQL> create table t (a int,b int,c int,d int);
Table created.
SQL> alter table t add constraint uk_t_1 unique (a,b);
Table altered.
SQL> alter table t add constraint uk_t_2 unique (c,d);
Table altered.
可以看到可以增加两个UNIQUE KEY。看看能不能增加两个主键:
SQL> alter table t add constraint pk_t primary key (c);
Table altered.
SQL> alter table t add constraint pk1_t primary key (d);
alter table t add constraint pk1_t primary key (d)
*
ERROR at line 1:
ORA-02260: table can have only one primary key
由此可以看到一个表只能有一个主键。
SQL> alter table t drop constraint pk_t;
Table altered.
SQL> insert into t (a ,b ) values (null,null);
1 row created.
SQL> /
1 row created.
SQL> insert into t (a ,b ) values (null,1);
1 row created.
SQL> /
insert into t (a ,b ) values (null,1)
*
ERROR at line 1:
ORA-00001: unique constraint (SYS.UK_T_1) violated
SQL> insert into t (a ,b ) values (1,null);
1 row created.
SQL> /
insert into t (a ,b ) values (1,null)
*
ERROR at line 1:
ORA-00001: unique constraint (SYS.UK_T_1) violated