怎么创建2个简单表的关联?

liuzhengkang 2009-04-16 11:19:52

2个表结构如下:
dict表:
id (PK,bigint,not null),
kind (varchar(32),null),
name (varchar(32),null)

dictBook表:
id (PK,bigint,not null),
kind (varchar(32),null),
Values (varchar(100),null)

现在假如:
dict表中有记录如下:
id 、 kind 、name
1 、 sex 、性别
2 、 degree 、学历

dictBook表中有记录如下:
id 、 kind 、values
1 、 sex 、男
2 、 sex 、女
3 、 degree 、本科
4 、 degree 、大专

我不知道怎么设置表的关联,dictBook表中的kind关联的是dict表中的kind,
即当删除dict表中的sex时,dictBook表中的kind就会把所有sex的记录都删除。
请问各位大哥,这样要怎么做?
...全文
169 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
sdhdy 2009-04-16
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 liuzhengkang 的回复:]

创建不了外键啊,这2个表的主键是定的,不能改,因为还有第3个表要用到上面2个表的主键。

请问一下触发器怎么做到他们添加、更新、删除时的关联?
[/Quote]
如果要删除的话,看看2楼的。
子陌红尘 2009-04-16
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 liuzhengkang 的回复:]

创建不了外键啊,这2个表的主键是定的,不能改,因为还有第3个表要用到上面2个表的主键。

请问一下触发器怎么做到他们添加、更新、删除时的关联?
[/Quote]

那就创建触发器吧,参考联机帮助里的触发器相关文档。
yygyogfny 2009-04-16
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 yygyogfny 的回复:]
引用 6 楼 liuzhengkang 的回复:

创建不了外键啊,这2个表的主键是定的,不能改,因为还有第3个表要用到上面2个表的主键。

请问一下触发器怎么做到他们添加、更新、删除时的关联?


参考第5楼.
[/Quote]

MSDN 查"trigger"
yygyogfny 2009-04-16
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 liuzhengkang 的回复:]

创建不了外键啊,这2个表的主键是定的,不能改,因为还有第3个表要用到上面2个表的主键。

请问一下触发器怎么做到他们添加、更新、删除时的关联?
[/Quote]

参考第5楼.
liuzhengkang 2009-04-16
  • 打赏
  • 举报
回复

创建不了外键啊,这2个表的主键是定的,不能改,因为还有第3个表要用到上面2个表的主键。

请问一下触发器怎么做到他们添加、更新、删除时的关联?
yygyogfny 2009-04-16
  • 打赏
  • 举报
回复


create table dict
(id bigint not null primary key,kind nvarchar(32),[name] varchar(32))


create table dictbook
(id bigint not null primary key,kind varchar(32),[values] varchar(100))

insert into dict
select 1,'sex','性别'
union all
select 2,'degree','学历'

insert into dictbook
select 1,'sex','男'
union all
select 2,'sex','女'
union all
select 3,'degree','本科'
union all
select 4,'degree','大专'



create trigger tri_dele
on dict
for delete
as
begin
delete dictbook from dictbook b,deleted d where b.kind = d.kind
end

delete dict where id = 1

select * from dict
select * from dictbook

id kind name
-------------------- -------------------------------- --------------------------------
2 degree 学历

(1 row(s) affected)

id kind values
-------------------- -------------------------------- ----------------------------------------------------------------------------------------------------
3 degree 本科
4 degree 大专

(2 row(s) affected)



rucypli 2009-04-16
  • 打赏
  • 举报
回复
createTRIGGER test
ON dict
AFTER delete
AS
BEGIN
declare @temp varchar(10)
select @temp = kind from deleted
delete from dictBook where kind = @temp

END
ks_reny 2009-04-16
  • 打赏
  • 举报
回复
用外鍵級聯刪除.
sdhdy 2009-04-16
  • 打赏
  • 举报
回复
--主外键级联(kind)删除或用触发器
create trigger trig_delete_dict on dict
for delete as
delete dictBook from deleted where dictBook.kind=deleted.kind
yygyogfny 2009-04-16
  • 打赏
  • 举报
回复
可以做外键级联删除,也可以做触发器
  • 打赏
  • 举报
回复
1、设置主外键,设置不成功的话,检查下关联字段的数据类型是否相同,(字符串的话,还要检查下长度)。然后设置级联更新,删除
2、使用触发器来实现
riveryeah 2009-04-16
  • 打赏
  • 举报
回复
就用触发器 在MSDN里看看 不用看别的
yygyogfny 2009-04-16
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 liuzhengkang 的回复:]

呵呵!谢谢大家,
刚才上网找了一下,使用Hibernate的话会和触发器有冲突吧,
晕啊!现在不知道怎么做,没思路了啊!
[/Quote]

这是纯数据库层的..和你使用的语言,框架都没关系~~
liuzhengkang 2009-04-16
  • 打赏
  • 举报
回复

呵呵!谢谢大家,
刚才上网找了一下,使用Hibernate的话会和触发器有冲突吧,
晕啊!现在不知道怎么做,没思路了啊!
claro 2009-04-16
  • 打赏
  • 举报
回复
帮顶

34,590

社区成员

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

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