Ensuring Data Integrity with Unique Constraints in SQL Server 2005
Data integrity is paramount in database management. One crucial aspect is guaranteeing the uniqueness of specific values within a table. This is readily achieved by implementing a unique constraint. This guide explains how to create these constraints in SQL Server 2005.
Using T-SQL
The Transact-SQL (T-SQL) command provides a direct method for adding unique constraints:
<code class="language-sql">ALTER TABLE <tablename> ADD CONSTRAINT <constraintname> UNIQUE NONCLUSTERED (<columnname>)</code>
For instance:
<code class="language-sql">ALTER TABLE Employee ADD CONSTRAINT UniqueEmployeeID UNIQUE NONCLUSTERED (EmployeeID)</code>
This code adds a unique constraint named UniqueEmployeeID
to the Employee
table, ensuring that the EmployeeID
column contains only unique values.
Graphical Approach via Database Diagram
Alternatively, you can create unique constraints using the database diagram interface:
By employing either method, you effectively prevent duplicate entries in the specified column(s), maintaining data accuracy and consistency.
The above is the detailed content of How to Create a Unique Constraint in SQL Server 2005?. For more information, please follow other related articles on the PHP Chinese website!