在很多的时候,一个良好的数据库设计会因为后期的需求变化或其他原因,需要在当前表进行字段的扩展,这个是系统本身的设计问题,为了达到系统的功能需求,不可避免需要更改数据库的设计。
但是有时候我们做系统集成,要与其他系统做接口的时候,如果需要更改表的设计,我认为就不是一个很好的设计思路了,做为这种需求,我们可以通过设计对应表来满足接口的需求。
还有一种方案就是在不改变的原表,将原表改名,然后建立一个原表与接口对应信息表的视图,视图命名为原来的表名,这样就可以通过更新视图来处理各种业务了。
以下为实例代码:
<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:0; mso-generic-font-family:roman; mso-font-pitch:variable; mso-font-signature:-1610611985 1107304683 0 0 159 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-unhide:no; mso-style-qformat:yes; mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman","serif"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} .MsoChpDefault {mso-style-type:export-only; mso-default-props:yes; font-size:10.0pt; mso-ansi-font-size:10.0pt; mso-bidi-font-size:10.0pt; mso-ascii-font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-hansi-font-family:"Times New Roman"; mso-font-kerning:0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} -->
-- 创建原表
create table tab( id int , nam varchar ( 10))
insert tab
select 1, 'AA' union all
select 2, 'BB' union all
select 3, 'CC'
go
-- 创建扩展表
create table tab1( id int , nam1 varchar ( 10))
insert tab1
select 1, 'ERP1' union all
select 2, 'ERP2' union all
select 3, 'ERP3'
go
-- 创建视图
create view T1
as
select tab. id, nam, nam1
from tab, tab1
where tab. id= tab1. id
go
-- 插入测试数据
insert T1
select 4, 'DD' , 'ERP4'
go
-- 提示以下错误
-- 消息 4405 ,级别 16 ,状态 1 ,第 8 行
-- 视图或函数 'T1' 不可更新,因为修改会影响多个基表。
-- 需要创建 Instead OF 触发器来处理更新的问题
create trigger trg_v1 on T1
instead of insert
as
begin
insert tab select id, nam from inserted
insert tab1 select id, nam1 from inserted
end
go
-- 再次插入测试数据
insert T1
select 4, 'DD' , 'ERP4'
-- 检查结果
select * from T1
select * from Tab
select * from Tab1
go
-- 同理创建删除的触发器
create trigger trg_v2 on T1
instead of delete
as
begin
delete from Tab where id in ( select id from deleted)
delete from Tab1 where ID in ( select id from deleted)
end
-- 删除测试
go
delete from T1
where id= 4
go
-- 检查结果
select * from T1
select * from Tab
select * from Tab1
go
-- 删除测试环境
drop table tab, tab1
drop trigger trg_v1, trg_v2
drop view T1
go
--测试环境:Windows2003 SP2 SQL Server2005 SP3