Oracle:查找主鍵——PK和NN

[英]Oracle: Finding Primary Keys - PK and NN


I am working with a database and was told the Table USERS has a single Primary Key - USERID. Using ALL_CONS_COLS gives me the following:

我正在處理一個數據庫,有人告訴我表用戶只有一個主鍵USERID。使用ALL_CONS_COLS給了我以下信息:

OWNER   | TABLE_NAME | CONSTRAINT_NAME | COLUMN_NAME | POSITION
----------------------------------------------------------------
MONSTER |   USERS    |   USER_ID_PK    |   USERREF   |   2
MONSTER |   USERS    |   USER_ID_NN    |   USERID
MONSTER |   USERS    |   USER_ID_PK    |   USERID    |   1

This suggests the PK is a composite comprised of: USERREF and USERID.

這說明PK是由USERREF和USERID組成的組合。

There are a few things I do not follow:

有幾件事我不明白:

Why does USERID have both a NOT NULL constraint and also a PK constraint? PK by definition means NN. Secondly, as USERID has a NN constraint, why does USERREF not have a NN constraint? Thirdly, why is CONSTRAINT_NAME "suggesting" the PK is USER_ID (USERID), i.e., why is the constraint named after a single column; wouldn't USERS_PK be a better constraint name?

為什么USERID既具有非空約束,又有一個PK約束?PK的定義是NN。其次,由於USERID有NN約束,為什么USERREF沒有NN約束?第三,為什么CONSTRAINT_NAME "表示PK為USER_ID (USERID),即。,為什么約束以單個列命名;USERS_PK不是更好的約束名嗎?

So, ultimately, is it a Composite PK or not?

那么,最終,它是一個復合PK還是不是?

1 个解决方案

#1


2  

So, ultimately, is it a Composite PK or not?

那么,最終,它是一個復合PK還是不是?

Yes. The primary key is defined on two columns together. The position will tell you the leading column. USER_ID_PK primary key constraint is defined on USERID as leading column followed by USERREF.

是的。主鍵一起定義在兩列上。這個職位會告訴你最重要的一欄。USER_ID_PK主鍵約束在USERID上定義為前導列,后跟USERREF。

Why does USERID have both a NOT NULL constraint and also a PK constraint?

為什么USERID同時具有非空約束和PK約束?

Because someone created a NOT NULL constraint on the primary key column explicitly.

因為有人顯式地在主鍵列上創建了NOT NULL約束。

Let's test and see.

讓我們來測試看看。

Single primary key

單一的主鍵

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

Composite primary key

復合主鍵

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

Primary key and 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

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/10/14/72fead1d08cddacd058cb2d183e46a7f.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com