<p>I am currently creating a master ddl for our database.  Historically we have used backup/restore to version our database, and not maintained any ddl scripts.  The schema is quite large.</p>

<p>My current thinking:</p>

<ul>
<li><p>Break script into parts (possibly in separate scripts):</p>

<ol>
<li>table creation</li>
<li>add indexes</li>
<li>add triggers</li>
<li>add constraints</li>
</ol></li>
<li><p>Each script would get called by the master script.</p></li>
<li>I might need a script to drop constraints temporarily for testing</li>
<li>There may be orphaned tables in the schema, I plan to identify suspect tables.</li>
</ul>

<p>Any other advice?</p>

<p>Edit: Also if anyone knows good tools to automate part of the process, we're using MS SQL 2000 (old, I know).</p>

[{'answer_id': 5005, 'author': 'Adam Lerman', 'author_id': 673, 'author_profile': 'https://Stackoverflow.com/users/673', 'pm_score': 1, 'selected': False, 'text': '<p>What you have there seems to be pretty good. My company has on occasion, for large enough databases, broken it down even further, perhaps to the individual object level. In this way each table/index/... has its own file. Can be useful, can be overkill. Really depends on how you are using it.</p>\n\n<p>@Justin</p>\n\n<p>By domain is mostly always sufficient. I agree that there are some complexities to deal with when doing it this way, but that should be easy enough to handle.  </p>\n\n<p>I think this method provides a little more seperation (which in a large database you will come to appreciate) while still making itself pretty manageable. We also write Perl scripts that do a lot of the processing of these DDL files, so that might be an option of a good way to handle that.</p>\n'}, {'answer_id': 5008, 'author': 'Stu', 'author_id': 414, 'author_profile': 'https://Stackoverflow.com/users/414', 'pm_score': 1, 'selected': False, 'text': '<p>Invest the time to write a generic "drop all constraints" script, so you don\'t have to maintain it. </p>\n\n<p>A cursor over the following statements does the trick.</p>\n\n<pre><code>Select * From Information_Schema.Table_Constraints \n\nSelect * From Information_Schema.Referential_Constraints\n</code></pre>\n'}, {'answer_id': 5016, 'author': 'Justin Standard', 'author_id': 92, 'author_profile': 'https://Stackoverflow.com/users/92', 'pm_score': 1, 'selected': False, 'text': '<p>@Adam</p>\n\n<p>Or how about just by domain -- a useful grouping of related tables in the same file, but separate from the rest?</p>\n\n<p>Only problem is if some domains (in this somewhat legacy system) are tightly coupled.  Plus you have to maintain the dependencies between your different sub-scripts.</p>\n'}, {'answer_id': 15793, 'author': 'vzczc', 'author_id': 224, 'author_profile': 'https://Stackoverflow.com/users/224', 'pm_score': 0, 'selected': False, 'text': '<p>I previously organised my DDL code organised by one file per entity and made a tool that combined this into a single DDL script.</p>\n\n<p>My former employer used a scheme where all table DDL was in one file (stored in oracle syntax), indicies in another, constraints in a third and static data in a fourth. A change script was kept in paralell with this (again in Oracle). The conversion to SQL was manual. It was a mess. I actually wrote a handy tool that will convert Oracle DDL to SQL Server (it worked 99.9% of the time).</p>\n\n<p>I have recently switched to using <a href="http://msdn.microsoft.com/en-us/vsts2008/db/default.aspx" rel="nofollow noreferrer">Visual Studio Team System for Database professionals</a>. So far it works fine, but there are some glitches if you use CLR functions within the database.</p>\n'}, {'answer_id': 57111, 'author': 'Mat Roberts', 'author_id': 4401, 'author_profile': 'https://Stackoverflow.com/users/4401', 'pm_score': 3, 'selected': False, 'text': '<p>I think the basic idea is good.  </p>\n\n<p>The nice thing about building all the tables first and then building all the constraints, is that the tables can be created in any order.  When I\'ve done this I had one file per table, which I put in a directory called "Tables" and then a script which executed all the files in that directory.  Likewise I had a folder for constraint scripts (which did foreign key and indexes too), which were executed when after the tables were built.</p>\n\n<p>I would separate the build of the triggers and stored procedures, and run these last.  The point about these is they can be run and re-run on the database without affecting the data.  This means you can treat them just like ordinary code.  You should include "if exists...drop" statements at the beginning of each trigger and procedure script, to make them re-runnable.</p>\n\n<p>So the order would be</p>\n\n<ol>\n<li>table creation</li>\n<li>add indexes</li>\n<li>add constraints</li>\n</ol>\n\n<p>Then</p>\n\n<ol start="4">\n<li>add triggers</li>\n<li>add stored procedures</li>\n</ol>\n\n<p>On my current project we are using MSBuild to run the scripts.  There are some <a href="http://www.codeplex.com/sdctasks" rel="noreferrer">extension targets</a> that you can get for it which allow you to call sql scripts.  In the past I have used perl which was fine too (and batch files...which I would not recommend - the\'re too limited).</p>\n'}, {'answer_id': 76191, 'author': 'Philippe Grondier', 'author_id': 11436, 'author_profile': 'https://Stackoverflow.com/users/11436', 'pm_score': 1, 'selected': False, 'text': '<p>If you are looking for an automation tool, I have often worked with EMS SQLManager, which allows you to generate automatically a ddl script from a database.</p>\n\n<p>Data inserts in reference tables might be mandatory before putting your database on line. This can even be considered as part of the ddl script. EMS can also generate scripts for data inserts from existing databases.</p>\n\n<p>Need for indexes might not be properly estimated at the ddl stage. You will just need to declare them for primary/foreign keys. Other indexes should be created later, once views and queries have been defined </p>\n'}, {'answer_id': 3593835, 'author': 'Darryl', 'author_id': 284749, 'author_profile': 'https://Stackoverflow.com/users/284749', 'pm_score': 1, 'selected': False, 'text': '<p>there is a neat tools that will iterate through the entire sql server and extract all the table, view, stored proceedures and UDF defintions to the local file system as SQL scripts (Text Files). I have used this with 2005 and 2008, not sure how it wil work with 2000 though. Check out <a href="http://www.antipodeansoftware.com/Home/Products" rel="nofollow noreferrer">http://www.antipodeansoftware.com/Home/Products</a></p>\n'}]
['https://Stackoverflow.com/questions/4994', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/92/']