You are on page 1of 38

Udaypratapsingh Rajput Database Mangement 3CE3

PRACTICAL-1
Creating and Manipulating Database objects and Applying Constraints (DDL)

1) Write a query to create the table of DEPOSIT.


QUERY: create table DEPOSIT(act_no number(5) PRIMARY KEY, c_name
varchar2(10), b_name varchar2(10), amount number(10,4), a_date date);

2) Write a query to create the table of BRANCH.


QUERY: create table BRANCH(b_name varchar2(10) PRIMARY KEY, city
varchar2(10));

3) Write a query to create the table of CUSTOMER.


QUERY: create table CUSTOMER(c_name varchar2(10) PRIMARY KEY, city
varchar2(10));

4) Write a query to create the table of BORROW.


QUERY:
create table BORROW(loan_no number(5) PRIMARY KEY, c_name varchar2(10), b_name
varchar2(10), amount number(10,4));

5) Write a query to create table of PERSON.


QUERY: create table PERSON(p_id number(5) PRIMARY KEY, p_name varchar2(20), age
number(3));

P a g e 1 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

6) Write a query to alter table of PERSON.


(i) Alter table by using ADD.
QUERY: alter table PERSON ADD(salary
number(10,4));

(ii) Alter table by using MODIFY.


QUERY: alter table PERSON MODIFY(age int NOT NULL, salary number (10,4)
NOT NULL);

7) Write a query to truncate table of PERSON.


QUERY: truncate table
PERSON;

P a g e 2 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

8) Write a query to drop table of PERSON.


QUERY: drop table
PERSON

P a g e 3 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

PRACTICAL-2
Manipulating Data with Database Objects (DML)

1) Write a query to insert values in the table of DEPOSIT.


QUERY:
insert into DEPOSIT values('&act_no', '&c_name', '&b_name', '&amount', '&a_date');

2) Write a query to insert values in the table of BRANCH.


QUERY:
insert into BRANCH values('&b_name', '&city');

P a g e 4 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

3) Write a query to insert values in the table of CUSTOMER.


QUERY:
insert into CUSTOMER values('&c_name', '&city');

P a g e 5 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

4) Write a query to insert values in the table of BORROW.


QUERY:
insert into BORROW values('&loan_no', '&c_name', '&b_name', '&amount');

P a g e 6 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

5) Write a query to insert values in the table of PERSON.


QUERY:
insert into PERSON values('&p_id', '&p_name', '&age', '&salary');

P a g e 7 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

6) Write a query to update values in the table of PERSON.


QUERY:
update PERSON SET p_name = 'Jainil', age = 24 WHERE p_id = 10002;

P a g e 8 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

7) Write a query to delete values of the row in the table of PERSON.


QUERY:
delete from PERSON WHERE p_name = 'Jainil';

P a g e 9 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

PRACTICAL-3
Retrieving, Restricting and Sorting Data (DRL)

1) Write a query to display the details of the table of DEPOSIT.


QUERY:
select * from DEPOSIT;

2) Write a query to display the details of the table of BRANCH.


QUERY:
select * from BRANCH;

3) Write a query to display the details of the table of CUSTOMER.


QUERY:
select * from CUSTOMER;

4) Write a query to display the details of the table of BORROW.


QUERY:

P a g e 10 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

select * from BORROW;

5) Write a query to display all the account in DEPOSIT along with their account
number and amount.
QUERY:
select act_no, amount from DEPOSIT;

6) Write a query to display details from BORROW where amount not equal to
50000.
QUERY:
1111select * from BORROW WHERE amount != 50000;

7) Write a query to display details from BORROW where amount is less than or
equal to 100000.
QUERY:
select * from BORROW WHERE amount <= 100000;

P a g e 11 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

8) Write a query to display details from BORROW where amount equals to


500000.
QUERY:
select * from BORROW WHERE amount = 500000;

9) Write a query to display details from BORROW where amount is less than
200000.
QUERY:
select * from BORROW WHERE amount < 200000;

10) Write a query to check whether the entered age of person is greater than or
equal to 18 by altering the table of PERSON.
QUERY:
alter table PERSON ADD CHECK(age >= 18);

11) Write a query to find out the customer’s name starting with ‘P’ from the table
of CUSTOMER.
QUERY:
select c_name from CUSTOMER where c_name LIKE 'P%';

P a g e 12 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

12) Write a query to find out the customer’s name ending with ‘a’ from the table of
CUSTOMER.
QUERY:
select c_name from CUSTOMER where c_name LIKE '%a';

13) Write a query to find out the customer’s name starting with ‘P’ and ending with
‘a’ from the table of CUSTOMER.
QUERY:
select c_name from CUSTOMER where c_name LIKE 'P%a';

14) Write a query to find out the customer’s name having substring ‘oo’ in
between from the table of CUSTOMER.
QUERY:
select c_name from CUSTOMER where c_name LIKE '%oo%';

15) Write a query to find out the customer’s name whose second letter is ‘o’ from
the table of CUSTOMER.
QUERY:
select c_name from CUSTOMER where c_name LIKE '_o%';

P a g e 13 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

16) Write a query to find out the customer’s name starting with ‘P’ and having at
least three letters from the table of CUSTOMER.
QUERY:
select c_name from CUSTOMER where c_name LIKE 'P__%';

17) Write a query to find out the person’s name having age between 16 and 19
from the table of PERSON.
QUERY:
select p_name from person where age BETWEEN 16 AND 19;

18) Write a query to find out the customer’s name who opened account between
‘18-NOV2020’ and ‘18-NOV-2021’ from the table of DEPOSIT.
QUERY: select c_name from DEPOSIT where a_date BETWEEN '18-NOV-
2020' AND '18-NOV2021';

19) Write a query to find out the customer’s name who is living in city ‘Anand’ or
‘Ahmedabad’ from the table of CUSTOMER.

i) Using OR.

QUERY:select c_name from CUSTOMER where city = 'Anand' OR city = 'Ahmedabad';

P a g e 14 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

ii) Using IN.

QUERY: select c_name from CUSTOMER where city IN('Anand',


'Ahmedabad');

20)Write a query to find out the customer’s name who is not living in city ‘Anand’
from the table of CUSTOMER.
QUERY:
select c_name from CUSTOMER where NOT city = 'Anand';

21) Write a query to find out the customer’s name who is neither living in city
‘Anand’ nor in Ahmedabad’ from the table of CUSTOMER.

i) Using NOT.

QUERY: select c_name from CUSTOMER where NOT city = 'Anand' AND NOT
city = 'Ahmedabad';

ii)Using NOT IN. QUERY: select c_name from CUSTOMER where city NOT
IN('Anand', 'Ahmedabad');

P a g e 15 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

22) Write a query to display the details of the table DEPOSIT in ascending order
by name. QUERY: select * from DEPOSIT ORDER BY c_name ASC;

23) Write a query to display the details of the table DEPOSIT in Descending
order by name. QUERY:

select * from DEPOSIT ORDER BY c_name DESC;

P a g e 16 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

PRACTICAL-4
SQL Single Row Functions

1) Write a query to convert the string in c_name to Upper Case from the table of
CUSTOMER.
QUERY:
select UPPER(c_name) from CUSTOMER;

2) Write a query to convert the string in c_name to Lower Case from the table of
CUSTOMER.
QUERY:
select LOWER(c_name) from CUSTOMER;

3) Write a query to convert the string in c_name by Capping Initial Letter from the
table of CUSTOMER.
QUERY:
select INITCAP(c_name) from CUSTOMER;

P a g e 17 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

4) Write a query to concatenate string ‘Hello! ‘ and string in c_name from the table of
CUSTOMER.
QUERY: select CONCAT('Hello! ',
c_name) from CUSTOMER;

5) Write a query to create substring starting from 2 nd position and having size of 4
from c_name from the table of CUSTOMER.
QUERY:
select SUBSTR(c_name, 2, 4) from CUSTOMER;

6) Write a query to find out the position of character ‘a’ of c_name from the table of
CUSTOMER.
QUERY:
select INSTR(c_name, 'a') from CUSTOMER;

P a g e 18 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

7) Write a query to fill remaining places (padding) with ‘_’ of c_name having string size
of 10 from the table of CUSTOMER.

i) Using RPAD (Right Padding).


QUERY: select RPAD(c_name, 10,
'_') from CUSTOMER;

ii) Using LPAD (Left Padding).


QUERY: select LPAD(c_name, 10,
'_') from CUSTOMER;

8) Write a query to round the number 123.456 to 2 decimal place.


QUERY:
select ROUND(123.456, 2) from dual;

P a g e 19 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

9) Write a query to round the number 123.456 to -1 decimal place.


QUERY:
select ROUND(123.456, -1) from dual;

10) Write a query to truncate the number 123.456 to 2 decimal place.


QUERY:
select TRUNC(123.456, 2) from dual;

11) Write a query to truncate the number 123.456 to -1 decimal place.


QUERY:
select TRUNC(123.456, -1) from dual;

12) Write a query to display the current date of the dataase.


QUERY:
select sysdate from dual;

P a g e 20 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

13) Write a query to display the current time of the database.


QUERY:
select systimestamp from dual;

14) Write a query to find out the customer’s account number and since how many days
that customer has opened their account from the table of DEPOSIT.
QUERY:
select act_no, (sysdate - a_date) from DEPOSIT;

15) Write a query to find out the customer’s account number and since how many
months that customer has opened their account from the table of DEPOSIT.
QUERY:
select act_no, MONTHS_BETWEEN(sysdate, a_date) from DEPOSIT;

16) Write a query to find out the resultant month by adding 2 months to the current
month from the system.
QUERY:
select ADD_MONTHS(sysdate, 2) from dual;

P a g e 21 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

17) Write a query to find out the last day of month from the system.
QUERY:
select LAST_DAY(sysdate) from dual;

18) Write a query to find out the date of next Sunday respective to the current date
from system. QUERY:
select NEXT_DAY(sysdate, 1) from dual;

OR select NEXT_DAY(sysdate, 'SUNDAY') from dual;

19) Write a query to display the act_no, c_name, b_name from DEPOSIT and amount
increase by 15% and label the column name as ’new_amount’.
QUERY:
select act_no, c_name, b_name, (amount*1.15) as new_amount from DEPOSIT;

P a g e 22 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

P a g e 23 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

PRACTICAL-5
Multiple Row Functions (Aggregate Function)

1) Write a query to display minimum amount from DEPOSIT.


QUERY: select min(amount) from DEPOSIT;

2) Write a query to display minimum amount from DEPOSIT


and label it as ‘min_amount’. QUERY:

select min(amount) as min_amount from DEPOSIT;

3) Write a query to display maximum amount from DEPOSIT.


QUERY:

select max(amount) from DEPOSIT;

4) Write a query to display the total number of accounts from


the table DEPOSIT. QUERY:

select count(act_no) from DEPOSIT;

P a g e 24 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

5) Write a query to display the average amount from


DEPOSIT. QUERY:

select AVG(amount) from DEPOSIT;

6) Write a query to display the sum of amount from DEPOSIT.


QUERY:

select SUM(amount) from DEPOSIT;

7) Write a query to list out the number of customers in each


city from the table of CUSTOMER. QUERY:

select city, count(c_name) from CUSTOMER GROUP BY city;

8) Write a query to list out the number of customers in each


city sorted high to low from the table of CUSTOMER.

P a g e 25 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

QUERY: select city, count(c_name) from CUSTOMER GROUP BY


city ORDER BY count(c_name) DESC;

9) Write a query to list out the number of customers living in


same cities from the table of CUSTOMER. QUERY:

select city, count(c_name) from CUSTOMER GROUP BY city HAVING


count(c_name) >= 2;

10) Write a query to display the name of distinct cities from


the table of CUSTOMER. QUERY:

select DISTINCT city from CUSTOMER;

11) Write a query to display the number of distinct cities from


the table of CUSTOMER. QUERY:

select count(DISTINCT city) from CUSTOMER;

P a g e 26 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

PRACTICAL-6

Displaying Data from Multiple Tables (Join)

1) Write a query to display the details of required customer.


QUERY:
select d.act_no, d.c_name, d.amount, d.a_date, c.city, b.b_name, b.city from DEPOSIT d,
BRANCH b, CUSTOMER c WHERE d.c_name = c.c_name AND
d.b_name = b.b_name AND d.c_name = '';

2) Write a query to find out the customers’ name who are depositor as well as borrower and
living in city ‘Vadodara’.
QUERY:
select c.c_name from CUSTOMER c, DEPOSIT d, BORROW b WHERE c.c_name = d.c_name
AND c.c_name = b.c_name AND c.city = 'Anand';

3) Write a query to find out that city in which customer is living is same as city of branch.
QUERY:
select c.city from CUSTOMER c, BRANCH b WHERE c.city = b.city;

4) Write a query to display the employee name, department number, department name for all
employees.
QUERY:
select e.e_name, e.d_no, d.d_name from EMPLOYEE e, DEPARTMENT d WHERE e.d_no =
d.d_no;

P a g e 27 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

5) Write a query to create unique listing of all jobs that are in department number ‘102’.
Include the location of department in output.
QUERY:
select j.j_id, j.j_title, e.d_no, d.d_location from JOBS j, EMPLOYEE e, DEPARTMENT d
WHERE
j.j_id = e.j_id AND e.d_no = d.d_no AND e.d_no = 102;

6) Write a query to display the employee name, department number, department name for all
employees who work in ‘New York’.
QUERY:
select e.e_name, e.d_no, d.d_name from EMPLOYEE e, DEPARTMENT d WHERE e.d_no =
d.d_no AND d_location = 'New York';

P a g e 28 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

PRACTICAL-7

Using Commit and Rollback show Transaction ACID Property

A transaction is a unit of work that is performed against a database. Transactions are units
sequences of work accomplished in a logical order, whether in a manual fashion by a user or
automatically by some sort of a database program.

A transaction is the propagation of one or more changes to the database. For example, if you are
creating a record or updating a record or deleting a record from the table, then you are performing

A transaction on that table. It is important to control these transactions to ensure the data integrity
and to handle database errors.

Practically, you will club many SQL queries into a group and you will execute all of them
together as a part of a transaction.

➢ Properties of Transactions
Transactions have the following four standard properties, usually referred to by the acronym
ACID.

1) ATOMICITY − ensures that all opera􀆟ons within the work unit are completed
successfully. Otherwise, the transaction is aborted at the point of failure and all the
previous operations are rolled back to their former state.

2) CONSISTENCY − ensures that the database properly changes states upon a successfully
committed transaction.

3) ISOLATION − enables transac􀆟ons to operate independently of and transparent to


each other.

4) DURABILITY − ensures that the result or effect of a committed transaction persists in


case of a system failure.

➢ Transaction Control
The following commands are used to control transactions.

1)COMMIT − to save the changes.

2) ROLLBACK − to roll back the changes.

3) SAVEPOINT − creates points within the groups of transactions in which to


ROLLBACK.

4) TRANSACTION − places a name on a transaction.

P a g e 29 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

➢ Transactional Control Commands


Transactional control commands are only used with the DML Commands such as - INSERT,
UPDATE and DELETE only. They cannot be used while creating tables or dropping them
because these operations are automatically committed in the database.

➢ 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

Consider the CUSTOMERS table having the following records:

ID NAME AGE ADDRESS SALARY


1 Uday 19 Anand 1000000.00
2 Yash 18 Ahmedabad 500000.00
3 Vinit 20 Anand 400000.00

4 Vipul 20 Vadodara 300000.00

Following is an example which would delete those records from the table which have age = 18
and then COMMIT the changes in the database.

SQL> DELETE FROM CUSTOMERS WHERE AGE = 18;


SQL> COMMIT;

Thus, two rows from the table would be deleted and the SELECT statement would produce as
below:
ID NAME AGE ADDRESS SALARY
1 Uday 19 Anand 1000000.00
3 Vinit 20 Anand 400000.00

4 Vipul 20 Vadodara 300000.00

P a g e 30 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

➢ The ROLLBACK Command

The ROLLBACK 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
Consider the CUSTOMERS table having the following records:
ID NAME AGE ADDRESS SALARY
1 Uday 19 Anand 1000000.00
2 Yash 18 Ahmedabad 500000.00
3 Vinit 20 Anand 400000.00

4 Vipul 20 Vadodara 300000.00

Following is an example, which would delete those records from the table which have the
age = 18 and then ROLLBACK the changes in the database
.
SQL> DELETE FROM CUSTOMERS WHERE AGE = 18;
SQL> ROLLBACK;

Thus, the delete operation would not impact the table and the SELECT statement would produce
the following result.

ID NAME AGE ADDRESS SALARY

1 Uday 19 Anand 1000000.00

2 Yash 18 Ahmedabad 500000.00

3 Vinit 20 Anand 400000.00

4 Vipul 20 Vadodara 300000.00

➢ 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;

P a g e 31 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

Following is an example where you plan to delete the three different records from the
CUSTOMERS table. You want to create a SAVEPOINT before each delete, so that you can
ROLLBACK to any SAVEPOINT at any time to return the appropriate data to its original state.

❖ Example
Consider the CUSTOMERS table having the following records.

The following code block contains the series of operations.

ID NAME AGE ADDRESS SALARY


1 Uday 19 Anand 1000000.00
2 Yash 18 Ahmedabad 500000.00
3 Vinit 20 Anand 400000.00

4 Vipul 20 Vadodara 300000.00

SQL> SAVEPOINT SP1;


Savepoint created.

SQL> DELETE FROM CUSTOMERS WHERE ID=1;


1 row deleted.

SQL> SAVEPOINT SP2;


Savepoint created.

SQL> DELETE FROM CUSTOMERS WHERE ID=2;


1 row deleted.

SQL> SAVEPOINT SP3;


Savepoint created.

SQL> DELETE FROM CUSTOMERS WHERE ID=3;


1 row deleted.

Now that the three deletions have taken place, let us assume that you have changed your mind
and decided to ROLLBACK to the SAVEPOINT that you identified as SP2. Because SP2
was created
after the first deletion, the last two deletions are undone:

SQL> ROLLBACK TO SP2;


Rollback complete.

Notice that only the first deletion took place since you rolled back to SP2.

P a g e 32 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

SQL> SELECT * FROM CUSTOMERS

ID NAME AGE ADDRESS SALARY


2 Yash 18 Ahmedabad 500000.00
3 Vinit 20 Anand 400000.00

4 Vipul 20 Vadodara 300000.00

➢ The RELEASE SAVEPOINT Command


The RELEASE SAVEPOINT command is used to remove a SAVEPOINT that you have created.
The syntax for a RELEASE SAVEPOINT command is as follows:

RELEASE SAVEPOINT SAVEPOINT_NAME;

Once a SAVEPOINT has been released, you can no longer use the ROLLBACK command to
undo
transactions performed since the last SAVEPOINT.

➢ The SET TRANSACTION Command


The SET TRANSACTION command can be used to initiate a database transaction. This
command is used to specify characteristics for the transaction that follows. For example, you can
specify a transaction to be read only or read write.

SQL language is divided into four types of primary language statements: DML, DDL,
DCand 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.

➢ TCL (Transaction Control Language)


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

1) BEGIN Transaction – opens a transaction.

2) COMMIT Transaction – commits a transaction.

3) ROLLBACK Transaction – ROLLBACK a transaction in case of any error.

P a g e 33 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

➢ Difference between ROLLBACK and COMMIT commands:

ROLLBACK COMMIT

ROLLBACK command is used to The COMMIT command is used to


undo save
the changes made by the DML the modification done to the
commands. database
values by the DML commands.
It rollbacks all the changes of the It will make all the changes
current transaction. permanent
that cannot be rolled back.
Syntax: Syntax:
DELETE FROM table_name COMMIT;
ROLLBACK;

P a g e 34 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

PRACTICAL-8
Securing data using Views and Controlling User Access (DCL)

➢ Introduction to Privileges
A privilege is a right to execute a particular type of SQL statement or to access another user's
object.Some examples of privileges include the right to:

• Connect to the database (create a session).

• Create a table.

• Select rows from another user's table.

• Execute another user's stored procedure.

➢ DCL (Data Control Language)


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

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

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

You grant privileges to users so these users can accomplish tasks required for their job. You
should grant a privilege only to a user who absolutely requires the privilege to accomplish
necessary work.
Excessive granting of unnecessary privileges can compromise security. A user can receive a
privilege in two different ways:

• You can grant privileges to users explicitly. For example, you can explicitly grant the
privilege to insert records into the employees table to the user SCOTT.

• You can also grant privileges to a role (a named group of privileges), and then grant the role
to one or more users. For example, you can grant the privileges to select, insert, update, and
delete records from the employees table to the role named clerk, which in turn you can grant
to the users scott and brian.

Because roles allow for easier and better management of privileges, you should normally grant
privileges to roles and not to specific users.

There are two types of different privileges:


1. System privileges
2. Schema object privileges

➢ System privileges
A system privilege is the right to perform a particular action, or to perform an action

P a g e 35 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

on any schema objects of a particular type. For example, the privileges to create
table spaces and to delete the rows of any table in a database are system privileges.
There are over 60 distinct system privileges.

➢ Grant and Revoke System Privileges


You can grant or revoke system privileges to users and roles. If you grant system
privileges to roles,then you can use the roles to manage system privileges. For
example, roles permit privileges to be made selectively available.Use either of the
following to grant or revoke system privileges to users and roles:

Oracle Enterprise Manager Console


The SQL statements GRANT and REVOKE

❖ Who Can Grant or Revoke System Privileges?

Only users who have been granted a specific system privilege with the ADMIN OPTION or
users with the system privileges GRANT ANY PRIVILEGE or GRANT ANY OBJECT
PRIVILEGE can grant or revoke system privileges to other users.

➢ Schema Object Privileges


A schema object privilege is a privilege or right to perform a particular action on a specific
schema
object:
• Table
• View
• Sequence
• Procedure
• Function
• Package

Different object privileges are available for different types of schema objects. For example, the
privilege to delete rows from the departments table is an object privilege.

Some schema objects, such as clusters, indexes, triggers, and database links, do not have
associated object privileges. Their use is controlled with system privileges.
For example, to alter a cluster, a user must own the cluster or have the ALTER ANY CLUSTER
system privilege. A schema object and its synonym are equivalent with respect to privileges.That
is, the object privileges granted for a table,view, sequence, procedure, function, or package apply
whether referencing the base object by name or using a synonym.

For example, assume there is a table jward.emp with a synonym named jward.employee and the
user jward issues the following statement:
GRANT SELECT ON emp TO swilliams;

The user swilliams can query jward.emp by referencing the table by name or using the
synonym jward.employee:

SELECT * FROM jward.emp;


SELECT * FROM jward.employee;

P a g e 36 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

If you grant object privileges on a table, view, sequence, procedure, function, or package to a
synonym for the object, the effect is the same as if no synonym were used. For example, if
jward wanted to grant the SELECT privilege for the emp table to swilliams, jward could issue
either of the
following statements:

GRANT SELECT ON emp TO swilliams;

GRANT SELECT ON employee TO swilliams;

If a synonym is dropped, all grants for the underlying schema object remain in effect, even if
the privileges were granted by specifying the dropped synonym.

➢ Grant and Revoke Schema Object Privileges


Schema object privileges can be granted to and revoked from users and roles. If you grant object
privileges to roles, you can make the privileges selectively available. Object privileges for users
and roles can be granted or revoked using the following:

• The SQL statements GRANT and REVOKE, respectively


• The Add Privilege to Role/User dialog box and the Revoke Privilege from
Role/User dialog box of Oracle Enterprise Manager.

❖ Who Can Grant Schema Object Privileges?

A user automatically has all object privileges for schema objects contained in his or her schema.
A user can grant any object privilege on any schema object he or she owns to any other user or
role. A user with the GRANT ANY OBJECT PRIVILEGE can grant or revoke any specified
object privilege to another user with or without the GRANT OPTION of the GRANT statement.

Otherwise, the grantee can use the privilege, but cannot grant it to other users.

For example, assume user SCOTT has a table named t2:

SQL>GRANT grant any object privilege TO U1;


SQL> connect u1/u1
Connected.
SQL> GRANT select on scott.t2 \TO U2;
SQL> SELECT GRANTEE, OWNER, GRANTOR, PRIVILEGE, GRANTABLE FROM
DBA_TAB_PRIVS
WHERE TABLE_NAME = 'employees';

GRANTEE OWNER GRANTOR PRIVILEGE GRANTABLE


U2 SCOTT SCOTT SELECT NO

➢ Privileges Required to Create Views


To create a view, you must meet the following requirements:

You must have been granted one of the following system privileges, either explicitly or through a
role:

• The CREATE VIEW system privilege (to create a view in your schema).

P a g e 37 | 38
Udaypratapsingh Rajput Database Mangement 3CE3

• The CREATE ANY VIEW system privilege (to create a view in another user's schema).

You must have been explicitly granted one of the following privileges:

• The SELECT, INSERT, UPDATE, or DELETE object privileges on all base objects underlying
the view.

• The SELECT ANY TABLE, INSERT ANY TABLE, UPDATE ANY TABLE, or DELETE
ANY TABLE system privileges.

Additionally, in order to grant other user’s access to your view, you must have received object
privileges to the base objects with the GRANT OPTION clause or appropriate system privileges
with the ADMIN OPTION clause. If you have not, grantees cannot access your view.

➢ Increase Table Security with Views


To use a view, you require appropriate privileges only for the view itself. You do not require
privileges on base objects underlying the view.
Views add two more levels of security for tables, column-level security and value-based security:

• A view can provide access to selected columns of base tables. For example, you can define a
view on the employees table to show only the employee_id, last_name, and manager_id columns:

CREATE VIEW employees_manager AS SELECT last_name, employee_id, manager_id


FROM employees;

• A view can provide value-based security for the information in a table. A WHERE clause in the
definition of a view displays only selected rows of base tables. Consider the following two
examples:

CREATE VIEW lowsal AS SELECT * FROM employees WHERE salary < 10000;

• The LOWSAL view allows access to all rows of the employees table that have a salary value
less than 10000. Notice that all columns of the employees table are accessible in the LOWSAL
view.

CREATE VIEW own_salary AS SELECT last_name, salary FROM employees WHERE


last_name = USER;

In the own_salary view, only the rows with an last_name that matches the current user of the view
are accessible. The own_salary view uses the user pseudocolumn, whose values always refer to
the current user. This view combines both column-level security and valuebased security.

P a g e 38 | 38

You might also like