Structured Query Language: Oracle Tutorials
Structured Query Language: Oracle Tutorials
Oracle Tutorials
SQL
Structured Query Language
(1/2)
Giacomo Govi
IT/ADC
25 January 2005
CERN-IT/ADC
Overview
• Goal:
– Learn the basic for interacting with a RDBMS
• Outline
– SQL generalities
– Available statements
– Restricting, Sorting and Aggregating data
– Manipulating Data from different tables
– SQL Functions
SQL (1/2) 2
25 January 2005
[email protected]
CERN-IT/ADC
SQL Definition
SQL (1/2) 3
25 January 2005
[email protected]
CERN-IT/ADC
SQL as RDBMS interface
SQL provides statements for a variety of tasks, including:
Data Definition
• Creating, replacing, altering, and dropping objects
Data Manipulation
• Querying data
• Inserting, updating, and deleting rows in a table
Data Control
• Controlling access to the database and its objects
• Guaranteeing database consistency and integrity
SQL (1/2) 4
25 January 2005
[email protected]
CERN-IT/ADC
Available statements
Statement Description
SELECT Data retrieval
INSERT
UPDATE Rows Data Manipulation Language (DML)
DELETE
CREATE
ALTER
Tables/Objects
DROP Data Definition Language (DDL)
RENAME
TRUNCATE
COMMIT
Manages
ROLLBACK DML Transaction Control
SAVEPOINT
GRANT
Data Control Language (DCL)
REVOKE
SQL (1/2) 5
25 January 2005
[email protected]
CERN-IT/ADC
SQL & Tools
• Benthic Software
to install it, refer to:
G:\Applications\Benthic\Benthic_license_CERN.html
http://www.benthicsoftware.com/
SQL (1/2) 7
25 January 2005
[email protected]
CERN-IT/ADC
Datatypes
SQL (1/2) 8
25 January 2005
[email protected]
CERN-IT/ADC
Oracle Built-in Datatypes
CHAR (size) fixed-length char array
VARCHAR2(size) Variable-length char string
SQL (1/2) 9
25 January 2005
[email protected]
CERN-IT/ADC
ANSI Data types translation
ANSI data type Oracle
integer NUMBER(38)
smallint NUMBER(38)
numeric(p,s) NUMBER(p,s)
varchar(n) VARCHAR2(n)
char(n) CHAR(n)
datetime DATE
float NUMBER
real NUMBER
SQL (1/2) 10
25 January 2005
[email protected]
CERN-IT/ADC
NULL value
NULL is a special value that means:
– unavailable
– unassigned
– unknown
– inapplicable
NULL value is not equivalent to
– zero
– blank space
SQL (1/2) 11
25 January 2005
[email protected]
CERN-IT/ADC
Schema
SQL (1/2) 12
25 January 2005
[email protected]
CERN-IT/ADC
Schema objects
-Tables
-Indexes
-Constraints
… but also (in ORACLE)
-Links
-Views
-Triggers
-Operators
-Sequences
-Stored functions
-Stored procedures
-Synonyms
…and more
SQL (1/2) 13
25 January 2005
[email protected]
CERN-IT/ADC
Basic SQL
Aim: be able to perform the basic operation of the
RDBMS data model:
SQL (1/2) 14
25 January 2005
[email protected]
CERN-IT/ADC
Create a table
SQL (1/2) 15
25 January 2005
[email protected]
CERN-IT/ADC
Create (and describe) a table
CREATE TABLE employees (
id NUMBER(4),
surname VARCHAR2(50),
name VARCHAR2(100),
hiredate DATE DEFAULT SYSDATE,
division VARCHAR2(20),
email VARCHAR2(20),
citizenship VARCHAR2(20)
);
SQL (1/2) 17
25 January 2005
[email protected]
CERN-IT/ADC
Create a relational table
CREATE TABLE employees (
id NUMBER(4) NOT NULL,
surname VARCHAR2(50) NOT NULL,
name VARCHAR2(100) NOT NULL,
hiredate DATE DEFAULT SYSDATE,
div_id NUMBER(2),
email VARCHAR2(20) UNIQUE,
cit_id NUMBER(3),
CONSTRAINT emp_pk PRIMARY KEY (id),
CONSTRAINT emp_div_fk FOREIGN KEY(div_id)
REFERENCES divisions(id),
CONSTRAINT emp_email_un UNIQUE(email)
);
SQL (1/2) 18
25 January 2005
[email protected]
CERN-IT/ADC
Object identifiers
SQL (1/2) 19
25 January 2005
[email protected]
CERN-IT/ADC
Coding Conventions
SQL (1/2) 20
25 January 2005
[email protected]
CERN-IT/ADC
Alter table
Modify the name:
ALTER TABLE employees RENAME TO newemployees;
Modify the layout:
ALTER TABLE employees ADD (salary NUMBER(7));
ALTER TABLE employees RENAME COLUMN div_id TO
dep_id;
ALTER TABLE employees DROP (hiredate);
But also:
• Add/modify/drop constraints
• Enable/Disable constraints
• Modify more advanced properties…
SQL (1/2) 21
25 January 2005
[email protected]
CERN-IT/ADC
Drop table
Remove the table from the user schema (recoverable in
Oracle10g):
DROP TABLE employees;
->effects: the table is removed (or moved in the recycle bin) with
all its data, and dependencies (indexes, etc…)
SQL (1/2) 22
25 January 2005
[email protected]
CERN-IT/ADC
Insert data in a table
Data are added in a table as new rows
SQL (1/2) 25
25 January 2005
[email protected]
CERN-IT/ADC
Aggregating data
SQL (1/2) 26
25 January 2005
[email protected]
CERN-IT/ADC
Group functions
SQL (1/2) 27
25 January 2005
[email protected]
CERN-IT/ADC
Set operators
Combine multiple queries
Union without duplicates:
SELECT name, email FROM employees UNION
SELECT name, email FROM visitors;
Union with the whole row set:
SELECT cit_id FROM employees UNION ALL
SELECT cit_id FROM visitors;
Intersect:
SELECT name FROM visitors INTERSECT
SELECT name FROM former_employees;
Minus:
SELECT name FROM visitors MINUS
SELECT name FROM former_employees;
SQL (1/2) 28
25 January 2005
[email protected]
CERN-IT/ADC
Restricting and sorting data
• Need to restrict and filter the rows of data that are
displayed and/or specify the order in which these rows
are displayed
– ORDER BY
SQL (1/2) 29
25 January 2005
[email protected]
CERN-IT/ADC
Restricting data selection (I)
Filter the rows according to specified condition
Simple selections:
SELECT * FROM employees WHERE id = 30;
SELECT name FROM employees WHERE NOT div_id =
2;
SELECT name FROM employees WHERE salary > 0;
SELECT * FROM employees WHERE hiredate <
TO_DATE(‘01-01-2000', ‘DD-MM-YYYY');
SELECT name FROM employees WHERE email IS
NULL;
More Conditions (AND/OR):
SELECT * FROM employees WHERE div_id = 20 AND
hiredate > TO_DATE(‘01-01-2000',
‘DD-MM-YYYY');
SQL (1/2) 30
25 January 2005
[email protected]
CERN-IT/ADC
Restricting data selection (II)
More selection operators
Use of wildcards
SELECT * FROM employees WHERE name LIKE ‘C%’;
Ranges
SELECT count(*) FROM employees WHERE salary
BETWEEN 1000 and 2000;
Selection from a list
SELECT * FROM employees WHERE div_id IN
(4,9,12);
List from an other selection
SELECT name FROM divisions WHERE id IN (SELECT
div_id FROM employees WHERE salary > 2000);
SQL (1/2) 31
25 January 2005
[email protected]
CERN-IT/ADC
Sorting selected data
SQL (1/2) 32
25 January 2005
[email protected]
CERN-IT/ADC
Aggregating Clauses
Divide rows in a table into smaller groups:
SELECT column, group_function(column) FROM table
[WHERE condition] GROUP BY group_by_expression;
Example:
SELECT div_id, MIN(salary), MAX (salary) FROM
employees GROUP BY div_id;
• All columns in the SELECT that are not in the group function must be
included in the GROUP BY clause
• GROUP BY column does not have to be in the SELECT
Restrict the groups:
SELECT div_id, MIN(salary), MAX (salary) FROM
employees GROUP BY division HAVING MIN(salary)
< 5000;
SQL (1/2) 33
25 January 2005
[email protected]
CERN-IT/ADC
Update data in a table
Aim: change existing values in a table
With no clause all the rows will be updated:
UPDATE employees SET salary=1000;
A single result select can be used for update:
UPDATE employees SET salary=(SELECT MAX(salary));
The previous value can be used for the update:
UPDATE employees SET salary=salary+1000;
In order to update a specific row(s), a WHERE clause can be provided:
UPDATE employees SET salary=5000 WHERE
name=smith;
UPDATE employees SET salary=5000 WHERE div_id=3;
The syntax for the WHERE clause is the same as for the SELECT
statements…
SQL (1/2) 34
25 January 2005
[email protected]
CERN-IT/ADC
Delete data from a table
SQL (1/2) 35
25 January 2005
[email protected]
CERN-IT/ADC
Manipulating data from more tables
SQL (1/2) 36
25 January 2005
[email protected]
CERN-IT/ADC
Types of join
Outerjoin It returns also the rows that does not satisfy the join
condition
SQL (1/2) 37
25 January 2005
[email protected]
CERN-IT/ADC
Equijoin
Foreign Key
EMP.NAME EMP.DIV_ID
KING 10 Primary Key
BLAKE 30 DIV.ID DIV.NAME
10 ACCOUNTING
CLARK 10
30 SALES
20 OPERATIONS
SQL (1/2) 38
25 January 2005
[email protected]
CERN-IT/ADC
Outerjoin
Foreign Key
EMP.NAME EMP.DIV_ID Primary Key
KING 10 DIV.ID DIV.NAME
CLARK 10 30 SALES
MARTIN 20 20 OPERATIONS
TURNER 10
JONES NULL
EMP.NAME EMP.DIV_ID DIV.NAME
KING 10 ACCOUNTING
CLARK 10 ACCOUNTING
MARTIN 20 OPERATIONS
TURNER 10 ACCOUNTING
JONES NULL NULL
SQL (1/2) 39
25 January 2005
[email protected]
CERN-IT/ADC
Join Examples Syntax
Equijoins:
ANSI syntax:
SELECT employees.name, divisions.name FROM employees INNER
JOIN divisions ON employees.div_id=divisions.id;
Oracle:
SELECT employees.name, divisions.name FROM employees,
divisions WHERE employees.div_id=divisions.id;
Outerjoins:
ANSI syntax (LEFT,RIGHT,FULL)
SELECT employees.name, divisions.name FROM employees
FULL OUTER JOIN divisions
ON employees=division.id;
Oracle:
SELECT employees.name, divisions.name FROM employees,
divisions WHERE employees.div_id(+)=divisions.id;
SQL (1/2) 40
25 January 2005
[email protected]
CERN-IT/ADC
SQL Functions
Oracle provides a set of SQL functions for
manipulation of column and constant values
Type Functions
CHAR concat, length, lower, upper, trim, substr
SQL (1/2) 41
25 January 2005
[email protected]
CERN-IT/ADC
Character manipulation Functions
String concatenation:
SELECT CONCAT(CONCAT(name, ‘ email is '), email)
FROM employees WHERE id = 152;
String length:
SELECT LENGTH(email) FROM employees WHERE
citizenship = 5;
Set the Case (LOWER/UPPER):
SELECT CONCAT(LOWER(name),’@cern.ch’) FROM
employees;
More operators:
TRIM,LTRIM,RTRIM Remove characters from the string start/end
SUBSTR Extract a specific portion of the string
SQL (1/2) 42
25 January 2005
[email protected]
CERN-IT/ADC
Numeric functions (I)
SQL Function for numeric types (column value or expression):
ABS(p)
• Returns the absolute value of the column or the expression
CEIL(p)
• Returns the smalles integer greater then or equal to the parameter value
FLOOR(p)
• Returns largest integer equal to or less than the parameter value
MOD(m, n)
• Returns the remainder of m divided by n (or m if n is 0)
POWER(p, n)
• Returns p raised to the nth power
SQL (1/2) 43
25 January 2005
[email protected]
CERN-IT/ADC
Numeric functions (II)
ROUND(p,n)
• Returns p rounded to n places to the right of the decimal point (default
n=0)
SIGN(p)
• Returns the sign of p
SQRT(p)
• Returns the square root of p.
TRUNC(m, n)
• Returns n truncated to m decimal places
POWER(m, n)
• Returns m raised to the nth power (default n=0)
SQL (1/2) 46
25 January 2005
[email protected]
CERN-IT/ADC
The DUAL table
Table automatically created by Oracle Database in the
schema of SYS user.
• Accessible (read-only) to all users.
By selecting from the DUAL table one can:
• Compute constant expressions with functions:
SELECT ABS(-15) FROM DUAL;
ABS(-15)
----------
15
• Retrieve some Environment parameters:
SELECT UID, USER FROM DUAL;
UID USER
--------- -------------
578 GOVI
SQL (1/2) 47
25 January 2005
[email protected]
CERN-IT/ADC
Summary
• What is SQL, for what and how do we use it
• ANSI and Oracle-specific SQL Datatypes
• User’s schema
• Basic SQL for :
- Create, Modify, Delete a table
- Insert data into a table
- Select data from one or more tables with/without conditions
- Update or delete data from a table
• SQL functions
• The Oracle DUAL table
SQL (1/2) 48
25 January 2005
[email protected]
CERN-IT/ADC
Documentation
• Oracle SQL: The essential reference
David Kreines, Ken Jacobs
O'Reilly & Associates; ISBN: 1565926978; (October 2000)
• http://otn.oracle.com
SQL (1/2) 49
25 January 2005
[email protected]
CERN-IT/ADC
SQL (1/2) 50
25 January 2005
[email protected]