Docsity

SQL Commands and Categories: DDL, DQL, DML, DCL, and TCL, Lecture notes of Database Management Systems (DBMS)

Toggle sidebar
bg1
Structured Query Language (SQL) as we all know is the database language by the use of which
we can perform certain operations on the existing database and also we can use this language to
create a database. SQL uses certain commands like Create, Drop, Insert etc. to carry out the
required tasks.
These SQL commands are mainly categorized into four categories as:
1. DDL โ€“ Data Definition Language
2. DQl โ€“ Data Query Language
3. DML โ€“ Data Manipulation Language
4. DCL โ€“ Data Control Language
Though many resources claim there to be another category of SQL clauses TCL โ€“ Transaction Control
Language. So we will see in detail about TCL as well.
1. DDL (Data Definition Language) : DDL or Data Definition Language actually 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.
Examples of DDL commands:
pf3
pf4
pf5

Partial preview of the text

Download SQL Commands and Categories: DDL, DQL, DML, DCL, and TCL and more Lecture notes Database Management Systems (DBMS) in PDF only on Docsity!

Structured Query Language (SQL) as we all know is the database language by the use of which

we can perform certain operations on the existing database and also we can use this language to

create a database. SQL uses certain commands like Create, Drop, Insert etc. to carry out the

required tasks.

These SQL commands are mainly categorized into four categories as:

1. DDL โ€“ Data Definition Language

2. DQl โ€“ Data Query Language

3. DML โ€“ Data Manipulation Language

4. DCL โ€“ Data Control Language

Though many resources claim there to be another category of SQL clauses TCL โ€“ Transaction Control

Language. So we will see in detail about TCL as well.

1. DDL (Data Definition Language) : DDL or Data Definition Language actually 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.

Examples of DDL commands:

o CREATE โ€“ is used to create the database or its objects (like table, index,

function, views, store procedure and triggers).

o DROP โ€“ is used to delete objects from the database.

o ALTER -is used to alter the structure of the database.

o TRUNCATE โ€“ is used to remove all records from a table, including all spaces

allocated for the records are removed.

o COMMENT โ€“ is used to add comments to the data dictionary.

o RENAME โ€“ is used to rename an object existing in the database.

2. 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 some schema relation based on the query passed to

it.

Example of DQL:

o SELECT โ€“ is used to retrieve data from the a database.

3. 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.

Examples of DML:

o INSERT โ€“ is used to insert data into a table.

o UPDATE โ€“ is used to update existing data within a table.

o DELETE โ€“ is used to delete records from a database table.

4. DCL(Data Control Language) : DCL includes commands such as GRANT and

REVOKE which mainly deals with the rights, permissions and other controls of the

database system.

Examples of DCL commands:

o GRANT - gives userโ€™s access privileges to database.

o REVOKE - withdraw userโ€™s access privileges given by using the GRANT

command.

5. TCL (transaction Control Language) : TCL commands deals with the transaction

within the database.

Examples of TCL commands:

o COMMIT โ€“ commits a Transaction.

o ROLLBACK โ€“ rollbacks a transaction in case of any error occurs.

o SAVEPOINT โ€“ sets a savepoint within a transaction.

o SET TRANSACTION โ€“ specify characteristics for the transaction.

DDL Commands Explained

๏ƒ˜ CREATE

๏ƒ˜ DROP

๏ƒ˜ ALTER

๏ƒ˜ TRUNCATE

๏ƒ˜ RENAME

SQL | CREATE

There are two CREATE statements available in SQL:

and can store characters and the size 20 specifies that these two fields can hold maximum of 20

characters.

DROP

DROP is used to delete a whole database or just a table. The DROP statement destroys the

objects like an existing database, table, index, or view.

A DROP statement in SQL removes a component from a relational database management system

(RDBMS).

Syntax:

DROP object object_name

Examples: DROP TABLE table_name; table_name : Name of the table to be deleted.

DROP DATABASE database_name; database_name : Name of the database to be deleted.

TRUNCATE

TRUNCATE statement is a Data Definition Language (DDL) operation that is used to mark the

extents of a table for deallocation (empty for reuse). The result of this operation quickly removes

all data from a table, typically bypassing a number of integrity enforcing mechanisms. It was

officially introduced in the SQL:2008 standard.

The TRUNCATE TABLE mytable statement is logically (though not physically) equivalent to

the DELETE FROM mytable statement (without a WHERE clause).

Syntax:

TRUNCATE TABLE table_name; table_name : Name of the table to be truncated. DATABASE name - student_data

DROP vs TRUNCATE

๏‚ท Truncate is normally ultra-fast and its ideal for deleting data from a temporary table.

๏‚ท Truncate preserves the structure of the table for future use, unlike drop table where the

table is deleted with its full structure.

๏‚ท Table or Database deletion using DROP statement cannot be rolled back, so it must be

used wisely.

๏ƒ˜ SQL | ALTER (ADD, DROP, MODIFY)

ALTER TABLE is used to add, delete/drop or modify columns in the existing table. It is also

used to add and drop various constraints on the existing table.

ALTER TABLE โ€“ ADD

ADD is used to add columns into the existing table. Sometimes we may require to add additional

information, in that case we do not require to create the whole database again, ADD comes to

our rescue.

Syntax:

ALTER TABLE table_name ADD (Columnname_1 datatype, Columnname_2 datatype,

Columnname_n datatype);

ALTER TABLE โ€“ DROP

DROP COLUMN is used to drop column in a table. Deleting the unwanted columns from the

table.

Syntax:

ALTER TABLE table_name DROP COLUMN column_name;

ALTER TABLE-MODIFY

It is used to modify the existing columns in a table. Multiple columns can also be modified at

once.

*Syntax may vary slightly in different databases.

Syntax (Oracle,MySQL,MariaDB):

ALTER TABLE table_name MODIFY column_name column_type; (We use this for Now)

Syntax (SQL Server):

ALTER TABLE table_name ALTER COLUMN column_name column_type;

QUERY:

๏‚ท To ADD 2 columns AGE and COURSE to table Student.

ALTER TABLE Student ADD (AGE number(3),COURSE varchar(40));

MODIFY column COURSE in table Student

ALTER TABLE Student MODIFY COURSE varchar(20);

After running the above query maximum size of Course Column is reduced to 20 from 40.

  • DROP column COURSE in table Student. ALTER TABLE Student DROP COLUMN COURSE;

DQL (Data Query Language) COMMANDS

๏ƒ˜ SELECT

Select is the most commonly used statement in SQL. The SELECT Statement in SQL is used to

retrieve or fetch data from a database. We can fetch either the entire table or according to some

specified rules. The data returned is stored in a result table. This result table is also called result-

set.

With the SELECT clause of a SELECT command statement, we specify the columns that we

want to be displayed in the query result and, optionally, which column headings we prefer to see

above the result table.

The select clause is the first clause and is one of the last clauses of the select statement that the

database server evaluates. The reason for this is that before we can determine what to include in

the final result set, we need to know all of the possible columns that could be included in the

final result set.

Basic Syntax:

SELECT column1,column2 FROM table_name column1 , column2 : names of the fields of the table table_name: from where we want to fetch

This query will return all the rows in the table with fields column1 , column2.

๏‚ท To fetch the entire table or all the fields in the table:

SELECT * FROM table_name;

๏‚ท Query to fetch the fields ROLL_NO, NAME, AGE from the table Student:

SELECT ROLL_NO, NAME, AGE FROM Student;