Week 12-13 DDL
Week 12-13 DDL
Lecture # 23-26
Instructor
Rida Ayesha
Lecture # 23-26 Agenda:
SQL: Data Definition Language (Create, Alter, Drop, Truncate)
Introduction to SQL
• Objectives of SQL
• History of SQL
• Importance of SQL
• Terminology
Data Definition
• Creating a Database
• Dropping & Renaming a Database
• Creating a Table (CREATE TABLE with default & named constraints)
• Changing a Table Definition (ALTER TABLE – add column(s), change datatype of column(s),
rename column(s), drop column(s), add constraint(s), drop constraint(s))
• Removing a Table (DROP TABLE)
• Truncate a Table (TRUNCATE TABLE)
2
Introduction to SQL
What is SQL?
SQL (Structured Query Language) is a standard language used to manage and manipulate
relational databases.
Objectives of SQL
History of SQL
Importance of SQL
Key Terminology
Term Meaning
3
The ISO SQL Data Types
SQL Identifiers
Image
Binary BINARY(n), VARBINARY(n) Binary data
VARBINARY(MAX)
UID
Unique ID UNIQUEIDENTIFIER Globally unique value
UNIQUEIDENTIFIER
4
Data Type Description Example
DECIMAL(p,s) Fixed precision and scale numbers DECIMAL(5,2) → 123.45
FLOAT Approximate floating point numbers 123.4567
4. Boolean / Logical
5. Unique Identifiers
6. Binary Data
5
Examples: Choosing Data Types in a Table
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(100),
BirthDate DATE,
IsActive BIT,
Salary DECIMAL(10, 2)
);
1. Single-Line Comment
2. Multi-Line Comment
/*
This query retrieves employee details
who are currently active
*/
SELECT * FROM Employees
WHERE IsActive = 1;
6
SQL Constraints in Table Creation
Constraints enforce rules on data.
PRIMARY
Uniquely identifies each row EmployeeID INT PRIMARY KEY
KEY
UNIQUE Ensures all values in a column are different Email VARCHAR(100) UNIQUE
7
Salary DECIMAL(10, 2),
IsActive BIT DEFAULT 1,
DeptID INT,
-- Constraints
CONSTRAINT pk_empId PRIMARY KEY (EmployeeID),
CONSTRAINT uq_emp_email UNIQUE (Email),
CONSTRAINT chk_emp_salary CHECK (Salary > 0),
CONSTRAINT fk_emp_dept FOREIGN KEY (DeptID) REFERENCES Departments(DeptID)
);
Primary Key CONSTRAINT pk_empId PRIMARY KEY (...) Named pk_empId for EmployeeID
Modify column datatype ALTER TABLE Employees ALTER COLUMN Name VARCHAR(150);
Add constraint ALTER TABLE Employees ADD CONSTRAINT chk_salary CHECK (Salary > 1000);
8
ALTER TABLE table_name
ADD column_name datatype NOT NULL DEFAULT default_value;
Example
Let’s say you have an Employees table, and you want to add a new column called Gender that
should not be null.
This will:
Without a DEFAULT:
If the table has existing data and you don't use DEFAULT, this will throw an error:
DROP TABLE Delete the entire table & structure DROP TABLE Employees;
TRUNCATE TABLE Remove all rows (faster than DELETE) TRUNCATE TABLE Employees;
Notes:
9
RENAME Operations in SQL Server
1. Rename a Column
Syntax:
EXEC sp_rename 'table_name.old_column_name', 'new_column_name', 'COLUMN';
Example:
2. Rename a Table
Syntax:
EXEC sp_rename 'old_table_name', 'new_table_name';
Example:
3. Rename a Database
Renaming a database must be done carefully — make sure no one is connected to it.
Example:
ALTER DATABASE CompanyDB MODIFY NAME = CorporateDB;
Ensure:
• You are not currently using the database you're trying to rename.
• Switch to master first:
USE master;
10
DDL for Relationships in SQL Server
1. One-to-One (1:1)
Definition: One record in Table A is related to one and only one record in Table B.
Explanation:
2. One-to-Many (1:N)
Definition: One record in Table A can be related to many records in Table B.
Explanation:
11
• One department can have many employees.
• Each employee belongs to only one department.
You can define it after the column definitions using PRIMARY KEY (column1, column2, ...).
Example:
CREATE TABLE Orders (
OrderID INT,
ProductID INT,
Quantity INT,
PRIMARY KEY (OrderID, ProductID)
);
2. Named Constraint
Notes:
12
3. Many-to-Many (M:N)
Definition: Multiple records in Table A relate to multiple records in Table B.
-- Junction table
CREATE TABLE StudentCourse (
StudentID INT,
CourseID INT,
EnrollmentDate DATE,
PRIMARY KEY (StudentID, CourseID),
FOREIGN KEY (StudentID) REFERENCES Student(StudentID),
FOREIGN KEY (CourseID) REFERENCES Course(CourseID)
);
Explanation:
13