那么,最终它是一个复合PK还是不是?
是。主键一起定义在两列上。该职位将告诉你领导专栏。 USER_ID_PK
主键约束在USERID
上定义为领先列,然后是USERREF
。
为什么USERID同时具有NOT NULL约束和PK约束?
因为有人在主键列创建NOT NULL约束明确。
让我们来看看。
单主键
SQL> create table t(a number primary key);
Table created.
SQL> SELECT a.table_name,
2 b.column_name,
3 a.constraint_type,
4 b.position
5 FROM user_constraints a
6 JOIN user_cons_columns b
7 ON a.owner = b.owner
8 AND a.constraint_name = b.constraint_name
9 AND a.table_name = b.table_name
10 AND a.constraint_type IN ('P', 'C');
TABLE_NAME COLUMN_NAM C POSITION
---------- ---------- - ----------
T A P 1
复合主键
SQL> drop table t purge;
Table dropped.
SQL> create table t(a number, b number);
Table created.
SQL> alter table t add constraint t_pk PRIMARY KEY(a,
Table altered.
SQL> SELECT a.table_name,
2 b.column_name,
3 a.constraint_type,
4 b.position
5 FROM user_constraints a
6 JOIN user_cons_columns b
7 ON a.owner = b.owner
8 AND a.constraint_name = b.constraint_name
9 AND a.table_name = b.table_name
10 AND a.constraint_type IN ('P', 'C');
TABLE_NAME COLUMN_NAM C POSITION
---------- ---------- - ----------
T A P 1
T B P 2
主键和NOT NULL
SQL> drop table t purge;
Table dropped.
SQL> create table t(a number primary key not null);
Table created.
SQL> SELECT a.table_name,
2 b.column_name,
3 a.constraint_type,
4 b.position
5 FROM user_constraints a
6 JOIN user_cons_columns b
7 ON a.owner = b.owner
8 AND a.constraint_name = b.constraint_name
9 AND a.table_name = b.table_name
10 AND a.constraint_type IN ('P', 'C');
TABLE_NAME COLUMN_NAM C POSITION
---------- ---------- - ----------
T A C
T A P 1
俺们这些问题大部分都是(可能):因为有人用这种方式创建它(特别是名称和NN约束) –
谢谢!那么它是一个复合'PK'? – monster