How can I make 2 fields in one primary key with SQL?

左心房为你撑大大i 提交于 2019-12-13 07:52:35

问题


OrderID and ProductID in my OrderID table, for example, act as a single primary key. I'm just not sure how to do this with SQL.

Example:

CREATE TABLE [Order Details]
(
[OrderID] [int] IDENTITY(1,1) NOT NULL,
[ProductID] [int] NOT NULL,
...
)

Can I simply add PRIMARY KEY to both lines after NOT NULL to make them both the primary key?


回答1:


You need to declare the columns participating in the primary key at the end

CREATE TABLE [Order Details]
(
[OrderID] [int] NOT NULL,
[ProductID] [int] NOT NULL,
...,
PRIMARY KEY(OrderID, ProductID)
)



回答2:


SQL syntax:

ALTER TABLE <TABLE> ADD CONSTRAINT <PK_NAME> PRIMARY KEY (<COLUMN1>,<COLUMN2>);



回答3:


In SQL server Identity columns are not automatically primary keys, so you just have to do this:

CREATE TABLE [Order Details]
(
[OrderID] [int] IDENTITY(1,1) NOT NULL,
[ProductID] [int] NOT NULL,
...
constraint pk_OrderProductID primary key (OrderID,ProductID)
)



回答4:


OK first , IF Orderid is a FK to the Order table as it should be, then you do not want this to be an identity column in the order details table. An identity will give a different value to each detail line and you will want the orderid to be associated with all the products ordered in the same order. Further, if you try to relate them, then they would relate to the wrong order because you are not pulling the value from the order table. And since order details will have many more records than the order table, you will quickly run out of ids that have been used in the order table and be unable to insert records at all.

A compound PK between Orderid and product id is good only if you can never have the same product id in the details table twice. For instance, I want to order 1 magazine sent to me and the same magazine as a gift to my mother in the same order. Both would have the same product ID but different details and so might end up needing to have different lines in the detail table. Same as if I wanted 1 pink sweater and 1 black one of the same style. It might depend on whether your product ids are different if some of the variable details are different or not. Or maybe I want two but in two different sizes (so I can give one as gift and keep one). So whether this is a good PK is not something we can tell you, it depends on the meaning of the data and business rules which only your business can answer for you. Depending on how your products will be stored, how your product ids are determined (you have limited resources for changing them if they are set by other vendors for instance) will depend on whether you can have anything except an autogenerated id as a pk.

If so - you still want to do create a PK even if you have no natural keys; it is difficult to do updates if the database cannot determine which record to update. In this case you add an OrderDrtailId that is autogenerated. Make sure to still retain the Orderid and ProductID fields that are FKs to their respective parent tables.




回答5:


Order Details [ Pk OrderID] [ FK CustomerID] Product ID ( 1:many) customer can have one order ID TO many ProductID. SQL for UnitpriceГpurchase= (Fields) (1:many).



来源:https://stackoverflow.com/questions/34319747/how-can-i-make-2-fields-in-one-primary-key-with-sql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!