外键约束问题:

netbir 2003-09-18 12:35:45
我建了一个客户表bcustomer表,一个销售记录表sales,这两个表中均有customerID字段,并建立了外键关系,现在我想让bcustomer中的某一客户的customerid改变时,sales表中相应客户的customerid自动改变,单纯用sql-server如何实现呀?
...全文
34 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
welyngj 2003-09-18
  • 打赏
  • 举报
回复
修正:
alter table sales
drop constraint constraintname
go
alter table sales
add constraint fk_bc
foreign key(customerID)
references bcustomer(customerid)
on update cascade on delete cascade
welyngj 2003-09-18
  • 打赏
  • 举报
回复
alter table sales
drop constraint constraintname
go
alter table sales
add constraint fk_bc
foreign key(customerID)
references bcustomer(customerid)
on update cascade
伍子V5 2003-09-18
  • 打赏
  • 举报
回复
alter table sales
add constraint fk_sales_customerID foreign key(customerID) references bcustomer(customerID)
on update cascade on delete cascade
伍子V5 2003-09-18
  • 打赏
  • 举报
回复
级联更新、级联删除是指在建立主外键关系的时候设定的一个选项
主要是主表里数据发生变化时从表也跟着变化,但是只有更新和删除,没有插入
你可以在企业管理器里做,也可以写SQL 语句
alter table score
add constraint fk_score_xh foreign key(xh) references student(xh)
on update cascade on delete cascade

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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