0% found this document useful (0 votes)
4 views

SQL DDL DML DQL

The document provides an overview of SQL commands categorized into DDL, DML, DQL, DCL, and TCL, detailing their functions and examples. It also covers schema refinement concepts such as normalization forms and integrity constraints. Additionally, it explains various SQL statements for creating, modifying, and querying databases and tables.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

SQL DDL DML DQL

The document provides an overview of SQL commands categorized into DDL, DML, DQL, DCL, and TCL, detailing their functions and examples. It also covers schema refinement concepts such as normalization forms and integrity constraints. Additionally, it explains various SQL statements for creating, modifying, and querying databases and tables.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

lOMoARcPSD|40804004

UNIT – III
SQL: QUERIES, CONSTRAINTS, TRIGGERS: form of basic SQL query, UNION, INTERSECT,
and EXCEPT, Nested Queries, aggregation operators, NULL values, complex integrity constraints in
SQL, triggers and active databases. Schema Refinement: Problems caused by redundancy,
decompositions, problems related to decomposition, reasoning about functional dependencies, FIRST,
SECOND, THIRD normal forms, BCNF, lossless join decomposition, multi-valued dependencies,
FOURTH normal form, FIFTH normal form.

1. SQL COMMANDS
Structured Query Language (SQL) is the database language used to create a database and
to perform operations on the existing database. SQL commands are instructions used to
communicate with the database to perform specific tasks and queries with data. These SQL
commands are categorized into five categories as:

i. DDL: Data Definition Language


ii. DML: Data Manipulation Language
iii. DQL: Data Query Language
iv. DCL : Data Control Language
v. TCL : Transaction Control Language.

SQL commands

DDL DML DQL DCL TCL


Data Definition Data Manipulation Data Query Data Control Transaction
Language Language Language Language Control Language

CREATE INSERT GRANT COMMIT


SELECT

ALTER DELETE REVOKE ROLLBACK

DROP SAVEPOINT
UPDATE

TRUNCATE

i. DDL(Data Definition Language) : DDL or Data Definition Language consists of the


SQL commands that can be used to define the database schema. It simply deals with
descriptions of the database schema and is used to create and modify the structure of
database objects in the database. The DQL commands are:

Ravindar.M, Asso.Prof, CSE Dept, JITS-KNR

Downloaded by Vemula Satish ([email protected])


lOMoARcPSD|40804004

 CREATE: It is used to create the database or its objects (like table, index, function,
views, store procedure and triggers).
 DROP: It is used to delete objects from the database.
 ALTER: It is used to alter the structure of the database.
 TRUNCATE: It is used to remove all records from a table, including all spaces
allocated for the records are removed.
ii. DQL (Data Query Language): DML statements are used for performing queries on the
data within schema objects. The purpose of DQL Command is to get data from some schema
relation based on the query passed to it. The DQL commands are:
 SELECT – is used to retrieve data from the database.

iii. DML (Data Manipulation Language): The SQL commands that deals with the
manipulation of data present in the database belong to DML or Data Manipulation Language
and this includes most of the SQL statements. The DML commands are:

 INSERT – is used to insert data into a table.


 UPDATE – is used to update existing data within a table.
 DELETE – is used to delete records from a database table.
iv. DCL (Data Control Language): DCL includes commands which mainly deal with the
rights, permissions and other controls of the database system. The DCL commands are:
 GRANT-gives user’s access privileges to database.
 REVOKE-withdraw user’s access privileges given by using the GRANT command.
v. TCL (transaction Control Language): TCL commands deals with the transaction
within the database. The TCL commands are:
 COMMIT– commits a Transaction.
 ROLLBACK– rollbacks a transaction in case of any error occurs.
 SAVEPOINT–sets a save point within a transaction.

2. DDL COMMANDS
DDL or Data Definition Language consists of the SQL commands that can be used to define
the database schema. It simply deals with descriptions of the database schema and is used to
create and modify the structure of database objects in the database. The DQL commands are:

Ravindar.M, Asso.Prof, CSE Dept, JITS-KNR

Downloaded by Vemula Satish ([email protected])


lOMoARcPSD|40804004

i. CREATE: It is used to create the database or its objects like table, index, function, views,
store procedure and triggers.

a) The ‘CREATE DATABASE’ Statement: This statement is used to create a database.

Syntax: CREATE DATABASE Database_Name;

Example: CREATE DATABASE Employee;

It creates Employee database.

b) The ‘CREATE TABLE’ Statement: This statement is used to create a table.


Syntax:
CREATE TABLE TableName (
Column1 datatype(size)[column_constraint],
Column2 datatype(size)[column_constraint],
....
ColumnN datatype(size)[column_constraint],
[table_constraint]
[,table_constraint]
);

Note: The content in the square brackets indicates it is optional. If not required, you can skip it.

Column constraints

o PRIMARY KEY // Use only, If one column name as primary key.


o NOT NULL // It does not accept NULL value in that column.
o DEFAULT value // It store default value in that column, if no value is inserted
o UNIQUE // It allows to store only unique values in the column

Table constraints

o PRIMARY KEY(column_name1, column_name2, …)


Use it, If one column name or multiple column names acts as primary key.
o UNIQUE(column_name1, column_name2, …)
Use it, if one column name or multiple column names should contain unique values.
If multiple column names are used, then for each row, it consider values from all the columns
mentioned to decide the uniqueness, but not column wise.
o FOREIGN KEY (column_name1) REFERENCES other_table_name (column_name2)
It is used to link data from one table to other table.
o CHECK(condition)
It does not allow inserting value(s), if the condition is not satisfied. The condition may also
contain multiple column names.

Ravindar.M, Asso.Prof, CSE Dept, JITS-KNR

Downloaded by Vemula Satish ([email protected])


lOMoARcPSD|40804004

Example 1: Creating table without any constraints


CREATE TABLE Employee_Info
(
EmployeeID int,
EmployeeName varchar(20),
PhoneNumber numeric(10),
City varchar(20),
Country varchar(20)
);
Example 2: Using PRIMARY KEY and NOT NULL as column constraints
CREATE TABLE Departments
(
DeptID int PRIMARY KEY,
DeptName varchar(20)NOT NULL,
Hod varchar(20),
Location varchar(20)
);
Example 3: Using PRIMARY KEY, NOT NULL, UNIQUE and DEFAULT as column constraints and FOREIGN
KEY as table constraint.
CREATE TABLE Students_Info
(
HallTicketNo int PRIMARY KEY,
Name varchar(20)NOT NULL,
Mobile numeric(10)NOT NULL UNIQUE,
DepartmentID int,
City varchar(20)DEFAULT ‘Hyderabad’,
FOREIGN KEY(DepartmentID) REFERENCES Departments (DeptID)
);

Example 4: Using NOT NULL, UNIQUE as column constraints and PRIMARY KEY and CHECK as table
constraints.
CREATE TABLE Voter_list
(
VoterID numeric(10),
AdhaarNo numeric(12)NOT NULL UNIQUE,
Name varchar(20)NOT NULL,
Age int,
Mobile numeric(10) UNIQUE,
City varchar(20),
PRIMARY KEY(VoterID),
CHECK(AGE>18)
);

c) The ‘CREATE TABLE AS’ Statement: You can also create a table from another
existing table. The newly created table also contains data of existing table.
Syntax: CREATE TABLE NewTableName AS(SELECT Column1, column2, ..., ColumnN
FROM ExistingTableName
WHERE [condition]);

Example: CREATE TABLE ExampleTable AS ( SELECT EmployeeName, PhoneNumber


FROM Employee_Info );

Ravindar.M, Asso.Prof, CSE Dept, JITS-KNR

Downloaded by Vemula Satish ([email protected])


lOMoARcPSD|40804004

ii. DROP: This statement is used to drop an existing table or a database.


a) The ‘DROP DATABASE’ Statement: This statement is used to drop an existing
database. When you use this statement, complete information present in the database will
be lost.
Syntax: DROP DATABASE DatabaseName; .

Example: DROP DATABASE Employee;

b) The ‘DROP TABLE’ Statement: This statement is used to drop an existing table. When
you use this statement, complete information present in the table will be lost.
Syntax: DROP TABLE TableName; .

Example: DROP TABLE Employee;

iii. TRUNCATE: This command is used to delete the information present in the table but
does not delete the table. So, once you use this command, your information will be lost, but
not the table.
Syntax: TRUNCATE TABLE TableName; .

Example: TRUNCATE TABLE Employee_Info;

iv. ALTER: This command is used to add, delete or modify column(s) in an existing table. It
can also be used to rename the existing table and also to rename the existing column name.
a) The ‘ALTER TABLE’ with ADD column: You can use this command to add a new
column to the existing table.
Syntax: ALTER TABLE TableName
ADD ColumnName Datatype;

Example: Adding Blood Group column to the Employee_Info table


ALTER TABLE Employee_Info
ADD BloodGroup varchar(10);
b) The ‘ALTER TABLE’ with DROP column: You can use this command to remove a
column from the existing table.
Syntax: ALTER TABLE TableName
DROP ColumnName;
Example: Removing Blood Group column from the Employee_Info table
ALTER TABLE Employee_Info
DROP BloodGroup;

Ravindar.M, Asso.Prof, CSE Dept, JITS-KNR

Downloaded by Vemula Satish ([email protected])


lOMoARcPSD|40804004

c) The ‘ALTER TABLE’ with MODIFY COLUMN: This statement is used to change
the data type or size of data type of an existing column in a table.

Syntax: ALTER TABLE TableName


MODIFY COLUMN ColumnName Datatype;

Example 1: Changing the size of column ‘EmployeeName’ in table ‘Employee_info’ from


20 to 30.

ALTER TABLE Employee_Info


MODIFY EmployeeName varchar(30);

Example 2: Changing the data type of column ‘EmployeeID’ in the table ‘Employee_info’
from int to char(10).

ALTER TABLE Employee_Info


MODIFY EmployeeID char(10);

d) The ‘ALTER TABLE’ with CHANGE column name: This statement is used to change
the column name of an existing column in a table.

Syntax: ALTER TABLE TableName


CHANGE COLUMN OldColumnName NewColumnName;

Example 1: Changing the column name ‘EmployeeName’ to ‘EmpName’ in table


‘Employee_info’.

ALTER TABLE Employee_Info


CHANGE COLUMN EmployeeName EmpName;

e) The ‘ALTER TABLE’ with RENAME table name: This statement is used to change
the table name in the database.

Syntax: ALTER TABLE OldTableName


RENAME TO NewTableName;

Example: Changing the table name from ‘Employee_Info’ to ‘Employee_Data’.

ALTER TABLE Employee_Info


RENAME TO Employee_Data;

3. DML COMMANDS: The SQL commands that deals with the manipulation of data
present in the database belong to DML or Data Manipulation Language and this includes
most of the SQL statements. The DML commands are:
i. INSERT: This statement is used to insert new record (row) into the table.

Ravindar.M, Asso.Prof, CSE Dept, JITS-KNR

Downloaded by Vemula Satish ([email protected])


lOMoARcPSD|40804004

Syntax: INSERT INTO TableName[(Column1, Column2,..., ColumnN)]


VALUES (value1, value2,..., valueN);

Example1 :
INSERT INTO Employee_Info( EmployeeID, EmployeeName, PhoneNumber, City,Country)
VALUES ('06', 'Sanjana', '9921321141', 'Chennai', 'India');

Example2 : When inserting all column values as per their order in the table, you can omit
column names.
INSERT INTO Employee_Info
VALUES ('07', 'Sayantini','9934567654', 'Pune', 'India');

ii. DELETE: This statement is used to delete the existing records in a table.

Syntax: DELETE FROM TableName


WHERE Condition;

Example:
DELETE FROM Employee_Info
WHERE EmployeeName='Preeti';

Note: If where condition is not used in DELETE command, then all the rows data will be deleted. If used
only rows which satisfies the condition are deleted.

iii. UPDATE: This statement is used to modify the record values already present in the table.

Syntax: UPDATE TableName


SET Column1 = Value1, Column2 = Value2, ...
[WHERE Condition];

Example:

UPDATE Employee_Info
SET EmployeeName = 'Jhon', City= 'Ahmedabad'
WHERE EmployeeID = 1;

Note: If where condition is not used in UPDATE command, then in all the rows Employee Name
changes to 'Jhon' and City name changes to 'Ahmedabad'. If used only
rows which satisfies the condition are updated.

4. DQL COMMAND: The purpose of DQL Command is to get data from one or more
tables based on the query passed to it.
i. SELECT: This statement is used to select data from a database and the data returned is
stored in a result table, called the result-set.

Ravindar.M, Asso.Prof, CSE Dept, JITS-KNR

Downloaded by Vemula Satish ([email protected])


lOMoARcPSD|40804004

Syntax: SELECT [DISTINCT] * / Column1,Column2,...ColumN


FROM TableName
[WHERE search_condition]
[GROUP BY column_names
[HAVING search_condition_for_GROUP_BY]
[ORDER BY column_name ASC/DESC] ;

Example 1: SELECT * FROM table_name;

Example 2:
SELECT EmployeeID, EmployeeName
FROM Employee_Info;

The ‘SELECT with DISTINCT’ Statement: This statement is used to display only
different unique values. It mean it will not display duplicate values.

Example : SELECT DISTINCT PhoneNumber FROM Employee_Info;

The ‘ORDER BY’ Statement: The ‘ORDER BY’ statement is used to sort the required
results in ascending or descending order. The results are sorted in ascending order by
default. Yet, if you wish to get the required results in descending order, you have to use
the DESC keyword.

Example

/* Select all employees from the 'Employee_Info' table sorted by


City */

SELECT * FROM Employee_Info


ORDER BY City;

/*Select all employees from the 'Employee_Info' table sorted by


City in Descending order */

SELECT * FROM Employee_Info


ORDER BY City DESC;

/* Select all employees from the 'Employee_Info' table sorted by


City and EmployeeName. First it sort the rows as per city, then
sort by employee name */

SELECT * FROM Employee_Info


ORDER BY City, EmployeeName;

/* Select all employees from the 'Employee_Info' table sorted by


City in Descending order and EmployeeName in Ascending order: */

SELECT * FROM Employee_Info


ORDER BY City ASC, EmployeeName DESC;

Ravindar.M, Asso.Prof, CSE Dept, JITS-KNR

Downloaded by Vemula Satish ([email protected])

You might also like