sqlserver創(chuàng)建聯(lián)合主鍵 SQL Server怎么建立聯(lián)合主鍵?
SQL Server怎么建立聯(lián)合主鍵?1、SQL server創(chuàng)建聯(lián)合主鍵的方法:1。創(chuàng)建表時寫入。語句如下:create table name(field name 1,int not null,f
SQL Server怎么建立聯(lián)合主鍵?
1、SQL server創(chuàng)建聯(lián)合主鍵的方法:1。創(chuàng)建表時寫入。語句如下:create table name(field name 1,int not null,field name 2,nvarchar(13)not null primary key(field name 1,field name 2),field name 3 field name n)2。創(chuàng)建表后,語句如下:alter table name with nocheck add constraint[PK]utable name]primary key nonclustered([field name 1],[field name 2])2。聯(lián)合主鍵的優(yōu)點是:使用兩個字段(或多個字段,它們與兩個字段具體組合)來確定一個記錄。這表明這兩個字段不是唯一的,并且這兩個字段可以分別重復(fù)。此設(shè)置的優(yōu)點是可以直觀地看到重復(fù)字段中的記錄數(shù)。3、 聯(lián)合主鍵的使用:例如,order表中有許多字段。通常,您只需要有一個訂單號billuno就可以用作主鍵。但是,要求可以有相同訂單號的補充訂單。此時,不允許單獨使用訂單號,因為會有重復(fù)。然后您可以使用另一個訂單序列號bill作為區(qū)別。將bill No和bill Set SEQ作為聯(lián)合主鍵。即使是比爾,不,比爾,和SEQ不一樣也沒關(guān)系。擴(kuò)展數(shù)據(jù):示例如下:主鍵A和主鍵B構(gòu)成聯(lián)合主鍵。主鍵A和主鍵B的數(shù)據(jù)可以完全相同。聯(lián)合是指由主鍵A和主鍵B組成的聯(lián)合主鍵是唯一的。在下面的示例中,主鍵a的數(shù)據(jù)是1,主鍵B的數(shù)據(jù)是1。實際上,聯(lián)合主鍵是11,這是唯一的值。絕對不允許使用唯一值11。(這是多對多關(guān)系)主鍵a數(shù)據(jù)主鍵B數(shù)據(jù)1 1 2 3 3主鍵a和主鍵B的最大聯(lián)合主鍵值是11 12 13 21 22 23 31 32 33
主鍵是數(shù)據(jù)庫表的一個重要屬性。主鍵的建立可以避免表中存在相同的記錄,即表中主鍵的記錄值是唯一的。建立主鍵有兩種方式:一種是在數(shù)據(jù)庫提供的GUI環(huán)境中建立主鍵,另一種是通過執(zhí)行SQL語句建立主鍵,如下所述。1數(shù)據(jù)庫提供的內(nèi)置GUI環(huán)境(以SQL7為例)。輸入表信息后,按CTRL鍵同時選中多行,然后單擊上面的主鍵按鈕。2通過執(zhí)行SQL語句設(shè)置。有兩種方法:一種是在創(chuàng)建表的語句中直接寫入,另一種是在創(chuàng)建表后更改表結(jié)構(gòu)。在表創(chuàng)建語句中,直接寫入:create table name(field name 1intnotnull,field name 2nvarchar(13)notnullprimarykey(field name 1,field name 2),field name 3 field name n)創(chuàng)建表后,更改表結(jié)構(gòu):create table name(field name 1intnotnull,field name 2nvarchar(13)notnull,field name 3 field名稱n)使用checkadd constraint[PK]uu Table name]primarykeynoclustered([field name 1],[field name 2])go可以參考,互聯(lián)網(wǎng)上有很多相關(guān)信息。