1 create table UserType 2 3 ( 4 5 Id int primary key identity(1,1), Name nvarchar(25) not null 6 7 ) go
1 create table UserInfo 2 3 ( 4 5 Id int primary key identity(1,1), LoginPwd varchar(50) not null, LoginName varchar(50) not null, Name varchar(25) not null, Gender bit not null default 1 check(Gender=1 or Gender=0), Email varchar(25) not null, Adress nvarchar(50) not null, Phone int not null, BlogDespt nvarchar(50) default '這個人很懶,什么都沒有留下', UserTypeId int foreign key references UserType (Id) 6 7 ) 8 9 go
1 create table BlogType 2 3 ( 4 5 Id int primary key identity(1,1), Name nvarchar(30) not null 6 7 ) 8 9 go
1 create table Blog 2 3 ( 4 5 Id int primary key identity(1,1), Title Nvarchar(100) not null, Summary nvarchar(max), Essay text, BlogData datetime not null default getdate(), Clicks int not null default 0, BlogTypeId int foreign key references BlogType (Id), UserId int foreign key references UserInfo (Id) 6 7 ) go
1 create table Comment 2 3 ( 4 5 Id int primary key identity(1,1), CommentText text not null, Commenttary int not null default 0, CommentDate datetime not null default getdate(), CommentBlog int foreign key references Blog (Id) 6 7 ) go
1 insert into UserType (Name)values('管理員') insert into UserType(Name)values('普通用戶') 2 3 insert into UserInfo (LoginPwd,LoginName,Name,Gender,Email,Adress,Phone,BlogDespt,UserTypeId) values ('123', 'Admin', '當時明月', 0, 'admin@blog.com', '北京市-海淀區', '13811389272','我是管理員,歡迎光臨我的個人博客', 1) 4 5 insert into BlogType (Name)values('體育') insert into BlogType (Name)values('財經') insert into BlogType (Name)values('房產') insert into BlogType (Name)values('娛樂') insert into BlogType (Name)values('計算機技術') insert into BlogType (Name)values('其他') 6 7 insert into Blog(Title,Summary,Essay,BlogTypeId,Clicks) values('這個我的第一篇博文','開通博客','大家好,我剛剛開通了博客,希望可以在這里交到更多的朋友!',6,0) 8 9 10 11 insert into Comment(CommentText,Commenttary,CommentBlog) values('好',0,1) go 12 13 insert into Comment(CommentText,Commenttary,CommentBlog) values('很好!',0,1) go
SQL Server中有主外鍵約束關系的表刪除問題作者:博博 orderforcard中存在外鍵對應表client中的主鍵。當用戶買卡時會在client表中添加記錄,當交易被撤消時client中的記錄要刪除同時orderforcard表中的記錄也要隨之刪除。這時可以采用下面的方法。
第一種(這種方法不好): 1、禁用約束 alter table ××× nocheck constraint all 2、刪除數據 delete from ××× 3、恢復約束 alter table ××× check constraint all 第二種方法: 采用級聯的方法,當含有主鍵的表中的數據刪除時,外鍵表的數據自動進行刪除操作。 alter table dbo.OrderForCard add constraint FK_ORDERFOR_REFERENCE_CLIENT foreign key (ClientId) references dbo.Client (ClientId) on delete cascade 這樣在進行刪除數據的時候就不用兩張表中的數據依次進行刪除了而直接刪除主表中的記錄就可以了,含有外鍵的記錄自動就隨之刪除了。
SQL級聯刪除——刪除主表同時刪除從表——同時刪除具有主外鍵關系的表 create table a ( id varchar(20) primary key, password varchar(20) not null )
create table b ( id int identity(1,1) primary key, name varchar(50) not null, userId varchar(20), foreign key (userId) references a(id) on delete cascade ) 表B創建了外碼userId 對應A的主碼ID,聲明了級聯刪除 測試數據: insert a values ('11','aaa') insert a values('23','aaa') insert b values('da','11') insert b values('das','11') insert b values('ww','23') 刪除A表內id為‘11’的數據,發現B表內userId 為“11”也被數據庫自動刪除了,這就是級聯刪除 delete a where id='11'