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

首頁 > 數據庫 > SQL Server > 正文

sql server中的外鍵約束

2024-08-31 00:48:09
字體:
來源:轉載
供稿:網友
,歡迎訪問網頁設計愛好者web開發。
sql server中建立外鍵約束有3中方式:
1.enterprise manager中,tables,design table,設置table的properties,
   可以建立constraint, reference key;
2.enterprise manager中,diagrams, new diagrams,建立兩個表的關系。
3.直接用transact sql語句。

三個方法都需要先建立數據表。
-- 創建表author :
create table [dbo].[author] (
 [id] [bigint] not null ,
 [authorname] [char] (10) null ,
 [address] [char] (480)  null ,
 [introduction] [ntext]  null
)

-- 創建表mybbs:
reate table [dbo].[mybbs] (
 [id] [bigint] identity (1, 1) not null ,
 [authorid] [bigint] not null ,
 [title] [char] (40)  null ,
 [date_of_created] [datetime] null ,
 [abstract] [char] (480)  null ,
 [content] [ntext]  null
)

設置表mybbs中的authorid為外鍵,參照author表的id字段,直接使用transact sql語句,過程如下:
--增加表mybbs(authorid)的外鍵約束fk_mybbs_author,表mybbs中的authorid受表author中的主鍵id約束:
begin transaction
alter table dbo.mybbs add constraint fk_mybbs_author
  foreign key (authorid)
  references  dbo.author([id]) on update cascade on delete cascade

--刪除外鍵約束fk_mybbs_author:
--alter table dbo.mybbs drop constraint fk_mybbs_author
--rollback
commit transaction

上面on update cascade,on delete cascade兩個選項,指明以后author表的id字段有delete,update操作時,mybbs表中的id也會被級聯刪除或更新。如果沒有選中,是不可以對author表中已被mybbs表關聯的id進行update或者delete操作的。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平度市| 萝北县| 南和县| 吉林市| 定兴县| 古浪县| 筠连县| 张北县| 理塘县| 大庆市| 岳西县| 自贡市| 西和县| 隆德县| 晋宁县| 宁河县| 同德县| 巧家县| 漠河县| 庆城县| 凯里市| 慈溪市| 台南市| 青岛市| 垦利县| 应城市| 乃东县| 绥滨县| 通化县| 南涧| 绥化市| 开封市| 玛多县| 周口市| 江阴市| 黎平县| 嘉黎县| 玉门市| 仙游县| 宁远县| 淮阳县|