SQL « Różności …

Różności …

7 maja 2014

JOINS

Zaszufladkowany do: Bez kategorii,SQL — Tagi: — Jacek @ 07:43

references = foreign key

equijoin = inner join

9 kwietnia 2014

regular expressions

Zaszufladkowany do: Programowanie,SQL — Tagi: — Jacek @ 13:34

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

12 marca 2014

case decode

Zaszufladkowany do: SQL — Tagi: — Jacek @ 07:58

4 marca 2014

Oracle read consistency.

Zaszufladkowany do: Oracle,Programowanie,SQL — Tagi: , — Jacek @ 06:44

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;

17 lutego 2014

joins

Zaszufladkowany do: Bazy danych,Oracle,Programowanie,SQL — Tagi: — Jacek @ 06:30

NATURAL JOIN

INNER JOINS

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 ;

13 lutego 2014

group_by

Zaszufladkowany do: Bazy danych,Programowanie,SQL — Tagi: — Jacek @ 06:57

group by

AVG
COUNT
MAX
MIN
STDDEV
SUM
VARIANCE

12 lutego 2014

NVL NULL, to_date, to_char

Zaszufladkowany do: Oracle,SQL — Tagi: , — Jacek @ 06:47

to_char

23 listopada 2011

SQL – duplicates

Zaszufladkowany do: SQL — Tagi: — Jacek @ 12:06

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

18 października 2011

(SQL) select with translation table

Zaszufladkowany do: Programowanie,SQL — Tagi: — Jacek @ 10:03

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;

28 grudnia 2010

Oracle – notki, SQL, PL/SQL

Zaszufladkowany do: Bazy danych,Oracle,PL/SQL,Programowanie,SQL — Tagi: , , — Jacek @ 11:16
sql> host – aby dostać się do promptu hosta
c:> echo %oracle_sid% lub echo $oracle_sid$
c:> exit
sql>
po zalaogowaniu można wydać komendę:
sql> define
i wyświetlą się parametry informujące o tym jako kto jesteśmy podłączeni
będąc podłączonym można podłączyć się jako inny user
sql> connect sys as sysdba
Sprawdzenie jakie tabele są w schemacie użytkownika:
sql> select table_name from user_tables;
sql> desc ps$ (opis tabeli)
sql> set pagesize 200
sql> / (ponowne uruchomienie polecenia z buforu)
sql> l lub list – wyswietla to co jest w buforze
w buforze jest tylko ostatnie polecenie
jeżeli chcemy coś zmienić w buforze:
sql> 2
podajemy numer linii w której chcemy zrobić zmianę
sql> c/name/surename
i określamy (c – change) co na co chcemy zamienić
SQL – Structured Query Language
określenie w jakim formacie ma zostać wyświetlana data;
sql> alter session set nls_date_format=’mm/dd/yyyy’;
można określić jak inne dane mają być wyświetlane:
sql> col sal fromat $999,999.00
sql> col ename heading ‘Employee|Name’
Jeżeli chcemy coś dodać do bufora:
sql> 1
sql> a ,nazwisko
aby format danej kolumny był taki jak jakiejś innej można:
sql> col comm like sal heading “Commision”
aby kolumna była szersza:
sql> col job format a15
wyszyszczenie formatowania:
sql> col comm clear
lub
sql> clear columns
jeżeli chcemy zachować ostatnią komendę z bufora:
sql> save plik.sql
uruchomienie
sql> @plik.sql
zapis wyników:
sql> spool plik.txt
sql> spool off
sql> host
c:> type plik.txt
c:> exit
sql> spool plik.txt append
można robić zmiany bufora w edytorze:
ed
i wyświetlić/zdefiniować edytor
define _editor
define _editor=c:\notepad
Jeżeli chcemy wstawić zmienną do zapytania
… where job=’&p’
sql> undefine p
aby wyświetlać po stronie:
sql> set pause on
sql> set pagesize 35
Plik z ustawieniami środowiska startowy:
SQLPATH=c:\oracle\product
tam trzeba umieścić plik:
login.sql
sql> help index
sql> help define
zmiana hasła
sql> passw user1
Na serwerze aplikacji można uruchomić isqlplus, który pozwala na dostęp do bazy danych przez przeglądarkę internetową:
c:> isqlplusctl start
potem w przeglądarce
http://localhost:5560/isqlplus

Strona startowa: www.jaceksen.pl