0% found this document useful (0 votes)
89 views19 pages

Practical-1: AIM: Practical On Transaction Control Language. Theory

The document discusses transaction control language (TCL) commands used to manage changes made by data manipulation language (DML) statements. It describes the COMMIT, ROLLBACK, SAVEPOINT, and SET TRANSACTION commands. COMMIT permanently saves transactions, ROLLBACK restores the database to the last committed state, SAVEPOINT temporarily saves a transaction point, and SET TRANSACTION specifies a transaction as read-only or read/write. Practical examples are provided for each command.

Uploaded by

Himanshu Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views19 pages

Practical-1: AIM: Practical On Transaction Control Language. Theory

The document discusses transaction control language (TCL) commands used to manage changes made by data manipulation language (DML) statements. It describes the COMMIT, ROLLBACK, SAVEPOINT, and SET TRANSACTION commands. COMMIT permanently saves transactions, ROLLBACK restores the database to the last committed state, SAVEPOINT temporarily saves a transaction point, and SET TRANSACTION specifies a transaction as read-only or read/write. Practical examples are provided for each command.

Uploaded by

Himanshu Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Himanshu Jain(01050118118)

DBMS File

PRACTICAL-1

AIM : Practical on Transaction Control Language. Theory:

 TCL stands for Transaction Control Language.


 This command is used to manage the changes made by DML statements.
 TCL allows the statements to be grouped together into logical transactions.

TCL commands are as follows:


1. COMMIT
2. SAVEPOINT
3. ROLLBACK
4. SET TRANSACTION

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. Following
is commit command's syntax,

Commit;
Himanshu Jain(01050118118)
DBMS File

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.
Following is rollback command's syntax
Rollback to savepoint name;

SAVEPOINT command:-
SAVEPOINT command is used to temporarily save a transaction so that you can rollback to that
point whenever required.
Following is savepoint command's syntax,
SAVEPOINT savepoint name;
Himanshu Jain(01050118118)
DBMS File

4. SET TRANSACTION:-

 SET TRANSACTION is used for placing a name on a transaction.

Syntax:
SET TRANSACTION [Read Write | Read Only];

 You can specify a transaction to be read only or read write.


 This command is used to initiate a database transaction.
Himanshu Jain(01050118118)
DBMS File

PRACTICAL -2

AIM : Practical on Data Control Language(Grant & Revoke).

 DCL stands for Data Control Language.


 DCL is used to control user access in a database.
 This command is related to the security issues.
 Using DCL command, it allows or restricts the user from accessing data in database
schema.
DCL commands are as follows,
1. GRANT
2. REVOKE

 It is used to grant or revoke access permissions from any database user.

Data Control Language(DCL) is used to control privileges in Database. To perform any


operation in the database, such as for creating tables, sequences or views, a user needs
privileges. Privileges are of two types,

 System: This includes permissions for creating session, table, etc and all types of

other system privileges.

 Object: This includes permissions for any command or query to perform any

operation on the database tables.

In DCL we have two commands,

GRANT: Used to provide any user access privileges or other priviliges for the database.
REVOKE: Used to take back permissions from any user.

Allow a User to create session


When we create a user in SQL, it is not even allowed to login and create a session until
and unless proper permissions/priviliges are granted to the user.
Following command can be used to grant the session creating priviliges.
GRANT CREATE SESSION to username;

GRANT USAGE ON `employeedata`.* TO ‘Himanshu’@'localhost' WITH GRANT OPTION;
Himanshu Jain(01050118118)
DBMS File
Himanshu Jain(01050118118)
DBMS File

Allow a User to create table


To allow a user to create tables in the database, we can use the below command,

GRANT CREATE TABLE TO username;

GRANT CREATE ON `employeedata`.* TO 'Himanshu'@'localhost';

Grant all privilege to a User


sysdba is a set of priviliges which has all the permissions in it. So if we want to provide all the privileges
to any user, we can simply grant them
the sysdba permission.

GRANT sysdba TO username

GRANT ALL PRIVILEGES ON `employeedata`.* TO 'HImanshuk'@'localhost' WITH GRANT


OPTION;
Grant permission to create any table
Sometimes user is restricted from creating come tables with names which are reserved for
system tables. But we can grant privileges to a user to create any table using the below
command,

GRANT CREATE ANY TABLE TO username


GRANT CREATE ON `employeedata`.* TO 'Himanshu'@'localhost';

Grant permission to drop any table


As the title suggests, if you want to allow user to drop any table from the database, then
grant this privilege to the user,

GRANT DROP ANY TABLE TO username


GRANT DROP ON `employeedata`.* TO 'Himanshu'@'localhost';
Himanshu Jain(01050118118)
DBMS File

To take back Permissions


And, if you want to take back the privileges from any user, use the Revoke command.

REVOKE CREATE TABLE FROM username


REVOKE ALL PRIVILEGES ON `employeedata`.* FROM 'Himanshu'@'localhost';
Himanshu Jain(01050118118)
DBMS File

PRACTICAL- 3
AIM : Practical based on Natural Join.
SQL Join is used to fetch data from two or more tables, which is joined to appear as single
set of data. It is used for combining column from two or more tables by using values
common to both tables.
JOIN Keyword is used in SQL queries for joining two or more tables. Minimum required
condition for joining table, is (n-1) where n, is number of tables. A table can also join to
itself, which is known as, Self Join.

Types of JOIN
Following are the types of JOIN that we can use in SQL:

 Inner

 Outer

 Left

 Right

SELECT * FROM

table-name1 NATURAL JOIN table-name2;

Natural JOIN
Natural Join is a type of Inner join which is based on column having same name and same
datatype present in both the tables to be joined.
The syntax for Natural Join is,
Example of Natural JOIN:-
Here is the test table,
Himanshu Jain(01050118118)
DBMS File

and the test2 table,

Natural join query will be,

SELECT * from Test NATURAL JOIN Test2;

The resultent table will look like,

In the above example, both the tables being joined have ID column(same name and same
datatype), hence the records for which value of ID matches in both the tables will be the result of
Natural Join of these two tables.
Himanshu Jain(01050118118)
DBMS File

PRACTICAL -4

AIM : Practical based on Outer Join.


Outer Join is based on both matched and unmatched data. Outer Joins subdivide further into:-

1. Left Outer Join


2. Right Outer Join
3. Full Outer Join
LEFT Outer Join
The left outer join returns a resultset table with the matched data from the two tables and
then the remaining rows of the left table and null from the right table's columns.
Syntax for Left Outer Join is,
SELECT column-name-list FROM

table-name1 LEFT OUTER JOIN table-name2

ON table-name1.column-name = table-name2.column-name;

To specify a condition, we use the ON keyword with Outer Join. Left


outer Join Syntax for Oracle is,

SELECT column-name-list FROM

table-name1, table-name2 on table-name1.column-name = table-


name2.column-name(+);

Example of Left Outer Join


Left Outer Join query will be,

SELECT * FROM Test LEFT OUTER JOIN Test2 ON (Test.id = Test2.id2);


The resultent table will look like,
Himanshu Jain(01050118118)
DBMS File
Himanshu Jain(01050118118)
DBMS File

RIGHT Outer Join


The right outer join returns a resultset table with the matched data from the two tables
being joined, then the remaining rows of the right table and null for the remaining left
table's columns.
Syntax for Right Outer Join is,
SELECT column-name-list FROM

table-name1 RIGHT OUTER JOIN table-name2

ON table-name1.column-name = table-name2.column-name;

Right outer Join Syntax for Oracle is,


SELECT column-name-list FROM table-name1, table-name2
ON table-name1.column-name(+) = table-name2.column-name;

Example of Right Outer Join

Right Outer Join query will be,

SELECT * FROM class RIGHT OUTER JOIN class_info ON (class.id = class_info.id);


Himanshu Jain(01050118118)
DBMS File

Full Outer Join


The full outer join returns a resultset table with the matched data of two table then
remaining rows of both left table and then the right table.
Syntax of Full Outer Join is,
SELECT column-name-list FROM

table-name1 FULL OUTER JOIN table-name2

ON table-name1.column-name = table-name2.column-name;

Example of Full outer join is


Full Outer Join query will be like,

SELECT * FROM class FULL OUTER JOIN class_info ON (class.id = class_info.id);

The resultent table will look like,


Practical-7
Aim:- Create views and test it on database.

Views in SQL are kind of virtual tables. A view also has rows and columns as they are in a real
table in the database. We can create a view by selecting fields from one or more tables present in
the database. A View can either have all the rows of a table or specific rows based on certain
condition.
CREATING VIEWS
We can create View using CREATE VIEW statement. A View can be created from a single table
or multiple tables.
Syntax:

CREATE VIEW view_name AS


SELECT column1, column2.....
FROM table_name
WHERE condition;

view_name: Name for the View


table_name: Name of the table
condition: Condition to select rows

Examples:
 Creating View from a single table:
 In this example we will create a View named DetailsView from the table
StudentDetails.
Query:
CREATE VIEW DetailsView AS
SELECT FirstName, LastName
FROM Test
WHERE id < 5;

To see the data in the View, we can query the view in the same manner as we query a table.

SELECT * FROM DetailsView;


In this example, we will create a view named StudentNames from the table Test
Query:
CREATE VIEW StudentNames AS
SELECT id, FirstName
FROM Test
ORDER BY LastName;

select * from StudentNames


Practical-6
Aim:- Write and execute basic pl/sql program.

Every PL/SQL statement ends with a semicolon (;). PL/SQL blocks can be nested within other
PL/SQL blocks using BEGIN and END. Following is the basic structure of a PL/SQL block −
DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling>
END;

Example:-

The 'Hello World' Example

DECLARE
message varchar2(20):= 'Hello, World!';
BEGIN
dbms_output.put_line(message);
END;
Practical-7
Aim:- Implement PL/SQL program using control structure.

Pl/Sql Control Structures consists of following:-


 Loops
 Case Statements

FOR Loop:-
A FOR LOOP is a repetition control structure that allows you to efficiently
write a loop that needs to execute a specific number of times.
Syntax;-
FOR counter IN initial_value .. final_value LOOP
sequence_of_statements;
END LOOP;
Following is the flow of control in a For Loop −
 The initial step is executed first, and only once. This step allows you to declare and
initialize any loop control variables.
 Next, the condition, i.e., initial_value .. final_value is evaluated. If it is TRUE, the body of
the loop is executed. If it is FALSE, the body of the loop does not execute and the flow of
control jumps to the next statement just after the for loop.
 After the body of the for loop executes, the value of the counter variable is increased or
decreased.
 The condition is now evaluated again. If it is TRUE, the loop executes and the process
repeats itself (body of loop, then increment step, and then again condition). After the
condition becomes FALSE, the FOR-LOOP terminates.
Example:-
Case Statement:-

The CASE statement goes through conditions and returns a value


when the first condition is met (like an IF-THEN-ELSE statement). So, once a condition is true, it
will stop reading and return the result. If no conditions are true, it returns the value in the ELSE
clause.

If there is no ELSE part and no conditions are true, it returns NULL.

CASE Syntax:-
CASE
     WHEN condition1 THEN result1
     WHEN condition2 THEN result2
   WHEN conditionN THEN resultN
     ELSE result
END;

Example:-

GOTO Statement:-
The SQL GOTO statement is used to alter the flow of a program. When the
execution reaches the GOTO statement, it will jump unconditionally to the label specified in the
goto statement.

Syntax:-

--List of Statements
GOTO label
........
........
label:
statements
Example:-

IF-THEN Statement:-

This is a single level conditional statement. This conditional structure is commonly


used to manipulate the data in the tables from the command prompt.

The basic syntax of IF THEN statement is as follows:-

IF ( condition ) THEN
statement
END IF;

Example:-

You might also like