sql. more than 100 dbms support sql –used by dbas and application programmers –structured query...

41
SQL

Upload: chloe-biscoe

Post on 30-Mar-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

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

SQL

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

• More than 100 DBMS support SQL– Used by DBAs and application programmers– Structured Query Language or SEQUEL– ORACLE-> relation database based on SQL– Standard database language

• SQL– Reserved Words

– To retrieve data

– insert, update and delete

– SQL and QBE– Does not contain flow of control commands like

• IF, ELSE, THEN,WHILE,GOTO,DO

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

Writing SQL• Case insensitive

– exception: literal character data

• Data Manipulation– SELECT– INSERT– UPDATE– DELETE

• All non-numeric data must be enclosed in single quotesINSERT INTO (property_for_rent(pno,street,area,city,type,rooms,rent,ono,sno,bno)

VALUES (‘PA14’,’16Holland’,’Dee’,’Arbeen’,’House’,6,650.00,’C640’,’SA9’,’B7’);

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

• CREATE TABLE, INSERT, SELECT– e.g:CREATE TABLE staff(sno VARCHAR(5), lname VARCHAR(15), salary

DECIMAL(7,2));

INSERT INTO staff

VALUES (‘SG16’,’Brown’,8300);

SELECT sno,lname,salary

FROM staff

where salary>60000;

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

ISO SQL data typesData type Declarations

character CHAR, VARCHAR

bit BIT, BIT VARYING

exact numeric NEUMARIC, DECIMAL, INTEGER, SMALLINT

approximate numeric FLOAT, REAL, DOUBLE PRECISION

datetime DATE, TIME, TIMESTAMP

interval INTERVAL

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

DDL

• Create Table Branch (

BranchNo VARCHAR(6) NOT NULL,

Street VARCHAR(15) NOT NULL,

City VARCHAR(10) NOT NULL,

Postcode VARCHAR(8) NOT NULL,

Primary Key(BranchNo));

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

STAFF

• CREATE TABLE staff (

staffno VARCHAR(5) NOT NULL,

fname VARCHAR(15)NOT NULL,

lname VARCHAR(15)NOT NULL,

positionVARCHAR(10)NOT NULL,

sex CHAR,

dob DATE,

salary DECIMAL(7,2)NOT NULL,

brano VARCHAR(3) NOT NULL)

Primary Key (staffno),

Foreign Key(brano) References branch);

• Describe staff

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

PROPERTY_FOR_RENTCREATE TABLE property_for_rent (

pno VARCHAR(5) NOT NULL,

street VARCHAR(25) NOT NULL,

city VARCHAR(15) NOT NULL,

pcode VARCHAR(8),

type CHAR NOT NULL,

rooms SMALLINT NOT NULL,

rent DECIMAL(6,2) NOT NULL,

ownerno VARCHAR(5) NOT NULL,

staffno VARCHAR(5),

Branch No VARCHAR(3) NOT NULL

Primary Key (pno)

Foreign Key (Branch No) References Branch,

Foreign Key (staffno) References Staff,

Foreign Key(ownerno) References Owner);

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

INSERT . . . VALUES

Insert a new record into the staff table supplying data for all columns

INSERT INTO staff

VALUES (‘SG16’,’Alan’,’Brown’, ’Manager’,’M’,DATE ‘’1957-05-25’,8300,’B3’);

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

Retrieve all columns, all rows

List full details of all staffSELECT sno,fname,lname,position,sex,dob,salary,bno

FROM staff;

SELECT *

FROM staff;

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

RETRIEVE SPECIFIC COLUMNS, ALL ROWS

List of salaries for all staff with first name, last name and Sno.

SELECT sno,fname,lname,salary

FROM staff;

• Use of DISTINCTList of property numbers of all properties viewed

SELECT pno

FROM viewing;

SELECT DISTINCT pno

FROM VIEWING;

pno

PA14

PG4

PA14

PG36

PG4

pno

PA14

PG4

PG36

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

CALCULATED FIELDS

List of monthly salaries for all staff,the first and last names.

SELECT sno,fname,lname,salary/12

FROM staff;

SELECT sno,fname,lname,salary/12 AS monthly_salary

FROM staff;

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

UPDATE SPECIFIC ROWS

Give all staff a 3% raise

UPDATE staff

SET salary=salary*1.03;

Give all managers a 5% raise

UPDATE staff

SET salary=salary*1.05

WHERE position=‘Manager’;

UPDATE ALL ROWS

UPDATE MULTIPLE COLUMNSPromote David Ford(Sno=‘SG14’) to Manager and change his salary to $28,000

UPDATE staff

SET position=‘manager’, salary=28000

WHERE sno=‘SG14’;

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

DELETE SPECIFIC ROWS

Delete all viewings that relate to property PG4

DELETE FROM viewing

WHERE pno=‘PG4’;

DELETE FROM viewing;

DELETE ALL ROWS

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

Comparison Search Condition

List all staff with salary greater than $10,000

SELECT sno,fname,lname,position,salary

FROM staff

WHERE salary>10000;

Comparison Operators

=, <, >, <=, >=, <>, !=

Logical Operators

AND, OR, NOT

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

Compound Comparison Search Condition

List the addresses of all branch offices in London or Glasgow

SELECT bno,street,area,city,pcode

FROM branch

WHERE city=‘London’ OR city=‘Glasgow’;

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

RANGE SEARCH CONDITION

List all staff with salary between $20,000 and 30,000

SELECT sno,fname,lname,position,salary

FROM staff

WHERE salary BETWEEN 20000 AND 30000;

SELECT sno,fname,lname,position,salary

FROM staff

WHERE salary>=20000 AND salary<=30000;

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

Set membership search condition (IN/NOT IN)

List all Managers and Deputy Managers

SELECT sno,fname,lname,position

FROM staff

WHERE position IN (‘Manager’,Deputy’);

SELECT sno,fname,lname,position

FROM staff

WHERE position=‘Manager’ OR position=‘Deputy’;

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

Pattern match search condition (LIKE/ LIKE NOT)

Find all staff with the string ‘Glasgow’ in their addresses

SELECT sno,fname,lname,address,salary

FROM staff

WHERE address LIKE ‘%Glasgow%’;

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

Single Column Ordering

Produce a list of salaries of all staff in descending order of salary

SELECT sno,fname,lname,salary

FROM staff

ORDER BY salary DESC;

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

Multiple Column ordering

Produce an abbreviated list of all properties arranged in order of property type.

SELECT pno,type, rooms,rent

FROM property_for_rent

ORDER BY type;

SELECT pno,type, rooms,rent

FROM property_for_rent

ORDER BY type, rent DESC;

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

Using the SQL Aggregate Functions

• COUNT• SUM• AVG• MIN• MAX

Specified Column operations

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

How many properties cost more than $350 p/month

SELECT COUNT(*) AS count

FROM property_for_rent

WHERE rent>350;

How many properties were viewed in May 1998

SELECT COUNT(DISTINCT pno) AS count

FROM viewing

WHERE date BETWEEN ‘1-May-98’ AND ‘31-May-98’;

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

Find the total number of managers and

sum of their salaries

SELECT COUNT (sno) AS count , SUM(salary) AS sum

FROM staff

WHERE position=‘manager’;

Find the minimum, maximum and average staff salarySELECT MIN(salary) AS min, MAX(salary) AS max, AVG(salary) AS average

FROM staff;

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

Use of Group ByFind the number of staff working in each branch and the sum of

their salaries.

SELECT bno, COUNT(sno) AS count,SUM(salary) AS sum

FROM staff

GROUP BY bno

ORDER BY bno;

Use of Having (Filters groups)For each branch office with more than one member of staff, find the number of

staff working in each branch and the sum of their salaries

SELECT bno, COUNT(sno) AS count,SUM(salary) AS sum

FROM staff

GROUP BY bno

HAVING COUNT(sno)>1

ORDER BY bno;

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

SUBQUERIES

• Using a subquery with equalityList the staff who work in the branch at ‘163 Main St.’

SELECT sno, fname, lname, position

FROM staff

WHERE bno=(SELECT bno

FROM branch

WHERE street=‘163 Main St.’);

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

Using a subquery with an aggregate function

List all staff whose salary is greater than the average salary, and list by how much.

SELECT sno,fname,lname,position, salary- (SELECT avg(salary) FROM staff)

AS sal_diff

FROM staff

WHERE salary>

(SELECT avg(salary)

FROM staff);

SELECT sno,fname,lname,position,salary-17000 AS sal_diff

FROM staff

WHERE salary>17000

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

Nested queries; use of INList the properties that are handled by staff who work in the branch at ‘163 Main St.’

SELECT pno,street,area,city,pcode,type,rooms,rent

FROM property_for_rent

WHERE sno IN

(SELECT sno

FROM staff

WHERE bno =

(SELECT bno

FROM branch

WHERE street=‘163 Main St’));

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

Use of ANY/SOME

Find staff whose salary is larger than the salary of at least one member at branch B3

SELECT sno,fname,lname,position,salary

FROM staff

WHERE salary> SOME

(SELECT salary

FROM staff

WHERE bno=‘B3’);

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

Use of ALL

Find staff whose salary is larger than the salary of every member of the staff at branch B3

SELECT sno,fname,lname,position,salary

FROM staff

WHERE salary> ALL

(SELECT salary

FROM staff

WHERE bno=‘B3’);

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

Simple Join

List the names of all renters who have viewed a property along with any comment supplied

SELECT r.rno,fname,lname,pno,comment

FROM renter r, viewing v

WHERE r.rno=v.rno;

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

Sorting a Join

For each branch office, list the names of all staff who manage properties and the properties they manage

SELECT s.bno,s.sno,fname,lname,pno

FROM staff s, property_for_rent p

WHERE s.sno=p.sno;

SELECT s.bno,s.sno,fname,lname,pno

FROM staff s, property_for_rent p

WHERE s.sno=p.sno

ORDER BY s.bno,s.sno,pno;

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

Three table Join

For each branch, list the staff who manage properties, including the city in which the branch is located and the properties they manage

SELECT b.bno,b.city, s.sno, fname,lname,pno

FROM branch b, staff s, property_for_rent p

WHERE b.bno = s.bno AND s.sno=p.sno

ORDER BY b.bno,s.sno,pno;

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

Multiple Grouping Columns

Find the number of properties handled by each staff member

SELECT s.bno, s.sno, COUNT(*) AS count

FROM staff s, property_for_rent p

WHERE s.sno = p.sno

GROUP BY s.bno, s.sno

ORDER BY s.bno, s.sno;

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

bno bcity

B3 Glasgow

B4 Bristol

B2 London

pno pcity

PA14 Aberdeen

PL94 London

PG4 Glasgow

BRANCH1 PROPERTY_FOR_RENT1

Inner join of these two tables:

SELECT b*, p*

FROM branch b, property_for_rent p

WHERE b.bcity = p.pcity;

bno bcity pno pcity

B3 Glasgow PG4 Glasgow

B2 London PL94 London

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

Left outer JoinList the branch offices and properties that are in the same cities along

with any unmatched branches

SELECT b*, p*

FROM branch1 b LEFT JOIN property_for_rent1 p ON b.bcity=p.pcity;

bno bcity pno pcity

B3 Glasgow PG4 Glasgow

B4 Bristol NULL NULL

B2 London PL94 London

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

Right Outer Join

List the branch offices and properties in the same city and any unmatched properties

SELECT b*, p*

FROM branch1 b RIGHT JOIN property_for_rent1 p ON b.bcity=p.pcity;

bno bcity pno pcity

NULL NULL PA14 Aberdeen

B3 Glasgow PG4 Glasgow

B2 London PL94 London

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

Full Outer Join

List the branch offices and properties in the same city and any unmatched branches or properties

SELECT b*, p*

FROM branch1 b FULL JOIN property_for_rent1 p ON b.bcity=p.pcity;

bno bcity pno pcity

NULL NULL PA14 Aberdeen

B3 Glasgow PG4 Glasgow

B4 Bristol NULL NULL

B2 London PL94 London

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

Use of Union

Construct a list of all areas where there is either a property or office.

(SELECT area

FROM branch

WHERE area IS NOT NULL)

UNION

(SELECT area

FROM property_for_rent

WHERE area IS NOT NULL);

(SELECT *

FROM branch

WHERE area IS NOT NULL)

UNION CORRESPONDING BY area

(SELECT *

FROM property_for_rent

WHERE area IS NOT NULL);

OR

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

Use of Intersect

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

(SELECT *

FROM branch)

INTERSECT CORRESPONDING BY city

(SELECT *

FROM property_for_rent);

OR

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

REMOVE A TABLE

DROP TABLE property_for_rent;

CREATE UNIQUE INDEX sno_ind ON staff (sno);

CREATE UNIQUE INDEX pno_ind ON property_for_rent (pno);

CREATING AN INDEX

DROP INDEX rent_id;

REMOVING AN INDEX