dml, ddl, dcl ,drl/dql and tcl statements in sql with examples

27
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples

Upload: lgsgbhsic-university-of-south-asia

Post on 14-Apr-2017

6.508 views

Category:

Data & Analytics


3 download

TRANSCRIPT

Page 1: DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples

DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with

Examples

Page 2: DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
Page 3: DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
Page 4: DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples

SQL language is divided into four types of primary language statements: DML, DDL, DCL and TCL. Using these statements, we can define the structure of a database by creating and altering database objects, and we can manipulate data in a table through updates or deletions. We also can control which user can read/write data or manage transactions to create a single unit of work.

Page 5: DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples

The five main categories of SQL statements are as follows:

1. DML (Data Manipulation Language)2. DDL (Data Definition Language)3. DCL (Data Control Language)4. TCL (Transaction Control Language)

5. DRL

Page 6: DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples

DML (Data Manipulation Language) DML statements affect records in a table.

These are basic operations we perform on data such as selecting a few records from a table, inserting new records, deleting unnecessary records, and updating/modifying existing records.

DML statements include the following: SELECT – select records from a table

INSERT – insert new recordsUPDATE – update/Modify existing recordsDELETE – delete existing records

Page 7: DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples

INSERT - insert data into a table UPDATE - updates existing data within a

table DELETE - deletes all records from a table,

the space for the records remain MERGE - UPSERT operation (insert or

update) CALL - call a PL/SQL or Java subprogram EXPLAIN PLAN - explain access path to data LOCK TABLE - control concurrency

Page 8: DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples

DDL (Data Definition Language)

DDL statements are used to alter/modify a database or table structure and schema. These statements handle the design and storage of database objects.

CREATE – create a new Table, database, schemaALTER – alter existing table, column descriptionDROP – delete existing objects from database

Page 9: DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples

CREATE - to create objects in the database

ALTER - alters the structure of the database

DROP - delete objects from the database

TRUNCATE - remove all records from a table, including all spaces allocated for the records are removed

COMMENT - add comments to the data dictionary

RENAME - rename an object

Page 10: DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples

DCL (Data Control Language)

DCL statements control the level of access that users have on database objects.

GRANT - gives user's access privileges to database

GRANT – allows users to read/write on certain database objects

REVOKE - withdraw access privileges given with the GRANT command

REVOKE – keeps users from read/write permission on database objects

Page 11: DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples

TCL (Transaction Control Language)

TCL statements allow you to control and manage transactions to maintain the integrity of data within SQL statements.

BEGIN Transaction – opens a transactionCOMMIT Transaction – commits a transactionROLLBACK Transaction – ROLLBACK a transaction in case of any error

Page 12: DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples

COMMIT - save work done SAVEPOINT - identify a point in a

transaction to which you can later roll back

ROLLBACK - restore database to original since the last COMMIT

SET TRANSACTION - Change transaction options like isolation level and what rollback segment to use

Page 13: DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples

TCL command

Transaction Control Language(TCL) commands are used to manage transactions in database.These are used to manage the changes made by DML statements. It also allows statements to be grouped together into logical transactions.

Page 14: DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples

Commit command Commit command is used to

permanently save any transaaction into database.

Following is Commit command's syntax,

commit;

Page 15: DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples

Rollback command This command restores the database

to last commited state. It is also use with savepoint command to jump to a savepoint in a transaction.

Following is Rollback command's syntax,

rollback to savepoint-name;

Page 16: DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples

Savepoint commandsavepoint command is used to

temporarily save a transaction so that you can rollback to that point whenever necessary.

Following is savepoint command's syntax,

savepoint savepoint-name;

Page 17: DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples

Example of Savepoint and RollbackFollowing is the class table

ID NAME1 dar2 wahab4 sadiq

Page 18: DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples

Lets use some SQL queries on the above table and see the results.

INSERT into class values(5,'Rahul'); commit; UPDATE class set name='abhijit' where id='5'; savepoint A; INSERT into class values(6,'Chris'); savepoint B; INSERT into class values(7,'Bravo'); savepoint C; SELECT * from class;

Page 19: DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples

The resultant table will look like,ID NAME1 dar2 wahab4 sadiq5 hmftj6 maha7 anza

Page 20: DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples

Now rollback to savepoint B rollback to B; SELECT * from class;

Page 21: DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples

The resultant table will look likeID NAME1 dar2 wahab4 sadiq5 maha6 anza

Page 22: DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples

Now rollback to savepoint A

rollback to A; SELECT * from class;

Page 23: DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples

The result table will look like

ID NAME1 dar2 wahab4 sadiq5 sarim

Page 24: DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples

DRL/DQL DQL: Data Query Language OR

DRL: Data Retrieval Language

DRL means Data Retrieval Language. This will be used for the retrieval of the data from the database.In order to see the data present in the database, we will use DRL statement. We have only one DRLstatement.

SELECT is the only DRL statement in SQL

Select is DRL/DQL i.e. data retrieval Language

Page 25: DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples

SQL statements are often divided into three categories:

DML (Data Manipulation Language). These SQL statements are used to retrieve and manipulate data. This category encompasses the most fundamental commands including DELETE, INSERT, SELECT, and UPDATE. DML SQL statements have only minor differences between SQL variations. DML SQL commands include the following:

DELETE to remove rows. INSERT to add a row. SELECT to retrieve row. UPDATE to change data in specified columns. DDL (Data Definition Language). These SQL statements define the structure of a database,

including rows, columns, tables, indexes, and database specifics such as file locations. DDL SQL statements are more part of the DBMS and have large differences between the SQL variations. DML SQL commands include the following:

CREATE to make a new database, table, index, or stored query. DROP to destroy an existing database, table, index, or view. DBCC (Database Console Commands) statements check the physical and logical consistency of

a database. DCL (Data Control Language). These SQL statements control the security and permissions of

the objects or parts of the database(s). DCL SQL statements are also more part of the DBMS and have large differences between the SQL variations. DML SQL commands include the following:

GRANT to allow specified users to perform specified tasks. DENY to disallow specified users from performing specified tasks. REVOKE to cancel previously granted or denied permissions. »

Page 26: DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples

What is the fastest command type from the 3 command types: DDL, DML and DCL?

During the execution of DDL command. DDL command would not copy the actual content to rollback table space, hence it is fast compared to DML command.

Page 27: DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples