references = foreign key
equijoin = inner join
http://docs.oracle.com/cd/E11882_01/appdev.112/e17125/adfns_regexp.htm#ADFNS232
Precedence:
1. Arithmetic (pemdas)
2. Concat
3. Comparisons
4. is null, like, between
5 not between
6. not equal
7. not
8. AND
9. OR
Read consistency is an automatic implementation. It keeps a partial copy of the database in
the undo segments. The read-consistent image is constructed from the committed data in the
table and the old data that is being changed and is not yet committed from the undo segment.
When an insert, update, or delete operation is made on the database, the Oracle server takes
a copy of the data before it is changed and writes it to an undo segment.
All readers, except the one who issued the change, see the database as it existed before the
changes started; they view the undo segment’s “snapshot” of the data.
The relationship between the EMPLOYEES and DEPARTMENTS tables is
an equijoin; that is, values in the DEPARTMENT_ID column in both tables must be equal.
Often, this type of join involves primary and foreign key complements.
Note: Equijoins are also called simple joins or inner joins.
SELECT e.employee_id, e.last_name, e.department_id,
d.department_id, d.location_id
FROM
employees e, departments d
WHERE e.department_id = d.department_id;
SELECT d.department_id, d.department_name, l.city
FROM
departments d, locations l
WHERE
d.location_id = l.location_id
AND d.department_id IN (20, 50);
NATURAL JOIN
SELECT department_id, department_name,
location_id, city
FROM
departments NATURAL JOIN locations ;
USING
SELECT employee_id, last_name,
location_id, department_id
FROM
employees JOIN departments USING (department_id) ;
ON
SELECT e.employee_id, e.last_name, e.department_id,
d.department_id, d.location_id
FROM
employees e JOIN departments d
ON
(e.department_id = d.department_id);
NONEQUIJOINS
SELECT e.last_name, e.salary, j.grade_level
FROM
employees e, job_grades j
WHERE e.salary
BETWEEN j.lowest_sal AND j.highest_sal;
SELECT e.last_name, e.salary, j.grade_level
FROM
employees e JOIN job_grades j
ON
e.salary BETWEEN j.lowest_sal AND j.highest_sal;
The outer join operator is the plus sign (+)
SELECT e.last_name, e.department_id, d.department_name
FROM
employees e, departments d
WHERE e.department_id(+) = d.department_id ;
SELECT e.last_name, d.department_id, d.department_name
FROM
employees e RIGHT OUTER JOIN departments d
ON
(e.department_id = d.department_id) ;
Self join
SELECT worker.last_name || ‘ works for ‘
|| manager.last_name
FROM
employees worker, employees manager
WHERE worker.manager_id = manager.employee_id ;
UNDO
(5 minut temu)
select * from testable as of timestamp (SysDate – 5/1440)
select * from testtable as of timestamp(’08-Aug-2011 5:00:00 PM’,'DD-MON-YYYY HH:MI:SS AM’);
flashback table testtable to before drop;
NATURAL JOIN
SELECT employee_id, last_name,
department_name
FROM
hr.employees JOIN hr.departments
USING (department_id) ;
SELECT l.city, d.department_name
FROM
locations l JOIN departments d USING (location_id)
WHERE location_id = 1400;
SELF JOIN
Sometimes you need to join a table to itself. To find the name of each employee’s manager,
you need to join the EMPLOYEES table to itself, or perform a self-join.
SELECT worker.last_name emp, manager.last_name mgr
FROM
employees worker JOIN employees manager
ON
(worker.manager_id = manager.employee_id);
NONEQUIJOINS
To return the unmatched rows, you can use
an OUTER join. An OUTER join returns all rows that satisfy the join condition and also returns
some or all of those rows from one table for which no rows from the other table satisfy the join
condition.
There are three types of OUTER joins:
•LEFT OUTER
•RIGHT OUTER
•FULL OUTER
SELECT e.last_name, e.department_id, d.department_name
FROM
employees e LEFT OUTER JOIN departments d
ON
(e.department_id = d.department_id) ;
This query retrieves all the rows in the EMPLOYEES table, which is the left table, even if there
is no match in the DEPARTMENTS table.
SELECT e.last_name, d.department_id, d.department_name
FROM
employees e RIGHT OUTER JOIN departments d
ON
(e.department_id = d.department_id) ;
This query retrieves all the rows in the DEPARTMENTS table, which is the table at the right,
even if there is no match in the EMPLOYEES table.
SELECT e.last_name, d.department_id, d.department_name
FROM
employees e FULL OUTER JOIN departments d
ON
(e.department_id = d.department_id) ;
This query retrieves all rows in the EMPLOYEES table, even if there is no match in the
DEPARTMENTS table. It also retrieves all rows in the DEPARTMENTS table, even if there is no
match in the EMPLOYEES table.
——— CARTESIAN PRODUCT ————
SELECT last_name, department_name
FROM
employees CROSS JOIN departments ;
Here’s a handy query for finding duplicates in a table. Suppose you want to find all email addresses in a table that exist more than once:
SELECT email, COUNT(email) AS NumOccurrences FROM users GROUP BY email HAVING ( COUNT(email) > 1 )
You could also use this technique to find rows that occur exactly once:
SELECT email FROM users GROUP BY email HAVING ( COUNT(email) = 1 )
Źródło: http://www.petefreitag.com/item/169.cfm
CREATE TABLE HR.JS_TAB1(MIASTO VARCHAR2(20), ULICA VARCHAR2(20));
SELECT * FROM HR.JS_TAB1;
CREATE TABLE HR.JS_TRANS1(ULICA_Z VARCHAR2(30), ULICA_NA VARCHAR(30));
SELECT * FROM HR.JS_TRANS1;
INSERT INTO HR.JS_TAB1 VALUES(‘GDYNIA’,'NAGIETKOWA’);
INSERT INTO HR.JS_TAB1 VALUES(‘GDYNIA’,'SWIETOJANSKA’);
INSERT INTO HR.JS_TAB1 VALUES(‘GDANSK’,'KOLOBRZESKA’);
INSERT INTO HR.JS_TAB1 VALUES(‘GDANSK’,'MALBORSKA’);
INSERT INTO HR.JS_TRANS1 VALUES (‘KOLOBRZESKA’,'CHLOPSKA’);
INSERT INTO HR.JS_TRANS1 VALUES (‘SWIETOJANSKA’,'WLADYSLAWA IV’);
SELECT MIASTO, ULICA FROM HR.JS_TAB1;
SELECT MIASTO,
Nvl((SELECT ULICA_NA FROM HR.JS_TRANS1 TRAN1 WHERE TAB1.ULICA = TRAN1.ULICA_Z),TAB1.ULICA)
FROM HR.JS_TAB1 TAB1;
PL/SQL Procedural Language SQL
anonymous blocks – bez nazwy
procedures/functions – stored in data dictionary
packages – zgrupowane procedury i funkcje
Data Types
- scalar (binary_integer, integer, string …)
- composite (nested tables, varrays, record …)
r employees%rowtype;
nested tables of numbers:
TYPE – określamy jakiego jest typu
linia 5 – konstruktor – przypisujemy konstruktor do zmiennej (na razie on nie istnieje)
tworzymy go przez extend. W lini 7 tworzenie slotu dla zmiennych.
- reference – procedury mogą odwoływać się do refcursors.
Anonymous blocks:
od DECLARE do BEGIN – deklarujemy zmienne
od BEGIN do EXCEPTION – kod
“:=” (przypisanie wartości)
anchor data type – można przypisać typ danych do zmiennej na podstawie tabeli (linia 6)
Procedures:
tu są nasze procedury
select * from user_source;
select text from user_source where name = ‘nazwa_procedury’;
Zamiast DECLARE (jak w anoymouns blocks) jest AS. Pomiędzy AS a BEGING deklarujemy zmienne.
Wywołanie takiej procedury:
SQL> BEGIN
SQL> give_raises(1.5);
SQL> END;
SQL> /
lub:
exec give_raises(1.5);
CURSOR – to jest SELECT wewnątrz bloku PL/SQL który może zwrócić dużą liczbę rekordów.
Jeżeli SELECT może mieć więcej niż jeden wynik trzeba używać CUROSR’ów.
CURSOR – deklaracja kursora – i następnie SELECT
Cursor zostaje wykonane w kodzie (po BEGIN)
rec – nazwa zmiennej IN in nazwa kursora
count – metoda należąca do nested tables … count równe 1,2,3 …
Te wartości które mamy w tablicy możemy przkazać np do innej procedury.
Cursor teraz zawiera employee_id, hire_data
Aby zrobic updaty na cursorze trzeba go zrobic FOR UPDATE
WHER CURRENT OF EMP_CUR – praca na aktualnych danych w kursorze
LOOPS
EXCEPTION HANDLING
crate or replace procedure add_emp(eid number, last varchar2, first varchar2, email varchar2, hire_date date)
as
hire_date_excepition exception;
email_exception exception;
begin
if hire date > sysdate then
raise hird_date_exception;
end if;
if email is not null and email not like ‘%@%.%’ then
raise email_exception;
end if;
— jakis kod dodajacy pracownika
exception
when hire_date_exception then
dbms_output.put_line(‘wrong date’);
when email_exception then
dbms_output.put_line(‘wrong email format’);
end;
/
exception – tu się zaczyna blok exception
Wywołujemy procedurę:
exec add_emp(500,’Joe’,'Jule’,'joe@serwer.com’,sysdate + 2)
i zwraca błąd, że jest zła data.
select * from USER_CONSTRAINTS;
select * from DBA_CONSTRAINTS;
aby sprawdzić na jakich kolumnach są nałożone constraints.
select constraint_name, column_name, position
from USER_CONS_COLUMNS
where constraint_name in(select constraint_name from user_constraints where table_name = ‘CITIES’);
create table as statement
create table COLORADO_PERSON as select * from PERSON where st = ‘CO’;
UNIC CONSTRAINT
FOREIGN CONSTRAINT – pozwala znaleźć releacje pomiędzy dwoma tablicami
tabela cities jest tu tabelą parent i tu są zdefiniowane miasta i stany. Przy próbie wpisu to tabeli person miasta i stanu którego nie ma w tabeli cities otrzymamy błąd.
Wyłączenie danego CONSTRAINT:
alter table PERSON disable constraint fk_person_cities;
alter table PERSON enable constraint fk_person_cities;
alter table users add constraint fk_users_person
foreign key(username) references person(username)
deferrable initially deferred;
SEQUENCE
VIEWS:
W drugim wypadku kolumny dokładnie odpowiadają nazwom kolumn w tabeli dlatego ten widok można wykorzystać do updateowania tabeli.
Strona startowa: www.jaceksen.pl