zoukankan
html css js c++ java
用触发器实现主从表关系(主表更改从表更改 )
用触发器实现的 插入 更新 删除 子表也变化
CREATE
TABLE
[
dbo
]
.
[
tablex
]
(
[
idx
]
[
int
]
IDENTITY
(
1
,
1
)
NOT
NULL
,
[
ProductID
]
[
int
]
NULL
,
[
productName
]
[
char
]
(
10
) COLLATE Chinese_PRC_CI_AS
NULL
)
ON
[
PRIMARY
]
GO
CREATE
TABLE
[
dbo
]
.
[
tabley
]
(
[
idy
]
[
int
]
IDENTITY
(
1
,
1
)
NOT
NULL
,
[
ProductID
]
[
int
]
NULL
,
[
productname
]
[
char
]
(
10
) COLLATE Chinese_PRC_CI_AS
NULL
)
ON
[
PRIMARY
]
GO
CREATE
TRIGGER
triDelete
ON
[
dbo
]
.
[
tablex
]
FOR
delete
AS
begin
declare
@aa
varchar
(
200
)
set
@aa
=
(
select
productid
from
deleted)
if
@@rowcount
>
0
delete
tabley
where
productid
=
@aa
end
CREATE
trigger
tritmp
on
tablex
for
insert
as
insert
into
tabley(ProductID)
select
i.ProductId
from
inserted
as
i
where
i.ProductId
>
100
CREATE
TRIGGER
triUpdate
ON
[
dbo
]
.
[
tablex
]
FOR
UPDATE
AS
IF
UPDATE
(productname)
begin
declare
@aa
varchar
(
200
)
set
@aa
=
(
select
productid
from
INSERTED)
declare
@bb
varchar
(
200
)
set
@bb
=
(
select
productname
from
INSERTED)
if
(
@@rowcount
>
0
)
update
tabley
set
productname
=
@bb
where
productid
=
@aa
end
insert
tablex
values
(
300
,
'
东方
'
)
update
tablex
set
productname
=
'
大海
'
where
productid
=
300
select
*
from
tablex
select
*
from
tabley
delete
from
tablex
where
productid
=
300
select
*
from
tablex
select
*
from
tabley
其实删除时也可以用外键
删除
CREATE
TABLE
[
dbo
]
.
[
TABLE1
]
(
[
UserId
]
[
int
]
IDENTITY
(
1
,
1
)
NOT
NULL
,
[
name
]
[
char
]
(
10
) COLLATE Chinese_PRC_CI_AS
NULL
)
ON
[
PRIMARY
]
CREATE
TABLE
[
dbo
]
.
[
TABLE2
]
(
[
id
]
[
int
]
IDENTITY
(
1
,
1
)
NOT
NULL
,
[
Userid
]
[
int
]
NULL
,
[
name
]
[
char
]
(
10
) COLLATE Chinese_PRC_CI_AS
NULL
)
ON
[
PRIMARY
]
GO
ALTER
TABLE
[
dbo
]
.
[
TABLE2
]
ADD
CONSTRAINT
[
FK_TABLE2_TABLE1
]
FOREIGN
KEY
(
[
Userid
]
)
REFERENCES
[
dbo
]
.
[
TABLE1
]
(
[
UserId
]
)
ON
DELETE
CASCADE
GO
select
*
from
table1
select
*
from
table2
insert
table1
values
(
'
def
'
)
insert
table2
values
(
5
,
'
def
'
)
select
*
from
table1
select
*
from
table2
delete
from
table1
where
userid
=
5
查看全文
相关阅读:
linux date使用
SHELL输出带颜色字体
vimrc配置
你所不知道的C++
temp
说什么好呢3
Extjs3 Combo实现百度搜索查询
Extjs3笔记 fbar
Extjs combo赋值与刷新的先后顺序
sql中nvarchar(max)长度测试
原文地址:https://www.cnblogs.com/gwazy/p/484536.html
最新文章
Linux环境下,Shell脚本启动java程序
https数据传输对称、非对称、数字证书详解
Web开发遇到中文乱码[实战]
一个感兴趣的尝试,计算德州扑克胜率
java并发编程的艺术,读线程之间的通信的思考
9/22号的债
9/15的债
9/8号的债
2019/9/1
2019/8/25日之债
热门文章
2019/8/18
2019/8/18日之债
java并发编程的艺术(四)---ConcurrentHashMap原理解析
vmstat命令
IOSTAT详解
Linux 登陆配置读取顺序
LINUX磁盘分区
VMSTAT监控CPU使用率,内存使用,虚拟内存交换情况
shell编程规范
shell编程规范:引用
Copyright © 2011-2022 走看看