general form of the select statement

29
1 General Form of the SELECT Statement SELECT [DISTINCT | ALL {* | col_expr [AS new_name] [,…]} FROM table_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

Upload: micheal

Post on 05-Feb-2016

39 views

Category:

Documents


0 download

DESCRIPTION

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] - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: General Form of the SELECT Statement

1

General Form of the SELECT Statement

SELECT [DISTINCT | ALL {* | col_expr [AS new_name] [,…]}

FROM table_name [,…] [,…][WHERE condition][GROUP BY col_list][HAVING condition][ORDER BY col_list]

The order of the clauses of the SELECT statement cannot be changedThe order of the col_exp of the SELECT statement is not significant

Page 2: General Form of the SELECT Statement

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)

Page 3: General Form of the SELECT Statement

3

Foreign KeysReferential 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.

Page 4: General Form of the SELECT Statement

4

Foreign Key Syntax

PRIMARY KEY (...),

FOREIGN KEY (…) REFERENCES tablename

ON DELETE …

ON UPDATE ...

Page 5: General Form of the SELECT Statement

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 BY bno

ORDER BY bno;

Page 6: General Form of the SELECT Statement

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;

Page 7: General Form of the SELECT Statement

7

SubqueriesCan be used in WHERE or HAVING clauses

List the properties that are handled by staff that work at the 163.. 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%’));

Page 8: General Form of the SELECT Statement

8

SubqueriesANY (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.

Page 9: General Form of the SELECT Statement

9

Joins

Branch1

bno bcity

B3 Glasgow

B4 Bristol

B2 London

Prop_For_Rent1

pno pcity

PA14 Aberdeen

PL94 London

PG4 Glasgow

Page 10: General Form of the SELECT Statement

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;

Page 11: General Form of the SELECT Statement

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;

Page 12: General Form of the SELECT Statement

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;

Page 13: General Form of the SELECT Statement

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;

Page 14: General Form of the SELECT Statement

14

Default Values

CREATE tablename

(test_id CHAR(5) NOT NULL,

pass_score DECIMAL(6,2) NOT NULL WITH DEFAULT 65,

…);

Page 15: General Form of the SELECT Statement

15

Constraints

CREATE tablename

(test_id CHAR(5) NOT NULL,

gender CHAR(1),

pass_score DECIMAL(6,2) NOT NULL WITH DEFAULT 65,

,PRIMARY KEY (keycol),

CONSTRAINT gender

CHECK (gender IN ( ‘F’, ‘M’));

Page 16: General Form of the SELECT Statement

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

Page 17: General Form of the SELECT Statement

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

Page 18: General Form of the SELECT Statement

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

Page 19: General Form of the SELECT Statement

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 <trigger event> ON <table_name>

[REFERENCING <old_or_new_values> ]

[FOR EACH {ROW | STATEMENT}]

[WHEN (trigger_condition) ]

<trigger-body>

Page 20: General Form of the SELECT Statement

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;

Page 21: General Form of the SELECT Statement

21

Update Example

UPDATE staff

SET Salary = Salary*1.05

Where Position = ‘Manager’ ;

Page 22: General Form of the SELECT Statement

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

Page 23: General Form of the SELECT Statement

23

ALTER ExamplesALTER 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

Page 24: General Form of the SELECT Statement

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 pWHERE s.sno = p.snoGROUP BY s.sno, Fname, Lname);

SELECT * FROM Staff_prop_rent;

Page 25: General Form of the SELECT Statement

25

CASE ExpressionsSELECT Fname, Lname,

(CASE

WHEN gender = ‘M’ THEN ‘Male’

WHEN gender = ‘F’ THEN ‘FEMALE’

ELSE

‘Unknown’

END) AS Staff_Gender

FROM staff;

Page 26: General Form of the SELECT Statement

26

DELETE ExpressionsDELETE FROM table_name

[WHERE search_condition];

DELETE FROM viewing

WHERE pno=‘PG4’;

DELETE FROM viewing;

Page 27: General Form of the SELECT Statement

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

Page 28: General Form of the SELECT Statement

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;

Page 29: General Form of the SELECT Statement

29

Relational Operators

Snum Name Major123 Jones History158 Parks Math271 Smith History

Number Name Interest105 Anderson Management123 Jones History