请教同步数据表问题(急)

temok 2007-10-20 12:23:07
同一个数据库中有两个结构相同的数据表,要求表1更新后(只有表1更新),表2保持与第1的表数据同步。用Sql Server2000应该解决,多谢!

表结构如下
表一
stuid1 (学号)
card1 (考勤号码)
name1 (姓名)
表二
stuid2 (学号)
card2 (考勤号码)
name2 (姓名)

俺是菜鸟,要详细的代码,正确给出后给分。谢谢!在线等待

...全文
84 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
baicai715 2007-10-25
  • 打赏
  • 举报
回复
学习
temok 2007-10-25
  • 打赏
  • 举报
回复
谢aniven!
结贴
stbigcrab 2007-10-23
  • 打赏
  • 举报
回复
用1楼的触发器最简单。
temok 2007-10-22
  • 打赏
  • 举报
回复
wewaa ,能简单解释一下如何用级联吗, 谢!
laowang2 2007-10-21
  • 打赏
  • 举报
回复
用级联更新和删除。
编译失败 2007-10-20
  • 打赏
  • 举报
回复
用触发器将是最好的选择
temok 2007-10-20
  • 打赏
  • 举报
回复
谢谢aniven。
一会去办公室研究测试一下,顶!
编译失败 2007-10-20
  • 打赏
  • 举报
回复
假设stuid1和stuid2为主键


--同步插入和更新
create trigger tr_sync_add on table1
for insert,update as
set nocount on
if exists(select 1 from table2 where stuid2 in (select stuid1 from inserted))
update table2 set card2=y.card1,name2=y.name1 from table2 x,inserted y where x.stuid2=y.stuid1
else
insert into table2 select * from inserted
go

--同步删除
create trigger tr_sync_del on table1
for delete as
set nocount on
delete table2 where stuid2 in (select stuid1 from deleted)
temok 2007-10-20
  • 打赏
  • 举报
回复
谢谢 aniven 。
我对触发器不是太了解,能给出实现代码吗?

1,178

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 数据库及相关技术
社区管理员
  • 数据库及相关技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧