SQL Ddl Dml Tcl Commands With Examples
SQL Ddl Dml Tcl Commands With Examples
SQL
SQL Commands
There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL.
1. DDL Commands - Data Definition Language
Data Definition Language or DDL commands in SQL are used for changing the
structure of a table. In other words, DDL commands are capable of creating,
deleting, and modifying data.
All DDL commands are auto-committed which means that changes made by them
are automatically saved in the database. Following are the various DDL commands:
ALTER
Used for altering the structure of a database. Typically, the ALTER command is used
either to add a new attribute or modify the characteristics of some existing attribute.
General Syntax:
Example:
The ALTER command can also be used for dropping a column from the table:
General Syntax:
Example:
</pre.
CREATE
Used for creating a new table in the database. General Syntax:
CREATE TABLE table_name (column_name1 data_type(size), column_name2
data_type(size),…., column_nameN data_type(size));
Example:
DROP
Used for deleting an entire table from the database and all the data stored in it.
General Syntax:
Example:
RENAME
Used for renaming a table.
General Syntax:
Example:
TRUNCATE
Used for deleting all rows from a table and free the space containing the table.
General Syntax:
Example:
TRUNCATE TABLE Student;
DELETE
Used for removing one or more rows from a table.
General Syntax:
DELETE FROM table_name; (deletes all rows from a table)
DELETE FROM table_name WHERE some_condition; (delete only the row(s) where
the condition is true)
Example:
INSERT
Used for inserting data into the row of a table.
General Syntax:
OR
Example:
Insert command can also be used for inserting data into a table from another table.
General Syntax:
INSERT INTO table_name1 SELECT column_name1, column_name2,….,column_nameN FROM
table_name2;
Example:
UPDATE
Used to modify or update the value of a column in a table. It can update all rows or
some selective rows in the table.
General Syntax:
Example:
ALTER
DELETE
INSERT
SELECT
UPDATE
GRANT
Used for granting user access privileges to a database.
General Syntax:
Example:
This will allow the user to run only SELECT and UPDATE operations on the Student
table.
REVOKE
Used for taking back permission given to a user.
General Syntax:
Example:
Note: - A user who is not the owner of a table but has been given the privilege to
grant permissions to other users can also revoke permissions.
COMMIT;
Example:
COMMIT;
ROLLBACK
Used to undo transactions that aren’t yet saved in the database. Ends the
transaction and undoes all changes made during the transaction. Releases all
transaction locks acquired on tables.
General Syntax:
ROLLBACK;
Example:
ROLLBACK;
SAVEPOINT
Used for rolling back to a certain state known as the save point. Savepoints need to
be created first so that they can be used for rollbacking transactions partially.
General Syntax:
SAVEPOINT savepoint_name;
Note: - An active savepoint is one that has been specified since the last COMMIT or
ROLLBACK command.
SELECT expressions
FROM table_name
WHERE condition1, condition2,…., conditionN;
Example:
Suppose we have a relational table called Student that has all the information
regarding a student, such as name, roll no., stream, age, address, etc. and we need
to fetch data regarding all student names who are less than 18 years of age, then we
can use the SELECT command as follows:
SELECT student_name
FROM student
The result will be a list of all the student names who are less than 18 years of age.
The SELECT command can also be used for eliminating duplicates from a table.
General Syntax:
Example:
This command will scan through entire rows and will eliminate rows that are
identical.
Rows retrieved using the SELECT command can be sorted in either ascending or
descending order.
General Syntax:
Example:
SELECT Name, Age FROM Student ORDER BY Name;