Week 8-9 DDL Queries
Week 8-9 DDL Queries
DDL Queries
By shafiullah Durrani
Lecturer CS/IT
Agricultural university Peshawar
Data Definition Language(DDL)
•In order to make/perform changes on the physical structure of any table
residing inside a database, DDL is used. These commands when executed
are auto commit in nature and all the changes in the table are reflected
and saved immediately. DDL commands includes :
Creating Databases Code
A specific example:
CREATE DATABASE IT_students;
2) To drop a database:
DROP DATABASE database_name;
For Example:
DROP DATABASE It_students;
Remember to be careful with this command! Once you drop a database, it's gone!
3) Use a Database command
USE <database name>;
-- example:
USE IT_students;
Example
CREATE TABLE student (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
2) The SQL SHOW TABLE COLUMN STATEMENT
Example
ALTER TABLE Student
ADD Email varchar(255);
4) ALTER TABLE - DROP COLUMN
To delete a column in a table, use the following syntax (notice that
some database systems don't allow deleting a column):
ALTER TABLE table_name
DROP COLUMN column_name;
Example
ALTER TABLE Student
DROP COLUMN Email;
'students.name', 'StudentName' ,
EXEC sp_rename
'COLUMN' ;
SQL Constraints
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);
SQL constraints are used to specify rules for the data in a table.
Constraints are used to limit the type of data that can go into a table. This
ensures the accuracy and reliability of the data in the table. If there is any
violation between the constraint and the data action, the action is aborted.
•INDEX - Used to create and retrieve data from the database very quickly
1)SQL NOT NULL on CREATE TABLE
The following SQL ensures that the "ID", "LastName", and "FirstName" columns
will NOT accept NULL values when the "Persons" table is created:
Example
CREATE TABLE Student (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);
Example
DROP TABLE Student;