0% found this document useful (0 votes)
15 views14 pages

TCL - Transaction Control Language

Uploaded by

lovlamba7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
15 views14 pages

TCL - Transaction Control Language

Uploaded by

lovlamba7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 14

These SQL commands are mainly categorized into four categories as:

Though many resources claim there to be one more categories of SQL clauses TCL
- Transaction Control Language that we will see in detail.

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(structure). 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:

a.) CREATE - It is used to create the database or its objects (like table, index,
function, views, store procedure and triggers).

• CREATE DATABASE database_name;

• CREATE TABLE table_name (column1 datatype, column2 datatype, column3


datatype, ....);

b.) DROP - It is used to delete an existing table from the database.

• DROP TABLE table_name;

c.) ALTER - It is used to add, delete, or modify columns in an existing table. It is


also used to add and drop various constraints on an existing table.

• ALTER TABLE table_name ADD column_name datatype;

• ALTER TABLE table_name DROP COLUMN column_name;

• ALTER TABLE table_name MODIFY COLUMN column_name datatype;

d.) TRUNCATE - It is used to remove all records(rows) from a table in MySQL. It


performs the same function as a DELETE statement without a WHERE clause.

• TRUNCATE TABLE table_name;


e.) RENAME - The rename command is used to change the name of an existing
database object(like Table, column) to a new name.

• RENAME TABLE current_table_name TO new_table_name;

• ALTER TABLE table_name RENAME COLUMN old_column_name TO


new_column_name;

2. DQL/DRL/DSL (Data Query /Data Retrieval /Data Selection ) :

DQL statements are used for performing queries on the data within schema
objects. The purpose of the DQL Command is to get some schema relation based
on the query passed to it.

Example of DQL command:

° SELECT – is used to retrieve data from the database.

• SELECT column1, column2, ...FROM table_name;

• SELECT * FROM table_name;

Note :- '*' means all, so the above statement is used to retrieves or give the whole
table.

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 command:


a.) INSERT – is used to insert data into a table. It is possible to write the INSERT
INTO statement in two ways:

1. Specify both the column names and the values to be inserted:

• INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1,


value2, value3, ...);

2. If you are adding values for all the columns of the table, you do not need to
specify the column names in the SQL query. However, make sure the order of the
values is in the same order as the columns in the table.

• INSERT INTO table_name VALUES (value1, value2, value3, ...);

b.) UPDATE – is used to update existing data within a table.

• UPDATE table_name SET column1 = value1, column2 = value2, ...WHERE


condition;

Note: Be careful when updating records in a table! Notice the WHERE clause in
the UPDATE statement. The WHERE clause specifies which record(s) that should
be updated. If you omit the WHERE clause, all records in the table will be updated!

c.) DELETE – is used to delete records from a database table.

• DELETE FROM table_name WHERE condition;

4. DCL(Data Control Language) :

DCL includes commands such as GRANT and REVOKE which mainly deal with the
rights, permissions and other controls of the database system.

Examples of DCL commands:


a.) GRANT-gives user’s access privileges to the database.

• GRANT privileges_names ON object TO user;

- Parameters Used:

1. privileges_name: These are the access rights or privileges granted to the

user.

2. object: It is the name of the database object to which permissions are

being granted. In the case of granting privileges on a table, this would

be the table name.

3. user: It is the name of the user to whom the privileges would be

granted.

b.) REVOKE- withdraw user’s access privileges given by using the GRANT

command.

• REVOKE privileges ON object FROM user;

For Examples on Grant & revoke pls consider this :- Revoke & Grant by GFG

5. TCL(transaction Control Language):

• What are transactions ?

Transactions group a set of tasks into a single execution unit. Each transaction
begins with a specific task and ends when all the tasks in the group successfully
complete. If any of the tasks fail, the transaction fails. Therefore, a transaction has
only two results: success or failure.
A database transaction, by definition, must be atomic, consistent, isolated and
durable. These are popularly known as ACID properties.

• ACID properties

Any transaction must maintain the ACID properties, viz. Atomicity, Consistency,
Isolation, and Durability.

- Atomicity − This property states that a transaction is an atomic unit of


processing, that is, either it is performed in its entirety or not performed at all. No
partial update should exist.

- Consistency − A transaction should take the database from one consistent state
to another consistent state. It should not adversely affect any data item in the
database.

- Isolation − A transaction should be executed as if it is the only one in the system.


There should not be any interference from the other concurrent transactions that
are simultaneously running.

- Durability − If a committed transaction brings about a change, that change


should be durable in the database and not lost in case of any failure.

• Transaction Operations

The low level operations performed in a transaction are −

a.) begin_transaction − A marker that specifies start of transaction

execution.

b.) read_item or write_item − Database operations that may be

interleaved with main memory operations as a part of transaction.

c.)end_transaction − A marker that specifies end of transaction.


d.) commit − A signal to specify that the transaction has been

successfully completed in its entirety and will not be undone.

e.) rollback − A signal to specify that the transaction has been

unsuccessful and so all temporary changes in the database are

undone. A committed transaction cannot be rolled back.

for using the above operations :TCL commands by GFG

• What is a Schedule?

A schedule is a series of operations from one or more transactions. A schedule


can be of two types:

- Serial Schedule: When one transaction completely executes before starting


another transaction, the schedule is called serial schedule. A serial schedule is
always consistent.

- Concurrent Schedule: When operations of a transaction are interleaved with


operations of other transactions of a schedule, the schedule is called Concurrent
schedule.

Also read - Conflict Serializability in DBMS by GFG ( IMP )


• Concurrency Control & types

please consider this link for above topic :- Concurrency control by GURU99

Other clauses used are as follows :-

1.) FROM - It is used for selecting a table name in a database

• SELECT * FROM officers ;

2.) WHERE - It specifies which rows to retrieve.The WHERE clause works like an if
condition in any programming language. The WHERE clause is very useful when
you want to fetch the selected rows from a table.

where can be used with - - Select- Update- Delete etc.

• SELECT column_name(s) FROM table_name WHERE condition ;

3.) GROUP BY - It is used to arrange the data into groups.The GROUP BY


statement groups rows that have the same values into summary rows, like "find
the number of customers in each country".

• The GROUP BY statement is often used with aggregate functions to group the
result-set by one or more columns.

• Aggregate functions :- Aggregate functions perform a calculation on a set of


values and return a single value. for ex :- AVG, MAX, MIN, SUM, COUNT(),
COUNT(*)

• For syntax & examples of above - Group by in mysql by JAVATPOINT .


4.) HAVING - The HAVING clause was added to SQL because the WHERE keyword
could not be used with group functions.
• The HAVING clause is often used with the GROUP BY clause to filter groups
based on a specified condition. If the GROUP BY clause is omitted, the HAVING
clause behaves like the WHERE clause.

• For syntax & examples of the - Having clause by JAVATPOINT .

5.) ORDER BY - The ORDER BY keyword is used to sort the result-set in ascending
or descending order. The ORDER BY keyword sorts the records in ascending order
by default. To sort the records in descending order, use the DESC keyword.

• SELECT column1, column2, ...FROM table_name ORDER BY column1, column2, ...


ASC|DESC;

• SELECT * FROM Customers ORDER BY Country;

6.) AS - It provides an alias which can be used to temporarily rename tables or


columns . An alias only exists for the duration of the query.

• SELECT col_name AS alias_name FROM table_name;

• SELECT col_name1, col_name2,... FROM table_name AS alias_name;

7.) JOIN - A JOIN clause is used to combine rows from two or more tables, based
on a related column between them.

• SELECT columns FROM table1 INNER JOIN table2

ON table1.column = table2.column;

Different Types of SQL JOINs :- Here are the different types of the JOINs in SQL:

• (INNER) JOIN: Returns records that have matching values in both tables
• LEFT (OUTER) JOIN: Returns all records from the left table, and the matched
records from the right table

• RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched
records from the left table

• FULL (OUTER) JOIN: Returns all records when there is a match in either left or
right table

Other Keywords :
1.) DISTINCT Keyword - Eliminate duplicate rows. If column contains NULL value,
distinct treats NULL as same value. Can be used with more than one column. Can
be used with • Select • Where • Group by • Aggregate functions • Limit clause .

• SELECT DISTINCT column1, column2, ...FROM table_name;

• SELECT DISTINCT Country FROM Customers;

Distinct & Group by :-


• Group by behaves like distinct if used without aggregate functions.

• DISTINCT clause is a special case of the GROUP BY clause.


• The difference between DISTINCT clause and GROUP BY clause is that the
GROUP BY clause sorts the result set whereas the DISTINCT clause does not.

2.) LIMIT Keyword - MySQL supports the LIMIT clause to select a limited number
of records

• SELECT column_name(s) FROM table_name WHERE condition LIMIT number;

3.) IN Keyword - The IN operator allows you to determine if a specified value


matches any one of a list or a subquery. The IN operator returns 1 if the value of
the column or the result of the expr expression is equal to any value in the list,
otherwise, it returns 0. Applies Binary search for searching of values if all values
are constants.

• SELECT 10 IN(15,10,25);
• SELECT column1,column2,...FROM table_name WHERE (expr|column_1) IN
('value1','value2',...);

• The IN operator is a shorthand for multiple OR conditions.

• You can combine the IN operator with the NOT operator to determine if a value
does not match any value in a list or a subquery.
4.) BETWEEN Keyword - BETWEEN Condition will return the records where
expression is within the range of value1 and value2 (inclusive).
• You can use BETWEEN clause to replace a combination of "greater than equal
AND less than equal" conditions.

• SELECT column_name(s) FROM table_name WHERE column_name

BETWEEN value1 AND value2;

• You can combine the Between operator with the NOT operator to return all
rows where value does not lie in the given range.

5.) LIKE Keyword - The LIKE operator is used in a WHERE clause to search for a
specified pattern in a column. There are two wildcards used in conjunction with
the LIKE operator:
• % - The percent sign represents zero, one, or multiple characters

• _ - The underscore represents a single character


• SELECT column1, column2, ...FROM table_name WHERE column LIKE pattern;
Escape modifier :-

Let's say you wanted to search for a % or a _ character in the MySQL LIKE
condition. You can do this using an Escape character.
• SELECT * FROM table_name WHERE column_name LIKE 'G\%';
• We can override the default escape character in MySQL by providing the
ESCAPE modifier as follows:
• SELECT * FROM table_name WHERE column_name LIKE 'G!%' ESCAPE '!';

Other Commands :-

To Show Databases or tables ;

• show databases; show tables;

To Select Database –

• use database-name;

To Describe table –

• describe table-name;

You might also like