sql - Do I need to specify ON DELETE NO ACTION on my Foreign Key? -


i have following ddl using sql server 2012:

create table subject (    [subjectid] int identity (1, 1) not null,    [name] nvarchar (50) not null,    constraint [pk_subject] primary key clustered ([subjectid] asc) )             create table topic (    [topicid] int identity (1, 1) not null,    [name] nvarchar (50) not null,    [subjectid] int not null,    constraint [pk_topic] primary key clustered ([topicid] asc) ) alter table [topic] check add  constraint [fk_topicsubject]     foreign key([subjectid]) references [subject] ([subjectid])     on delete no action 

what want sql server stop me deleting parent if reference parent exists in child? example want delete on subjectid=3 in subject fail if there children subjectid's of 3.

for unclear , cannot seem find answer. need add "delete no action" or can not remove these 3 words.

i'm asking question in similar question had response should define trigger on parent. thought defining foreign key stop me deleting parent if child exists.

from column_constraint page on msdn:

on delete { no action | cascade | set null | set default }

specifies action happens rows in table altered, if rows have referential relationship , referenced row deleted parent table. default no action.

so, can elide on delete no action if , work same.

no action means nothing happen when delete subject table topic table. in case, if there row in topic given subjectid cannot delete without breaking referential integrity, delete rolled back.

more msdn:

no action - sql server database engine raises error , delete action on row in parent table rolled back.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -