The Oracle Database System. Connecting to the Database At the command line prompt, write: sqlplus In the beginning your password.

Slides:



Advertisements
Similar presentations
SQL: The Query Language Part 1 R &G - Chapter 5 Life is just a bowl of queries. -Anon (not Forrest Gump)
Advertisements

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Review Session ER and Relational –ER  Relational –Constraints, Weak Entities, Aggregation, ISA Relational Algebra  Relational Calculus –Selections/Projections/Joins/Division.
1 Designing Tables for an Oracle Database System Database Course, Fall 2003.
Introduction to Database Systems 1 SQL: The Query Language Relation Model : Topic 4.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
1 Lecture 11: Basic SQL, Integrity constraints
CS 166: Database Management Systems
SQL: Queries, Constraints, Triggers
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
Database Management Systems 1 Raghu Ramakrishnan SQL: Queries, Programming, Triggers Chpt 5.
SQL: The Query Language Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein and etc for some slides.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
CS 405G: Introduction to Database Systems
CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.
SQL Review.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
1 Reminder We have covered: –Creating tables –Converting ER diagrams to table definitions Today we’ll talk about: –Altering tables –Inserting and deleting.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 90 Database Systems I SQL Queries.
1 SQL (Simple Query Language). 2 Query Components A query can contain the following clauses –select –from –where –group by –having –order by Only select.
1 The Oracle Database System Building a Database Database Course The Hebrew University of Jerusalem.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
Rutgers University SQL: Queries, Constraints, Triggers 198:541 Rutgers University.
SQL: The Query Language Part 1
1 Designing Tables for an Oracle Database System Database Course, Fall 2005.
1 Table Alteration. 2 Altering Tables Table definition can be altered after its creation Adding columns Changing columns’ definition Dropping columns.
1 Table Alteration. 2 Altering Tables Table definition can be altered after its creation –Adding columns –Changing columns’ definition –Dropping columns.
1 Rewriting Minus Queries Using Not In SELECT S.sname FROM Sailors S, Boats B, Reserves R WHERE S.sid = R.sid and R.bid = B.bid and B.color = ‘red’ MINUS.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem.
1 SQL: Structured Query Language Chapter 5. 2 SQL and Relational Calculus relationalcalculusAlthough relational algebra is useful in the analysis of query.
1 Designing Tables for an Oracle Database System Database Course, Fall 2004.
1 Advanced SQL. 2 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name,
CSC343 – Introduction to Databases - A. Vaisman1 SQL: Queries, Programming, Triggers.
Introduction to SQL Basics; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 1 Introduction to SQL Basics --- SQL in 45 Minutes Chapter 5.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L6_SQL(1) 1 SQL: Queries, Constraints, Triggers Chapter 5 – Part 1.
SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby debby lilly.
SQL Examples CS3754 Class Note 11 CS3754 Class Note 11, John Shieh,
Unit 5/COMP3300/ SQL: Queries, Programming, Triggers Chapter 5.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
SQL: Queries, Programming, Triggers. Example Instances We will use these instances of the Sailors and Reserves relations in our examples. If the key for.
ICS 321 Fall 2009 SQL: Queries, Constraints, Triggers Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 9/8/20091Lipyeow.
1 Database Systems ( 資料庫系統 ) October 24, 2005 Lecture #5.
Introduction to SQL ; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 1 Using SQL as a Query Language COSC 6340.
CMPT 258 Database Systems SQL Queries (Chapter 5).
1 SQL: Queries, Constraints, Triggers Chapter 5. 2 Overview: Features of SQL  Data definition language: used to create, destroy, and modify tables and.
CMPT 258 Database Systems Final Exam Review.
1 SQL: The Query Language (Part II). 2 Expressions and Strings v Illustrates use of arithmetic expressions and string pattern matching: Find triples (of.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
SQL: The Query Language Part 1 R &G - Chapter 5 The important thing is not to stop questioning. Albert Einstein.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
SQL CS 186, Spring 2007, Lecture 7 R&G, Chapter 5 Mary Roth   The important thing is not to stop questioning. Albert Einstein Life is just a bowl of.
SQL: The Query Language Part 1 R&G - Chapter 5 1.
SQL. Internet technologies – Ohad © Database  A database is a collection of data  A database management system (DBMS) is software designed to assist.
1 Designing Tables for a Database System. 2 Where we were, and where we’re going The Entity-Relationship model: Used to model the world The Relational.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Basic SQL Queries.
1 CS122A: Introduction to Data Management Lecture 9 SQL II: Nested Queries, Aggregation, Grouping Instructor: Chen Li.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples.
1 CS122A: Introduction to Data Management Lecture 8 Introduction to SQL Instructor: Chen Li.
SQL: The Query Language Part 1 R&G - Chapter 5 Lecture 7 The important thing is not to stop questioning. Albert Einstein.
Basic SQL Queries Go over example queries, like 10 > ALL.
COP Introduction to Database Structures
© פרופ' יהושע שגיב, האוניברסיטה העברית
01/31/11 SQL Examples Edited by John Shieh CS3754 Classnote #10.
Designing Tables for a Database System
DB Review.
Basic SQL Lecture 6 Fall
CS 405G: Introduction to Database Systems
Designing Tables for a postgreSQL Database System
SQL: The Query Language Part 1
Presentation transcript:

The Oracle Database System

Connecting to the Database At the command line prompt, write: sqlplus In the beginning your password is the same as your login. You can change your password with the command: password

Creating a Table The basic format of the CREATE TABLE command is: CREATE TABLE TableName( Column1 DataType1 ColConstraint, … ColumnN DataTypeN ColConstraint, TableConstraint1, … TableConstraintM );

An Example If you issue the command describe Cars you get: Name Null? Type LICENSE NUMBER COLOR VARCHAR2(15) CREATE TABLE Cars( LicenseNUMBER, Color VARCHAR2(15)); Note that the definition is case insensitive

Data Types CHAR(n)String of length n (n <= 2000) VARCHAR2(n)Variable length string of size <= n (n <= 4000) DATEValid dates CLOBCharacter large object (<= 4Gb) NUMBERUp to 40 digits NUMBER(n)Number of size n NUMBER(n,m)Number of size n with m digits after decimal place

Constraints 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

Not Null Constraint CREATE TABLE Employee( SSNNUMBER NOT NULL, Fname VARCHAR2(20), LnameVARCHAR2(20), GenderCHAR(1), SalaryNUMBER(5) NOT NULL, DeptNUMBER );

Default Values CREATE TABLE Employee( SSNNUMBER NOT NULL, Fname VARCHAR2(20), LnameVARCHAR2(20), GenderCHAR(1) DEFAULT(‘F’), SalaryNUMBER(5) NOT NULL, DeptNUMBER );

Unique Constraint CREATE TABLE Employee( SSNNUMBER UNIQUE NOT NULL, Fname VARCHAR2(20), LnameVARCHAR2(20), GenderCHAR(1) DEFAULT(‘F’), SalaryNUMBER(5) NOT NULL, DeptNUMBER, constraint Emp_UQ UNIQUE(Fname, Lname) );

Primary Key Constraint CREATE TABLE Employee( SSNNUMBER PRIMARY KEY, Fname VARCHAR2(20), LnameVARCHAR2(20), GenderCHAR(1) DEFAULT(‘F’), SalaryNUMBER(5) NOT NULL, DeptNUMBER, constraint Emp_UQ UNIQUE(Fname, Lname) ); Primary Key also implies NOT NULL. There can only be one primary key.

Another Table CREATE TABLE Department( DeptNumNUMBER PRIMARY KEY, Name VARCHAR2(20), ManagerIdNUMBER ); Shouldn’t all department numbers in Employee appear in Department ?

Foreign Key Constraint CREATE TABLE Employee( SSNNUMBER PRIMARY KEY, Fname VARCHAR2(20), LnameVARCHAR2(20), GenderCHAR(1) DEFAULT(‘F’), SalaryNUMBER(5) NOT NULL, DeptNUMBER, constraint Emp_UQ UNIQUE(Fname, Lname), FOREIGN KEY (Dept) REFERENCES Department(DeptNum) );

Alternative Notation CREATE TABLE Employee( SSNNUMBER PRIMARY KEY, Fname VARCHAR2(20), LnameVARCHAR2(20), GenderCHAR(1) DEFAULT(‘F’), SalaryNUMBER(5) NOT NULL, DeptNUMBER REFERENCES Department(DeptNum), constraint Emp_UQ UNIQUE(Fname, Lname) );

Understanding 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. What happens to Employee s in department 312 when Department 312 is removed from the Department table?

Deleting 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

Remembering ER-Diagrams PersonCar Owns ssn nameaddress sincelicensecolor model What foreign keys appear naturally in this context? CREATE TABLE Owns( SSNNUMBER REFERENCES Person(SSN), LicenseNUMBER REFERENCES Car(License), SinceDATE);

Cyclic Foreign Keys CREATE TABLE Department( DeptNumNUMBER PRIMARY KEY, Name VARCHAR2(20), ManagerIdNUMBER REFERENCES Employee(SSN) ); Do you see a problem in inserting data now? We should revise the Department table:

Solution to Cyclic Constraints Add one of the constraints later on (after insertion): ALTER TABLE Department ADD(FOREIGN KEY (ManagerId) REFERENCES Employee(SSN));

Check Conditions A check condition is a Boolean expression: –“And”s and “Or”s of conditions of the type X > 5… On a column: it can refer only to the column On a table: it can refer only to multiple columns in the table

Check Constraints CREATE TABLE Employee( SSNNUMBER PRIMARY KEY, Fname VARCHAR2(20), LnameVARCHAR2(20), GenderCHAR(1) DEFAULT(‘F’) CHECK(Gender = ‘F’ or Gender = ‘M’), SalaryNUMBER(5) NOT NULL, CHECK (Gender = ‘M’ or Salary > 10000) );

Deleting a Table To delete the table Employee : DROP TABLE Employee;

Inserting Data Into a Table

Inserting 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 needn’t be specified if values are specified for all columns and in the order defined by the table

Some 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…

Querying the Data

Example Tables Used Reserves sidbidday /10/96 11/12/96 Sailors sidsnameratingage Dustin Lubber Rusty Boats bidbnamecolor Nancy Gloria red green

Basic 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

Basic SQL Query SELECT Distinct A 1,…,A n FROM R 1,…,R m WHERE C; This translates to the expression in relational algebra:  A 1,…,A n (  C (R 1 x…x R m ))

Sailors 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))

SailorsReserves sidsnameratingagesidbidday 22Dustin /10/96 22Dustin /12/96 31Lubber /10/96 31Lubber /12/96 58Rusty /10/96 58Rusty /12/96 Sailors x Reserves  Sailors.sid = Reserves.sid  bid = 103  sname

Range 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

Sailors Who’ve Reserved a Boat SELECT sid FROM Sailors S, Reserves R WHERE S.sid = R.sid; Would adding DISTINCT give a different result?

Expressions 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

Sailors who’ve 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)) What would happen if we replaced or by and ?

Sailors who’ve 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?

The Second Version in Relational Algebra  sid (  color = ‘red’ (Sailors  Reserves  Boats))   sid (  color = ‘green’ (Sailors  Reserves  Boats))

Sailors who’ve 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’;

Nested Queries SELECT S.sid FROM Sailors S WHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid = 103); Names of sailors who’ve reserved boat 103: The SELECT, FROM and WHERE clauses can have sub-queries. They are computed using nested loops. What would happen if we wrote NOT IN?

Correlated Nested Queries SELECT S.sid FROM Sailors S WHERE EXISTS (SELECT * FROM Reserves R WHERE R.bid = 103 and S.sid = R.sid); Names of sailors who’ve reserved boat 103: What would happen if we wrote NOT EXISTS?

More Set-Comparison Queries SELECT * FROM Sailors S1 WHERE S1.age > ANY (SELECT S2.age FROM Sailors S2); Sailors who are not the youngest: We can also use op ALL (op is >, =, ).

Some Small Details

Input 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