Posted on Monday December 20, 2010

SQL create table reference

Here’s a quick reference for scripting table creation in SQL server by hand, where the text is readable and pretty rather than an autogenerated mess.

It’s usually easier to bulk add the foreign keys after, that way the order of the CREATE TABLES doesn’t matter:

ALTER TABLE [dbo].[sometable] ADD CONSTRAINT [FK_sometable_users_id] 
	FOREIGN KEY([userid]) REFERENCES [dbo].[users] ([id])