1 General Form of the SELECT Statement SELECT [DISTINCT | ALL {* | col_expr [AS new_name] [,…]} FROMtable_name [,…] [,…] [WHERE condition] [GROUP BY col_list]

Slides:



Advertisements
Similar presentations
SQL. More than 100 DBMS support SQL –Used by DBAs and application programmers –Structured Query Language or SEQUEL –ORACLE-> relation database based on.
Advertisements

February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL.
RELATIONAL DATABASES. Relational data Structure RELATION: Table with columns and rows ATTRIBUTE: Column of a relation DOMAIN: Set of allowable values.
1 Constraints, Triggers and Active Databases Chapter 9.
CSC271 Database Systems Lecture # 11.
CSC271 Database Systems Lecture # 13. Summary: Previous Lecture  Grouping through GROUP BY clause  Restricted groupings  Subqueries  Multi-Table queries.
Branch (Bno, Street, Area, City, Pcode, Tel_No, Fax_NO) Staff (Sno, FName, LName, Address, Tel_No, Position, Sex, DOB, Salary, NIN, Bno) Property_for_Rent.
1 Minggu 4, Pertemuan 8 SQL: Data Manipulation (Cont.) Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
Chapter 6 SQL: Data Manipulation Cont’d. 2 ANY and ALL u ANY and ALL used with subqueries that produce single column of numbers u ALL –Condition only.
Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database.
Relational Database Management System A type of database in which records are stored in relational form is called relational database management system.
LOGICAL DATABASE DESIGN
Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial Password: UWPstudent Password is case sensitive.
Relational Model & Relational Algebra. 2 Relational Model u Terminology of relational model. u How tables are used to represent data. u Connection between.
Lecture 17 Object relational databases. 2 Example Table Creation based on UDT CREATE TABLE Person ( info PersonType, CONSTRAINT DOB_Check CHECK(dateOfBirth.
Chapter 6 SQL Data Definition Language Chapter 7 in Textbook.
Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform.
SQL: Overview SQL Overview © Pearson Education Limited 1995, 2005.
CSC271 Database Systems Lecture # 12. Summary: Previous Lecture  Row selection using WHERE clause  WHERE clause and search conditions  Sorting results.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
Chapter 7 SQL: Data Manipulation Chapter#6 in the text book Pearson Education © 2009.
Chapter 6 SQL: Data Manipulation (Advanced Commands) Pearson Education © 2009.
1 The Relational Database Model. 2 Learning Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical.
SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.
Chapter 5 SQL Data Manipulation Language Chapter 5 in Textbook.
Chapter 5 SQL: Data Manipulation © Pearson Education Limited 1995, 2005.
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
1 Pertemuan > > Matakuliah: >/ > Tahun: > Versi: >
Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Chapter 7 SQL: Data Manipulation Chapter #6 in the textbook Pearson Education © 2009.
CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Objectives Database triggers and syntax
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Chapter Name SQL: Data Definition
IST 210 SQL Todd Bacastow IST 210: Organization of Data.
Chapter 8 Introduction to SQL
1 DBS201: More on SQL Lecture 3. 2 Agenda How to use SQL to update table definitions How to update data in a table How to join tables together.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 9 Database Triggers.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Chapter 4 Practice Problem Solutions. A list (Bno, Street, Area,Tel_No, Fax_No) of branches located in Bay Area Branch (Bno, Street, Area, City, Pcode,
Branch (Bno, Area, City, Pcode) Staff (Sno, FName, LName, Position, Sex, DOB, Salary, Bno) Property_for Rent (Pno, Area, City, Type, Rooms, Rent, Ono,
Data Definition Language
IST 210 More SQL Todd Bacastow IST 210: Organization of Data.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
SQL: Additional Notes. 2 Example 5.3 Use of DISTINCT List the property numbers of all properties that have been viewed. SELECT propertyNo FROM Viewing;
Chapter 6 SQL: Data Manipulation Pearson Education © 2009.
Teacher Workshop Database Design Pearson Education © 2014.
Chapter 11 SQL: Data Manipulation
國立臺北科技大學 課程:資料庫系統 Chapter 7 SQL Data Definition.
CS 3630 Database Design and Implementation
Data Manipulation Language
Minggu 5, Pertemuan 9 SQL: Data Definition
Introduction to SQL Chapter 3.
The Relational Database Model
Chapter Name SQL: Data Manipulation Chapter #6 in the textbook
Chapter Name SQL: Data Manipulation Chapter #6 in the textbook
Chapter Name SQL: Data Manipulation
SQL – Data Manipulation
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Chapter Name SQL: Data Manipulation
Chapter Name SQL: Data Manipulation Transparencies JOINS
Chapter Name SQL: Data Manipulation Transparencies
Prof. Arfaoui. COM390 Chapter 9
Presentation transcript:

1 General Form of the SELECT Statement SELECT [DISTINCT | ALL {* | col_expr [AS new_name] [,…]} FROMtable_name [,…] [,…] [WHERE condition] [GROUP BY col_list] [HAVING condition] [ORDER BY col_list] The order of the clauses of the SELECT statement cannot be changed The order of the col_exp of the SELECT statement is not significant

2 The Rental Database Branch(Bno, Street, Area, City, Pcode, Tel_No, Fax_No Staff(Sno, FName, LName, Address, Tel_No, Position, Sex, DOB, Salary, Bno) Prop_For_Rent (Pno, Stree, Area, City, Pcode, Type, Rooms, Rent, Ono, Sno, Bno) Renter(Rno, FName, LName, Address, Tel_No, Pref_Type, Max_Rent) Owner(Ono, FName, LName, address, Tel_No) Viewing(Rno, Pno, Date, Comment)

3 Foreign Keys Referential Integrity NO ACTION--Prevents deletion from the parent table if there are references to child table CASCADE--When parent occurrence is deleted, automatically delete any child occurrences SET NULL--When parent occurrence deleted, the foreign key values in all of its child occurrences are set to NULL. SET DEFAULT--When a parent occurrence is deleted, the foreign key values in all of the child occurrences are set to their default (requires setting default value) NO CHECK--When a parent occurrence is deleted, do nothing to ensure that referential integrity is maintained.

4 Foreign Key Syntax PRIMARY KEY (...), FOREIGN KEY (…) REFERENCES tablename ON DELETE … ON UPDATE...

5 Group By Find the number of staff working in each branch and the total of their salaries SELECT bno, COUNT(Sno) as count, SUM(salary) AS sum FROM staff GROUP BYbno ORDER BYbno;

6 Nested Queries Find the number of staff working in each branch and the total of their salaries SELECT bno,(SELECT COUNT(sno) AS count FROM staff s WHERE s.bno = b.bno), (SELECT SUM(salary) AS sum FROM staff s WHERE s.bno=b.bno) FROM branch b ORDER BY bno;

7 Subqueries Can be used in WHERE or HAVING clauses List the properties that are handled by staff that work at the branch SELECT pno, street, area, city, pcode, type, rooms, rent FROM Prop_For_Rent WHERE sno IN (SELECT sno FROM staff WHERE bno = (SELECT bno FROM branch WHERE street LIKE ‘163 Main%’));

8 Subqueries ANY (SOME) and ALL Find staff whose salary is larger than the salary of at least one member of the staff at branch B3. SELECT sno, Fname, Lname, position, salary FROM staff WHERE salary > ANY (SELECT salary FROM staff WHERE bno = ‘B3’); Try the above using ALL instead of ANY.

9 Joins Branch1 bnobcity B3Glasgow B4Bristol B2London Prop_For_Rent1 pnopcity PA14Aberdeen PL94London PG4Glasgow

10 Inner Join List the branch offices and properties that are in the same city SELECT b.bno, b.city, p.pno, p.city FROM branch b, Prop_For_Rent p WHERE b.city = p.city;

11 Left Outer Join List the branch offices and properties that are in the same city along with any unmatched branches SELECT b.bno, b.city, p.pno, p.city FROM branch b LEFT JOIN Prop_For_Rent p ON b.city = p.city;

12 Right Outer Join List the branch offices and properties that are in the same city along with any unmatched branches SELECT b.bno, b.city, p.pno, p.city FROM branch b RIGHT JOIN Prop_For_Rent p ON b.city = p.city;

13 Full Outer Join List the branch offices and properties that are in the same city along with any unmatched branches SELECT b.bno, b.city, p.pno, p.city FROM branch b FULL JOIN Prop_For_Rent p ON b.city = p.city;

14 Default Values CREATE tablename (test_idCHAR(5) NOT NULL, pass_scoreDECIMAL(6,2) NOT NULL WITH DEFAULT 65, … );

15 Constraints CREATE tablename (test_idCHAR(5) NOT NULL, genderCHAR(1), pass_scoreDECIMAL(6,2) NOT NULL WITH DEFAULT 65, …, PRIMARY KEY (keycol), CONSTRAINT gender CHECK (gender IN ( ‘F’, ‘M’));

16 Union Construct a list of all areas where there is either a branch office or a rental property (SELECT area FROM branch WHERE area is NOT NULL) UNION (SELECT area FROM Prop_For_Rent WHERE area is NOT NULL);

17 INTERSECT (Product) Construct a list of all cities where there is both a branch office and a rental property (SELECT city FROM branch) INTERSECT (SELECT city FROM Prop_For_Rent);

18 EXCEPT (Difference) Construct a list of all cities where there is a branch office but no rental property (SELECT city FROM branch) EXCEPT (SELECT city FROM Prop_For_Rent);

19 Triggers A trigger is a set of actions that will be executed when a defined event occurs--namely an INSERT, UPDATE, or DELETE CREATE TRIGGER trigger_name BEFORE | AFTER ON [REFERENCING ] [FOR EACH {ROW | STATEMENT}] [WHEN (trigger_condition) ]

20 Trigger Example CREATE TRIGGER insert_mailshot_table AFTER INSERT ON prop_for_rent REFERENCING NEW ROW as pfr BEGIN INSERT INTO mailshot (SELECT r.Fname, r.lname, r.address, r.max_rent, pfr.pno, pfr.street, pfr.area, pfr.city, pfr.pcode, pfr.type, pfr.rooms, pfr.rent, FROM renter r WHERE r.bno = pfr.bno AND (r.pref_type = pfr.type AND r.max_rent <= pfr.rent)) END;

21 Update Example UPDATE staff SET Salary = Salary*1.05 Where Position = ‘Manager’ ;

22 ALTER Statement Add a new column to a table Drop a column from a table Add a new table constraint Drop a table constraint Set a default for a column Drop a default for a column

23 ALTER Examples ALTER TABLE inventory ADD location CHAR(4); ALTER TABLE quotations FOREIGN KEY supp (supplrNbr) REFERENCES suppliers; ALTER TABLE quotations DROP FOREIGN KEY supp; ALTER TABLE inventory ADD CONSTRAINT example CHECK(location =‘LOC1’); ALTER TABLE inventory DROP CONSTRAINT example; ALTER TABLE inventory DROP location; ‘generally will not work if data in the column’ ALTER TABLE inventory MODIFY partname char(12); ‘not always supported

24 Populating a table with Insert and Select DROP TABLE staff_prop_count; CREATE TABLE staff_prop_count (sno char(4) NOT NULL, Fname char(10), Lname char(10), prop_count smallint, PRIMAY KEY (sno)); INSERT INTO staff_prop_count (SELECT s.sno, Fname, Lname, count(*) FROM staff s, prop_for_rent p WHERE s.sno = p.sno GROUP BY s.sno, Fname, Lname); SELECT * FROM Staff_prop_rent;

25 CASE Expressions SELECT Fname, Lname, (CASE WHEN gender = ‘M’ THEN ‘Male’ WHEN gender = ‘F’ THEN ‘FEMALE’ ELSE ‘Unknown’ END) AS Staff_Gender FROM staff;

26 DELETE Expressions DELETE FROM table_name [WHERE search_condition]; DELETE FROM viewing WHERE pno=‘PG4’; DELETE FROM viewing;

27 Creating Views Views are virtual relations (produced upon request) and can be used to advantage as follows: –Restrict access--create a table without sensitive data –Computed columns –Avoid writing common queries –Make it easier for end users--complex queries written by IS personnel to create a table for easier data for end users

28 Create View Example CREATE VIEW prop_owner AS (SELECT p.pno, p.street, p.city, o.Fname, o.Lname FROM Prop_for_Rent p, owner o WHERE p.ono = o.ono); SELECT * FROM prop_owner;

29 Relational Operators