Title: The Oracle Database System
1The Oracle Database System
2Connecting to the Database
- At the command line prompt, write
- sqlplus login/password_at_stud.cs
- In the beginning your password is the same as
your login. You can change your password with the
command - password
3Creating a Table
- The basic format of the CREATE TABLE
- command is
- CREATE TABLE TableName(
- Column1 DataType1 ColConstraint,
- ColumnN DataTypeN ColConstraint,
- TableConstraint1,
- TableConstraintM
- )
4An Example
CREATE TABLE Cars( License NUMBER, Color
VARCHAR2(15))
- If you issue the command describe Cars you get
- Name Null? Type
- -------- ----- ------------
- LICENSE NUMBER
- COLOR VARCHAR2(15)
5Data Types
6Constraints in Create Table
- Adding constraints to a table enables the
database system to enforce data integrity. - However, adding constraints also makes inserting
data slower.
- Different types of constraints
- Not Null Default Values
- Unique Primary Key
- Foreign Key Check Condition
7Not Null Constraint
CREATE TABLE Employee( SSN NUMBER NOT
NULL, Fname VARCHAR2(20), Lname VARCHAR2(20
), Gender CHAR(1), Salary NUMBER(5) NOT
NULL, Dept NUMBER )
8Default Values
CREATE TABLE Employee( SSN NUMBER NOT
NULL, Fname VARCHAR2(20), Lname VARCHAR2(20
), Gender CHAR(1) DEFAULT(F), Salary NUMBER(5)
NOT NULL, Dept NUMBER )
9Unique Constraint
CREATE TABLE Employee( SSN NUMBER UNIQUE NOT
NULL, Fname VARCHAR2(20), Lname VARCHAR2(20
), Gender CHAR(1) DEFAULT(F), Salary NUMBER(5)
NOT NULL, Dept NUMBER, constraint Emp_UQ
UNIQUE(Fname, Lname) )
10Primary Key Constraint
CREATE TABLE Employee( SSN NUMBER PRIMARY
KEY, Fname VARCHAR2(20), Lname VARCHAR2(20)
, Gender CHAR(1) DEFAULT(F), Salary NUMBER(5)
NOT NULL, Dept NUMBER, constraint Emp_UQ
UNIQUE(Fname, Lname) )
Primary Key also implies NOT NULL. There can only
be one primary key.
11Another Table
CREATE TABLE Department( DeptNum NUMBER PRIMARY
KEY, Name VARCHAR2(20), ManagerId NUMBER )
12Foreign Key Constraint
CREATE TABLE Employee( SSN NUMBER PRIMARY
KEY, Fname VARCHAR2(20), Lname VARCHAR2(20)
, Gender CHAR(1) DEFAULT(F), Salary NUMBER(5)
NOT NULL, Dept NUMBER, constraint Emp_UQ
UNIQUE(Fname, Lname), FOREIGN KEY (Dept)
REFERENCES Department(DeptNum) )
13Alternative Notation
CREATE TABLE Employee( SSN NUMBER PRIMARY
KEY, Fname VARCHAR2(20), Lname VARCHAR2(20)
, Gender CHAR(1) DEFAULT(F), Salary NUMBER(5)
NOT NULL, Dept NUMBER REFERENCES
Department(DeptNum), constraint Emp_UQ
UNIQUE(Fname, Lname) )
14Understanding Foreign Keys
- The constraint on the last table should be read
as The field Dept in Employee is a foreign key
that references the field DeptNum in Department - Meaning Every non-null value in the field Dept
in Employee must appear in the field DeptNum in
Department.
15Deleting a Referenced Value
- If nothing additional is specified, then Oracle
will - not allow Department 312 to be deleted if there
- are Employees working in this department.
If the constraint is written as FOREIGN KEY
(Dept) REFERENCES Department(DeptNum) ON
DELETE CASCADE then Employees working in 312 will
be deleted automatically from the Employee table
16Remembering ER-Diagrams
name
address
Owns
Person
Car
model
ssn
since
license
color
CREATE TABLE Owns( SSN NUMBER REFERENCES
Person(SSN), License NUMBER REFERENCES
Car(License), Since DATE)
17Cyclic Foreign Keys
We should revise the Department table
CREATE TABLE Department( DeptNum NUMBER PRIMARY
KEY, Name VARCHAR2(20), ManagerId NUMBER
REFERENCES Employee(SSN) )
18Solution to Cyclic Constraints
Add one of the constraints later on (after
insertion) ALTER TABLE Department ADD(FOREIGN
KEY (ManagerId) REFERENCES Employee(SSN))
19Check Conditions
- A check condition is a Boolean expression
- Ands and Ors of conditions of the type X gt 5
- On a column it can refer only to the column
- On a table it can refer only to multiple columns
in the table
20Check Constraints
CREATE TABLE Employee( SSN NUMBER PRIMARY
KEY, Fname VARCHAR2(20), Lname VARCHAR2(20)
, Gender CHAR(1) DEFAULT(F)
CHECK(Gender F or
Gender M) , Salary NUMBER(5) NOT
NULL, CHECK (Gender M or Salary gt 10000) )
21Deleting a Table
- To delete the table Employee
- DROP TABLE Employee
22Inserting Data Into a Table
23Inserting a Row
- To insert a row into the Employee table
- INSERT INTO
- Employee(SSN, Fname, Lname, Salary)
- VALUES(121, Sara, Cohen, 10000)
- The remaining columns get default values (or
NULL) - The fields neednt be specified if values are
specified for all columns and in the order
defined by the table
24Some More Details
- An example of inserting into the Owns table
- INSERT INTO Owns
- VALUES(121, 4545, 01-DEC-02)
- We can also use the Oracle Loader Details in Ex2
25Querying the Data
26Example Tables Used
27Basic SQL Query
SELECT Distinct target-list FROM
relation-list WHERE condition
- relation-list A list of relation names (possibly
with a range-variable after each name) - target-list A list of fields of relations in
relation-list - condition A Boolean condition
- DISTINCT Optional keyword to delete duplicates
28Basic SQL Query
SELECT Distinct A1,,An FROM R1,,Rm WHERE C
This translates to the expression in relational
algebra ?A1,,An (?C(R1 xx Rm))
29Sailors Who Reserved Boat 103
SELECT sname FROM Sailors, Reserves WHERE
Sailors.sid Reserves.sid and bid 103
?sname(?Sailors.sid Reserves.sid ? bid 103
(Sailors x Reserves))
30Sailors x Reserves
31Range Variables
SELECT S.sname FROM Sailors S, Reserves R
WHERE S.sid R.sid and R.bid 103
- Range variables are good style.
- They are necessary if the same relation appears
twice in the FROM clause
32Sailors Whove Reserved a Boat
SELECT sid FROM Sailors S, Reserves R WHERE
S.sid R.sid
33Expressions and Strings
SELECT age, (age-5)2 as age1 FROM Sailors,
WHERE sname LIKE B_B
- Expressions in SELECT clause
- Renaming of column in result
- String comparison _ is a single character and
is 0 or more characters
34Sailors whove reserved a red or green boat
SELECT S.sid FROM Sailors S, Boats B, Reserves
R WHERE S.sid R.sid and R.bid B.bid
and (B.color red or B.color green)
?sid(?color red ? color green (Sailors
?? Reserves ?? Boats))
35Sailors whove reserved red or green boat
SELECT S.sid FROM Sailors S, Boats B, Reserves
R WHERE S.sid R.sid and R.bid B.bid
and B.color red UNION SELECT S.sid FROM
Sailors S, Boats B, Reserves R WHERE S.sid
R.sid and R.bid B.bid and B.color
green
What would happen if we wrote MINUS? Or INTERSECT?
36The Second Version in Relational Algebra
?sid(?color red (Sailors ?? Reserves ??
Boats)) ? ?sid(?color green (Sailors ??
Reserves ?? Boats))
37Sailors whove reserved red and green boat
SELECT S.sid FROM Sailors S, Boats B1, Reserves
R1, Boats B2, Reserves R2 WHERE S.sid
R1.sid and R1.bid B1.bid and B1.color
red and S.sid R2.sid and R2.bid B2.bid
and B2.color green
38Nested Queries
Names of sailors whove reserved boat 103
SELECT S.sid FROM Sailors S WHERE S.sid IN
(SELECT R.sid FROM Reserves R WHERE
R.bid 103)
What would happen if we wrote NOT IN?
The SELECT, FROM and WHERE clauses can have
sub-queries. They are computed using nested
loops.
39Correlated Nested Queries
Names of sailors whove reserved boat 103
SELECT S.sid FROM Sailors S WHERE EXISTS
(SELECT FROM Reserves R WHERE R.bid
103 and S.sid R.sid)
What would happen if we wrote NOT EXISTS?
40More Set-Comparison Queries
Sailors who are not the youngest
SELECT FROM Sailors S1 WHERE S1.age gt ANY
(SELECT S2.age FROM Sailors S2)
We can also use op ALL (op is gt, lt, , gt, lt, or
ltgt).
41Some Small Details
42Input and Output Files
- Commands can be put in a file and then read into
Oracle - start commands.sql
- Output can be placed in a file
- spool commands.out
- Spooling can be turned off with
- spool off