Pages

Thursday, May 22, 2014

How to Create Primary Keys in SQL Server

To create a primary key

  • Using SQL Server Management Studio
1. In Object Explorer, right-click the table to which you want to add a unique constraint, and click Design.

2. In Table Designer, click the row selector for the database column you want to define as the primary key. If you want to select multiple columns, hold down the CTRL key while you click the row selectors for the other columns.

3. Right-click the row selector for the column and select Set Primary Key.
  • Using Transact-SQL
1. In Object Explorer, connect to an instance of Database Engine.

2. On the Standard bar, click New Query.

3. Copy and paste the following example into the query window and click Execute. The example creates a primary key on the column .
ALTER TABLE tablename  ADD CONSTRAINT PK_ConName PRIMARY KEY CLUSTERED (column);


To create a primary key in a new table
1. In Object Explorer, connect to an instance of Database Engine.

2. On the Standard bar, click New Query.
3. Copy and paste the following example into the query window and click Execute. The example creates a table and defines a primary key on the column .

CREATE TABLE tablename ( columnName datatype NOT NULL, CONSTRAINT PK_
ConName PRIMARY KEY CLUSTERED (column) ); 
Friends, If you like this post, please share with your friends on Facebook and Google+ and recommend us on Google using the g+1 button on the top right hand corner.

No comments:

Post a Comment