Vous êtes sur la page 1sur 26

Oracle is a relational database.

Relational databases store data in tables like this:



COUNTRY

CTRY_ID CTRY_NAME CTRY_AREA CTRY_POP POP_UPD_AT CURRENCY
14 Germany 137882 82046000 30-nov-08 Euro
48 Ghana 92100 23837000 null Cedi
53 Australia 2969907 21884000 4-sep-09 Australian Dollar
73 Greece 50949 11257285 1-Jan-09 Euro
122 Georgia 26900 4382100 1-Jan-09 Lari
123 New Zealand 104428 4320300 4-sep-09 New Zealand Dollar
147 Gambia 4361 1705000 null Dalasi
149 Gabon 103347 1475000 null CFA Iranc

Communication with the database is perIormed using Structured Query Language (SQL).

SQL is used to create, alter and destroy tables in the database and to insert, update, delete and
query the data in these tables.

Applications may use APIs and Irameworks such as ODBC, JDBC etc but ultimately these will
all talk to the database using SQL.

SQL consists oI:
O %he SELECT statement
O Data Manipulation Language (DML)
O Data DeIinition Language (DDL)
Let's take a tour oI SQL, starting with the SELEC% statement.

Selection and Projection
A SELECT statement looks like this:
SELECT columns
FRJM tables
WHERE conditions
JRDER BY columns;
SELECT and FRJM are mandatory, WHERE and JRDER BY are optional, and statements are
terminated by a semicolon.

Let's start with the simplest oI queries. %o retrieve all oI the columns and all oI the rows in a
table, use this SQL:
SELECT
FRJM country;
%he asterisk (*) is a quick way to select all columns.
CTRY_ID CTRY_NAME CTRY_AREA CTRY_POP POP_UPD_ON CURRENCY
14 Germany 137882 82046000 30-nov-08 Euro
48 Ghana 92100 23837000 null Cedi
53 Australia 2969907 21884000 4-sep-09 Australian Dollar
73 Greece 50949 11257285 1-Jan-09 Euro
122 Georgia 26900 4382100 1-Jan-09 Lari
123 New Zealand 104428 4320300 4-sep-09 New Zealand Dollar
147 Gambia 4361 1705000 null Dalasi
149 Gabon 103347 1475000 null CFA Iranc

We can return speciIic columns by naming them in a comma separated list. %his is called
projection.
SELECT ctry_name, ctry_area, currency
FRJM country;
CTRY_NAME CTRY_AREA CURRENCY
Germany 137882 Euro
Ghana 92100 Cedi
Australia 2969907 Australian Dollar
Greece 50949 Euro
Georgia 26900 Lari
New Zealand 104428 New Zealand Dollar
Gambia 4361 Dalasi
Gabon 103347 CFA Iranc

%o return a subset oI rows we include the WHERE clause. %his is known as selection or restriction.
SELECT
FRJM country
WHERE currency = 'Euro';
CTRY_ID CTRY_NAME CTRY_AREA CTRY_POP POP_UPD_ON CURRENCY
14 Germany 137882 82046000 30-nov-08 Euro
73 Greece 50949 11257285 1-Jan-09 Euro

And we combine both techniques to target just the values we are interested in.
SELECT ctry_name, ctry_area, currency
FRJM country
WHERE currency = 'Euro';
String literals are enclosed in single quotes.
CTRY_NAME CTRY_AREA CURRENCY
Germany 137882 Euro
Greece 50949 Euro

Incidentally, SQL is case insensitive so the previous statement could have been written like this:
select CTRY_NAME, CTRY_AREA
from CJUNTRY
where CTRY_ID = 53;

ogical Operators
We can combine the results oI multiple conditions using AND and JR. For example, suppose we
wish to Iind large countries which use the Euro as their currency:
SELECT ctry_name, ctry_area, ctry_pop, currency
FRJM country
WHERE ctry_area = 100000
AND currency = 'Euro';
%he Iollowing comparison conditions are available: , , ~, , ~.

Not equals can be ! or ~.
CTRY_NAME CTRY_AREA CTRY_POP CURRENCY
Germany 137882 82046000 Euro

Care must be taken with the operator precedence when using AND and JR. Suppose we wish to
extend our deIinition oI a large country to include countries with a large population.

We may be tempted to write this SQL:
SELECT ctry_name, ctry_area, ctry_pop, currency
FRJM country
WHERE ctry_area = 100000
JR ctry_pop = 10000000
AND currency = 'Euro';
But these incorrect results would be produced:
CTRY_NAME CTRY_AREA CTRY_POP CURRENCY
Germany 137882 82046000 Euro
Australia 2969907 21884000 Australian Dollar
Greece 50949 11257285 Euro
New Zealand 104428 4320300 New Zealand Dollar
Gabon 103347 1475000 CFA Iranc

Several countries have been selected which do not use the Euro. %his is because the AND
condition has greater precedence than JR, so we must use parentheses to override the deIault.
SELECT ctry_name, ctry_area, ctry_pop, currency
FRJM country
WHERE (ctry_area = 100000
JR ctry_pop = 10000000)
AND currency = 'Euro';
CTRY_NAME CTRY_AREA CTRY_POP CURRENCY
Germany 137882 82046000 Euro
Greece 50949 11257285 Euro

Pattern Matching
Use the LIKE condition to perIorm pattern matching.
SELECT currency
FRJM country
WHERE currency LIKE '%Dollar%';
CURRENCY
Australian Dollar
New Zealand Dollar

matches zero or more characters.
matches exactly one character.
SELECT ctry_name
FRJM country
WHERE ctry_name LIKE '_e%';
CTRY_ID
Germany
Georgia
New Zealand

For more sophisticated pattern matching use REGEXP_LIKE.
SELECT currency
FRJM country
WHERE REGEXP_LIKE (currency, '.Dd,ollar.');
CURRENCY
Australian Dollar
New Zealand Dollar

Range Condition
BE%WEEN provides a range condition. %hese two statements are equivalent.
SELECT ctry_name, ctry_pop
FRJM country
WHERE ctry_pop BETWEEN 500000 AND 10000000;

SELECT ctry_name, ctry_pop
FRJM country
WHERE ctry_pop = 500000
AND ctry_pop <= 10000000;
Both produce these results:
CTRY_NAME CTRY_POP
Georgia 4382100
New Zealand 4320300
Gambia 1705000
Gabon 1475000

BETWEEN can also be useIul when checking date ranges.
SELECT ctry_name, ctry_pop, pop_upd_on
FRJM country
WHERE pop_upd_on BETWEEN '30-nov-08' AND '1-jan-09';
CTRY_NAME CTRY_POP POP_UPD_ON
Germany 82046000 30-nov-08
Greece 11257285 1-Jan-09
Georgia 4382100 1-Jan-09

Note that the speciIied values are inclusive.
Membership Condition
Suppose we wish to test iI a column is equal to one oI several values in a list. We could use the
JR condition like this:
SELECT ctry_name, ctry_area
FRJM country
WHERE ctry_name = 'Ghana'
JR ctry_name = 'Australia'
JR ctry_name = 'Greece';
But a more convenient solution is to use the IN condition.
SELECT ctry_name, ctry_area
FRJM country
WHERE ctry_name IN ('Ghana', 'Australia', 'Greece');
CTRY_NAME CTRY_AREA
Ghana 92100
Australia 2969907
Greece 50949

%his is known as a membership condition. Use NJT IN to negate the result.
SELECT ctry_name, ctry_area
FRJM country
WHERE ctry_name NJT IN ('Ghana', 'Australia', 'Greece');
%his time, all countries 349 present in the list are returned.
CTRY_NAME CTRY_AREA
Germany 137882
Georgia 26900
New Zealand 104428
Gambia 4361
Gabon 103347

Multi-value expression lists are also possible in Oracle SQL. We can compare both the
CURRENCY and PJP_UPD_JN columns to values in a list, like this:
SELECT ctry_name, pop_upd_on, currency
FRJM country
WHERE (currency, pop_upd_on) IN (
('Euro', '30-NJV-08'),
('Lari', '01-JAN-09') );
CTRY_NAME POP_UPD_ON CURRENCY
Germany 30-nov-08 Euro
Georgia 1-Jan-09 Lari

Distinct
Suppose we need a list oI the currencies used by Germany, Greece and Georgia.
SELECT currency
FRJM country
WHERE ctry_name IN ('Germany', 'Greece', 'Georgia');
CURRENCY
Euro
Euro
Lari

%he problem here is that the Euro is included twice, once Ior Germany and once Ior Greece.
However, we can remove duplicates using the DISTINCT keyword.
SELECT DISTINCT currency
FRJM country
WHERE ctry_name IN ('Germany', 'Greece', 'Georgia');
%he UNIQUE keyword is synonymous with DISTINCT
CURRENCY
Euro
Lari

Null Comparison
II a column in a row has no value, then the column is said to contain null.
SELECT ctry_name, pop_upd_on
FRJM country;
CTRY_NAME POP_UPD_ON
Germany 30-nov-08
Ghana (null)
Australia 4-sep-09
Greece 1-Jan-09
Georgia 1-Jan-09
New Zealand 4-sep-09
Gambia (null)
Gabon (null)

%he nulls in the POPUPDON column indicate that we don't know when the population was
updated. %here is no meaningIul date that we can enter Ior that row.

We use IS NULL or IS NJT NULL when perIorming a null comparision.
SELECT ctry_name, pop_upd_on
FRJM country
WHERE pop_upd_on IS NULL;
CTRY_NAME POP_UPD_ON
Ghana (null)
Gambia (null)
Gabon (null)
SELECT ctry_name, pop_upd_on
FRJM country
WHERE pop_upd_on IS NJT NULL;
CTRY_NAME POP_UPD_ON
Germany 30-nov-08
Australia 4-sep-09
Greece 1-Jan-09
Georgia 1-Jan-09
New Zealand 4-sep-09

Other operators such as or ! will not work with null. All oI these SQL statements will return
NO rows.
SELECT FRJM country WHERE pop_upd_on = null;
-- 0 rows selected

SELECT FRJM country WHERE pop_upd_on != null;
-- 0 rows selected

SELECT FRJM country WHERE null=null;
-- 0 rows selected

SELECT FRJM country WHERE null!=null;
-- 0 rows selected

Ordering Results
Use the JRDER BY clause to order the results oI a query. %his example will order the countries by
size, largest Iirst:
SELECT ctry_name, ctry_area
FRJM country
JRDER BY ctry_area DESC;
CTRY_NAME CTRY_AREA
Australia 2969907
Germany 137882
New Zealand 104428
Gabon 103347
Ghana 92100
Greece 50949
Georgia 26900
Gambia 4361

Append ASC or DESC to the column name to indicate ascending or descending sequence. ASC is
the deIault.

Multiple column names can be speciIied in the JRDER BY clause and the columns will be ordered
with leIt to right precedence.

%his query will order the rows into descending C%RYNAME 93 ascending CURRENCY.
SELECT currency, ctry_name
FRJM country
JRDER BY currency, ctry_name DESC;
%he ordering on C%RYNAME only comes into play when there are duplicate CURRENCY
values such as the Euro.
CURRENCY CTRY_NAME
Australian Dollar Australia
Cedi Ghana
CFA Iranc Gabon
Dalasi Gambia
Euro Greece
Euro Germany
Lari Georgia
New Zealand Dollar New Zealand

JRDER BY is the only way to guarantee ordering oI the result set. Without this clause, rows could
be returned in a diIIerent sequence Irom one execution to the next.
Scalar Functions
Oracle includes many useIul scalar Iuntions. For example:
SELECT
ctry_name || ' has an area of ' || ctry_area || ' sq miles' AS "Country",
to_char(ctry_pop, '999,999,999') AS "Population",
nvl(to_char(pop_upd_on, 'ddspth Monthyyyy'), 'Unknown') AS "Updated Jn",
trunc(ctry_pop / ctry_area, 2) AS "Density"
FRJM country
WHERE soundex(ctry_name) IN (
soundex('grease'),
soundex('jorjia'),
soundex('ganah'));
Country Population Updated On Density
Ghana has an area oI 92098 sq miles 23,837,000 Unknown 258.82
Greece has an area oI 50949 sq miles 11,257,285 Iirst January 2009 220.95
Georgia has an area oI 26900 sq miles 4,382,100 Iirst January 2009 162.9

Double pipe (,,) concatenates two strings (C%RYAREA is converted to a string in the Iirst
column).

to_char() uses a number Iormat model (999,999,999) or a datetime Iormat model (ddspth
Monthyyyy) to describe the Iormat oI the display.

nvl() replaces a null with a speciIied string (the 'Unknown' string in this case).

trunc() truncates a number to the speciIied number oI decimal places.

soundex() lets you compare words that are spelled diIIerently, but sound alike in English.

We have also introduced a column alias using the AS keyword. Oracle will use this alias as the
column heading oI the result set. Without this, the Iirst column would be called ctry_name || '
has an area of ' || ctry_area || ' sq miles'

Enclose alias names in double quotes iI they contain any special characters such as space or
punctuation, or to preserve the case.


Uracle Data Manipulation Language

uaLa ln Lhe Lables ls malnLalned uslng uaLa ManlpulaLlon Language (uML)

uML conslsLs of Lhe followlng sLaLemenLs (alLhough SLLLC1 ls someLlmes lncluded ln Lhls llsL Loo)
O -SL81
O D9uA1L
O uLLL1L
Insert
1he INSERT sLaLemenL Lakes Lwo forms
INSERT INTJ table_name (column_list) VALUES (value_list);

INSERT INTJ table_name VALUES (value_list);
LeLs use Lhe flrsL opLlon Lo add Spaln Lo Lhe CCD-18? Lable (wlLh some dellberaLe mlsLakes)
INSERT INTJ country (ctry_id, ctry_name, ctry_area, ctry_pop, pop_upd_on,
currency)
VALUES(28, 'pain', 195364, 5, '1-jul-2009', 'Norwegian Krone');

CJMMIT;

SELECT
FRJM country
WHERE ctry_id = 28;
C1k_ID C1k_NAML C1k_AkLA C1k_C C_UD_CN CUkkLNC
28 paln 193364 3 1[ul2009 -orweglan krone

1he column_llsL ls opLlonal so we could have used Lhls SCL lnsLead
INSERT INTJ country
VALUES (28, 'pain', 195364, 5, '1-jul-2009', 'Norwegian Krone');

CJMMIT;
L ls good pracLlce however Lo name Lhe columns ln Lhe INSERT sLaLemenL L makes Lhe SCL easler Lo
follow and appllcaLlons more reslllenL Lo change (lf Lhe column order ls changed or new columns are
added Lo Lhe Lable for example)

-amlng Lhe columns also allows us Lo omlL opLlonal values durlng Lhe lnserL
INSERT INTJ country (ctry_id, ctry_name, ctry_area)
VALUES(28, 'pain', 195364);

CJMMIT;
n Lhls case 9C9 9C9_D9u_C- and CD88L-C? are seL Lo null because no values have been provlded for
Lhem
Update
1he UPDATE sLaLemenL looks llke Lhls
UPDATE table_name
SET column1 = value1,
column2 = value2
WHERE conditions;
We use Lhls UPDATE sLaLemenL Lo flx Lhe mlsLakes we made when lnserLlng Lhe row
UPDATE country
SET ctry_name = 'Spain',
ctry_pop = 46661950,
currency = 'Euro'
WHERE ctry_id = 28;

-- 1 row updated

CJMMIT;

SELECT
FRJM country
WHERE ctry_id = 28;
C1k_ID C1k_NAML C1k_AkLA C1k_C C_UD_CN CUkkLNC
28 Spaln 193364 46661930 30nov08 Luro

Several rows can be changed wlLh a slngle UPDATE sLaLemenL dependlng on Lhe selecLlon crlLerla ln our
WHERE clause Pere we lncrease Lhe populaLlon by Lhree percenL and seL Lhe updaLed daLe Lo Lhe 1sL
!une 2009 for Spaln Cermany and Creece
UPDATE country
SET ctry_pop = trunc(ctry_pop 1.03),
pop_upd_on = '1-jun-09'
WHERE ctry_name IN ('Spain', 'Germany', 'Greece');

-- 3 rows updated

CJMMIT;

SELECT
FRJM country
WHERE ctry_name IN ('Spain', 'Germany', 'Greece');
C1k_ID C1k_NAML C1k_AkLA C1k_C C_UD_CN CUkkLNC
14 Cermany 137847 84307380 01!D-09 Luro
28 Spaln 193364 48061808 01!D-09 Luro
73 Creece 30949 11393003 01!D-09 Luro

Delete
1o deleLe rows from a Lable use Lhe DELETE sLaLemenL
DELETE FRJM table_name
WHERE conditions;
1hls example removes any small counLrles
DELETE FRJM country
WHERE ctry_area < 100000;

-- 4 rows deleted

CJMMIT;

SELECT
FRJM country;
C1k_ID C1k_NAML C1k_AkLA C1k_C C_UD_CN CUkkLNC
14 Cermany 137882 82046000 30nov08 Luro
33 AusLralla 2969907 21884000 4sep09 AusLrallan uollar
123 -ew Zealand 104428 4320300 4sep09 -ew Zealand uollar
149 Cabon 103347 1473000 null ClA franc
149 Spaln 193364 48061808 01!D-09 Luro

Chana Creece Ceorgla and Cambla have been deleLed
Transactions
?ou wlll have noLlced LhaL a CJMMIT sLaLemenL followed each of Lhe uML sLaLemenLs above

A CJMMIT makes permanenL all uML changes made durlng a LransacLlon A RJLLBACK sLaLemenL on
Lhe oLher hand undoes all uML changes made durlng a LransacLlon

A LransacLlon ls a loglcal unlL of work LhaL comprlses one or more SCL sLaLemenLs A LransacLlon beglns
when Lhe flrsL SCL sLaLemenL ls execuLed and ends wlLh a CJMMIT or RJLLBACK aL whlch polnL ALL
changes are made permanenL or -C-L of Lhem are

LeLs Lake a look aL LransacLlons ln acLlon by mlsLakenly updaLlng Lhe currency for AusLralla Lo -ew
Zealand uollar and removlng Lhe -ew Zealand row alLogeLher
UPDATE country
SET currency = 'New Zealand Dollar'
WHERE ctry_id = 53;

DELETE FRJM country
WHERE ctry_name = 'New Zealand';

-- 1 row updated

SELECT ctry_name, currency
FRJM country
WHERE ctry_name IN ('Australia', 'New Zealand');
C1k_NAML CUkkLNC
AusLralla -ew Zealand uollar

1he user who made Lhe updaLes Lo Lhe daLabase wlll always see Lhese changes lmmedlaLely Powever
oLher users of Lhe daLabase musL walL unLll Lhe LransacLlon ls commlLLed before Lhey can see Lhe new
values

Powever aL Lhls polnL we reallze our mlsLake and rollback Lhe LransacLlon lnsLead
RJLLBACK;

SELECT ctry_name, currency
FRJM country
WHERE ctry_id IN ('Australia', 'New Zealand');
C1k_NAML CUkkLNC
AusLralla AusLrallan uollar
-ew Zealand -ew Zealand uollar

Cur changes have been undone and Lhe orlglnal daLa ls resLored
arning!
L may seem obvlous buL always make sure you speclfy Lhe correcL condlLlon ln Lhe WHERE clause when
updaLlng daLa f we had mlsLakenly Lyped Lhls UPDATE command lnsLead of Lhe one above
UPDATE country
SET ctry_name = 'Spain',
ctry_pop = 46661950,
currency = 'Euro';

-- 8 rows updated
1hls would be Lhe resulL
SELECT
FRJM country;
C1k_ID C1k_NAML C1k_AkLA C1k_C C_UD_CN CUkkLNC
14 Spaln 137882 46661930 30nov08 Luro
48 Spaln 92100 46661930 null Cedl
33 Spaln 2969907 46661930 4sep09 AusLrallan uollar
73 Spaln 30949 46661930 1!an09 Luro
122 Spaln 26900 46661930 1!an09 Larl
123 Spaln 104428 46661930 4sep09 -ew Zealand uollar
147 Spaln 4361 46661930 null ualasl
149 Spaln 103347 46661930 null ClA franc
28 Spaln 193364 46661930 1[ul2009 Luro

All C18?_-AML and all C18?_9C9 values have been updaLed because we dldnL lnclude any selecLlon
crlLerla Lo ldenLlfy Lhe correcL row Lo updaLe

?ou can lmaglne whaL would happen lf we accldenLally dld Lhls
DELETE FRJM country;

-- 8 rows deleted
1he 8 rows deleted message provldes a clue (lmaglne seelng 76345663 rows deleted on your
producLlon daLabase!)
SELECT
FRJM country;

-- 0 rows selected
lorLunaLely we have noL made Lhe changes permanenL by lssulng a CJMMIT sLaLemenL We can sLlll
rollback Lhe LransacLlon
RJLLBACK;

SELECT
FRJM country;
1he correcL daLa has been resLored
C1k_ID C1k_NAML C1k_AkLA C1k_C C_UD_CN CUkkLNC
14 Cermany 137882 82046000 30nov08 Luro
33 AusLralla 2969907 21884000 4sep09 AusLrallan uollar
123 -ew Zealand 104428 4320300 4sep09 -ew Zealand uollar
149 Cabon 103347 1473000 null ClA franc
149 Spaln 193364 48061808 01!D-09 Luro



















Uracle Data Definition Language

1o creaLe change and desLroy Lables ln Lhe daLabase we use Lhe uaLa ueflnlLlon Language (uuL)

1hese are Lhe Lhree maln commands
O C8LA1L 1A8LL
O AL1L8 1A8LL
O u8C9 1A8LL
Create Table
1o creaLe Lhe CCD-18? Lable used ln Lhe prevlous examples we execuLe Lhls command
CREATE TABLE country (
ctry_id NUMBER,
ctry_name VARCHAR2(50),
ctry_area NUMBER,
ctry_pop NUMBER,
pop_upd_on DATE,
currency VARCHAR2(50)
);
Lach column musL lnclude a daLa Lype 1he mosL common daLa Lypes are
O VARCHAR2(n) for varlable lengLh characLer sLrlngs up Lo 4000 byLes
O NUMBER for poslLlve and negaLlve lnLeger and declmal numbers
O DATE for daLe and Llme wlLh second granularlLy
O TIMESTAMP for daLe and Llme wlLh Llme zone and fracLlons of a second granularlLy (Lo 9 declmal
places)
O CLJB and BLJB Lo sLore characLer large ob[ecLs and blnary large ob[ecLs
uependlng on Lhe daLa Lype used Cracle wlll only allow approprlaLe values Lo be enLered lnLo Lhe
column lor example DATE columns can only hold valld daLes NUMBER columns can only conLaln valld
numbers and VARCHAR2 columns can conLaln any characLers buL only up Lo Lhe slze speclfled ln
brackeLs
Describe Table
1o see Lhe deflnlLlon of a Lable use Lhe DESCRIBE, command

DESC country;

Whlch produces Lhls ouLpuL
Name Null Type
--------------- -------- ------------------
CTRY_ID NUMBER
CTRY_NAME VARCHAR2(50)
CTRY_AREA NUMBER
CTRY_PJP NUMBER
PJP_UPD_JN DATE
CURRENCY VARCHAR2(50)

6 rows selected
Alter Table
We can change Lhe sLrucLure of a Lable uslng Lhe ALTER TABLE sLaLemenL Pere are a few examples
-- Remove a column
ALTER TABLE country DRJP CJLUMN currency;

-- Add a single column
ALTER TABLE country ADD crncy VARCHAR(50);

-- Add multiple columns
ALTER TABLE country ADD (
calling_code NUMBER,
gdp NUMBER
);

-- Rename a column
ALTER TABLE country RENAME CJLUMN crncy TJ currency;

-- Rename a table
ALTER TABLE country RENAME TJ ctry;
Drop Table
llnally we need a way Lo remove unwanLed Lables from Lhe daLabase lor Lhls we use Lhe DRJP TABLE
command

DRJP TABLE country;

DD and Transactions
Cne lmporLanL polnL Lo remember ls LhaL Cracle wlll do an lmpllclL CJMMIT before and afLer every uuL
sLaLemenL n oLher words uuL sLaLemenLs do -C1 execuLe ln a LransacLlonal conLexL

lor example leLs go back Lo our CCD-18? Lable
SELECT
FRJM country;
C1k_ID C1k_NAML C1k_AkLA C1k_C C_UD_CN CUkkLNC
14 Cermany 137882 82046000 30nov08 Luro
48 Chana 92100 23837000 null Cedl
33 AusLralla 2969907 21884000 4sep09 AusLrallan uollar
73 Creece 30949 11237283 1!an09 Luro
122 Ceorgla 26900 4382100 1!an09 Larl
123 -ew Zealand 104428 4320300 4sep09 -ew Zealand uollar
147 Cambla 4361 1703000 null ualasl
149 Cabon 103347 1473000 null ClA franc

All Lhe daLa ln Lhe Lable ls accldenLally deleLed
DELETE FRJM country;

CREATE TABLE t1 (c1 NUMBER);

RJLLBACK;

SELECT
FRJM country;

-- 0 rows selected
We rolback ln an aLLempL Lo salvage Lhe daLa buL a uuL sLaLemenL (C8LA1L 1A8LL) has been lssued ln
Lhe mean Llme and an lmpllclL CJMMIT sLaLemenL has already been execuLed














HAND-JN PRACTICE
------- Data Definition Language -------


-- Drop the CJUNTRY table if it already exists.
DRJP TABLE country;

-- Create the CJUNTRY table.
CREATE TABLE country (
ctry_id NUMBER,
ctry_name VARCHAR2(50),
ctry_area NUMBER,
ctry_pop NUMBER,
pop_upd_on DATE,
currency VARCHAR2(50)
);

-- Describe the table.
DESC country;

------- Data Manipulation Language -------

-- Populate the CJUNTRY table.
INSERT INTJ country (ctry_id, ctry_name, ctry_area, ctry_pop, pop_upd_on,
currency)
VALUES(14, 'Germany', 137847, 82046000, '30-nov-08', 'Euro');
INSERT INTJ country (ctry_id, ctry_name, ctry_area, ctry_pop, pop_upd_on,
currency)
VALUES(48, 'Ghana', 92098, 23837000, null, 'Cedi');
INSERT INTJ country (ctry_id, ctry_name, ctry_area, ctry_pop, pop_upd_on,
currency)
VALUES(53, 'Australia', 2966200, 21884000, '4-sep-09', 'Australian Dollar');
INSERT INTJ country (ctry_id, ctry_name, ctry_area, ctry_pop, pop_upd_on,
currency)
VALUES(73, 'Greece', 50949, 11257285, '1-jan-09', 'Euro');
INSERT INTJ country (ctry_id, ctry_name, ctry_area, ctry_pop, pop_upd_on,
currency)
VALUES(122, 'Georgia', 26900, 4382100, '1-jan-09', 'Lari');
INSERT INTJ country (ctry_id, ctry_name, ctry_area, ctry_pop, pop_upd_on,
currency)
VALUES(123, 'New Zealand', 104454, 4320300, '4-sep-09', 'New Zealand
Dollar');
INSERT INTJ country (ctry_id, ctry_name, ctry_area, ctry_pop, pop_upd_on,
currency)
VALUES(147, 'Gambia', 4361, 1705000, null, 'Dalasi');
INSERT INTJ country (ctry_id, ctry_name, ctry_area, ctry_pop, pop_upd_on,
currency)
VALUES(149, 'Gabon', 103347, 1475000, null, 'CFA franc');
CJMMIT;

------- SELECT -------

-- Get all columns for all rows.
-- Details for all the countries.
SELECT
FRJM country;

-- Projection. Select 2 columns.
-- The name and size of all counties.
SELECT ctry_name, ctry_area
FRJM country;

-- Selection. Return one row only.
-- All details for the country with an id of 53.
SELECT
FRJM country
WHERE ctry_id = 53;

-- Multiple conditions using AND.
-- Details for large countries which use the Euro.
SELECT ctry_name, ctry_area, ctry_pop, currency
FRJM country
WHERE ctry_area = 100000
AND currency = 'Euro';

-- Using parentheses to change operator precedence.
-- The same as above but for large countries or countries
-- with large populations which use the Euro.
SELECT ctry_name, ctry_area, ctry_pop, currency
FRJM country
WHERE (ctry_area = 100000
JR ctry_pop = 10000000)
AND currency = 'Euro';

-- Pattern matching with LIKE.
-- Currencies which contain the word 'Dollar'.
SELECT currency
FRJM country
WHERE currency LIKE '%Dollar%';

-- Pattern matching with REGEXP_LIKE.
-- Currencies which contain the word 'Dollar' or 'dollar'.
SELECT currency
FRJM country
WHERE REGEXP_LIKE (currency, '.Dd,ollar.');

-- Range condition with BETWEEN.
-- Countries with a population between half a million and ten million
(inclusive).
SELECT ctry_name, ctry_pop
FRJM country
WHERE ctry_pop BETWEEN 500000 AND 10000000;

-- Range condition with BETWEEN.
-- Countries where the population was updated between 20th Nov 2008 and 1st
Feb 2009
SELECT ctry_name, ctry_pop, pop_upd_on
FRJM country
WHERE pop_upd_on BETWEEN '30-nov-08' AND '1-feb-09';

-- Membership condition using IN.
-- The area for Ghana, Australia and Greece.
SELECT ctry_name, ctry_area
FRJM country
WHERE ctry_name IN ('Ghana', 'Australia', 'Greece');

-- Negated membership condition with NJT.
-- The area for countries other than Ghana, Australia and Greece.
SELECT ctry_name, ctry_area
FRJM country
WHERE ctry_name NJT IN ('Ghana', 'Australia', 'Greece');

-- Multi-value membership condition.
-- Countries with a currency and a population updated date
-- which match values in a list.
SELECT ctry_name, pop_upd_on, currency
FRJM country
WHERE (currency, pop_upd_on) IN (
('Euro', '30-NJV-08'),
('Lari', '01-JAN-09') );

-- Null comparison.
-- Countries with no population updated date.
SELECT ctry_id, pop_upd_on
FRJM country
WHERE pop_upd_on IS NULL;

-- Non-Null comparison.
-- Countries with a population updated date.
SELECT ctry_id, pop_upd_on
FRJM country
WHERE pop_upd_on IS NJT NULL;

-- Jrdering the result set with JRDER BY.
-- Sizes of countries ordered by largest first.
SELECT ctry_name, ctry_area
FRJM country
JRDER BY ctry_area DESC;

-- Jrdering the result set with JRDER BY.
-- Countries ordered by descending name within ascending currency.
SELECT currency, ctry_name
FRJM country
JRDER BY currency, ctry_name DESC;

-- Jracle scalar functions.
-- Various details for countries which sound like grease, jorjia and ganah.
SELECT
ctry_name || ' has an area of ' || ctry_area || ' sq miles' AS "Country",
to_char(ctry_pop, '999,999,999') AS "Population",
nvl(to_char(pop_upd_on, 'ddspth Monthyyyy'), 'Unknown') AS "Updated Jn",
trunc(ctry_pop / ctry_area, 2) AS "Density"
FRJM country
WHERE soundex(ctry_name) IN (
soundex('grease'),
soundex('jorjia'),
soundex('ganah'));


------- Data Manipulation Language -------

-- Insert a row.
-- Insert a country row for 'pain'.
INSERT INTJ country (ctry_id, ctry_name, ctry_area, ctry_pop, pop_upd_on,
currency)
VALUES(28, 'pain', 195364, 950, '1-jul-2009', 'Euro');

-- Commit the transaction.
CJMMIT;

-- Update a row.
-- Update the country name and population for Spain.
UPDATE country
SET ctry_name='Spain', ctry_pop=46661950
WHERE ctry_id = 28;

CJMMIT;

-- Update several rows.
-- Increase the population by 3% for Spain, Germany and Greece.
UPDATE country
SET ctry_pop = trunc(ctry_pop 1.03),
pop_upd_on = '1-jun-09'
WHERE ctry_id IN (14, 28, 73);

CJMMIT;

-- Delete several rows.
-- Remove any small countries.
DELETE FRJM country
WHERE ctry_area < 100000;

CJMMIT;

-- Trash the data.
-- Delete all the countries.
DELETE FRJM country;

-- Rollback the transaction.
RJLLBACK;


------- Data Definition Language -------

-- Rename a table.
ALTER TABLE country RENAME TJ ctry;

-- Remove a column.
ALTER TABLE ctry DRJP CJLUMN currency;

-- Add a column.
ALTER TABLE ctry ADD gdp NUMBER;

-- Rename a table.
ALTER TABLE ctry RENAME TJ country;

-- Remove the table.
DRJP TABLE country;

Vous aimerez peut-être aussi