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

SQL 7

This document discusses database security concepts in SQL including privileges, roles, and granting access. It covers granting and revoking privileges for both system level privileges and object level privileges. It also discusses how roles can be used to group privileges and grant them to users.

Uploaded by

Ravi Payagond
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

SQL 7

This document discusses database security concepts in SQL including privileges, roles, and granting access. It covers granting and revoking privileges for both system level privileges and object level privileges. It also discusses how roles can be used to group privileges and grant them to users.

Uploaded by

Ravi Payagond
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

7.1.

Introduction
Objective

 ANSI SQL

 DUAL Table

 Pseudo-column

Introduction

Welcome to Advanced SQL course. You have already learnt Basics of SQL and here you will learn
more about SQL. In this module you will learn more DML queries, DCL (Data control Language) and
Sub queries. You have already learned single row functions and aggregate function. In this module
you will cover additional SQL functions like date functions, data type conversion functions and
analytic functions. Analytical functions are are very useful in BI applications and analytics. In
addition, this course also covers conditional statements, set operators and database objects. Happy
Learning.

ANSI SQL

ANSI SQL or SQL is the American National Standards Institute standardized Structured Query
Language. But there are many different versions of the SQL language. ANSI SQL is the base for
several different SQL languages such as T-SQL and PL/SQL. ANSI SQL is used to Create, Alter, and
View data stored within a database. 

SQL is the standard language for Relation Database Systems. All relational database management
systems like MySQL, MS Access, Oracle, Sybase, Informix, postgres and SQL Server use SQL as
standard database language.

Also, they are using different dialects, such as:

 T-SQL (MS SQL Server)

 PL/SQL (Oracle)

 JET SQL (MS Access)

Dual Table

 DUAL is a table automatically created by Oracle Database along with the data dictionary.  

 DUAL is in the schema of the user SYS but is accessible by the name DUAL to all users. 

 It has one column, DUMMY, defined to be VARCHAR2(1), and contains one row with a value
X. 

Selecting from the DUAL table is useful for computing a constant expression with the SELECT
statement. Because DUAL has only one row, the constant is returned only once. Alternatively, you
can select a constant, pseudo-column, or expression from any table, but the value will be returned as
many times as there are rows in the table. 

Example:-
    select 2+3 from Dual;        -    will display 5

    select 'Hello World' from Dual    -    will display 'Hello World'

Pseudo-column

A pseudo-column behaves like a table column, but it is not actually stored in the table. You can
select from pseudo-columns, but you cannot insert, update, or delete their values.  

ROWNUM pseudo-column

For each row returned by a query, the ROWNUM pseudo-column returns a number indicating the
order in which Oracle selects the row from a table or set of joined rows. The first row selected has a
ROWNUM of 1, the second has 2, and so on.

You can use ROWNUM to limit the number of rows returned by a query, as in this example:

    SELECT * FROM employees WHERE ROWNUM < 10;

ROWID Pseudo-column

For each row in the database, the ROWID pseudo-column returns the address of the row. Oracle
Database rowid values contain information necessary to locate a row or record of data.

7.2. Advanced DML and DCL


Objectives

 Copy and insert

 Insert All Query

 DCL Commands

 Privileges

 Roles

Insert Query – Advanced Options

Copy and Insert

Syntax:-

INSERT INTO table_name [(column list)] (sub query);

Example:-

INSERT INTO Student_Total_Marks (SELECT m.student_id, sum(m.marks) AS Total_Marks FROM


tc_marks m GROUP BY m.student_id);
Here the output of the sub query (the select query inside the brackets) will be inserted into the table

Multi–Table INSERT

This feature of Oracle allows user to define multiple insert targets.

Syntax:-

INSERT ALL|FIRST

[WHEN condition THEN] INTO target [VALUES]

[WHEN condition THEN] INTO target [VALUES]

...

[ELSE] INTO target [VALUES]

SELECT ...

FROM source_query;

Example :

INSERT ALL INTO Employee_Details (salary, employee_id, name, age) VALUES (15000, 2001, 'Anurag
Sidd', 25) INTO employee_details (salary, employee_id, name, age) VALUES (17000, 2002, 'Priyanka
Madhur', 23)

SELECT 1 FROM DUAL;


Records insertion in multiple tables

Example :

INSERT ALL INTO Employee_Details (salary, employee_id, name, age) VALUES (16000, 2003,


'MadhavMhajan', 24) INTO Project_Details(project_id, project_name, client_name)
VALUES (11, 'Project 1', 'Client 1') INTO Project_Allocation (project_id, employee_id) VALUES (11,
2003) SELECT 1 FROM DUAL;
Copying Records to multiple tables

Example :
INSERT ALL INTO Employee_Details_23 INTO Employee_Details_24 INTO
Employee_Details_25 SELECT * FROM Employee_Details;
Here all the data from the table Employee_Details is copied to  the three  tables
Employee_details_23, Employee_details_24 and  Employee_details_25

Conditional Multiple Inserts

Example :
INSERT FIRST WHEN age = 23 THEN INTO Employee_Details_23 (salary, employee_id, name,
age) VALUES (salary, employee_id, name, age) WHEN age = 24 THEN INTO Employee_Details_24
(salary, employee_id, name, age) VALUES (salary, employee_id, name, age) WHEN age = 25
THEN INTO Employee_Details_25 (salary, employee_id, name, age) VALUES (salary, employee_id,
name, age) SELECT * FROM Employee_Details;

Here all the data from the table Employee_Details is copied to  the three tables 

Employee_details_23, Employee_details_24 and  Employee_details_25 according to the condition

Note:- The columns in purple colour are from the source table Employee_Details

Data Control Language

 The Data Control Language (DCL) is a subset of the Structured Query Lanaguge (SQL) that is
used to control data. 

 It is used by the database administrators or owners of database objects to control the user
access to various database objects.

 They are used to enforce database security in a multiple user database environment.

DCL Commands

 Grant – To give privileges

 Revoke – To remove the previously given privileges

Privileges
A database user cannot do anything with the database unless he has the basic set of privileges. A
user should have certain System Privileges so that they can

      Connect to the database (CREATE SESSION)

      Create objects (CREATE TABLE, CREATE INDEX) 

      Modify or delete objects

Once the users start creating objects, only the user who has created that object can access it. Other
users will not be able to do anything with those objects. So the object owner can give other users
access or Object Privileges to 

      Modify the objects (ALTER, DELETE, UPDATE, INSERT) 

      Query tables views etc (SELECT)

      Execute procedures functions etc(EXECUTE)

Object Privileges

Database Users need privileges to access, modify, execute or delete any database objects (Tables,
views etc). 

Following are some of the available privileges on each database objects.

 ALL

 ALTER 

 DELETE 

 INSERT

 SELECT

 UPDATE

 EXECUTE

Owner of the data base objects can issue privileges to the other users or can remove the privileges
using GRANT and REVOKE statements in Oracle.

System Privileges

System privileges are given to users to perform a particular action, or to perform an action on any
schema objects of a particular type. They allow users to perform certain functions that deal with
managing the database and the server

Following are some of the system privileges.

 CREATE USER         

 CREATE SESSION  

 CREATE ANY TABLE

 DROP ANY TABLE


 ALTER DATABASE

 ALTER ANY TABLE

Grant

Granting Object Privileges

Syntax:-

Granting System Privileges

Syntax:-

Grant - Example

System Privileges

GRANT CREATE ANY TABLE, ALTER ANY TABLE, DROP ANY TABLE TO user_name1;
GRANT CREATE ANY INDEX TO user_name1;
GRANT CREATE ANY INDEX TO user_name2 WITH ADMIN OPTION;
GRANT CREATE SESSION TO user_name2

Object Privileges

GRANT SELECT, INSERT, UPDATE, DELETE ON product_table TO user2


GRANT ALL ON suppliers_table TO user2;        

note: - ALL implies ALL privileges

Revoke

Revoking Object Privileges


Syntax:-

Revoking System Privileges


Syntax:-

Revoke - Examples

System Privileges

REVOKE CREATE ANY TABLE, ALTER ANY TABLE, DROP ANY TABLE FROM user_name1;
REVOKE CREATE ANY INDEX FROM user_name1;
REVOKE ALL FROM user_name2;

note: - ALL implies ALL privileges

REVOKE CREATE SESSION FROM user_name2

Object Privileges

REVOKE SELECT, INSERT, UPDATE, DELETE ON product_table FROM user2


REVOKE ALL ON suppliers_table FROM user2;        

note: - ALL implies ALL privileges


Roles

A role is a set or group of privileges that can be granted to users or another role. Creating a role will
help in reducing efforts to give grants to each user.

role_nameis the name of the new role that you are creating. This is how you will refer to the grouping
of privileges.

NOT IDENTIFIED ==> Specifies that the role is immediately enabled. No password is required to
enable the role.

IDENTIFIED ==> Specifies that a user must be authorized by a specified method before the role is
enabled. Authorization will be done using password that we are providing.

EXTERNALLY and GLOBALLY ==> Used along with identified clause to specify whether role can
applicable to external specified users or all users.

Note: - If both NOT IDENTIFIED and IDENTIFIED are omitted in the CREATE ROLE statement, the role
will be created as a NOT IDENTIFIED role.

Grant or Revoke Privileges to Roles

Once a role is created in Oracle, the next step is to grant privileges to that role.

Syntax:-

The privileges that are given to a role can also be revoked.

Syntax:-

Grant Role to User

After creating the role and assigning the privileges to the role, the final step is to grant the role to
specific users. 

Syntax:

Roles - Examples

CREATE ROLE test_role1;


CREATE ROLE test_role2 IDENTIFIED BY test123;;
GRANT select, insert, update, delete ON products_table TO test_role1;
GRANT ALL ON products_table TO test_role2;
GRANT test_role1 TO user_name2;
REVOKE delete ON products_table FROM test_role1;
REVOKE all ON products_table FROM test_role2;

7.3. Practise Problems - Advanced DML


1. Create the following tables and insert the records using a single insert query
2. Create the following table and insert the records using a single insert query.

3. Create the following table and insert the records from previously created table in exercise 1 and
exercise 2.
4. Create the following tables and insert the records from previously created table in exercise 1 and
exercise 2.

 Project_Allocation_1001 should only have the employee id and name of the employees
alocated to project 1001

 Project_Allocation_1002 should only have the employee id and name of the employees
alocated to project 1002

7.4. Practise Problems - Advanced DCL


1. Create the following table in your work schema.
2. Create a user ‘info_trainee’ and give Select privilege to the user 'info_trainee' to the above table
'Animal_Scientific_Names'. Select the table data from user 'info_trainee'.

3. Provide Update, insert and delete privileges to the user 'info_trainee' to the above table
'Animal_Scientific_Names'.  Insert the below records to the table from info_trainee

4. Update the record

5. Remove the 'Delete' privilege from info_trainee for this table.

You might also like