Create Index Syntax

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 the leaf level of the index. Create a single nonclustered index CREATE UNIQUE NONCLUSTERED INDEX IX_NC_PresidentNumber — specify index name     ON dbo.Presidents (PresidentNumber) — specify table and column name Create a multi-column (composite) nonclustered index CREATE UNIQUE NONCLUSTERED INDEX IX_NC_PresidentNumber_PresidentName — specify index name     ON dbo.Presidents (PresidentNumber,PresidentName) — specify table and column names Create a multi-column (composite) clustered index CREATE UNIQUE CLUSTERED INDEX IX_C_PresidentNumber — specify index name     ON dbo.Presidents (PresidentNumber,PresidentName) — specify table and column names Create a non clustered index with included columns CREATE NONCLUSTERED INDEX IX_NC_PresidentNumber — specify index name ON dbo.Presidents (PresidentNumber) — specify table and column name INCLUDE (President,YearsInOffice,RatingPoints) — specify included columns Create index with fill factor CREATE NONCLUSTERED INDEX IX_NC_PresidentNumber — specify index name ON dbo.Presidents (PresidentNumber) — specify table and column name     WITH (FILLFACTOR = 80) — specify the fill factor SQL Server 2008 options SQL Server 2008 provides additional index options. Most notably it provides the ability to filter an index. This can help decrease the size of the index for very large or partitioned table to only include the records that are normally queried. CREATE NONCLUSTERED INDEX IX_NC_PresidentNumber ON dbo.Presidents […]

Continue reading ...

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 exist in the table. For newly inserted records, the identity field will increment properly. Add a primary key for an existing column ALTER TABLE dbo.Presidents WITH NOCHECK ADD CONSTRAINT PK_PresidentNumber PRIMARY KEY NONCLUSTERED (PresidentNumber)

Continue reading ...

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()   ,Comments VARCHAR(255) SPARSE NULL — SQL Server 2008 sparse column )

Continue reading ...

Rename Column

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 just performed may have a negative effect on any stored procedures referencing the column. Make sure to check to see if any stored procedures are referencing the column prior to renaming it. If the column has a constraint, you must drop the constraint prior to renaming the column. Otherwise you will get the following error message: [code] Msg 15336, Level 16, State 1, Procedure sp_rename, Line 444 Object ‘Orders.OrderAmount’ cannot be renamed because the object participates in enforced dependencies. [/code] To drop the constraint, use the following example: ALTER TABLE Employee DROP CONSTRAINT pk_employee

Continue reading ...

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. For the example below, we will begin by creating a sample table, then we will modify the columns. 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 ) — Change the datatype to support 100 characters and make NOT NULL ALTER TABLE dbo.Employee ALTER COLUMN FirstName VARCHAR(100) NOT NULL — Change datatype and allow NULLs for DateHired ALTER TABLE dbo.Employee ALTER COLUMN DateHired SMALLDATETIME NULL — Set SPARSE columns for Middle Name (sql server 2008 only) ALTER TABLE dbo.Employee ALTER COLUMN MiddleName VARCHAR(100) SPARSE NULL Columns can be altered in place using alter column statement. Only the datatype, sparse attribute (2008) and the Nullable attribute of a column can be changed. You cannot add a NOT NULL specification if NULL values exist. In order to change or add a default value of a column, you need to use Add/Drop Constraint. In order to rename a column, you must use sp_rename.

Continue reading ...

Featured Articles

 Site Author

  • Thanks for visiting!