/*
RiCo技術學院
微軟SQL Server 2022基礎資料庫設計和開發
   主鍵(PK)和外鍵(FK)基本介紹
*/
use AdventureWorksDemo
go

create table parent
(
c1 int
,c2 int
,constraint PK_parent primary key(c1) 
)
insert into dbo.parent values(1,10),(2,20)

create table child
(
c1 int
,c2 int
,constraint PK_child primary key(c1) 
--,constraint FK_parent_c1 foreign key(c2) REFERENCES dbo.parent(c1)
)
insert into dbo.child values(1,100),(2,200)

begin tran
delete dbo.parent where c1=2
delete dbo.child where c1=2

select * from dbo.parent
select * from dbo.child

rollback

alter table dbo.child 
add constraint FK_parent_c1 foreign key(c2)
 references dbo.parent(c1)
--on delete CASCADE
--on update CASCADE

delete dbo.child
insert into dbo.child values(1,1),(2,2)

