[英]SQL - 2 Primary Keys in 1 Table
I have been researching this issue for several weeks now and despite the resources on this site and others, I cannot find any proper examples to lead me in the right direction. 我已经研究了这个问题好几个星期了,尽管这个站点和其他站点上有很多资源,但我找不到合适的例子来引导我朝着正确的方向前进。 For an assignment, I am trying to create a table in SQL containing two PKs.
对于一项任务,我试图在SQL中创建一个包含两个PK的表。 I now understand that this isn't possible, but I cannot seem to understand why, mainly because it has been required of me.
我现在知道这是不可能的,但是我似乎无法理解为什么,主要是因为这是我所必需的。 I attempted to change my code and separate the keys but I was told that was incorrect and to stick with the two PKs.
我试图更改代码并分离键,但被告知这是不正确的,并坚持使用两个PK。 Any assistance or direction would be greatly appreciated.
任何帮助或指示将不胜感激。 Here is my current code for the table:
这是我当前的表格代码:
SQL> CREATE TABLE solds_ss /* This is creating the SOLDS table from the instance charts */
2 (
3 invoice_no NUMBER(7)
4 CONSTRAINT invoice_no_pk PRIMARY KEY,
5 CONSTRAINT invoice_no_fk
6 FOREIGN KEY (invoice_no)
7 REFERENCES invoices_ss (invoice_no),
8 item_no NUMBER(6)
9 CONSTRAINT item_no_pk PRIMARY KEY,
10 CONSTRAINT item_no_nn NOT NULL,
11 CONSTRAINT item_no_fk
12 FOREIGN KEY (item_no)
13 REFERENCES items_ss (item_no),
14 item_qty_sold NUMBER(3)
15 CONSTRAINT item_qty_sold_nn NOT NULL,
16 item_price NUMBER(6,2)
17 CONSTRAINT item_price_nn NOT NULL,
18 item_tracking_no VARCHAR2(30)
19 CONSTRAINT item_tracking_no_nn NOT NULL
20 );
CONSTRAINT item_no_pk PRIMARY KEY,
*
ERROR at line 9:
ORA-02260: table can have only one primary key
Following up on my comment, I suspect that what you want is a composite primary key - two columns that together make up the primary key/uniquely identify a row in the table. 根据我的评论,我怀疑您想要的是一个复合主键-两列共同组成主键/唯一标识表中的一行。 This is very different to "having two primary keys" - the value of each column might be repeated, but the combination of column 1 and column 2 must be unique:
这与“具有两个主键”非常不同-每个列的值都可以重复,但是列1和列2的组合必须唯一:
InvoiceNo ItemNo
Inv1 Itm1
Inv1 Itm2
Inv2 Itm1
Inv2 Itm2
The table data above wouldn't be allowed if InvoiceNo was a primary key, or ItemNo was a primary key (each column contains repeated values when viewed in isolation), but is allowed if the primary key is defined as InvoiceNo and ItemNo in combination 如果InvoiceNo是主键,或者ItemNo是主键,则不允许使用上面的表数据(如果单独查看,每列包含重复的值),但是如果主键组合定义为InvoiceNo和ItemNo,则不允许
Hence I think the sql you're looking for is more like: 因此,我认为您正在寻找的sql更像是:
CREATE TABLE solds_ss /* This is creating the SOLDS table from the instance charts */
(
invoice_no NUMBER(7) NOT NULL,
item_no NUMBER(6) NOT NULL,
item_qty_sold NUMBER(3) NOT NULL,
item_price NUMBER(6,2) NOT NULL,
item_tracking_no VARCHAR2(30) NOT NULL,
CONSTRAINT invoice_no_fk
FOREIGN KEY (invoice_no)
REFERENCES invoices_ss (invoice_no),
CONSTRAINT item_no_fk
FOREIGN KEY (item_no)
REFERENCES items_ss (item_no),
--composite pk
CONSTRAINT solds_ss_pk PRIMARY KEY (invoice_no, item_no)
);
You cannot create two primary keys. 您不能创建两个主键。 Primary keys have three properties:
主键具有三个属性:
It is the third property that prevents you from having two of them. 这是防止您拥有其中两个的第三个属性。 You can declare one column or groups of columns as both
unique
and not null
. 您可以将一列或一组列声明为
unique
not null
。 But only one such set of keys can be a primary key. 但是只有一组这样的键可以是主键。
Primary key constraint designates a column as the primary key of a table or view.
主键约束将一列指定为表或视图的主键 。 A composite primary key designates a combination of columns as the primary key .
复合主键将列的组合指定为主键 。
Oracle doesn't allow multiple primary keys on a table or view. Oracle不允许在一个表或视图上使用多个主键。 Look for Primary key constraints https://docs.oracle.com/cd/B19306_01/server.102/b14200/clauses002.htm
查找主键约束 https://docs.oracle.com/cd/B19306_01/server.102/b14200/clauses002.htm
A primary key has three properties: 主键具有三个属性:
So although Oracle will only allow you to define one primary key constraint per table, you can easily make a second, third, or fourth column with all the properties of a primary key by: 因此,尽管Oracle仅允许您为每个表定义一个主键约束,但是您可以通过以下方式轻松地创建具有主键的所有属性的第二,第三或第四列:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.