Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
DESCRIBE Command
The DESCRIBE command displays the structure of the table. The syntax is:
DESCRIBE <table name>;
atau
DESC <table name>;
DESC returns the table name, table schema, tablespace name, indexes, triggers, c
onstraints, and comments, as well as the data types, primary and foreign keys, a
nd nullable columns.
Basic SELECT Statement
The SELECT * command returns all the rows in a table. The syntax is:
SELECT *
FROM <table name>;
Select Statement with a Condition
To return a subset of the data, modify the SELECT statement. The syntax is:
SELECT <column name 1, column name 2, etc.>
FROM <table name>
WHERE <condition>;
Inserting Data
Using the INSERT command, you can add a row of data to the table. The syntax is:
INSERT INTO <table name>
VALUES (value 1, value 2, value 3, etc);
S12_L03
ALTER TABLE Syntax
To add a new column to a table, use the ALTER TABLE command. The syntax is:
ALTER TABLE <table name>
ADD (<new_column_name> <data type>);
Drop a Column
To drop a column from a table, use the ALTER TABLE command. The syntax is:
ALTER TABLE <table name>
DROP COLUMN <column_name>;
Delete a Row
To delete a row from a table, use the DELETE command. The syntax is:
DELETE from <table name>
WHERE <column_name> = 'some value ;
S15_L01
SELECT Statement
The SELECT statement retrieves information from the database. The syntax for a S
ELECT statement is as follows:
SELECT <column_name(s)>
FROM <table_name>;
In its simplest form, a SELECT statement must include the following:
A SELECT clause, which specifies the columns to be displayed
A FROM clause, which specifies the table containing the columns listed in the SEL
ECT clause
Capabilities of SELECT Statements
Projection: Used to choose columns in a table -> SELECT
Selection: Used to choose rows in a table -> WHERE
Join: Used to bring together data that is stored in different tables by creating
a link between them.
Precedence in Arithmetic Operators
Arithmetic operators perform the mathematical operations of Multiplication, Divi
sion, Addition, and Subtraction. If these operators appear together in an expres
sion, multiplication and division are evaluated first. So the order is: * / + -.
An easy way to remember their operator precedence is the mnemonic device:
My Dear Aunt Sally
NULL Values: Unknown
->yang dioperasikan dengan NULL maka hasilnya NULL
Aliases
There are several rules when using column aliases to format output.
A column alias:
Renames a column heading
Is useful with calculations
Immediately follows the column name
May have the optional AS keyword between the column name and alias
Requires double quotation marks (" ") if the alias contains spaces or special cha
racters, or is case-sensitive
S16_L01
The Concatenation Operator
Concatenation means to connect or link together in a series. The symbol for conc
atenation is 2 vertical bars sometimes referred to as pipes. Values on either side
of the || operator are combined to make a single output column. The syntax is:
string1 || string2 || string_n
When values are concatenated, the resulting value is a character string.
SELECT department_id || ' ' ||department_name
FROM departments;
Concatenation and Literal Values
A literal value is a fixed data value such as a character, number, or date. The
following are examples of literal values:
dollars
1000
January 1, 2009
Using concatenation and literal values, you can create output that looks like a
sentence or statement.
SELECT last_name ||' has a monthly salary of ' || salary || ' dollars.' AS Pay F
ROM employees;
Using DISTINCT to Eliminate Duplicate Rows
SELECT DISTINCT department_id
FROM employees;
S16_L02
Comparison Operators in the WHERE Clause
The = sign can be used in the WHERE clause. In addition to the equal to operator (
=), other comparison operators can be used to compare one expression to another:
= equal to
> greater than
>= greater than or equal to
< less than
rue.
AND -- Returns TRUE if both conditions are true.
OR -- Returns TRUE if either condition is true.
NOT -- Returns TRUE if the condition is false.
ORDER
1
2
3
4
5
6
7
8
OPERATORS
Arithmetic + - * /
Concatenation ||
Comparison <, <=, >, >=, <>
IS (NOT) NULL, LIKE, (NOT) IN
(NOT) BETWEEN
NOT
AND
OR
S17_L02
ORDER BY Clause
The default sort order is ascending.
Numeric values are displayed lowest to highest.
Date values are displayed with the earliest value first.
Character values are displayed in alphabetical order.
Null values are displayed last in ascending order and first in descending order.
NULLS FIRST Specifies that NULL values should be returned before non-NULL values.
NULLS LAST Specifies that NULL values should be returned after non-NULL values.
SELECT title, year
FROM d_cds
ORDER BY year DESC;
You can order data by using a column alias. The alias used in the SELECT stateme
nt is referenced in the ORDER BY clause.
SELECT title, year AS "Recording Date"
FROM d_cds
ORDER BY "Recording Date";
Order of Execution
The order of execution of a SELECT statement is as follows:
1.FROM clause: locates the table that contains the data
2.WHERE clause: restricts the rows to be returned
3.SELECT clause: selects from the reduced data set the columns requested
4.ORDER BY clause: orders the result set
Sorting with Multiple Columns
To create an ORDER BY clause to sort by multiple columns, specify the columns to
be returned and separate the column names using commas. If you want to reverse
the sort order of a column, add DESC after its name.
SELECT title, year
FROM d_cds
ORDER BY year DESC, title;
S17_L03
Oracle has two distinct types of functions:
Single-Row (Single-row Input, Single Output)
Multiple-Row (Multiple-row Input, Single Output)/Group Functions
----------------------------------------------------------------Single-row function bisa di:
SELECT, WHERE, ORDER BY
Character Functions:
A. Case Manipulation Functions (hanya 1 parameter):
-LOWER
-UPPER
-INITCAP
B. Character Manipulation Functions:
-CONCAT (2 parameter yang akan digabungkan)
-SUBSTR (3 parameter: string_asal, indeks_karakter_mulai, sebanyak_brp_karakter)
-LENGTH (1 paramater)
-INSTR (2 parameter: string_asal, karakter_yg_dicari) ->posisi brp
-LPAD (3 parameter: string_asal, total_jml_karakter, karakter_utk_melengkapi_sd_
total_jml_karakter)
-RPAD (3 parameter: string_asal, total_jml_karakter, karakter_utk_melengkapi_sd_
total_jml_karakter)
-TRIM (karakter_yang_ingin_diremove FROM string_asal)
-REPLACE (3 parameter: string_asal, string_yang_akan_di_replace, replacement_str
ing)
Substitution Variables
dengan menambahkan :variable_name supaya nilai yang dicari di bagian WHERE sesua
i input user
misal where last_name= :l_name
Number Functions:
1. ROUND (2 parameter: column/expression, decimal_place)
Jika decimal_place tidak dispesifikasikan, maka sama dengan nol
2. TRUNC (2 parameter: column/expression, decimal_place)
Jika decimal_place tidak dispesifikasikan, maka sama dengan nol
3. MOD (2 parameter)
Date Functions: (Format: DD/MON/YYYY)
-SYSDATE
-MONTHS_BETWEEN (selisih brp bulan, 2 parameter:tgl1, tgl2)
-ADD_MONTHS (menambah brp bulan, 2 parameter: tgl, brp_bulan)
-NEXT_DAY (next day dari specified date, 2 parameter: tgl, nama_hari)
-LAST_DAY (last day dari bulan, 1 parameter: tgl)
-ROUND (2 parameter: tgl, 'MONTH'/'DATE')
-TRUNC (2 parameter: tgl, 'MONTH'/'DATE')
Conversion Functions:
-TO_CHAR (2 parameter: column/expression, format)
-TO_NUMBER (2 parameter: column/expression, format)
-TO_DATE (2 parameter: column/expression, format)
NULL Functions:
-NVL (2 parameter: column/expression, nilai_pengganti_jika_null)
-NVL2 (3 parameter: column/expression, nilai_pengganti_jika_tidak_null, nilai_pe
ngganti_jika_null)
-NULLIF (2 parameter: expression1, expression2)
Jika kedua eskpresi sama, return null. Jika tidak sama, return expression1
-COALESCE(expression1, expression2, ..., expression-n)
Return expression sampai ditemukan expression yg tidak null
Conditional Expressions:
-CASE
CASE expr WHEN comparison_expr1 THEN return_expr1
[WHEN comparison_expr2 THEN return_expr2
WHEN comparison_exprn THEN return_exprn
ELSE else_expr]
END
-DECODE
DECODE(columnl|expression, search1, result1
[, search2, result2,...,]
[, default])
JOIN ANSI&Oracle
-Cross Join
-----ANSI
-----SELECT name, event_date, loc_type, rental_fee
FROM d_events CROSS JOIN d_venues;
-----Oracle
-----SELECT name, event_date, loc_type, rental_fee
FROM d_events, d_venues;
-Equi Join
-----ANSI
-----Natural Join (nama kolom dan tipe data sama):
SELECT p.song_id , t.song_id
FROM d_play_list_items p NATURAL JOIN d_track_listings t
--------------------------------------------------------USING Clause (nama kolom sama, tipe data berbeda):
SELECT p.song_id , t.song_id
FROM d_play_list_items p JOIN d_track_listings t
USING(song_id);
--------------------------------------------------------ON Clause (nama kolom berbeda)
SELECT p.song_id , t.song_id
FROM d_play_list_items p JOIN d_track_listings t
ON (p.song_id = t.id_song);
-----Oracle
-----SELECT p.song_id , t.song_id
FROM d_play_list_items p, d_track_listings t
WHERE p.song_id = t.song_id;
-Non Equi Join
-----ANSI
-----SELECT p.code, e.cost
FROM d_packages p JOIN d_events e
ON (e.cost BETWEEN p.low_range AND p.high_range)
-----Oracle
-----SELECT p.code, e.cost
FROM d_packages p, d_events e
WHERE e.cost BETWEEN p.low_range AND p.high_range
-Outer Join
LEFT:
-----ANSI