Database Fundamentals #32: Create Unique Constraints with T-SQL
In the last Database Fundamentals post, I explained what a unique constraint was and how you can create them using the GUI. Using TSQL to create a constraint is very similar to the primary key and foreign key constraints that you created in this post. You can use either the ALTER TABLE command or create the constraint when you make the table with the CREATE TABLE command. Using CREATE TABLE to Make a Unique Constraint There aren’t any new tables we need for this post, so we’ll create a table that we can drop immediately as soon as we’re done experimenting. You should already be familiar with the CREATE TABLE statement (or follow the link above): CREATE TABLE dbo.ConstraintTest ( TestName VARCHAR(50), CONSTRAINT UniqueName UNIQUE (TestName) ); This script creates…