Skip to content
Archive of posts filed under the DDL category.

Add Column Constraint

To add a constraint to an existing table use the alter table statement with the add constraint command. There are four different types of constraints:

Primary Key Constraints – Enforces unique values for specified column, can be referenced.
Foreign Key Constraints – Enforces a reference to a primary key
Unique Constraints – Ensures unique values within a [...]

Add Column Default Value

In SQL Server there are two ways to add a column with a default value.
Add Default Value to Existing Column

– Add default to existing column DateOfHire:
ALTER TABLE [dbo].[Employees] ADD DEFAULT (getdate()) FOR [DateOfHire]

– Add default value to existing column IsTerminated
ALTER TABLE [dbo].[Employees] ADD DEFAULT ((0)) FOR [IsTerminated]

Add New Column with Default Value

– Add [...]

Add Foreign Key

To Add a foreign key to a column in an existing table, use ALTER TABLE ADD CONSTRAINT

ALTER TABLE dbo.President_Lookup
ADD CONSTRAINT fk_PresidentID
FOREIGN KEY (PresidentID)
REFERENCES dbo.Presidents (PresidentID)

Add Constraint

To add a constraint to an existing table use the alter table statement with the add constraint command. There are four different types of constraints:

Primary Key Constraints – Enforces unique values for specified column, can be referenced.
Foreign Key Constraints – Enforces a reference to a primary key
Unique Constraints – Ensures unique values within a [...]

Add Index

To add an index in SQL Server use the CREATE INDEX statements. When adding indexes remember there can only be one clustered index per table. The main options when creating an index are clutered or nonclustered or unique vs non unique. Using SQL 2005+ you can also specify columns to include at [...]

Add Primary Key

In order to add a primary key to an existing table we need to use the Alter Table command.
Add a primary key for a new column

ALTER TABLE dbo.Presidents
ADD PresidentID int NOT NULL IDENTITY(1,1) CONSTRAINT PK_PresidentID PRIMARY KEY NONCLUSTERED

Note however that the ordering of the identity field will not be predictable for the records that already [...]

Create Table

Below is the basic syntax for the create table script:

CREATE TABLE dbo.Employees
(
EmployeeID int IDENTITY (1,1) NOT NULL CONSTRAINT PK_EmployeeID PRIMARY KEY NONCLUSTERED
,FirstName varchar(50) NULL
,LastName varchar(50) NULL
,SSN varchar(9) NULL CONSTRAINT ssn_unique CHECK (SSN like ‘[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]‘) UNIQUE
,IsTerminated bit NOT NULL DEFAULT 0
,DateAdded datetime DEFAULT GETDATE()
[...]

Rename Column Name

In order to rename a column name, you must use sp_rename. The syntax of sp_rename is the following:
exec sp_rename ‘tablename.ColumnName’, ‘NewColumnName’, ‘column’ –objecttype
Here is an example:

CREATE TABLE Employee
(
ID int,
FName varchar(50)
)
GO

EXEC sp_rename ‘Employees.FName’, ‘FirstName’, ‘column’

After executing the script above, you will receive the following informational message:

This comes up as a reminder that the rename you [...]

Alter Table Alter Column

The Alter Column statement can modify the data type and the Nullable attribute of a column. The syntax is the same for SQL Server 2005 and SQL Server 2008 except 2008 allows the sparse attribute to be changed.

CREATE TABLE dbo.Employee
(
EmployeeID INT IDENTITY (1,1) NOT NULL
,FirstName VARCHAR(50) NULL
,MiddleName VARCHAR(50) NULL
,LastName VARCHAR(50) NULL
,DateHired datetime NOT NULL
)

– [...]

Alter Table Add Column

CREATE TABLE dbo.Employees
(
EmployeeID int IDENTITY (1,1) NOT NULL PRIMARY KEY NONCLUSTERED
)
GO

ALTER TABLE dbo.Employees
ADD
FirstName varchar(50) NULL
,LastName varchar(50) NULL
,SSN varchar(9) NULL CONSTRAINT ssn_unique UNIQUE
,IsTerminated bit NOT NULL DEFAULT 0
,DateAdded datetime DEFAULT GETDATE()
,Comments varchar(255) SPARSE NULL — SQL Server 2008 sparse column

Multiple columns can be specificied by using the syntax above. First issue an alter table [...]