3.5. SQL - DDL - Commands
3.5. SQL - DDL - Commands
DDL Commands
Saroj S Shivagunde
Overview
• Data Definition Language (DDL)
• CREATE Keyword
• Create new database, table or view
• DROP Keyword
• Delete a database, table or view
• ALTER Keyword
• Add/delete columns or constraints, change the data type of columns
• TRUNCATE Keyword
• Truncate a table
Data Definition Language (DDL)
• Structured Query Language (SQL) is used to build and access the database
• DDL has commands that facilitate the creation and deletion of databases, tables
and views
• It can also facilitate the alteration of the schema or datatype of columns of the
tables
• Creating a table
• Syntax- CREATE TABLE < Table_Name > ( < Column_Name_1 >< Datatype_1 >,
< Column_Name_2 >< Datatype_2 >, …
< Column_Name_n >< Datatype_n >);
• Example- Query to create a table named ‘Student’ that has 3 attributes- Roll number, Name
and Phone number
CREATE TABLE Student (Roll_No INT, Name VARCHAR(50), Phone_No INT);
CREATE Keyword
• Creating a view
• A view shows a subset of a table and it is virtual in nature
• To create a view we use SELECT statement that extracts the desired subset from the
underlying table in the dataset
• Syntax- CREATE VIEW < View_Name > AS < Subquery >;
• Example-
A query to create a view named ‘Student_View’ for a student with roll number 1. Being a
student, he can only see the details of himself.
CREATE VIEW Student_View AS SELECT ∗ FROM Students WHERE Roll_No = 1;
DROP Keyword
• DROP keyword is used to delete a database, table or a view
• Delete a database
• DROP DATABASE University;
• Delete a table
• DROP TABLE Student;
• Delete a view
• DROP VIEW Student_View;
ALTER Keyword
• ALTER keyword is used to add, modify or delete a column or a constraint from a
table
• TRUNCATE on a table
• TRUNCATE TABLE Students;