Skip to content

DDL in SQL Server: CREATE, ALTER, DROP, TRUNCATE

DDL commands create or change the STRUCTURE of tables.

CREATE   -> make a new table
ALTER    -> change a table
DROP     -> delete a table completely
TRUNCATE -> delete all rows, keep the table

This lesson creates its own small tables, so you can run it on any SQL Server database. The Output under each query is the real result.

-- Create a database for these basic lessons (only if it does not exist)
IF DB_ID('SqlBasicsDb') IS NULL
    CREATE DATABASE SqlBasicsDb;
GO

USE SqlBasicsDb;
GO

-- Delete old tables if they exist, so this file can be run again
DROP TABLE IF EXISTS Marks;
DROP TABLE IF EXISTS Students;
DROP TABLE IF EXISTS Teachers;
GO

CREATE Table

CREATE TABLE Students
(
    StudentID INT,
    Name      VARCHAR(50),
    Age       INT
);

SELECT * FROM Students;    -- empty table
Output
StudentIDNameAge
(0 rows)

ALTER Table - add a new column

ALTER TABLE Students ADD City VARCHAR(50);

SELECT * FROM Students;    -- City column is added
Output
StudentIDNameAgeCity
(0 rows)

ALTER Table - change a column's data type / size

ALTER TABLE Students ALTER COLUMN Name VARCHAR(100);

ALTER Table - remove a column

ALTER TABLE Students DROP COLUMN City;

Rename a column (SQL Server uses sp_rename)

EXEC sp_rename 'Students.Name', 'StudentName', 'COLUMN';

SELECT * FROM Students;
Messages
Caution: Changing any part of an object name could break scripts and stored procedures.
Output
StudentIDStudentNameAge
(0 rows)

TRUNCATE - removes All rows, but the table stays

INSERT INTO Students VALUES (1, 'Ravi', 20);
INSERT INTO Students VALUES (2, 'Sita', 21);
SELECT * FROM Students;    -- 2 rows

TRUNCATE TABLE Students;
SELECT * FROM Students;    -- 0 rows, table still exists
Messages
(1 row affected)
(1 row affected)
Output
StudentIDStudentNameAge
1Ravi20
2Sita21
Output
StudentIDStudentNameAge
(0 rows)

DROP - deletes the table completely

DROP TABLE Students;
-- SELECT * FROM Students;   -- this would now give an error: Invalid object name

-- =============================================
-- CONSTRAINTS (rules on columns)
--   PRIMARY KEY -> unique + not empty, identifies each row
--   NOT NULL    -> value is required
--   UNIQUE      -> no duplicate values
--   DEFAULT     -> value used when none is given
--   CHECK       -> value must pass a condition
--   FOREIGN KEY -> value must exist in another table
-- =============================================

CREATE TABLE Students
(
    StudentID INT PRIMARY KEY,
    Name      VARCHAR(50) NOT NULL,
    Email     VARCHAR(100) UNIQUE,
    Age       INT CHECK (Age >= 5),
    City      VARCHAR(50) DEFAULT 'Hyderabad'
);

CREATE TABLE Marks
(
    MarkID    INT PRIMARY KEY,
    StudentID INT FOREIGN KEY REFERENCES Students(StudentID),
    Subject   VARCHAR(30),
    Marks     INT
);

-- IDENTITY : SQL Server fills the number automatically (1, 2, 3 ...)
CREATE TABLE Teachers
(
    TeacherID INT IDENTITY(1,1) PRIMARY KEY,
    Name      VARCHAR(50)
);

INSERT INTO Teachers (Name) VALUES ('Meena');
INSERT INTO Teachers (Name) VALUES ('Kiran');
SELECT * FROM Teachers;    -- TeacherID is 1 and 2 automatically

-- See the columns of a table
EXEC sp_help 'Students';
Messages
(1 row affected)
(1 row affected)
Output
TeacherIDName
1Meena
2Kiran
Output
NameOwnerTypeCreated_datetime
Studentsdbouser table2026-10-18 18:22:00.167
Output
Column_nameTypeComputedLengthPrecScaleNullableTrimTrailingBlanksFixedLenNullInSourceCollation
StudentIDintno410 0 no(n/a)(n/a)NULL
Namevarcharno50 nononoSQL_Latin1_General_CP1_CI_AS
Emailvarcharno100 yesnoyesSQL_Latin1_General_CP1_CI_AS
Ageintno410 0 yes(n/a)(n/a)NULL
Cityvarcharno50 yesnoyesSQL_Latin1_General_CP1_CI_AS
Output
IdentitySeedIncrementNot For Replication
No identity column defined.NULLNULLNULL
Output
RowGuidCol
No rowguidcol column defined.
Output
Data_located_on_filegroup
PRIMARY
Output
index_nameindex_descriptionindex_keys
PK__Students__32C52A79A4BECDC0clustered, unique, primary key located on PRIMARYStudentID
UQ__Students__A9D1053452D0D8B6nonclustered, unique, unique key located on PRIMARYEmail
Output
constraint_typeconstraint_namedelete_actionupdate_actionstatus_enabledstatus_for_replicationconstraint_keys
CHECK on column AgeCK__Students__Age__6442E2C9(n/a)(n/a)EnabledIs_For_Replication([Age]>=(5))
DEFAULT on column CityDF__Students__City__65370702(n/a)(n/a)(n/a)(n/a)('Hyderabad')
PRIMARY KEY (clustered)PK__Students__32C52A79A4BECDC0(n/a)(n/a)(n/a)(n/a)StudentID
UNIQUE (non-clustered)UQ__Students__A9D1053452D0D8B6(n/a)(n/a)(n/a)(n/a)Email
Messages
No views with schema binding reference table 'Students'.
... 1 more result sets