Learn Sql Server   Learn Sql Theory   Learn Sql

 
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8

Stuff function in Sql Server

comma separated values in sql server

OUTPUT Clause with INSERT,UPDATE, DELETE

Deleting Rows in Chunks

Combining Result Sets with UNION

Using the TABLESAMPLE to Return Random Rows

Summarizing Data Using WITH CUBE

Using GROUPING with WITH CUBE

Summarizing Data with WITH ROLLUP

Adding a Column to an Existing Table

Changing a Column Definition

Dropping a Table Column

Sql Server Case Sensitive

CreateTable with a Primary Key

Adding a Primary Key Constraint to an Existing Table

Creating a Table with a Foreign Key in Sql Server

Adding a Foreign Key to an Existing Table

Sql Server Rebuilding Indexes

Rename a Column Name or Table Name

Displaying the Oldest Active Transaction

using group by all

Selectively Query Grouped Data Using HAVING

Sql Table Variables

Introduction to Locking in SQL Server

Viewing Lock Activity in Sql

Sql Server Isolation Levels

Using SET TRANSACTION ISOLATION LEVEL

Sql Server Blocking

Using Set Lock Timeout

Sql Server Deadlocking

Setting Deadlock Priority

Chapter 9
Chapter 10
Chapter 11
Chapter 12
Chapter 13
Chapter 14
Chapter 15
Chapter 16

Adding a Primary Key Constraint to an Existing Table in Sql Server

In this recipe, I’ll demonstrate how to add a primary key to an existing table using ALTER TABLE and ADD CONSTRAINT:

ALTER TABLE Person.EducationType
ADD CONSTRAINT PK_EducationType
PRIMARY KEY (EducationTypeID)

How It Works


In this recipe, ALTER TABLE was used to add a new primary key to an existing table that doesn’t already have one defined. The first line of code defined the table to add the primary key to:
ALTER TABLE Person.EducationType
The second line of code defined the constraint name:
ADD CONSTRAINT PK_EducationType On the last line of code in the previous example, the constraint type PRIMARY KEY was declared, followed by the column defining the key column in parentheses:
PRIMARY KEY (EducationTypeID)

Sponsored Links