how to compare/validate sql schema

醉酒当歌 提交于 2019-12-03 08:56:27

问题


I'm looking for a way to validate the SQL schema on a production DB after updating an application version. If the application does not match the DB schema version, there should be a way to warn the user and list the changes needed.

Is there a tool or a framework (to use programatically) with built-in features to do that? Or is there some simple algorithm to run this comparison?

Update: Red gate lists "from $395". Anything free? Or more foolproof than just keeping the version number?


回答1:


Try this SQL.
- Run it against each database.
- Save the output to text files.
- Diff the text files.

/* get list of objects in the database */
SELECT name, 
       type 
FROM  sysobjects
ORDER BY type, name

/* get list of columns in each table / parameters for each stored procedure */
SELECT so.name, 
       so.type, 
       sc.name, 
       sc.number, 
       sc.colid, 
       sc.status, 
       sc.type, 
       sc.length, 
       sc.usertype , 
       sc.scale 
FROM   sysobjects  so , 
       syscolumns  sc 
WHERE  so.id = sc.id 
ORDER BY so.type, so.name, sc.name

/* get definition of each stored procedure */
SELECT so.name, 
       so.type, 
       sc.number, 
       sc.text 
FROM   sysobjects  so , 
       syscomments sc 
WHERE  so.id = sc.id 
ORDER BY so.type, so.name, sc.number 



回答2:


I hope I can help - this is the article I suggest reading:

Compare SQL Server database schemas automatically

It describes how you can automate the SQL Server schema comparison and synchronization process using T-SQL, SSMS or a third party tool.




回答3:


You can do it programatically by looking in the data dictionary (sys.objects, sys.columns etc.) of both databases and comparing them. However, there are also tools like Redgate SQL Compare Pro that do this for you. I have specified this as a part of the tooling for QA on data warehouse systems on a few occasions now, including the one I am currently working on. On my current gig this was no problem at all, as the DBA's here were already using it.

The basic methodology for using these tools is to maintain a reference script that builds the database and keep this in version control. Run the script into a scratch database and compare it with your target to see the differences. It will also generate patch scripts if you feel so inclined.

As far as I know there's nothing free that does this unless you feel like writing your own. Redgate is cheap enough that it might as well be free. Even as a QA tool to prove that the production DB is not in the configuration it was meant to be it will save you its purchase price after one incident.




回答4:


You can now use my SQL Admin Studio for free to run a Schema Compare, Data Compare and Sync the Changes. No longer requires a license key download from here http://www.simego.com/Products/SQL-Admin-Studio

Also works against SQL Azure.

[UPDATE: Yes I am the Author of the above program, as it's now Free I just wanted to Share it with the community]




回答5:


If you are looking for a tool that can compare two databases and show you the difference Red Gate makes SQL Compare




回答6:


You didn't mention which RDMBS you're using: if the INFORMATION SCHEMA views are available in your RDBMS, and if you can reference both schemas from the same host, you can query the INFORMATION SCHEMA views to identify differences in: -tables -columns -column types -constraints (e.g. primary keys, unique constraints, foreign keys, etc)

I've written a set of queries for exactly this purpose on SQL Server for a past job - it worked well to identify differences. Many of the queries were using LEFT JOINs with IS NULL to check for the absence of expected items, others were comparing things like column types or constraint names.

It's a little tedious, but its possible.




回答7:


I found this small and free tool that fits most of my needs. http://www.wintestgear.com/products/MSSQLSchemaDiff/MSSQLSchemaDiff.html

It's very basic but it shows you the schema differences of two databases. It doesn't have any fancy stuff like auto generated scripts to make the differences to go away and it doesn't compare any data.

It's just a small, free utility that shows you schema differences :)




回答8:


Make a table and store your version number in there. Just make sure you update it as necessary.

CREATE TABLE version (
    version VARCHAR(255) NOT NULL
)
INSERT INTO version VALUES ('v1.0');

You can then check the version number stored in the database matches the application code during your app's setup or wherever is convenient.




回答9:


SQL Compare by Red Gate.




回答10:


Which RDBMS is this, and how complex are the potential changes?

Maybe this is just a matter of comparing row counts and index counts for each table -- if you have trigger and stored procedure versions to worry about also then you need something more industrial




回答11:


Try dbForge Data Compare for SQL Server. It can compare and sync any databases, even very large ones. Quick, easy, always delivers a correct result. Try it on your database and comment upon the product.

We can recommend you a reliable SQL comparison tool that offer 3 time’s faster comparison and synchronization of table data in your SQL Server databases. It's dbForge Data Compare for SQL Server.

Main advantages:

  • Speedier comparison and synchronization of large databases
  • Support of native SQL Server backups
  • Custom mapping of tables, columns, and schemas
  • Multiple options to tune your comparison and synchronization
  • Generating comparison and synchronization reports

Plus free 30-day trial and risk-free purchase with 30-day money back guarantee.



来源:https://stackoverflow.com/questions/165401/how-to-compare-validate-sql-schema

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!