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
查看全文
相关阅读:
python之函数嵌套与闭包
python之高阶函数
python之装饰器
python之内置函数
python之内置函数:map ,filter ,reduce总结
Python之reduce函数
install python2 python3 in same computer
git basic
git LF CRLF
2 thread, first to open chat window, second to make the phone
原文地址:https://www.cnblogs.com/gwazy/p/484536.html
最新文章
可以ping通,但是不能connect
Andriod ADB开启Activity、Service以及BroadCast(包括参数的传递)
Andriod ADB开启Activity、Service以及BroadCast(包括参数的传递)
直接插入排序
直接插入排序
选择排序
选择排序
工作日志
工作日志
#查看Linux的版本信息
热门文章
#查看Linux的版本信息
弓形矩阵
弓形矩阵
C++折半插入排序
C++折半插入排序
一个简单的网站首页制作
python之类的相关知识
python之random模块
Python之time模块
python之model模块和包的介绍
Copyright © 2011-2022 走看看