Sql Server外鍵約束


一、添加約束(級聯刪除)
1、創建表結構時添加
create table UserDetails
(
id int identity(1,1) primary key,
name varchar(50) not null, --真實姓名
userId int,
foreign key (userId) references UserInfo(UserId) on delete cascade
)
2、表結構已存在時添加
ALTER TABLE [dbo].[API_User_Label]   WITH CHECK Add CONSTRAINT [FK_API_User_Label_API_SearchLabel]  FOREIGN KEY([LabelId])
REFERENCES [dbo].[API_SearchLabel] ([Id]) on delete cascade
GO
 
二、刪除約束
ALTER TABLE [dbo].[API_User_Label] 
drop constraint FK_API_User_Label_API_SearchLabel
Go


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM