国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

分析數據庫的一些方法

2024-07-21 02:07:55
字體:
來源:轉載
供稿:網友

在工作中,我們有時需要分析一個現有軟件的數據庫結構,簡單的說,就是想知道兩點

1 、各種數據保存在哪個表
2 、在什么情況下,表中的數據會發生更新

下面我把自己的方法寫出來,如果您有更好的方法,請與我討論。

1、為數據庫中的每一個業務表建立對應的更新表
   當相應業務表的數據被更新時,觸發器會把更新的類型和記錄寫進相應的更新表
   更新表的字段除了包括相應業務表的所有字段,還添加了三個字段

   (1) 一個自增的id
   (2) 更新類型(i 插入;d 刪除;u 更新)
   (3) 更新時間
  

2、在數據庫中建立一個總更新表

   當任何一個業務表的數據被更新時,觸發器會把更新的類型和表名寫進總更新表,作用是快速找到當前發生數據更新的表
   總更新表有四個字段

   (1) 一個自增的id
   (2) 更新類型(i 插入;d 刪除;u 更新)
   (3) 更新的表名
   (4) 更新時間

3、為每一個業務表建立三個觸發器,分別對應插入、刪除、修改三種操作
當業務表發生更新時,會把更新前的記錄、更新后的記錄、刪除的記錄、插入的記錄寫入相應更新表

為此我專門寫了兩個存儲過程,適用于sql server 2000,如果您的數據庫不是sql server 2000,也可供您參考
為了新建立的表和觸發器和數據庫中原有的表和觸發器同名,采用了加后綴方法,比如
表名為 users的表,相應的更新表為users+后綴,當后綴為_1234567時,更新表的表名為users_1234567

下面是存儲過程p_analysis和p_clearup的腳本

/*=========================================================================
存儲過程 p_analysis
作用
為分析建立一個總的更新表 update+后綴+后綴
為每個表建立一個更新表   原表名+后綴
為每個表建立三個觸發器   tr_表名_+觸發器類型(i:插入 d:刪除 u:更新)+后綴

輸入參數  @postfix,以免分析用表和業務表名稱重復,分析用觸發器和原由觸發器重復
使用舉例  exec p_analysis '_1234567'
============================================================================*/

create procedure p_analysis
 @postfix char(8)
as
--測試是否會和數據庫原有的對象名(字段名)重復
if exists(select * from sysobjects where right(name,8)[email protected]) or exists(select * from syscolumns where

right(name,8)[email protected])
  print '對象名重復,請使用不同的后綴民名'
else
 begin
   --為每個表建立更新記錄表
   declare @tablename nvarchar(128)
   declare @columns varchar(8000)
   declare cur insensitive cursor
   for
   select name from sysobjects where xtype='u' and status>0
   open cur
   fetch next from cur into @tablename
   while(@@fetch_status=0)
   begin
        set @columns=''
 --建立更新表
        exec('select * into '[email protected][email protected]+' from '[email protected]+' where 1=0')
 --為更新表增加三個字段
        exec('alter table '[email protected][email protected] + ' add id'[email protected]+' int identity(1,1),oprtype'[email protected]+'

char(2),oprtime'[email protected]+' datetime default getdate()')
        --為每個業務表建立三個觸發器
        select @[email protected]+','+name from syscolumns where id=object_id(@tablename)

 --插入觸發器
 exec('create trigger tr_'[email protected]+'_i'[email protected]+' on '[email protected]+' for insert as'+
        ' insert update'[email protected][email protected]+'(tablename,oprtype)'+
        ' values('''[email protected]+''',''i'')'+
        ' insert '[email protected][email protected]+'(oprtype'[email protected][email protected]+')'+
        ' select ''i'''[email protected]+' from inserted')

        --刪除觸發器
 exec('create trigger tr_'[email protected]+'_d'[email protected]+' on '[email protected]+' for delete as'+
        ' insert update'[email protected][email protected]+'(tablename,oprtype)'+
        ' values('''[email protected]+''',''d'')'+
        ' insert '[email protected][email protected]+'(oprtype'[email protected][email protected]+')'+
        ' select ''d'''[email protected]+' from deleted')

        --更新觸發器
        exec('create trigger tr_'[email protected]+'_u'[email protected]+' on '[email protected]+' for update as'+
        ' insert update'[email protected][email protected]+'(tablename,oprtype)'+
        ' values('''[email protected]+''',''u'')'+
        ' insert '[email protected][email protected]+'(oprtype'[email protected][email protected]+')'+
        ' select ''bu'''[email protected]+' from deleted'+
        ' insert '[email protected][email protected]+'(oprtype'[email protected][email protected]+')'+
        ' select ''au'''[email protected]+' from inserted')

  fetch next from cur into @tablename
   end
   close cur
   deallocate cur
   --建立總記錄更新表
   exec('create table update'[email protected][email protected]+'(id numeric(18,0) identity(1,1),tablename varchar(256),oprtype

char(1),oprtime datetime default getdate())')
end
go

/*==================================================================
存儲過程 p_clearup

作用:清除新建的表/觸發器

輸入參數: @postfix 默認值 _1234567

使用例子: 使用舉例 exec p_clearup '_1234567'
====================================================================*/
create procedure p_clearup
@postfix char(8)='_1234567'
as
--刪除總更新表

   exec('if exists (select * from sysobjects where name =''update'[email protected][email protected]+''' and type=''u'')'+
        'drop table update'[email protected][email protected])
   declare @tablename nvarchar(128)
   declare cur cursor
   for
   select name from sysobjects where xtype='u' and status>0
   open cur
   fetch next from cur into @tablename
   while(@@fetch_status=0)
   begin
 --刪除更新表
        exec('if exists (select * from sysobjects where name ='''[email protected][email protected]+''' and type=''u'')'+
             'drop table '[email protected][email protected])
 --刪除插入觸發器
        exec('if exists (select * from sysobjects where name =''tr_'[email protected]+'_i'[email protected]+''' and type=''tr'')'+
     'drop trigger tr_'[email protected]+'_i'[email protected])
 --刪除刪除觸發器
        exec('if exists (select * from sysobjects where name =''tr_'[email protected]+'_d'[email protected]+''' and type=''tr'')'+
      'drop trigger tr_'[email protected]+'_d'[email protected])
 --刪除更新觸發器
        exec('if exists (select * from sysobjects where name =''tr_'[email protected]+'_u'[email protected]+''' and type=''tr'')'+
     'drop trigger tr_'[email protected]+'_u'[email protected])
  fetch next from cur into @tablename
   end
   close cur
   deallocate cur
go

商業源碼熱門下載www.html.org.cn

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 莲花县| 大邑县| 资溪县| 内江市| 城口县| 富裕县| 东乡族自治县| 龙岩市| 灌云县| 门头沟区| 咸丰县| 科尔| 延庆县| 吴堡县| 新民市| 濉溪县| 阿尔山市| 北安市| 台南市| 五指山市| 阿拉善左旗| 湟中县| 赤峰市| 来安县| 文登市| 昭通市| 西和县| 长治市| 泊头市| 定边县| 观塘区| 临武县| 瑞金市| 内黄县| 福安市| 房山区| 祁阳县| 邵阳市| 汉寿县| 贵德县| 潮州市|