SQL DDL DML DQL
SQL DDL DML DQL
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:
SQL commands
DROP SAVEPOINT
UPDATE
TRUNCATE
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:
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:
i. CREATE: It is used to create the database or its objects like table, index, function, views,
store procedure and triggers.
Note: The content in the square brackets indicates it is optional. If not required, you can skip it.
Column constraints
Table constraints
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]);
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; .
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; .
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;
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.
Example 2: Changing the data type of column ‘EmployeeID’ in the table ‘Employee_info’
from int to 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.
e) The ‘ALTER TABLE’ with RENAME table name: This statement is used to change
the table name in the database.
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.
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.
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.
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.
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.
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