Home Database Mysql Tutorial 数据库结构同步之通过DDL触发器记录数据库结构的变更

数据库结构同步之通过DDL触发器记录数据库结构的变更

Jun 07, 2016 pm 02:59 PM
ddl change Synchronize database structure trigger Record pass need

需求: 在开发多人协作的项目的时候,一般要同时使用多个数据库 常见的情况有: 一个开发者用的数据库(开发库), 一个测试者用的数据库(测试库), 一个正式开放给客户的数据库(正式库), 那么这三个数据库之间的数据结构的同步就将成为一个问题 如:当

需求:

在开发多人协作的项目的时候,一般要同时使用多个数据库

常见的情况有:

一个开发者用的数据库(开发库),

一个测试者用的数据库(测试库),

一个正式开放给客户的数据库(正式库),

那么这三个数据库之间的数据结构的同步就将成为一个问题

如:当开发者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
Copy after login

 和普通的创建触发器的过程类似

就说其中的两个地方吧

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
Copy after login

以上代码均在MSSQLSERVER2008下测试通过

其他数据库没有测试

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

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

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

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())

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

What are the syntax and structure characteristics of lambda expressions? What are the syntax and structure characteristics of lambda expressions? Apr 25, 2024 pm 01:12 PM

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.

How to use database callback functions in Golang? How to use database callback functions in Golang? Jun 03, 2024 pm 02:20 PM

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

How to save JSON data to database in Golang? How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

How to connect to remote database using Golang? How to connect to remote database using Golang? Jun 01, 2024 pm 08:31 PM

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.

PHP connections to different databases: MySQL, PostgreSQL, Oracle and more PHP connections to different databases: MySQL, PostgreSQL, Oracle and more Jun 01, 2024 pm 03:02 PM

PHP database connection guide: MySQL: Install the MySQLi extension and create a connection (servername, username, password, dbname). PostgreSQL: Install the PgSQL extension and create a connection (host, dbname, user, password). Oracle: Install the OracleOCI8 extension and create a connection (servername, username, password). Practical case: Obtain MySQL data, PostgreSQL query, OracleOCI8 update record.

See all articles