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
查看全文
相关阅读:
应用网络电视机顶盒通过宽带网络代替数字电视
该公众号暂时无法提供服务请稍后再试
有点坑爹的GDALComputeRasterMinMax函数
微信硬件平台框架说明及接入流程
微信硬件设备接入接口协议
微信思维
百度SEO建议
公众平台调整SSL安全策略,请开发者注意升级
微信支付开发教程
.net文件分片上传,断点续传
原文地址:https://www.cnblogs.com/gwazy/p/484536.html
最新文章
MySQL server has gone away 问题的解决方法
[MySQL] TRUNCATE数据库所有表,打印所有TRUNCATE表语句
Hibernate:**not found while looking for property: id https://blog.csdn.net/weixin_43827144/article/details/88935334
nested exception is org.hibernate.MappingException解决方案
JRE System Library、Referenced Libraries、Web App Libraries的含义
web App libraries跟referenced libraries的一些问题
Eclipse项目中web app libraries和 Referenced Libraries区别
Kryo官方文档-中文翻译
The method getTextContent() is undefined ?
安装ubuntu远程桌面xrdp可视化设置界面
热门文章
HDU 2461 线段树扫描线
在项目开发中使用Git版本号控制工具以提高效率
Yii中使用RBAC全然指南
分布式软件体系结构风格(C/S,B/S)
POJ 3468 A Simple Problem with Integers(线段树区间更新)
hdu 1671 Phone List 字典树
Java面向对象
mysql基础知识
Cocos2d-x3.3RC0载入Android的WebView
使用缓存Memcache存储更新微信access token
Copyright © 2011-2022 走看看