Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
PRODUCTION
ENVIRONMENT
PRE-PRODUCTION
ENVIRONMENT
ORABE
WEBLOGIC WEBLOGIC_DBA
OraGP
OraMON PORT01
OraBE
CARTE
Ora121 CARTE_DBA
DATABASES
Database users
OWNER USERS They contain all database objects and they must not have
privileges (e.g. weblogic_dba and carte_dba). Generally they
are locked.
APPLICATIVE USERS They have only the grant to a role through which they have
the system privilege of CREATE SESSION and they can
manipulate the objects of related owner schema.
DEVELOP USERS They have all the necessary privileges through CONNECT
and RESOURCE roles. They also be able to manipulate test
objects.
Passwords of applicative and develop users expire every 45 days and after
expiry date there are 5 days to change it (through an 8i or higher client). The
sixth day users will be lockes automatically. This is the tipical profile.
Database objects
View select no
EMPLOYEE DEPARTMENT
CODE LAST_N FIRST_ ... DEPT_ID DEPT_ID DEPT_N ... ... ...
PK EMPLOYEE FK PK DEPARTMENT
FOREIGN KEY FK is a relation between two tables. A foreign key can be null,
but it must reference a unique or primary key. If referenced
key is composite, foreign key must have the same structure.
Integrity Constraints
NOT NULL By default every column can be NULL, otherwise a not null
constraint can be defined on a column.
CHAR (size) Used to store fixed length character data of length size. Max size
size is 2000 bytes.
INSERT Data Manipulation Enters new rows, changes existing rows, and removes
DELETE Language (DML) unwanted rows from tables in the database, respectively. Use
the MERGE statement to select rows from one table for update
UPDATE or insertion into another table.
MERGE
CREATE Data Definition Sets up, changes, and removes data structures from tables.
ALTER Language (DDL)
DROP
TRUNCATE
COMMIT Transaction Manages the changes made by DML statements. Changes to
ROLLBACK Control the data can be grouped together into logical transactions.
GRANT Data Control Gives or removes access rights to both the Oracle database
REVOKE Language (DCL) and the structures within it.
SQL*Plus Overview
SQL*Plus is an interactive and batch query tool that is installed with every
Oracle Server or Client installation.
It has a command-line user interface; it has its own commands and it allows
enter and execute SQL, PL/SQL and operating system commands.
You can use SQL*Plus to generate reports and to output the results to text
file, to screen, or to HTML file for browsing on the Internet.
SQL> disc
Disconnected from Oracle9i Enterprise Edition Release
9.2.0.4.0 - 64bit Production
With the Partitioning option
JServer Release 9.2.0.4.0 - Production
Tables Used in the course
For our examples we will use the two simple tables of employees and
departments of a company:
Projection Choose the columns in a table that you want the query to
return. You can choose as few or as many columns of the
table as you require.
Selection Choose the rows in a table that you want the query to return.
You can use various criteria to restrict the rows that you see.
Join Bring together data stored in different tables by creating a link
through a column that appears in both tables.
8 rows selected.
Creating a Projection
Assume that you want to display only two columns (e.g DEPARTMENT_ID and
LOCATION_ID) of data stored in the DEPARTMENTS table. This “projection” is a
typical use of the SELECT statement in SQL.
SQL> SELECT dept_id, location_id from dept; SQL> SELECT dept_id AS Dept, location_id location
2 from dept;
DEPT_ID LOCATION_ID
---------- ----------- DEPT LOCATION
10 1700 ---------- -----------
20 1800 10 1700
50 1500 20 1800
60 1400 50 1500
80 2500 60 1400
90 1700 80 2500
110 1700 90 1700
190 1700 110 1700
190 1700
8 rows selected. 8 rows selected.
DEPT_ID
----------
90
90 SQL> SELECT DISTINCT dept_id FROM emp;
90
60 DEPT_ID
60 ----------
60 10
50 20
50 50
50 60
50 80
50 90
80 110
80
80
8 rows selected.
10
20
20
110
110
20 rows selected.
Restricting Data
You use a WHERE clause to restrict the rows returned by a query. A WHERE
clause contains a condition that must be met and can compare values in
columns, literal values, arithmetic expressions, or functions.
Operators
SQL> SELECT last_name, job_id, dept_id FROM emp
2 WHERE dept_id = 90 ; = Equal to
> Greater than
LAST_NAME JOB_ID DEPT_ID
------------------------- ---------- ---------- >= Greater than or equal to
King AD_PRES 90 < Less than
Kochhar AD_VP 90 <= Less than or equal to
De Haan AD_VP 90 <> Not equal to
LAST_NAME MANAGER_ID
------------------------- ----------
Ernst 103 Character strings and dates in
the WHERE clause must be
SQL> SELECT last_name, manager_id FROM emp WHERE last_name='ernst'; enclosed in single quotation
no rows selected
marks (’ ’). All character and
SQL> SELECT last_name, manager_id FROM emp date searches are case
2 WHERE LOWER (last_name) = ‘ernst'; sensitive.
LAST_NAME MANAGER_ID
------------------------- ----------
Ernst 103
BETWEEN, IN & IS NULL Operators
SQL> SELECT last_name, salary FROM emp WHERE salary BETWEEN 9000 AND 17000;
LAST_NAME SALARY
------------------------- ----------
De Haan 17000
Hunold 9000
Zlotkey 10500
Abel 11000
Hartstein 13000
Higgins 12000
6 rows selected.
SQL> SELECT last_name, salary, job_id FROM emp WHERE job_id LIKE 'M%';
LAST_NAME
-------------------------
Matos
SQL> SELECT last_name, job_id FROM emp WHERE job_id LIKE '%K\_%' ESCAPE'\';
LAST_NAME JOB_ID
------------------------- ----------
Hartstein MK_MAN
Fay MK_REP
Logical Operators
A logical operator combines the result of two or more component conditions
to produce additional or alternative conditions or to invert the result of a single
condition. Three logical operators are available in SQL: AND, OR and NOT.
8 rows selected.
Roles of Precedence
Oracle evaluates operators with higher precedence.
For operators with equal precedence, expression is evaluated from left to right.
Comparison operators are evaluated first; then in order of decreasing priority
we have NOT, AND, OR operators.
9 rows selected.
Sorting Data
You can use the ORDER BY clause to sort the rows. You must place the ORDER
BY clause last and you can specify a column, an expression or an alias to sort by.
Null values are displayed last for ascending sequences and first for
descending sequences.
SQL> SELECT last_name, dept_id, salary FROM emp SQL> SELECT emp_id, last_name,salary*12 annsal FROM emp
2 ORDER BY dept_id, salary DESC ; 2 ORDER BY annsal;
Single-row functions
Single-row functions operate on single rows only and return one result per
row. There are different types of single-row functions.
Multi-row functions
Multiple-row functions manipulate groups of rows to give one result per
group of rows.
Single-row Functions
Character Number
Single-row
functions
Conversion Date
NULL values
Dates
Characters.
Null Values Conversion
• A null value is a value that is unassigned, unknown. It is not the same as a
zero or a space: zero is a number and a space is a character.
• If any column value in an arithmetic expression is null, the result is null.
• The NVL function provides a mechanism to deal with null values.
SQL> SELECT last_name NAME,job_id, SQL> SELECT last_name, job_id,
2 12*salary*(1+commission_pct) sal_tot FROM emp; 2 12*salary*(1+NVL(commission_pct,0)) sal_tot FROM emp;
SQL> SELECT last_name, hire_date, hire_date+30 "NEW DATE" FROM emp WHERE last_name='Grant';
SQL> SELECT last_name, (SYSDATE-hire_date)/7 "WEEKS AT WORK" FROM emp WHERE dept_id =10;
LAST_NAME HIREDATE
------------------------- --------------------
Hunold January 03RD, 1990
Ernst May 21ST, 1991
Lorentz February 07TH, 1999
SQL> select last_name, manager_id, salary, TO_CHAR(hire_date, 'YYYY-MON-DD') as HIREDATE from emp
2 where hire_date like '%90';
SQL> select last_name, TO_CHAR(hire_date, 'fmdd "of" Month YYYY hh24:mi:ss') as HIREDATE from emp;
LAST_NAME HIREDATE
------------------------- -------------------------------------------
King 17 of June 1987 0:0:0
Kochhar 21 of September 1989 0:0:0
De Haan 13 of January 1993 0:0:0
Hunold 3 of January 1990 0:0:0
Ernst 21 of May 1991 0:0:0
Lorentz 7 of February 1999 0:0:0
....
20 rows selected.
Working with Characters
A string can be converted to either a number or a date using TO_NUMBER
or TO_DATE functions.
SALARY NEW_SALARY
------- ----------
2600 3600
LAST_NAME HIRE_DATE
------------------------- ---------
Grant 24-MAY-99
Multi-rows Functions
Unlike single-row functions, group functions operate on sets of rows to give one
result per group. Grouping functions are:
AVG
COUNT
MAX/MIN
SUM
8 rows selected.
Joining Tables
When you require data from more than one table in the database, you use a
join condition. Rows in one table can be joined to rows in another table
according to common values existing in corresponding columns. Typically, when
rows are joined using a common value, the column in the first table is a primary
key and the column in the second table is a foreign key.
19 rows selected.
LAST_NAME DEPT_NAME
------------------------- ------------------------------
King Administration
Kochhar Administration
De Haan Administration
Hunold Administration
Ernst Administration
Lorentz Administration
Mourgos Administration
Rajs Administration
Davies Administration
Matos Administration
Vargas Administration
Zlotkey Administration
Abel Administration
Taylor Administration
Grant Administration
Whalen Administration
Hartstein Administration
Fay Administration
Higgins Administration
....
160 rows selected.
Self Join
Sometimes you need to join a table to itself. This type of a join is called as self
join. For example, to find the name of each employee’s manager, you need to
join the EMP table to itself:
SQL> SELECT worker.last_name || ' works for ' || manager.last_name
2 FROM emp worker, emp manager WHERE worker.manager_id = manager.emp_id ;
WORKER.LAST_NAME||'WORKSFOR'||MANAGER.LAST_NAME
-------------------------------------------------------------
Kochhar works for King
De Haan works for King
Hunold works for De Haan
Ernst works for Hunold
Lorentz works for Hunold
Mourgos works for King
Rajs works for Mourgos
Davies works for Mourgos
Matos works for Mourgos
Vargas works for Mourgos
Zlotkey works for King
Abel works for Zlotkey
Taylor works for Zlotkey
Grant works for Zlotkey
Whalen works for Kochhar
Hartstein works for King
Fay works for Hartstein
Higgins works for Kochhar
Gietz works for Higgins
19 rows selected.
Outer Join
SQL> select e.dept_id,d.dept_id
SQL> select e.dept_id,d.dept_id
2 from emp e, dept d
2 from emp e, dept d
3 where e.dept_id(+) = d.dept_id;
3 where e.dept_id=d.dept_id;
DEPT_ID DEPT_ID
DEPT_ID DEPT_ID
---------- ----------
---------- ----------
10 10
90 90
20 20
90 90
20 20
90 90
50 50
60 60
50 50
60 60
50 50
60 60
50 50
50 50
50 50
50 50
60 60
50 50
60 60
50 50
60 60
50 50
80 80
80 80
80 80
80 80
80 80
80 80
90 90
10 10
90 90
20 20
90 90
20 20
110 110
110 110
110 110
110 110
190
19 rows selected.
20 rows selected.
Full Outer Join
SQL> select e.dept_id,d.dept_id
2 from emp e full outer join dept d
3 on (e.dept_id=d.dept_id);
DEPT_ID DEPT_ID
---------- ----------
90 90
90 90
90 90
60 60
60 60
60 60
50 50
50 50
50 50
50 50
50 50
80 80
80 80
80 80
10 10
20 20
20 20
110 110
110 110
190
21 rows selected.
Writing Subqueries
Suppose that you want to write a query to find out who earns a salary greater
than Hartstein’s salary. To solve this problem, you need two queries: one query to
find out what Hartstein earns and a second query to find out who earns more
than that amount. You can solve this problem by combining the two queries,
placing one query inside the other query. An inner query, or subquery, returns
a value that is used by the outer query or main query.
FIRST_NAME
-----------------
King
De Haan
Single-row Subqueries
A single-row subquery returns one row from the inner SELECT statement. This
type of subquery uses a single-row operator as listed in the slide.
LAST_NAME JOB_ID
------------------------- ----------
Hunold IT_PROG
Ernst IT_PROG
Lorentz IT_PROG
Subqueries that return more than one row are called multiple-row subqueries and
they must use a multiple-row operator.
SQL> SELECT last_name, job_id, salary FROM emp
2 WHERE salary IN (SELECT MAX(salary) FROM emp GROUP BY dept_id);
8 rows selected.
Manipulating Structures & Data
SQL> create table MY_DEPT
2 (dept_id NUMBER(4) PRIMARY KEY,
3 dept_name VARCHAR2(30) NOT NULL,
4 manager_id NUMBER(6),
5 location_id NUMBER(4));
Table created.
SQL> insert into MY_DEPT (dept_id, dept_name, manager_id, location_id) values (10,'Administration',200,1700);
1 row created.
SQL> insert into MY_DEPT (dept_id, dept_name, manager_id, location_id) select * from dept where dept_id=20;
1 row created.
COUNT(*)
----------
20
SQL> UPDATE emp_history SET dept_id = 60 WHERE emp_id = 206; SQL> DELETE FROM emp_history
1 row updated. 2 WHERE emp_id = 206;
1 rows deleted.
SQL> UPDATE emp
2 SET job_id = (SELECT job_id FROM emp WHERE emp_id = 205),
SQL> truncate table emp_history;
3 salary = (SELECT salary FROM emp WHERE emp_id = 205)
4 WHERE emp_id = 124;
Table truncated.
1 row updated.
SQL> drop table emp_history;
Table dropped.
Multiple Inserts
SQL> create table sal_history
2 (emp_id NUMBER(6) primary key,
3 hire_date DATE NOT NULL,
4 salary NUMBER(8,2));
Table created.
8 rows created.
6 rows created.
MERGE Statements
MERGE statement allows you to update or insert a row conditionally into a
table. The decision whether to update or insert into the target table is based
on a condition in the ON clause.
you need both INSERT and UPDATE privileges on the target table and the
SELECT privilege on the source table.
COUNT(*)
----------
3
COUNT(*)
----------
20
Database Transactions
Database users access the database in two ways:
A transaction begins when the first DML statement is encountered, and ends
when one of the following occurs:
FIRST_NAME
--------------------
Alexander
Locking
Oracle provide data concurrency and integrity between transactions using its
locking. Oracle uses two modes of locking in a multiuser
database:
SERIALIZABLE
Serializable transactions see only those changes that were committed at the
time the transaction began, plus those changes made by the transaction itself.
To make this determination efficiently, Oracle uses control information stored in
the data block that indicates which rows in the block contain committed and
uncommetted changes. The amount of history that is retained is controlled by
the INITRANS parameter of CREATE TABLE statement. Under same
circumstances, Oracle can have insufficient history information to determine
whether a row has been updated by a “too recent” transaction. Oracle
generates an error when a serializable transaction tries to update or delete data
modified by a transaction that commits after the serializable transaction began.
READ ONLY
Read-only transactions see only those changes that were committed at the time
the transaction began and do not allow INSERT, DELETE and UPDATE
statements.
Table Creation Guidelines
When you create a table, Oracle automatically allocates a data segment in a
tablespace to hold the table’s future data.
A database is divided into logical storage units called TABLESPACES, which
group related logical structures together.
Physically a tablespace is composed by one or more DATAFILES.
Logically it is divided into SEGMENTS, EXTENTS and DATABLOCKS.
TABLESPACE
EXTENT 01
EXTENT 02
EXTENT 03
.... DATABLOCKS
PCTUSED
PCTFREE
to the row data area in
HEAD
There are some roles to follow about objects’ naming standard. Look at intranet
documentation (Development intranet: DevCentral\Oracle Guidelines).
create table AN_MA_ANAG_CLIENTI
(AC_CLIENTE_ID number(10,0),
AC_NOME varchar2(50),
AC_COGNOME varchar2(50),
AC_INDIRIZZO varchar2(50),
AC_COMUNE varchar2(50),
AC_CAP varchar2(5) constraint AN_AC_CAP_NN_2001 NOT NULL,
AC_PROVINCIA varchar2(4),
AC_TELEFONO varchar2(15),
constraint AN_AC_CLID_PK_1001 primary key (AC_CLIENTE_ID)
using index tablespace @@TB_IDX_LIT@@ PCTFREE 5
)
tablespace @@TB_DATI_LIT@@
PCTFREE 5
PCTUSED 85
/