create table ta(id int identity(1,1),name varchar(100)) goname是和表c.myname相同,c.myname更新的时候a.name也会同步,请问如何写命令-- 在C上加一个触发器, create trigger tr_name on tablec for update as update a set name = i.name from ta a right join inserted i on a.id = b.id --id是两表关联 go
create table ta(id int identity(1,1),name varchar(100)) go name是和表c.myname相同,c.myname更新的时候a.name也会同步,请问如何写命令 -- 在C上加一个触发器, create trigger tr_name on tablec for update as update a set name = i.name from ta a right join inserted i on a.id = b.id --id是两表关联 go
可以设置级联更新实现,比如IF object_id('tc','u') IS NOT NULL DROP TABLE tc IF object_id('ta','u') IS NOT NULL DROP TABLE ta GOcreate table tc(myname varchar(10) not null) go create table ta(id int identity(1,1) ,name varchar(10) not null) goinsert tc select 'aa' union all select 'bb'insert ta select 'aa' union all select 'bb' union all select 'aa' goselect * from tc /* aa bb*/ select * from ta /*1 aa 2 bb 3 aa */--为tc创建主健 alter table tc add constraint c_pk primary key (myname) go --为ta创建外健,并指定级联更新 alter table ta add constraint a_fk foreign key (name) references tc(myname) on update cascade go --更新tc,看看是否ta也跟着变 update tc set myname='xx' where myname='aa' go select * from tc /* bb xx*/ select * from ta /* 1 xx 2 bb 3 xx 级联更新生效*/ goalter table ta drop constraint a_fk go
goname是和表c.myname相同,c.myname更新的时候a.name也会同步,请问如何写命令--
在C上加一个触发器,
create trigger tr_name
on tablec
for update
as
update a set name = i.name
from ta a
right join inserted i on a.id = b.id --id是两表关联
go
create table ta(id int identity(1,1),name varchar(100))
go name是和表c.myname相同,c.myname更新的时候a.name也会同步,请问如何写命令 --
在C上加一个触发器,
create trigger tr_name
on tablec
for update
as
update a set name = i.name
from ta a
right join inserted i on a.id = b.id --id是两表关联
go
DROP TABLE tc
IF object_id('ta','u') IS NOT NULL
DROP TABLE ta
GOcreate table tc(myname varchar(10) not null)
go
create table ta(id int identity(1,1) ,name varchar(10) not null)
goinsert tc select 'aa'
union all select 'bb'insert ta select 'aa'
union all select 'bb'
union all select 'aa'
goselect * from tc
/*
aa
bb*/
select * from ta
/*1 aa
2 bb
3 aa
*/--为tc创建主健
alter table tc add constraint c_pk primary key (myname)
go
--为ta创建外健,并指定级联更新
alter table ta add constraint a_fk foreign key (name) references tc(myname) on update cascade
go
--更新tc,看看是否ta也跟着变
update tc set myname='xx' where myname='aa'
go
select * from tc
/*
bb
xx*/
select * from ta
/*
1 xx
2 bb
3 xx
级联更新生效*/
goalter table ta drop constraint a_fk
go