数据库结构同步之通过DDL触发器记录数据库结构的变更
需求: 在开发多人协作的项目的时候,一般要同时使用多个数据库 常见的情况有: 一个开发者用的数据库(开发库), 一个测试者用的数据库(测试库), 一个正式开放给客户的数据库(正式库), 那么这三个数据库之间的数据结构的同步就将成为一个问题 如:当
需求:
在开发多人协作的项目的时候,一般要同时使用多个数据库
常见的情况有:
一个开发者用的数据库(开发库),
一个测试者用的数据库(测试库),
一个正式开放给客户的数据库(正式库),
那么这三个数据库之间的数据结构的同步就将成为一个问题
如:当开发者A在“开发库”中添加了一个表,开发者B修改了一个表...
这些数据库结构的变更势必要同步到“测试库”和“正式库”中去
但肉手记录数据库结构变更的方式即麻烦由容易出错...
如之奈何?
思考过程:
之前曾在数据库达人邹建那里看到一篇帖子(找不到了),
但试过之后发现,很多情况都会出异常...就放弃了
后来在WCF达人Artech这里看到一篇文章
追踪记录每笔业务操作数据改变的利器——SQLCDC
此文章是针对数据库表记录CURD操作的日志
并非表结构
后来我在文章评论中向Artech请教了我的问题,
得到了DDL Trigger的答案
记下来并分享
代码及解释:
USE MRLH_CM; GO --创建记录数据库结构变更的表 CREATE TABLE LogTable (DB_User nvarchar(200), EventType nvarchar(200), SQLString nvarchar(2000),ChangeTime datetime); GO CREATE TRIGGER LogTrigger ON DATABASE FOR DROP_TABLE, ALTER_TABLE ,CREATE_TABLE AS DECLARE @data XML SET @data = EVENTDATA() INSERT LogTable (DB_User, EventType, SQLString,ChangeTime) VALUES (CONVERT(nvarchar(100), CURRENT_USER), @data.value('(/EVENT_INSTANCE/EventType)[1]', 'nvarchar(100)'), @data.value('(/EVENT_INSTANCE/TSQLCommand)[1]', 'nvarchar(2000)'), GETDATE() ) ; GO
和普通的创建触发器的过程类似
就说其中的两个地方吧
1.FOR DROP_TABLE, ALTER_TABLE ,CREATE_TABLE
这里只记录了这几个事件
如果记录更多的事件请使用
FOR DDL_DATABASE_LEVEL_EVENTS
了解更多的事件情况请访问
http://msdn.microsoft.com/en-us/library/ms186456(SQL.90).aspx
2.SET @data = EVENTDATA()
EVENTDATA()是数据库自身的方法
返回有关服务器或数据库事件的信息(XML格式)
只有直接在 DDL 或登录触发器内部引用 EVENTDATA 时,EVENTDATA 才会返回数据。
如果 EVENTDATA 由其他例程调用(即使这些例程由 DDL 或登录触发器进行调用),将返回 NULL。
@data.value('(/EVENT_INSTANCE/EventType)[1]
这是使用XQUERY检索XML中的数据
详细的XQUERY教程请看这里
http://www.w3school.com.cn/xquery/index.asp
注意:
--想删除表LogTable必须先删除这个触发器 DROP TRIGGER LogTrigger on database GO --删除表 DROP TABLE LogTable GO
以上代码均在MSSQLSERVER2008下测试通过
其他数据库没有测试

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

Lambda expression is an anonymous function without a name, and its syntax is: (parameter_list)->expression. They feature anonymity, diversity, currying, and closure. In practical applications, Lambda expressions can be used to define functions concisely, such as the summation function sum_lambda=lambdax,y:x+y, and apply the map() function to the list to perform the summation operation.

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

1. Open DingTalk on your mobile phone and first select the avatar in the upper left corner. 2. Click Settings at the bottom of the pop-up page. 3. Then click on the settings on the page. 4. Open the calendar settings on the settings page. 5. Click on it and find the synchronized mobile schedule on the page. . 6. Then turn on the synchronization of mobile phone schedules.

Analysis of the basic principles of the MySQL database management system MySQL is a commonly used relational database management system that uses structured query language (SQL) for data storage and management. This article will introduce the basic principles of the MySQL database management system, including database creation, data table design, data addition, deletion, modification, and other operations, and provide specific code examples. 1. Database Creation In MySQL, you first need to create a database instance to store data. The following code can create a file named "my
