0% found this document useful (0 votes)
2 views

DBMS UNIT-4 SQL

This document provides an introduction to SQL, detailing its components including Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Transaction Control Language (TCL), and Data Query Language (DQL). It covers SQL commands for creating, modifying, and deleting databases and tables, as well as querying data and managing access permissions. Additionally, it outlines various SQL data types and their usage in database operations.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DBMS UNIT-4 SQL

This document provides an introduction to SQL, detailing its components including Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Transaction Control Language (TCL), and Data Query Language (DQL). It covers SQL commands for creating, modifying, and deleting databases and tables, as well as querying data and managing access permissions. Additionally, it outlines various SQL data types and their usage in database operations.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 54

UNIT – 4

Introduction to SQL

SQL: Introduction and features, Data Definition Language (DDL), Data


Manipulation Language (DML), Data Control Language (DCL), Transaction Control
Language (TCL), SQL functions: Mathematical Functions, Group Functions
What is SQL?

• SQL is a language to operate databases; it includes database creation,


deletion, fetching rows, modifying rows, etc.
• SQL is Structured Query Language, which is a computer language for
storing, manipulating and retrieving data stored in a relational database.
• SQL is the standard language for Relational Database System. All the
Relational Database Management Systems (RDMS) like MySQL, MS
Access, Oracle, Sybase, Informix, Postgres and SQL Server use SQL as
their standard database language.
• Also, they are using different dialects, such as −
MS SQL Server using T-SQL,
Oracle using PL/SQL,
MS Access version of SQL is called JET SQL (native format) etc.
Components of SQL

• SQL commands are divided into five categories:


DATA DEFINITION LANGUAGE

• The Data Definition Language (DDL) consist of SQL statements used to


define the database structure or schema. It simply deals with
descriptions of the database schema and is used to create and modify
the structure of database objects in databases.
• The DDL provides a set of definitions to specify the storage structure and
access methods used by the database system.
• SQL commands which comes under Data Definition Language are:
1. Create : To create tables in the database.
2. Alter : Alters the structure of the database.
3. Drop : Delete tables from database.
4. Truncate : Remove all records from a table, also release the space
occupied by those records.
DATA MANIPULATION LANGUAGE
• A Data Manipulation Language (DML) is a computer programming language
used for adding (inserting), removing (deleting), and modifying (updating) data in a
database. In SQL, the data manipulation language comprises the SQL-data change
statements, which modify stored data but not the schema of the database table.
• After the database schema has been specified and the database has been created,
the data can be manipulated using a set of procedures which are expressed by DML.
• The DML is basically of two types:
Procedural DML – Requires a user to specify what data is needed and how to get it.
Non-Procedural DML - Requires a user to specify what data is needed
without specifying how to get it.
• SQL commands which comes under Data Manipulation Language are :
1.Insert : Inserts data into a table
2.Update : Updates the existing data within a table.
3.Delete : Deletes all records from a table, but not the space occupied by them.
DATA CONTROL LANGUAGE

• A Data Control Language (DCL) is a programming language used to


control the access of data stored in a database.
• It is used for controlling privileges in the database (Authorization). The
privileges are required for performing all the database operations such
as creating sequences, views of tables etc.
• SQL commands which come under Data Control Language are:
1. Grant : Grants permission to one or more users to perform specific
tasks.
2. Revoke : Withdraws the access permission given by the GRANT
statement.
TRANSACTIONAL CONTROL
LANGUAGE
• Transactional control language (TCL) commands are used to manage
transactions in the database. These are used to manage the changes
made to the data in a table by DML statements.

• SQL command which come under Transfer Control Language are:


1. Commit : Saves any transaction into the database permanently.
2. Roll back : Restores the database to last commit state.
3. Save point : Temporarily save a transaction so that you can rollback.
DATA QUERY LANGUAGE

• The Data Query Language consist of commands used to query or retrieve


data from a database.

• One such SQL command in Data Query Language is


Select : It displays the records from the table.
SQL Data Types

• Data types are used to represent the nature of the data that can be
stored in the database table. For example, in a particular column of a
table, if we want to store a string type of data then we will have to
declare a string data type of this column.
• A data type also specifies the possible values for that type, the
operations that can be performed on that type and the way the values of
that type are stored.
• Data types mainly classified into three categories for every database.
1. String Data types
2. Numeric Data types
3. Date and time Data types
Oracle String data types

CHAR(size) It is used to store character data within the predefined


length. It can be stored up to 2000 bytes.
NCHAR(size) It is used to store national character data within the
predefined length. It can be stored up to 2000 bytes.
VARCHAR2(size) It is used to store variable string data within the
predefined length. It can be stored up to 4000 byte.
VARCHAR(SIZE) It is the same as VARCHAR2(size). You can also use
VARCHAR(size), but it is suggested to use VARCHAR2(size)
NVARCHAR2(size) It is used to store Unicode string data within the
predefined length. We have to must specify the size of
NVARCHAR2 data type. It can be stored up to 4000 bytes.
Oracle Numeric Data Types

NUMBER(p, s) It contains precision p and scale s. The precision p


can range from 1 to 38, and the scale s can range
from -84 to 127.
FLOAT(p) It is a subtype of the NUMBER data type. The
precision p can range from 1 to 126.
BINARY_FLOAT It is used for binary precision( 32-bit). It requires 5
bytes, including length byte.
BINARY_DOUBLE It is used for double binary precision (64-bit). It
requires 9 bytes, including length byte.
Oracle Date and Time Data
Types
DATE It is used to store a valid date-time format with
a fixed length. Its range varies from January 1,
4712 BC to December 31, 9999 AD.
TIMESTAMP It is used to store the valid date in YYYY-MM-DD
with time hh:mm:ss format.
SQL - CREATE Database
• The SQL CREATE DATABASE statement is used to create a new SQL database.
Syntax
• The basic syntax of this CREATE DATABASE statement is as follows −
• CREATE DATABASE DatabaseName; Always the database name should be unique within the
RDBMS.
Example
• If you want to create a new database <testDB>, then the CREATE DATABASE statement would be
as shown below −
• SQL> CREATE DATABASE testDB;
• Make sure you have the admin privilege before creating any database. Once a database is
created, you can check it in the list of databases as follows −
• SQL> SHOW DATABASES;
Database
information_schema
AMROOD
test
testDB
DROP or DELETE Database
• The SQL DROP DATABASE statement is used to drop an existing database in SQL schema.
Syntax
• The basic syntax of DROP DATABASE statement is as follows −
• DROP DATABASE DatabaseName;
• Always the database name should be unique within the RDBMS.
Example
• If you want to delete an existing database <testDB>, then the DROP DATABASE statement would
be as shown below −
• SQL> DROP DATABASE testDB;
NOTE − Be careful before using this operation because by deleting an existing database would
result in loss of complete information stored in the database.
• Make sure you have the admin privilege before dropping any database. Once a database is
dropped, you can check it in the list of the databases as shown below −
• SQL> SHOW DATABASES;
information_schema
AMROOD
test
SQL - SELECT Database/ USE
Statement
• When you have multiple databases in your SQL Schema, then before
starting your operation, you would need to select a database where all
the operations would be performed.
• The SQL USE statement is used to select any existing database in the SQL
schema.
• Syntax: USE DatabaseName;
• Always the database name should be unique within the RDBMS.
• You can check the available databases as shown below −
SQL> SHOW DATABASES;
• Example : SQL> USE AMROOD;
SQL CREATE TABLE

• SQL CREATE TABLE statement is used to create table in a database.


• If you want to create a table, you should name the table and define its
column and each column's data type.
• Syntax:
create table "tablename"
("column1" "data type",
"column2" "data type",
"column3" "data type",
...
"columnN" "data type");
Create table Example
• The following code block is an example, which creates a CUSTOMERS table
with an ID as a primary key and NOT NULL are the constraints showing that
these fields cannot be NULL while creating records in this table −
• SQL> CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID) );
• You can verify if your table has been created successfully by looking at the
message displayed by the SQL server, otherwise you can use
the DESC command as follows −
SQL> DESC CUSTOMERS;
SQL - DROP or DELETE Table

• The SQL DROP TABLE statement is used to remove a table definition and
all the data, indexes, triggers, constraints and permission specifications
for that table.
• NOTE − You should be very careful while using this command because
once a table is deleted then all the information available in that table will
also be lost forever.
• Syntax : DROP TABLE table_name;
• Example : SQL> DROP TABLE CUSTOMERS;
• Output :Query OK, 0 rows affected (0.01 sec)
SQL - ALTER TABLE Command

• The SQL ALTER TABLE command is used to add, delete or modify


columns in an existing table. You should also use the ALTER TABLE
command to add and drop various constraints on an existing table.
• SQL ALTER TABLE Add Column
ALTER TABLE table_name ADD column_name column-definition;
• If you want to add multiple columns in table, the SQL table will be
ALTER TABLE table_name
ADD (column_1 column-definition,
column_2 column-definition,
.....
column_n column-definition);
• SQL ALTER TABLE Modify Column
• If you want to modify an existing column in SQL table, syntax is given below:
• ALTER TABLE table_name MODIFY column_name column_type;
• If you want to modify multiple columns in table, the SQL table will be
• ALTER TABLE table_name
• MODIFY (column_1 column_type,
• column_2 column_type,
• .....
• column_n column_type);
• SQL ALTER TABLE DROP Column
• The syntax of alter table drop column is given below:
• ALTER TABLE table_name DROP COLUMN column_name;
• SQL ALTER TABLE RENAME Column
• The syntax of alter table rename column is given below:
• ALTER TABLE table_name
• RENAME COLUMN old_name to new_name;
SQL RENAME TABLE
• SQL RENAME TABLE syntax is used to change the name of a table.
Sometimes, we choose non-meaningful name for the table. So it is required
to be changed.
Let's see the syntax to rename a table from the database.
• ALTER TABLE table_name
RENAME TO new_table_name;
• Optionally, you can write following command to rename the table.
RENAME old_table _name To new_table_name;
• Example :
ALTER TABLE STUDENTS
RENAME TO ARTISTS;
• Or
RENAME STUDENTS TO ARTISTS;
After that the table "students" will be changed into table name "artists"
SQL - INSERT Query

• The SQL INSERT INTO Statement is used to add new rows of data to a
table in the database.
• Syntax :
1. INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)
VALUES (value1, value2, value3,...valueN);
2. INSERT INTO TABLE_NAME VALUES (value1,value2,value3,...valueN);
• Example :
1. INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES
(1, 'Ramesh', 32, 'Ahmedabad', 2000.00 );
2. INSERT INTO CUSTOMERS VALUES (7, 'Muffy', 24, 'Indore', 10000.00 );
Populate one table using
another table
• You can populate the data into a table through the select statement over
another table; provided the other table has a set of fields, which are
required to populate the first table.
• Syntax :
INSERT INTO first_table_name [(column1, column2, ... columnN)]
SELECT column1, column2, ...columnN FROM second_table_name
[WHERE condition];
Phases of select statement

• The SQL SELECT statement is used to fetch the data from a database
table which returns this data in the form of a result table. These result
tables are called result-sets.
• Syntax :
1. SELECT column1, column2, columnN FROM table_name;
Here, column1, column2... are the fields of a table whose values you
want to fetch. If you want to fetch all the fields available in the field,
then you can use the following syntax.
2. SELECT * FROM table_name;
• Example :
1. SQL> SELECT ID, NAME, SALARY FROM CUSTOMERS;
2. SQL> SELECT * FROM CUSTOMERS;
Optional clauses in SELECT
statement
There are some optional clauses in SELECT statement:

• [WHERE Clause] : It specifies which rows to retrieve.


• [GROUP BY Clause] : Groups rows that share a property so that the
aggregate function can be applied to each group.
• [HAVING Clause] : It selects among the groups defined by the GROUP BY
clause.
• [ORDER BY Clause] : It specifies an order in which to return the rows.
SQL - WHERE Clause
• The SQL WHERE clause is used to specify a condition while fetching the data
from a single table or by joining with multiple tables. If the given condition
is satisfied, then only it returns a specific value from the table. You should
use the WHERE clause to filter the records and fetching only the necessary
records.
• The WHERE clause is not only used in the SELECT statement, but it is also
used in the UPDATE, DELETE statement, etc., which we would examine in
the subsequent chapters.
• Syntax :
SELECT column1, column2, columnN
FROM table_name
WHERE [condition]
• Example :
SQL> SELECT ID, NAME, SALARY
FROM CUSTOMERS
WHERE SALARY > 2000;
SQL - UPDATE Query

• The SQL UPDATE Query is used to modify the existing records in a table.
You can use the WHERE clause with the UPDATE query to update the
selected rows, otherwise all the rows would be affected.
• Syntax :
UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
• Example : The following query will update the ADDRESS for a customer
whose ID number is 6 in the table.
SQL> UPDATE CUSTOMERS
SET ADDRESS = 'Pune'
WHERE ID = 6;
SQL - DELETE Query

• The SQL DELETE Query is used to delete the existing records from a
table.
• You can use the WHERE clause with a DELETE query to delete the
selected rows, otherwise all the records would be deleted.
• Syntax : DELETE FROM table_name WHERE [condition];
• Example : The following code has a query, which will DELETE a
customer, whose ID is 6.
SQL> DELETE FROM CUSTOMERS WHERE ID = 6;
• Note : If you want to DELETE all the records from the CUSTOMERS table,
you do not need to use the WHERE clause and the DELETE query would
be as follows −
SQL> DELETE FROM CUSTOMERS;
Now, the CUSTOMERS table would not have any record.
Logical operators

• The SQL AND & OR operators are used to combine multiple conditions to
narrow data in an SQL statement. These two operators are called as the
conjunctive operators or logical operators.
• These operators provide a means to make multiple comparisons with
different operators in the same SQL statement.
AND Operator
• The AND operator allows the existence of multiple conditions in an SQL
statement's WHERE clause.
• Syntax
• The basic syntax of the AND operator with a WHERE clause is as follows −
SELECT column1, column2, columnN FROM table_name WHERE
[condition1] AND [condition2]...AND [conditionN]; You can combine N
number of conditions using the AND operator. For an action to be taken by
the SQL statement, whether it be a transaction or a query, all conditions
separated by the AND must be TRUE.
• Example : Following is an example, which would fetch the ID, Name and
Salary fields from the CUSTOMERS table, where the salary is greater than
2000 and the age is less than 25 years −
• SQL> SELECT ID, NAME, SALARY FROM CUSTOMERS WHERE SALARY > 2000
AND age < 25;
The OR Operator
• The OR operator is used to combine multiple conditions in an SQL
statement's WHERE clause.
• Syntax
• The basic syntax of the OR operator with a WHERE clause is as follows −
• SELECT column1, column2, columnN FROM table_name WHERE
[condition1] OR [condition2]...OR [conditionN] You can combine N number
of conditions using the OR operator. For an action to be taken by the SQL
statement, whether it be a transaction or query, the only any ONE of the
conditions separated by the OR must be TRUE.
• Example : The following code block has a query, which would fetch the ID,
Name and Salary fields from the CUSTOMERS table, where the salary is
greater than 2000 OR the age is less than 25 years.
SQL> SELECT ID, NAME, SALARY FROM CUSTOMERS WHERE SALARY > 2000
OR age < 25;
Range searching in SQL

• The SQL BETWEEN Operator


• The BETWEEN operator selects values within a given range. The values
can be numbers, text, or dates.
• The BETWEEN operator is inclusive: begin and end values are included.
• Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
• Example : The following SQL statement selects all products with a price
BETWEEN 10 and 20
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
Pattern Matching

• The SQL LIKE Operator


• The LIKE operator is used in a WHERE clause to search for a specified
pattern in a column.
• There are two wildcards often used in conjunction with the LIKE operator:
• % - The percent sign represents zero, one, or multiple characters
• _ - The underscore represents a single character
• Syntax
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
• Example :
SELECT * FROM Customers
WHERE CustomerName LIKE 'a%';
LIKE Operator Description

WHERE CustomerName LIKE 'a%' Finds any values that start with "a"

WHERE CustomerName LIKE '%a' Finds any values that end with "a"

WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any
position

WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the
second position

WHERE CustomerName LIKE 'a_%' Finds any values that start with "a"
and are at least 2 characters in length

WHERE CustomerName LIKE 'a__%' Finds any values that start with "a"
and are at least 3 characters in length

WHERE ContactName LIKE 'a%o' Finds any values that start with "a"
and ends with "o"

SQL - Alias
SQL aliases are used to give a table, or a column in a table, a temporary name.
• Aliases are often used to make column names more readable.
• An alias only exists for the duration of the query.
• Syntax
1. The basic syntax of a table alias is as follows.
SELECT column1, column2.... FROM table_name AS alias_name WHERE
[condition];
2. The basic syntax of a column alias is as follows.
SELECT column_name AS alias_name FROM table_name WHERE [condition];
• Example
1. Now, the following code block shows the usage of a table alias.
SQL> SELECT C.ID, C.NAME, C.AGE, O.AMOUNT FROM CUSTOMERS AS C,
ORDERS AS O WHERE C.ID = O.CUSTOMER_ID;
2. Following is the usage of a column alias.
SQL> SELECT ID AS CUSTOMER_ID, NAME AS CUSTOMER_NAME FROM
CUSTOMERS WHERE SALARY IS NOT NULL;
SQL Functions
SQL provides many built-in functions to perform operations on data. These functions are useful while performing mathematical
calculations, string concatenations, sub-strings etc. SQL functions are divided into two categories,

• Aggregate/mathematical functions: • Scalar/group functions:


from the values of the column These
functions are used to do operations
and a single value is returned. These functions are based on user
input, these too returns single value.
AVG()
COUNT()
FIRST() UCASE()
LAST() LCASE()
MAX() MID()
MIN() LEN()
SUM()
ROUND()
NOW()
FORMAT()
Aggregate Functions

ID NAME MARKS AGE

1 Harsh 90 19

2 Suresh 50 20

3 Pratik 80 19

4 Dhanraj 95 21

5 Ram 85 18
• AVG(): It returns average value after calculating from values in a numeric
column.
Syntax: SELECT AVG(column_name) FROM table_name;
Queries: Computing average marks of students.
SELECT AVG(MARKS) AS AvgMarks FROM Students;
Output:
AvgMarks
80
• COUNT(): It is used to count the number of rows returned in a SELECT
statement. Syntax:SELECT COUNT(column_name) FROM table_name;
Queries: Computing total number of students.
SELECT COUNT(*) AS NumStudents FROM Stuents;
Output:
NumStudents
5
• FIRST(): The FIRST() function returns the first value of the selected column.
Syntax:SELECT FIRST(column_name) FROM table_name;
Queries: Fetching marks of first student from the Students table.
SELECT FIRST(MARKS) AS MarksFirst FROM Students;
Output:
MarksFirst
90
• LAST(): The LAST() function returns the last value of the selected column. It
can be used only in MS ACCESS.
Syntax:SELECT LAST(column_name) FROM table_name;
Queries: Fetching marks of last student from the Students table.
SELECT LAST(MARKS) AS MarksLast FROM Students;
Output:
MarksLast
82
• MAX(): The MAX() function returns the maximum value of the selected
column.
Syntax:SELECT MAX(column_name) FROM table_name;
Queries: Fetching maximum marks among students from the Students
table.
SELECT MAX(MARKS) AS MaxMarks FROM Students;
Output:
MaxMarks
95
• MIN(): The MIN() function returns the minimum value of the selected
column.
Syntax:SELECT MIN(column_name) FROM table_name;
Queries: Fetching minimum marks among students from the Students table.
SELECT MIN(MARKS) AS MinMarks FROM Students;
Output:
MinMarks
50
• SUM(): The SUM() function returns the sum of all the values of the selected
column.
Syntax:SELECT SUM(column_name) FROM table_name;
Queries: 1.Fetching summation of total marks among students from the
Students table.
SELECT SUM(MARKS) AS TotalMarks FROM Students;
Output:
TotalMarks
400
2.Fetching summation of total age among students from the Students table.
SELECT SUM(AGE) AS TotalAge FROM Students;
Output:
TotalAge
97
Scalar Functions

• UCASE(): It converts the value of a field to uppercase.


Syntax: SELECT UCASE(column_name) FROM table_name;
Queries: Converting names of students from the table Students to uppercase.
SELECT UCASE(NAME) FROM Students;
ID NAME MARKS AGE
Output:
1 Harsh 90 19
NAME
HARSH 2 Suresh 50 20

SURESH 3 Pratik 80 19

PRATIK 4 Dhanraj 95 21

DHANRAJ 5 Ram 85 18
RAM
• LCASE(): It converts the value of a field to lowercase.
Syntax : SELECT LCASE(column_name) FROM table_name;

Queries: Converting names of students from the table Students to lowercase.


SELECT LCASE(NAME) FROM Students;
Output: NAME
harsh ID NAME MARKS AGE

suresh 1 Harsh 90 19

2 Suresh 50 20
pratik
3 Pratik 80 19
dhanraj
4 Dhanraj 95 21
ram
5 Ram 85 18
• MID(): The MID() function extracts texts from the text field.
Syntax: SELECT MID(column_name,start,length) AS some_name FROM
table_name;
specifying length is optional here, and start signifies start position ( starting
from 1 )
Queries: Fetching first four characters of names of students from the
Students table.
SELECT MID(NAME,1,4) FROM Students;
Output: NAME
ID NAME MARKS AGE
HARS
1 Harsh 90 19
SURE
2 Suresh 50 20
PRAT
3 Pratik 80 19
DHAN
4 Dhanraj 95 21
RAM
5 Ram 85 18
• LEN(): The LEN() function returns the length of the value in a text field.
Syntax: SELECT LENGTH(column_name) FROM table_name;
Queries: Fetching length of names of students from Students table.
SELECT LENGTH(NAME) FROM Students;
Output: ID NAME MARKS AGE
NAME 1 Harsh 90 19
5 2 Suresh 50 20
6 3 Pratik 80 19
6 4 Dhanraj 95 21
7 5 Ram 85 18
3
• ROUND(): The ROUND() function is used to round a numeric field to the
number of decimals specified.
NOTE: Many database systems have adopted the IEEE 754 standard for
arithmetic operations, which says that when any numeric .5 is rounded it
results to the nearest even integer i.e, 5.5 and 6.5 both gets rounded off to
6.Syntax:
SELECT ROUND(column_name,decimals) FROM table_name;
decimals- number of decimals to be fetched.
Queries: Fetching maximum marks among students from the Students
table.
SELECT ROUND(MARKS,0) FROM table_name;
Output:
ID NAME MARKS AGE
MARKS 1 Harsh 90 19
90 2 Suresh 50 20
50 3 Pratik 80 19
80 4 Dhanraj 95 21

95 5 Ram 85 18

85
• NOW(): The NOW() function returns the current system date and time.
Syntax:SELECT NOW() FROM table_name;
Queries: Fetching current system time.
SELECT NAME, NOW() AS DateTime FROM Students;
Output:
NAME DateTime ID NAME MARKS AGE
HARSH 16/01/2021 1:30:11 PM 1 Harsh 90 19
SURESH 16/01/2021 1:30:11 PM 2 Suresh 50 20

PRATIK 16/01/2021 1:30:11 PM 3 Pratik 80 19

DHANRAJ 16/01/2021 1:30:11 PM 4 Dhanraj 95 21

RAM 16/01/2021 1:30:11 PM 5 Ram 85 18


• FORMAT(): The FORMAT() function is used to format how a field is to be
displayed.
Syntax: SELECT FORMAT(column_name,format) FROM table_name;
Queries: Formatting current date as ‘YYYY-MM-DD’.
SELECT NAME, FORMAT(Now(),'YYYY-MM-DD') AS Date FROM Students;
Output:
NAME Date ID NAME MARKS AGE
HARSH 2021-01-16 1 Harsh 90 19
SURESH 2021-01-16
2 Suresh 50 20
PRATIK 2021-01-16
DHANRAJ 2021-01-16 3 Pratik 80 19

RAM 2021-01-16 4 Dhanraj 95 21

5 Ram 85 18
Transaction Control
The following commands are used to control transactions.

• COMMIT − to save the changes.


• ROLLBACK − to roll back the changes.
• SAVEPOINT − creates points within the groups of transactions in which
to ROLLBACK.
The COMMIT Command

• The COMMIT command is the transactional command used to save


changes invoked by a transaction to the database.
• The COMMIT command is the transactional command used to save
changes invoked by a transaction to the database. The COMMIT
command saves all the transactions to the database since the last
COMMIT or ROLLBACK command.
• The syntax for the COMMIT command is as follows.
COMMIT;
• Example: Following is an example which would delete those records
from the table which have age = 25 and then COMMIT the changes in
the database.
SQL> DELETE FROM CUSTOMERS WHERE AGE = 25; SQL> COMMIT;
The ROLLBACK Command

• The ROLLBACK command is the transactional command used to undo


transactions that have not already been saved to the database. This
command can only be used to undo transactions since the last COMMIT
or ROLLBACK command was issued.
• The syntax for a ROLLBACK command is as follows −
ROLLBACK;
• Example: Following is an example, which would delete those records
from the table which have the age = 25 and then ROLLBACK the changes
in the database.
SQL> DELETE FROM CUSTOMERS WHERE AGE = 25; SQL> ROLLBACK;
The SAVEPOINT Command
• A SAVEPOINT is a point in a transaction when you can roll the transaction
back to a certain point without rolling back the entire transaction.
• The syntax for a SAVEPOINT command is as shown below.
SAVEPOINT SAVEPOINT_NAME;
This command serves only in the creation of a SAVEPOINT among all the
transactional statements. The ROLLBACK command is used to undo a group
of transactions.
• The syntax for rolling back to a SAVEPOINT is as shown below.
• ROLLBACK TO SAVEPOINT_NAME;
SQL> SAVEPOINT SP1; Savepoint created.
SQL> DELETE FROM CUSTOMERS WHERE ID=1; 1 row deleted.
SQL> rollback SP1;
THANK
YOU

You might also like