oracle聯(lián)合主鍵怎么創(chuàng)建 SQL Server怎么建立聯(lián)合主鍵?
SQL Server怎么建立聯(lián)合主鍵?1、SQL server創(chuàng)建聯(lián)合主鍵的方法:1。創(chuàng)建表時(shí)寫(xiě)入。語(yǔ)句如下:create table name(field name 1,int not null,f
SQL Server怎么建立聯(lián)合主鍵?
1、SQL server創(chuàng)建聯(lián)合主鍵的方法:1。創(chuàng)建表時(shí)寫(xiě)入。語(yǔ)句如下: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)建表后,語(yǔ)句如下:alter table name with nocheck add constraint[PK]utable name]primary key nonclustered([field name 1],[field name 2])2。聯(lián)合主鍵的優(yōu)點(diǎn)是:使用兩個(gè)字段(或多個(gè)字段,它們與兩個(gè)字段具體組合)來(lái)確定一個(gè)記錄。這表明這兩個(gè)字段不是唯一的,并且這兩個(gè)字段可以分別重復(fù)。此設(shè)置的優(yōu)點(diǎn)是可以直觀地看到重復(fù)字段中的記錄數(shù)。3、 聯(lián)合主鍵的使用:例如,order表中有許多字段。通常,您只需要有一個(gè)訂單號(hào)billuno就可以用作主鍵。但是,要求可以有相同訂單號(hào)的補(bǔ)充訂單。此時(shí),不允許單獨(dú)使用訂單號(hào),因?yàn)闀?huì)有重復(fù)。然后您可以使用另一個(gè)訂單序列號(hào)bill作為區(qū)別。將bill No和bill Set SEQ作為聯(lián)合主鍵。即使是比爾,不,比爾,和SEQ不一樣也沒(méi)關(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。實(shí)際上,聯(lián)合主鍵是11,這是唯一的值。絕對(duì)不允許使用唯一值11。(這是多對(duì)多關(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ù)庫(kù)表的一個(gè)重要屬性。主鍵的建立可以避免表中存在相同的記錄,即表中主鍵的記錄值是唯一的。建立主鍵有兩種方式:一種是在數(shù)據(jù)庫(kù)提供的GUI環(huán)境中建立主鍵,另一種是通過(guò)執(zhí)行SQL語(yǔ)句建立主鍵,如下所述。1數(shù)據(jù)庫(kù)提供的內(nèi)置GUI環(huán)境(以SQL7為例)。輸入表信息后,按CTRL鍵同時(shí)選中多行,然后單擊上面的主鍵按鈕。2通過(guò)SQL設(shè)置語(yǔ)句。有兩種方法:一種是在創(chuàng)建表的語(yǔ)句中直接寫(xiě)入,另一種是在創(chuàng)建表后更改表結(jié)構(gòu)。在表創(chuàng)建語(yǔ)句中,直接寫(xiě)入: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)信息。