简体   繁体   中英

SQL Server 2008, Normalization, and Primary Key

I have a query that I have been searching for a while on but have been unable to sufficiently answer.

I am inserting records into my 2008 SQL Server for my SQL class and I had to normalize the data into the third form. I believe I did this correctly and the example given to me was to separate records into their own atomic rows (among the other requirements). This produced several rows with the same OrderID (which is my primary key) and split the parts of the order up into said rows.

Example:

OrderID ItemNumber Price  QTY
101     1          $1.00  5
101     2          $9.00  4
101     3          $5.00  3

However how can I insert these rows with my PK (OrderID) duplicating itself so many times? I have tried once already and it rejects every INSERT INTO other than the first instance that isn't duplicated (gives me a PK constraint error). I apologize if this is a stupid question but I am trying hard to understand. The little example I posted above is EXACTLY how my instructor wanted it as well.

So as you may or may not have learned in class, one of the constraints of a primary key is that it must be unique.

From a more logical perspective, the primary key should in essence define what each row represents to a business user. In this case, each row represents an item, not an order. Each item is defined by the order number, and the item number of that order.

Your primary key should be a composite of OrderID and ItemNumber.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM