Program 1:
DDL (Data Definition Language) Commands in SQL
DDL commands are used to define, modify, and remove database objects such as tables.
1. CREATE TABLE
The CREATE TABLE command is used to create a new table in a database.
Syntax: CREATE TABLE Student (
Student_ID INT PRIMARY KEY,
CREATE TABLE table_name ( Name VARCHAR(50),
column1 datatype constraint, Age INT,
column2 datatype constraint, Course VARCHAR(30)
... );
);
Output:
A table named Student is created with four columns:
Student_ID
Name
Age
Course
2. ALTER TABLE
The ALTER TABLE command is used to modify the structure of an existing table.
(a) Add a New Column
ALTER TABLE table_name ALTER TABLE Student
ADD column_name datatype; ADD Email VARCHAR(100);
Result:
A new column Email is added to the Student table.
(b) Modify a Column
Example:
ALTER TABLE Student ALTER TABLE Student
MODIFY column name data type(100); MODIFY Name VARCHAR(100);
(c) Drop a Column
Example:
ALTER TABLE Student
DROP COLUMN Age;
Result:
The Age column is removed from the Student table.
3. DROP TABLE
The DROP TABLE command is used to permanently delete a table and all its data from
the database.
Syntax:
DROP TABLE table_name; DROP TABLE Student;
Result:
The Student table and all records stored in it are permanently removed from the
database.
Command Purpose Example
CREATE TABLE Creates a new table CREATE TABLE Student (...);
ALTER TABLE Student ADD Email
ALTER TABLE Modifies an existing table
VARCHAR(100);
DROP TABLE Deletes a table permanently DROP TABLE Student;
CREATE TABLE Students ( StudentID INT PRIMARY KEY, FirstName VARCHAR(50) NOT
NULL,
LastName VARCHAR(50), EnrollmentDate DATE );
ALTER TABLE Students ADD Email VARCHAR(100);
ALTER TABLE Students DROP COLUMN LastName;
ALTER TABLE Students MODIFY COLUMN FirstName VARCHAR(100);
DROP TABLE Students;