国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

唯一性約束

2019-11-18 15:18:03
字體:
來源:轉載
供稿:網友

所謂唯一性約束(unique constraint)不過是數據表內替代鍵的另一個名稱而已。替代鍵(alternate key)可以是數據表內不作為主鍵的其他任何列,只要該鍵對該數據表唯一即可。換句話說,在唯一列內不答應出現數據重復的現象。比方說,你可以用車輛識別代號(VIN)作為汽車(Automobile)數據表的替代鍵,在汽車數據表里,主鍵是汽車識別號(Automobile Identification),這是一種由系統自動生成的ID。你可以在汽車表內對VIN施加唯一性約束,同時再創建一個需要VIN的表。在這個新表內可以聲明外鍵指向汽車表。這樣,只要汽車表內有VIN輸入數據庫就會檢驗VIN輸入結果。這就是保證數據庫內數據完整性的另一種有效的措施。

以下是演示唯一性約束作為外鍵引用點的示例代碼:

create table parent
(parent_idint not null, -- PRimary key
parent_alternate_keyint not null, -- Alternate key
parent_col1 int null,
parent_col2 int null,
constraint pk_parent_id primary key (parent_id),
constraint ak_parent_alternate_key unique (parent_alternate_key)
)
go
insert parent values ( 1, 10, 150, 151)
insert parent values ( 2, 11, 122, 271)
insert parent values ( 3, 12, 192, 513)
insert parent values ( 4, 13, 112, 892)
go
create table child2
(child2_parent_id int not null, -- Primary key/Foreign key
child2_id int not null, -- Primary key
child2_col1 int null,
child2_parent_alternate_key int not null, -- Foreign key
constraint pk_child2 primary key (child2_parent_id, child2_id),
constraint fk_child2_parent foreign key (child2_parent_id) _
references dbo.parent(parent_id),
constraint fk_ak_child2_parent foreign key (child2_parent_alternate_key) references dbo.parent(parent_alternate_key)
)
go
insert child2 values (1,1,34,10)
insert child2 values (1,2,34,13)
insert child2 values (1,3,34,11)
go
insert child2 values (1,4,34,23) -- This one will fail
go

上一篇:我所認識的Apache

下一篇:微軟代碼

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 尼勒克县| 方山县| 齐齐哈尔市| 文安县| 沛县| 长兴县| 扬中市| 友谊县| 乌恰县| 阳春市| 阿克陶县| 洞口县| 侯马市| 平遥县| 神农架林区| 福州市| 仪征市| 大邑县| 温宿县| 博湖县| 裕民县| 图木舒克市| 峨眉山市| 合山市| 漳州市| 洪洞县| 凌云县| 当涂县| 即墨市| 周口市| 耿马| 三河市| 桐梓县| 尚义县| 奉节县| 德化县| 纳雍县| 河曲县| 武定县| 额敏县| 聊城市|