SQL 7
SQL 7
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.
PL/SQL (Oracle)
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:
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.
DCL Commands
Privileges
Roles
Syntax:-
Example:-
Multi–Table INSERT
Syntax:-
INSERT ALL|FIRST
...
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)
Example :
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
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
Note:- The columns in purple colour are from the source table Employee_Details
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
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
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
Object Privileges
Database Users need privileges to access, modify, execute or delete any database objects (Tables,
views etc).
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
CREATE USER
CREATE SESSION
Grant
Syntax:-
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
Revoke
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;
Object Privileges
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.
Once a role is created in Oracle, the next step is to grant privileges to that role.
Syntax:-
Syntax:-
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
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
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