Académique Documents
Professionnel Documents
Culture Documents
Once connected as SYSTEM, to generate a new account, the command used is: CREATE USER
Syntax:
CREATE USER <user name> IDENTIFIED BY <any password>;
Oracle gives various privileges to the users. Mainly two types of privileges are:
Database Administrator can simply retrieve information about user privileges related to the
system, tables and roles through DBA_SYS_PRIVS, DBA_TAB_PRIVS, and DBA_ROLE_PRIVS
respectively. SELECT * FROM DBA_TAB_PRIVS;
REVOKE
REVOKE statement is used to remove privileges from a specific user or role, or from all users, to
perform actions on database objects.
Revokes privileges already granted to other users.
For example to revoke select, update, insert privileges granted to user1, give the following
statement:
So everytime if one wants to see emp details and department names where they are working we
have to give a long join query. Instead of giving this join query again and again, we can create a
view on these table by using the command: CREATE VIEW
Syntax:
create view emp_det as select e.empno, e.ename,e.sal,e.deptno,d.dname,d.loc
from emp e, dept d where e.deptno=d.deptno;
Now to see the employee details and department names instead of giving a join query, one can
just type:
select * from emp_det;
Replacing/Altering Views
• To alter the definition of a view, one must replace the view using one of the following
methods:
• A view can be dropped and then re-created, all grants of corresponding view privileges are
revoked from roles and users.
• A view can be replaced by redefining it with a CREATE VIEW statement that contains the OR
REPLACE option.
• This option replaces the current definition of a view, but preserves the present security
authorizations.
For example, if ACCOUNTS_STAFF view has been created and you realize that you must redefine
the ACCOUNTS_STAFF view to correct the department number specified in the WHERE clause of
the defining query, because it should have been 30, you can replace the current version of the
ACCOUNTS_STAFF view with the following statement:
CREATE OR REPLACE VIEW Accounts_staff AS
SELECT Empno, Ename, Deptno FROM Emp
WHERE Deptno = 30;
To delete a view, use the DROP VIEW command.
Syntax: DROP VIEW view_name
create table Employee
(
ID VARCHAR(4 BYTE) NOT NULL,
First_Name VARCHAR(10 BYTE),
Last_Name VARCHAR(10 BYTE),
Start_Date DATE,
End_Date DATE,
Salary Number(8,2),
City VARCHAR(10 BYTE),
Description VARCHAR(15 BYTE)
)
Example:
CREATE INDEX emp_idx ON employee(last_name);
To view the indexes:
SELECT index_name, table_name, column_name
FROM USER_IND_COLUMNS WHERE table_name = 'EMP';
To drop the index:
drop index emp_idx;
Copying a Table
The contents of a table can be copied from one table to the other using the following command:
Syntax:
INSERT INTO <new table name> SELECT * from <old table name>;
Note: The new table should be created already in order to copy the contents from some other
table.
PL/SQL
• PL stands for Procedural Language. PL/SQL is referred to as a block structured language.
• All PL/SQL programs are made up of blocks, which can be nested within each other.
• Each block performs a logical action in the program.
• The subprograms can be either – Functions, procedures and can be grouped into packages.
• PL/SQL also allows access to databases via – Cursors, Triggers, stored procedures and
functions.
• A PL/SQL block is a syntactical unit that might contain program code, variable declarations,
error handlers, procedures, functions, and even other PL/SQL blocks.
Anonymous Block:
Block Structure
DECLARE
<Define PL/SQL objects to be used within this block>
BEGIN
<Executable Statements>
EXCEPTION
< Used to trap Predefined error conditions>
END;
Variables and Types
• Information is transmitted between a PL/SQL program and the database through variables.
• Every variable has a specific type associated with it that can be one of the types used by SQL
for database columns.
• PL/SQL also allows BOOLEAN variables.
• The most commonly used generic type is NUMBER.
• Variables of type NUMBER can hold either an integer or a real number.
• The most commonly used character string type is VARCHAR(n), where n is the maximum
length of the string in bytes.
• Any variable declarations must immediately follow DECLARE and come before BEGIN.
For example:
DECLARE
price NUMBER;
myobject VARCHAR(20);
Variables and Types
• Variable types in PL/SQL can be tricky as a variable is used to manipulate data stored in an
existing relation.
• It is essential that the variable has the same type as the relation column.
• If there is any type mismatch, variable assignments and comparisons may not work the way it
is expected. So, to be safe, instead of hard coding the type of a variable, one should use the
%TYPE operator.
For example: DECLARE
myobject Order.name%TYPE;
gives variable myobject whatever type was declared for the name column in relation Order.
A variable may also have a type that is a record with several fields by using %ROWTYPE.
The result is a record type in which the fields have the same names and types as the attributes of
the relation.
For instance: DECLARE
objectuple Order%ROWTYPE;
makes variable objectuple be a record with fields name and manufacturer assuming that the
relation has the schema Order(name, manufacturer).
PL/SQL
• The initial value of any variable, regardless of its type, is NULL.
• Values to the variables can be assigned using the ":=" operator.
• The assignment can occur either immediately after the type of the variable is declared or
anywhere in the executable portion of the program.
Example:
DECLARE
a NUMBER := 3;
BEGIN
a := a + 1;
END;
Anonymous Blocks
DECLARE
x NUMBER;
BEGIN
x := 72600;
dbms_output.put_line('The variable X = ');
dbms_output.put_line(x);
END;
/ Declare
NUM1 number:=20;
NUM2 number:=50;
SUM number:=0;
Begin
SUM := NUM1+NUM2;
dbms_output.put_line (SUM||',');
end;
/
Anonymous Block
CREATE TABLE
T1( e number,
f number );
INSERT INTO T1 VALUES(1, 3);
INSERT INTO T1 VALUES(2, 4);
Select * from T1;
Syntax:
IF <condition> THEN <statement_list> ELSE <statement_list> END IF;
ELSE part is optional. Nested If’s will have the following syntax:
DECLARE
x NUMBER := 0; DECLARE
BEGIN i NUMBER := 1;
LOOP BEGIN
DBMS_OUTPUT.PUT_LINE ('Inside loop: x = ' || x); LOOP
x := x + 1; INSERT INTO T1 VALUES(i,i);
IF x > 3 THEN i := i+1;
EXIT; EXIT WHEN i>100;
END IF; END LOOP;
END LOOP; END;
-- After EXIT, control resumes here
DBMS_OUTPUT.PUT_LINE(' After loop: x = ' ||x);
END;
/
Loop - Continue
DECLARE
x NUMBER := 0;
BEGIN
LOOP -- After CONTINUE statement, control resumes here
DBMS_OUTPUT.PUT_LINE ('Inside loop: x = ' ||x);
x := x + 1;
IF x < 3 THEN
CONTINUE;
END IF;
DBMS_OUTPUT.PUT_LINE
('Inside loop, after CONTINUE: x = ' || x);
EXIT WHEN x = 5;
END LOOP;
DECLARE
done BOOLEAN := FALSE;
BEGIN
WHILE done LOOP
DBMS_OUTPUT.PUT_LINE ('This line does not print.');
done := TRUE;
END LOOP;
<<print_now>>
DBMS_OUTPUT.PUT_LINE(TO_CHAR(n) || p);
END;
/
Named Block
PL / SQL has two types of subprograms
• Procedures
• Functions
Named Block Structure for PL / SQL Subprogram:
(Named Block)
HEADER
IS <Declaration Section>
BEGIN
<Executable Statements>
EXCEPTION
< Used to trap Predefined error conditions>
END;
Procedures
• PL/SQL procedure is a named block that does a specific task.
• It allows you to encapsulate complex business logic and reuse it in both database layer and
application layer.
Syntax:
CREATE [OR REPLACE] PROCEDURE proc_name [list of parameters]
IS
Declaration section
BEGIN
Execution section
EXCEPTION
Exception section
END;
Procedures