Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

Unit-I

Database
A database is a place/container where all the data is stored or Collection of related data.
Data:
Data are real facts. In a database, even the smallest piece of information becomes data.
For example, Name of a student is data, course is data, and Color is data, height, weight,
food, everything is data.
Need for Data
• To perform various actions on them.
• We can store all the information in the database.
• We can fetch information from the database, if we need to change any information,
we can update/delete them. But all the data will be in one place – Database.
Type of data stored in database
• In a database, related data are grouped together and stored under one group name
called a table.
• This helps in identifying which data is stored where and under what name.
• It reduces the time to search for particular data in a whole database.
For example: Student, Employee, Department, etc form individual tables.

File Processing System Vs DBMS


File Processing System

In earlier days, data was stored manually, using pen and paper but after computer was
discovered, the same task could be done by using files. A computer File is a resource which
uniquely records data, in a storage device in a computer. There are various formats in which
data can be stored. For eg., Text files can be stored in .txt format while pictures can be stored
in .png format etc. Each file is placed in relevant folders for the ease of access.

File Processing System (FPS) is a way of storing, retrieving and manipulating data which
is present in various files. All files are grouped based on their categories. The file names are
much related to each other and arranged properly to easily access the files. In file processing
system, if one needs to insert, delete, modify, store or update data, one must know the entire
hierarchy of the files.

• Records were stored in the form of files.


• The way we store in files is similar to papers, in the form of flat files –in notepad.
• The pieces of information where all in the notepads with each field of information
separated by space, tab, comma, semicolon, or any other symbol.

• All the files were grouped based on their categories.


• The file used to have only related information and each file is named properly.

Student files for each class were bundled inside different folders to identify it quickly.
• The file processing system is good when there is only a limited number of files and
data are very less.
• As the data and files in the system grow, handling them becomes difficult.
Advantages of File Processing System:

• Cost friendly –
There is a very minimal set up and usage fee for File Processing System.

• Easy to use –
File systems require very basic learning and understanding, hence, can be easily used.

• High scalability –
One can very easily switch from smaller to larger files as per his needs.

Disadvantages / Limitations of File System

1. Data Mapping and Access:


Although all the related information is grouped and stored in different files, there is no
mapping between any two files. i.e.; any two dependent files are not linked. Even
though Student files and Student_Report files are related, they are two different files
and they are not linked by any means. Hence if we need to display student details along
with his report, we cannot directly pick from those two files. We have to write a lengthy
program to search the Student file first, get all details, then go Student_Report file and
search for his report. When there is a very huge amount of data, it is always a time-
consuming task to search for particular information from the file system. It is always
an inefficient method to search for the data.

2. Data redundancy and Inconsistency: Data redundancy refers to the duplication of


data, let’s say we are managing the data of a college where a student is enrolled for two
courses, the same student details in such case will be stored twice, which will take more
storage than needed. Data redundancy often leads to higher storage costs and poor
access time.
• Data inconsistency: Data redundancy leads to data inconsistency, let’s take the same
example that we have taken above, a student is enrolled for two courses and we have
student address stored twice, now let’s say student requests to change his address, if the
address is changed at one place and not on all the records then this can lead to data
inconsistency.

3. Data Isolation: Because data are scattered in various files, and files may be in different
formats, writing new application programs to retrieve the appropriate data is difficult.
4. Atomicity Problem: Atomicity of a transaction refers to “All or nothing”, which means
either all the operations in a transaction executes or none.

For example: Let’s say Steve transfers 100$ to Negan’s account. This transaction
consists multiple operations such as debit 100$ from Steve’s account, credit 100$ to
Negan’s account. Like any other device, a computer system can fail. let’s say it fails after
first operation then in that case Steve’s account would have been debited by 100$ but
the amount was not credited to Negan’s account, in such case the rollback of operation
should occur to maintain the atomicity of transaction. It is difficult to achieve atomicity
in file processing systems.

5. Integrity Problem: If we need to check for certain insertion criteria while entering the
data into the file it is not possible directly. We can do it by writing programs. Say, if we
have to restrict the students above age 18, then it is by means of the program alone. There
is no direct checking facility in the file system. Hence these kinds of integrity checks are
not easy in the file systems.

6. Concurrent Access Anomalies: Multiple users accessing the same data from the same
file at a time is called concurrent access. In the file system, concurrent access leads to
incorrect data.
• For example, a student wants to borrow a book from the library. He searches for the
book in the library file and sees that only one copy is available. At the same time,
another student also wants to borrow the same book and checks that one copy available.
The first student opt for borrow and gets the book. But it is still not updated to zero-
copy in the file and the second student also opt for borrow. But there are no books
available. This is the problem of concurrent access to the file system.

7. Data Security: Data should be secured from unauthorised access, for example a student
in a college should not be able to see the payroll details of the teachers, such kind of
security constraints are difficult to apply in file processing systems.

8. Slow access time: Direct access of files is very difficult and one needs to know the
entire hierarchy of folders to get to a specific file. This involves a lot of time.

9. Unauthorized Access: Anyone who gets access to the file can read or modify the
data.
Note –
Database Management System (DBMS) is used to eliminate the disadvantages of the FPS.

Database Management System (DBMS)


• To overcome all drawbacks of the file processing system, a new system called
Database Management System is developed.
• All the files in the file processing system are known as tables in the database.
• In the database, each column values are known as attributes, and each row of
information is known as a record.
• There are no difference in the data being stored. But it is different from the file system
by the way data is stored and accessed in the database. So, each set of information is
stored in the form of rows and columns.
• We define unique key column for each table known as the primary key.
• Using the primary key, we can access the data much faster than the file system.
• We can also define a mapping between any two related tables. This helps in reducing
unnecessary data storage and faster retrieval of data.

Advantage of DBMS over file system


There are several advantages of Database management system over file system. Few of them
are as follows:

1. No redundant data: Redundancy removed by data normalization. No data duplication


saves storage and improves access time.
2. Data Consistency and Integrity: Data redundancy leads to data inconsistency, since
data normalization takes care of the data redundancy, data inconsistency also been taken
care as it is part of it.
3. Data Security: It is easier to apply access constraints in database systems so that only
authorized user is able to access the data. Each user has a different set of access thus data
is secured from the issues such as identity theft, data leaks and misuse of data.
4. Privacy: Limited access means privacy of data. DBMS can grant and revoke access to
the database on user level that ensures who is accessing which data. It also helps user to
manage the constraints on database, this ensures which type of data can be entered into
the table.
5. Easy access to data – Database systems manages data in such a way that the data is
easily accessible with fast response times. Even if the database size is huge, the DBMS
can still provide faster access and updation of data.
6. Easy recovery: Since database systems keeps the backup of data, it is easier to do a full
recovery of data in case of a failure. This is very useful especially for almost all the
organizations, as the data maintained over time should not be lost during a system crash
or failure.
7. Flexible: Database systems are more flexible than file processing systems. DBMS
systems are scalable, the database size can be increased and decreased based on the
amount of storage required. It also allows addition of additional tables as well as removal
of existing tables without disturbing the consistency of data.
8. Atomicity: DBMS makes sure either the transaction is fully complete or it is rolled back
to the previously committed state. It does not allow the system to be in a partially
committed state (complete state).

• In our example above, DBMS commits the transaction before transfer. If there is any
crash or shutdown of the system, before committing the transfer, transaction will be
rolled back to the original state. Hence it makes sure the atomicity of the transaction is
achieved.
9. Concurrent Access: DBMS provides access to multiple users to access the database
at the same time. It has its own mechanism to have concurrency accesses and hence
avoid any incorrect data in the system.
View of Data in DBMS – Three Level Architecture of a
Database
Abstraction is one of the main features of database systems.

Data Abstraction in DBMS


Database systems are made-up of complex data structures. To ease the user interaction with
database, the developers hide internal details from users. This process of hiding internal details
from user is called data abstraction.

For example: When you are booking a train ticket, you are not concerned how data is
processing at the back end when you click “book ticket”, what processes are happening when
you are doing online payments. You are just concerned about the message that pops up
when your ticket is successfully booked. This doesn’t mean that the process happening at the
back end is not relevant, it just means that you as a user are not concerned what is happening
in the database.

Instance and schema in DBMS


DBMS Schema

Definition of schema: Design of a database is called the schema.

For example: An employee table in database exists with the following attributes:

EMP_NAME EMP_ID EMP_ADDRESS EMP_CONTACT


-------- ------ ----------- -----------
This is the schema of the employee table. Schema defines the attributes of tables in the
database.

DBMS Instance
Definition of Instance: The data stored in database at a particular moment of time is called
instance of database. Database schema defines the attributes in tables that belong to a particular
database. The value of these attributes at a moment of time is called the instance of that
database.

For example, we have seen the schema of table “employee” above. Let’s see the table with the
data now. At this moment the table contains two rows (records). This is the current instance of
the table “employee” because this is the data that is stored in this table at this particular moment
of time.
EMP_NAME EMP_ID EMP_ADDRESS EMP_CONTACT
------- ------ ----------- -----------
Chaitanya 101 Noida 95********
Ajeet 102 Delhi 99********

Views of Data

DBMS Three Level Architecture Diagram

This architecture has three levels:


1. Internal level
2. Conceptual level
3. External level

1. Internal level / Physical level:

This is the lowest level of data abstraction. It describes how data is actually stored in
database. This level is also known as physical level. This level is also responsible for
allocating space to the data. This is the lowest level of the architecture.
2. Conceptual level / Logical level:

This is the middle level of 3-level data abstraction architecture. It describes what data is
stored in database.

It is also called logical level. The whole design of the database such as relationship among
data, schema of data etc. are described in this level.

3. External level / View Level:

Highest level of data abstraction. This level describes the user interaction with database
system. It is also called view level. The reason this level is called “view” is because users can
view their desired data from this level which is internally fetched from database with the help
of conceptual and internal level mapping.

The user doesn’t need to know the database schema details such as data structure, table
definition etc. user is only concerned about data which is returned back to the view level after
it has been fetched from database.

External level is the “top level” of the Three Level DBMS Architecture.

Example: Let’s say we are storing customer information in a customer table. At physical
level these records can be described as blocks of storage (bytes, gigabytes, terabytes etc.) in
memory. These details are often hidden from the programmers.

At the logical level these records can be described as fields and attributes along with their data
types, their relationship among each other can be logically implemented. The programmers
generally work at this level because they are aware of such things about database systems.

At view level, user just interact with system with the help of GUI and enter the details at the
screen, they are not aware of how the data is stored and what data is stored; such details are
hidden from them.
Data Independence

o Data independence can be explained using the three-schema architecture.


o Data independence refers the ability to modify the schema at one level of the database
system without altering the schema at the next higher level.

Fig: Data Independence

There are two types of data independence:

1. Logical Data Independence

o Logical data independence refers the ability to change the conceptual schema without
having to change the external schema.
o Logical data independence is used to separate the external level from the conceptual
view.
o If we do any changes in the conceptual view of the data, then the user view of the data
would not be affected.
o Logical data independence occurs at the user interface level.

2. Physical Data Independence

o Physical data independence can be defined as the ability to change the internal schema
without having to change the conceptual schema.
o If we do any changes in the storage size of the database system server, then the
Conceptual structure of the database will not be affected.
o Physical data independence is used to separate conceptual levels from the internal
levels.
o Physical data independence occurs at the logical interface level.

Database System Architecture


The architecture of DBMS depends on the computer system on which it runs. For example, in
a client-server DBMS architecture, the database systems at server machine can run several
requests made by client machine.

Types of DBMS Architecture

There are three types of DBMS architecture:

1. Single tier architecture


2. Two tier architecture
3. Three tier architecture

1. Single tier architecture


1 Tier Architecture in DBMS is the simplest architecture of Database in which the client,
server, and Database all reside on the same machine.

In this type of architecture, the database is readily available on the client machine, any request
made by client doesn’t require a network connection to perform the action on the database.

For example, lets say you want to fetch the records of employee from the database and the
database is available on your computer system, so the request to fetch employee details will be
done by your computer and the records will be fetched from the database by your computer as
well. This type of system is generally referred as local database system.
2. Two tier architecture

In two-tier architecture, the Database system is present at the server machine and the DBMS
application is present at the client machine, these two machines are connected with each other
through a reliable network as shown in the above diagram.

Whenever client machine makes a request to access the database present at server using a query
language like sql, the server perform the request on the database and returns the result back to
the client.
Two tier architecture provides added security to the DBMS as it is not exposed to the end-user
directly. It also provides direct and faster communication.

In the above 2 Tier client-server architecture of database management system, we can see that
one server is connected with clients 1, 2, and 3.

3. Three tier architecture


In three-tier architecture, another layer is present between the client machine and server
machine. In this architecture, the client application doesn’t communicate directly with the
database systems present at the server machine, rather the client application communicates with
server application and the server application internally communicates with the database system
present at the server.

Data Models
A database model is a logical structure of a database, which contains the relationships and
constraints that determine about how data is stored and accessed. The individual database
models are designed based on the rules and concepts.
Types of database models
There are so many database models, but most used database models are −
1. Hierarchical database model
2. Relational model
3. Network model

1. Hierarchical Model
• In Hierarchical Model, a hierarchical relation forms a tree-like structure.
• The relationship can be defined in the form of parent child type.
Example
• The hierarchy shows an Employee can be an Intern, on Contract or Full- Time. Sub-
levels show that Full-Time Employee can be hired as a Writer, Senior Writer or
Editor.

Advantages
• The design of the hierarchical model is simple.
• Provides Data Integrity since it is based on parent/ child relationship
• Data sharing is feasible since the data is stored in a single database.
• Even for large volumes of data, this model works perfectly.
Disadvantages
• Implementation is complex.
• This model has to deal with anomalies like Insert, Update and Delete.
• Maintenance is difficult since changes done in the database may want you to do
changes in the entire database structure.
2. Network Model
• The Hierarchical Model creates hierarchical tree with parent/ child relationship,
whereas the Network Model has graph and links.
• The relationship can be defined in the form of links and it handles many-to-many
relations. This states that a record can have more than one parent.
Example

Advantages
• Easy to design the Network Model
• The model can handle one-one, one-to-many, many-to-many relationships.
3. Relational Model
• A relational model groups data into one or more tables. These tables are related to
each other using common records.
• The data is represented in the form of rows and columns i.e. tables:
Example
• Let us see an example of two relations <Employee> and <Department> linked to
each other, with DepartmentID, which is Foreign Key of <Employee> table and
Primary key of <Department> table.

Advantages
• The Relational Model does not have any issues that we saw in the previous two
models i.e. update, insert and delete anomalies have nothing to do in this model.
• Changes in the database do not require you to affect the complete database.
• Implementation of a Relational Model is easy.
• To maintain a Relational Model is not a tiresome task.
Disadvantages
• Database inefficiencies hide and arise when the model has large volumes of data.
• The overheads of using relational data model come with the cost of using powerful
hardware and devices.

Comparison of Data Models


Difference between the three models on the basis of features and operations (DBMS)
o The major differences between the hierarchical, network and the relational models are
as follows –
Hierarchical Model Network Model Relational Model

One-to-many or one-to-one Allowed the network model to One-to-one, one-to-many, many-


relationship. support many-to-many to-one relationship.
relationships.

Based on parent child relationship. A record can have many parents as Based on relational data structures.
well as many children.

Cannot insert the information of a Does not suffer from any insertion Doesn’t suffer from any insertion
child who does not have any anomaly. anomaly.
parent.

Multiple occurrences of child Free from update anomalies. Free from update anomalies.
records which lead to problems of
inconsistency during the update
operation.

Deletion of parent results in Free from delete anomalies. Free from delete anomalies.
deletion of child record.

This model lacks data There is partial data independence. It provides data independence.
independence.

Less flexible. Flexible. Flexible.

Difficult to access data. Easier to access data. Easier to access data.

Arrange data in a tree-like Organizes data in graph-like Arrange data in tables.


structure. structure.

SQL (Structured Query Language)


o SQL stands for Structured Query Language. It is used for storing and managing data in
relational database management system (RDMS).
o It is a standard language for Relational Database System. It enables a user to create,
read, update and delete relational databases and tables.

Overview of SQL

SQL Commands
o SQL commands are instructions. It is used to communicate with the database. It is also
used to perform specific tasks, functions, and queries of data.
o SQL can perform various tasks like create a table, add data to tables, drop the table,
modify the table, set permission for users.

Types of SQL Commands


There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL.

• Data Definition Language (DDL) : used to create, alter and delete database objects.
• Data Manipulation Language (DML) : used to insert, modify and delete the data in
the database.
• Data Query Language (DQL) : enables the users to query one or more tables to get
the information they want.
• Data Control Language (DCL) : controls the user access to the database objects.-
Grant and Revoke
• Transaction Control Language (TCL) : manage all the changes made by the DML
statements - commit, savepoint, rollback
SQL Data Types

SQL supports the following data types:

1. Char(n) – fixed length string of exactly ‘n’ characters


2. Varchar(n) – varying length string whose maximum length is ‘n’characters
3. Float(n) – floating point number
4. Number(n) / int / integer – allows to store integer numbers
5. Number(p,d) / Numeric(p,d) – fixed point numbers with user specified precision.
Consists of p digits and d for decimal point
6. Smallint – a small integer

DATA DEFINITION LANGUAGE (DDL)

The DDL commands are as follows:


a. Create
b. Alter
c. Truncate
d. Drop
1. CREATE TABLE COMMAND
• A table is a unit of storage which holds data in the form of rows and columns.
• In a table,
• A unique column name should be specified.
• Proper data type along with its width should be specified.
Syntax: create table <table name> (column definition1, column definition 2, …);

Example: create table student (regno number(11), name varchar(25), addr varchar(25),
dept varchar2 (3));
Output:
Table created.

2. ALTER TABLE COMMAND


• This command is used to add a new column, modify the existing column definition,
include or drop integrity constraints.
Syntax: 1. alter table <table name> modify (column definition …);
2. alter table <table name> add (column definition …);
3. alter table <table name> rename column <existing column name> to <new
column name>;
4. alter table <tablename> rename to <new tablename>;
Example:
I. alter table student modify (name varchar2 (30));
Output:
Table altered.

II. alter table student add (contact number(10));


Output:
Table altered.

III. alter table student rename column rno to regno;


Output:
Table altered.

IV. To Rename Table

Syntax:
alter table <existing-table name> rename to <new-table name>;
Example:
alter table Student rename to Marksheet;
Output:
Table altered.
3. TRUNCATE TABLE COMMAND
• The truncate command deletes all rows from the table. Only the structure of the table
remains.
Syntax: Truncate table <table name>;
Example: Truncate table student;
Output:
Table truncated.

4. DESC COMMAND
• This command will display the structure of the table.
Syntax: Desc <table name>;

Example: Desc student;

Output:
Name Null? Type
---------------------------------- -------- ----------------------------
REGNO NUMBER(11)
NAME VARCHAR2(30)
DEPT VARCHAR2(3)
COMMENTS LONG

5. DROP TABLE COMMAND


• The drop table command is used to delete the table permanently from the database.
Syntax: Drop table <table name>;

Example: Drop table student;

Output:
Table dropped.

DATA MANIPULATION LANGUAGE (DML)


The DML commands are as follows:
a. Insert
b. Select
c. Update
d. Delete

1. INSERT COMMAND
• Used to add one or more rows to a table.
• While using this command the values are separated by commas and the data types
char and date are enclosed in apostrophes.
• The values must be entered in the same order as they are defined in the table.

Syntax:

i) To insert all column values for each row


SQL> Insert into <table_name> values (a list of data values);

Example:

INSERT INTO Book VALUES ("Sonoo", "DBMS");

ii) To insert specific column values for each row


SQL> Insert into <table_name> (col_names) values (list of values);

Example:

INSERT INTO Book (Author, Subject) VALUES ("Sonoo", "DBMS");

iii) Syntax:
SQL> Insert into <table_name> Values (&list of Column names,…..);
Example:
SQL> insert into biodata values(&rno,'&sname','&course','&dob','&address','&city',&phno);

2. SELECT COMMAND
• Used to perform a query. The query is a request for information from table in a database.
Syntax: Select [All/Distinct] column_name … from table_name …
[where conditions ]
[group by column_name …]
[having conditions ]
[order by column_name …] [DESC];
• To select only specific columns, specify the column names or use * to select all
columns from the table in the select command.
• The select command lists the items to be retrieved.
• The from clause specifies the table or tables from where the data has to be retrieved.
• The where clause specifies the search criteria.
• The group by clause will group the result set based on the given column.
• The having clause tells SQL to include only certain groups produced by the group
by clause in the query result set. It is used to specify the search criteria or search
condition when group by clause is specified.

• The order by clause sorts or orders the results based on the data in one or more
columns in the ascending or descending order.

SELECT * FROM table_name;


SELECT * FROM Customers;
SELECT CustomerName, City FROM Customers;

3. UPDATE COMMAND
• Used to alter the column values in a table.
• Specific rows could be updated based on a specific condition.
Syntax: Update table_name set field = value, … [where condition];
• The ‘where’ clause and the ‘set’ clause can also include queries.
Example:

SQL> UPDATE students SET User_Name = 'Sonoo' WHERE Student_Id = '3'

4. DELETE COMMAND
• Used to delete one or more rows to a table.
Syntax: Delete from <table_name> [where conditions];
Example:

SQL> DELETE FROM Book WHERE Author="Sonoo";


SQL Set Operations
The SQL Set operation is used to combine the two or more SQL SELECT statements.

Types of Set Operation

1. Union
2. Union All
3. Intersect
4. Minus

1. Union

o The SQL Union operation is used to combine the result of two or more SQL SELECT
queries.
o In the union operation, all the number of datatype and columns must be same in both
the tables on which UNION operation is being applied.
o The union operation eliminates the duplicate rows from its resultset.

Syntax

1. SELECT column_name FROM table1


2. UNION
3. SELECT column_name FROM table2;

Example:

The First table


ID NAME

1 Jack

2 Harry

3 Jackson

The Second table

ID NAME

3 Jackson

4 Stephan

5 David

Union SQL query will be:

1. SELECT * FROM First


2. UNION
3. SELECT * FROM Second;

The resultset table will look like:

ID NAME

1 Jack

2 Harry

3 Jackson

4 Stephan

5 David

2. Union All

Union All operation is equal to the Union operation. It returns the set without removing
duplication and sorting the data.
Syntax:

1. SELECT column_name FROM table1


2. UNION ALL
3. SELECT column_name FROM table2;

Example: Using the above First and Second table.

Union All query will be like:

1. SELECT * FROM First


2. UNION ALL
3. SELECT * FROM Second;

The resultset table will look like:

ID NAME

1 Jack

2 Harry

3 Jackson

3 Jackson

4 Stephan

5 David

3. Intersect

o It is used to combine two SELECT statements. The Intersect operation returns the
common rows from both the SELECT statements.
o In the Intersect operation, the number of datatype and columns must be the same.
o It has no duplicates and it arranges the data in ascending order by default.

Syntax

1. SELECT column_name FROM table1


2. INTERSECT
3. SELECT column_name FROM table2;
Example:

Using the above First and Second table.

Intersect query will be:

1. SELECT * FROM First


2. INTERSECT
3. SELECT * FROM Second;

The resultset table will look like:

ID NAME

3 Jackson

4. Minus

o It combines the result of two SELECT statements. Minus operator is used to display
the rows which are present in the first query but absent in the second query.
o It has no duplicates and data arranged in ascending order by default.

Syntax:

1. SELECT column_name FROM table1


2. MINUS
3. SELECT column_name FROM table2;

Example

Using the above First and Second table.

Minus query will be:

1. SELECT * FROM First


2. MINUS
3. SELECT * FROM Second;

The resultset table will look like:

ID NAME
1 Jack

2 Harry

DATA CONTROL LANGUAGE (DCL)


DCL is used for granting and revoking user access on a database –

Data Control Language statements are used to grant privileges on tables, views, sequences,
procedures to other users.

The DCL commands/statements are as follows:

a. Grant: used to grant privileges to other users


b. Revoke: used to take back privileges granted to other users.

Privileges are of two types:

a. System Privileges
b. Object Privileges
• System Privileges are granted by a DBA to users.
Examples of system privileges are CREATE TABLE, and CREATE USER etc.
• Object Privileges means privileges on objects such as tables, views, procedure.
These are granted by owner of the object.
Object privileges are ALTER, DELETE, INSERT, SELECT, and UPDATE etc.

1. CREATING USERS – Used to create a user

Syntax: Create user <username> identified by <password>;


Example: Create user scott identified by tiger;

2. GRANT PRIVILEGE COMMAND – Used to grant database object’s privileges [i.e.


right to access another user’s objects (table, view, …)] to other users.

Syntax: Grant <privileges> on <object-name> to <username>;

Example: SQL>grant select on emp to scott;


Grant succeeded.

3. REVOKE PRIVILEGE COMMAND –Used to withdraw (revoke) privileges already


granted to other users.

Syntax: Revoke <privileges> on <object-name> from <username>;

Example: SQL> revoke select on emp from scott;

Revoke succeeded.

Transaction Control Language (TCL) commands


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

Types of TCL Commands

1. Commit
2. Rollback
3. Savepoint

1. COMMIT command

COMMIT command is used to permanently save any transaction into the database.

When we use any DML command like INSERT, UPDATE or DELETE, the changes made
by these commands are not permanent, until the current session is closed, the changes made
by these commands can be rolled back.

To avoid that, we use the COMMIT command to mark the changes as permanent.

Commit command's syntax,

COMMIT;

2. ROLLBACK command

This command restores the database to last commited state. It is also used
with SAVEPOINT command to jump to a savepoint in an ongoing transaction.
If we have used the UPDATE command to make some changes into the database, and realise
that those changes were not required, then we can use the ROLLBACK command to rollback
those changes, if they were not commited using the COMMIT command.

Rollback command's syntax,

ROLLBACK TO savepoint_name;

3. SAVEPOINT command

SAVEPOINT command is used to temporarily save a transaction so that you can rollback to
that point whenever required.

Savepoint command's syntax,

SAVEPOINT savepoint_name;

Using Savepoint and Rollback

Following is the table class,

id name

1 Abhi

2 Adam

4 Alex

Example

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;

NOTE: SELECT statement is used to show the data stored in the table.
The resultant table will look like,

id name

1 Abhi

2 Adam

4 Alex

5 Abhijit

6 Chris

7 Bravo

use the ROLLBACK command to roll back the state of data to the savepoint B.

ROLLBACK TO B;

SELECT * FROM class;


Now our class table will look like,

id name

1 Abhi

2 Adam

4 Alex

5 Abhijit

6 Chris

Again use the ROLLBACK command to roll back the state of data to the savepoint A

ROLLBACK TO A;

SELECT * FROM class;

Now the table will look like,

id name

1 Abhi

2 Adam

4 Alex
5 Abhijit

You might also like