Practical
Short Notes
What is SQL?
- SQL stands for Structured Query Language.
- This database language is mainly designed
for maintaining the data relational
database management systems.
- SQL is standard language for accessing and
manipulating database.
Types of SQL commands
- DDL (Data Definition Language): CREATE,
ALTER, DROP and TRUNCATE.
- DML (Data Manipulation Language): INSERT,
UPDATE, DELETE.
- DCL (Data Control Language): GRANT,
REVOKE.
- TCL (Transaction Control Language):
COMMIT, ROLLBACK.
DDL commands:
- DDL (Data Definition Language): used to
change the structure of the table like
creating the table, altering the table and
deleting the table.
- All the commands in the DDL are auto
committed that means it permanently
saves all the changes in the database.
1. CREATE: This command is used to create a
new database or a new table.
SYNTAX:
CREATE TABLE table_name (
Column1 datatype,
Column2 datatype,
Column3 datatype
);
Example:
CREATE TABLE Employee (
EmployeeID int,
Firstname varchar(255),
Lastname varchar(255),
City varchar(255)
);
2. ALTER: It allows to add, modify and delete
columns of an existing table.
SYNTAX:
ALTER TABLE table_name
ADD column_name datatype
Example:
ALTER TABLE Employee
ADD Email varchar(255);
3. DROP: is used to drop an existing table in a
database. This command deletes both the
structures and records stored in table.
SYNTAX:
DROP TABLE table_name;
Example:
DROP TABLE Employee;
4. TRUNCATE: is used to remove all rows
(complete data) from a table. It is also similar
to DELETE statement with no WHERE clause.
SYNTAX:
TRUNCATE TABLE table_name;
Example:
TRUNCATE TABLE Employee;
DML commands:
- DML stands for Data Manipulation
Language, a subset of SQL (Structured
Query Language) commands used for
managing data within database objects.
- DML commands allow you to insert, modify,
retrieve, and delete data from database
tables.
1. INSERT: It is used to insert a single or a
multiple records in a table.
SYNTAX:
INSERT INTO table_name (Column1,
Column2, Column3) VALUES (Value1, Value2,
Value3);
Example:
INSERT INTO Students (Roll_no,
Name, Age, City) VALUES (1,'Bruce',
32, 'New York');
2. UPDATE: is used to modify the existing
records in a table.
SYNTAX:
UPDATE table_name SET Column1 = Value1,
Column2 = Value2, WHERE Condition;
Example:
UPDATE Customers SET Name = 'Lynn',
City = 'London' WHERE CustomerID =
101;
3. DELETE: is used to delete existing records
in a table.
SYNTAX:
DELETE FROM table_name WHERE Condition;
Example:
DELETE FROM Customers WHERE
CustomerName = 'Sarah';
DCL commands:
- DCL stands for Data Control Language, a
subset of SQL (Structured Query Language)
commands used to control access to data in
a database.
- DCL commands manage database security
by granting or revoking permissions for
users and roles.
1. GRANT: It is used to give user access
privileges to a database.
SYNTAX:
GRANT SELECT, INSERT ON MY_TABLE TO
SOME_USER, ANOTHER_USER;
Example:
GRANT SELECT, INSERT ON employees
TO user1;
2. REVOKE: This command withdraws the
user's access privileges given by using GRANT
command.
SYNTAX:
REVOKE SELECT , DELETE ON MY_TABLE
FROM USER1, USER2;
Example:
REVOKE DELETE ON departments FROM
user2;
TCL commands:
- TCL stands for Transaction Control
Language, a subset of SQL commands used
to manage transactions in a database.
- TCL ensures data integrity by controlling
when changes are permanently saved or
rolled back.
1. COMMIT: It saves all the transactions to
the database since the last COMMIT or
ROLLBACK command.
SYNTAX:
COMMIT;
Example:
DELETE FROM Student WHERE AGE = 20;
COMMIT;
2. ROLLBACK: If any error occurs with any of
the SQL grouped statements, all changes
need to be aborted. The process of reversing
changes is called rollback.
SYNTAX:
ROLLBACK;
Example:
DELETE FROM Student WHERE AGE = 20;
ROLLBACK;
THE END!!!