SQL Server Documentation

Home Search

Tables in SQL Server

Tables are the primary storage structure for relational data in SQL Server. They consist of rows and columns where each column has a defined data type and constraints.

Key Concepts

ConceptDescription
Primary KeyUniquely identifies each row in a table.
Foreign KeyDefines a relationship to a primary key in another table.
IdentityAuto-incrementing numeric column.
Computed ColumnColumn whose value is derived from an expression.
PartitioningSplits a large table into smaller, manageable pieces.

Creating a Table

CREATE TABLE dbo.Employee (
    EmployeeID INT IDENTITY(1,1) PRIMARY KEY,
    FirstName NVARCHAR(50) NOT NULL,
    LastName NVARCHAR(50) NOT NULL,
    HireDate DATE NOT NULL,
    Salary MONEY CHECK (Salary >= 0),
    DepartmentID INT FOREIGN KEY REFERENCES dbo.Department(DepartmentID)
);

Altering a Table

You can add, modify, or drop columns and constraints using ALTER TABLE statements.

-- Add a column
ALTER TABLE dbo.Employee
ADD Email NVARCHAR(255);

-- Modify a column
ALTER TABLE dbo.Employee
ALTER COLUMN Salary MONEY NULL;

-- Drop a constraint
ALTER TABLE dbo.Employee
DROP CONSTRAINT CK_Employee_Salary;

Best Practices

Related Topics