那麼,最終它是一個複合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