SQL-99: Schema Definition, Constraints, Queries, and Views

33
Ms. Hatoon Al-Sagri CCIS – IS Department SQL-99: Schema Definition, Constraints, Queries, and Views 1

description

SQL-99: Schema Definition, Constraints, Queries, and Views. Ms. Hatoon Al- Sagri CCIS – IS Department. Database Design. Steps in building a database for an application:. Real-world domain. Conceptual model. DBMS data model. Create Schema (DDL). Modify data (DML). - PowerPoint PPT Presentation

Transcript of SQL-99: Schema Definition, Constraints, Queries, and Views

Page 1: SQL-99: Schema Definition, Constraints, Queries, and Views

Ms. Hatoon Al-Sagri

CCIS – IS Department

SQL-99: Schema Definition, Constraints, Queries, and Views

1

Page 2: SQL-99: Schema Definition, Constraints, Queries, and Views

Database Design

Steps in building a database for an application:

Real-world domain

Conceptualmodel

DBMS data model

Create Schema

(DDL)

Modify data (DML)

2

Page 3: SQL-99: Schema Definition, Constraints, Queries, and Views

Data Manipulation Language (DML) Statements

The main SQL data manipulation language statements are:

SELECT

INSERT INTO

UPDATE

DELETE FROM

3

Page 4: SQL-99: Schema Definition, Constraints, Queries, and Views

Notations

Notations to define SQL statements:

• UPPER-CASE letters represents reserved words

• Lower-case letters represents user-defined words

• | indicates a choice among alternatives; (e.g. a | b | c)

• { } indicates a required element

• [ ] indicates an optional element

• … indicates optional repetition of an item zero or mote times

• Underlined words represent default values

4

Page 5: SQL-99: Schema Definition, Constraints, Queries, and Views

SELECT

SyntaxSELECT [DISTINCT|ALL]{*|column|column_expression [AS new_name][,…]} FROM table_name [alias] [, … ] [WHERE condition] [GROUP BY column_list] [HAVING condition] [ORDER BY column_list [ASC|DESC]] column represents a column name column_expression represents an expression on a column table_name is the name of an existing database table or view FROM specifies the table(s) to be used WHERE filters the rows subject to some condition GROUP BY forms groups of rows with the same column name SELECT specifies which column are to appear in the output ORDER BY specifies the order of the output Order of the clauses in the SELECT statement can not be changed The result of a query is another table Asterisk (*) means all columns.

5

Page 6: SQL-99: Schema Definition, Constraints, Queries, and Views

Retrieve all columns & rows

Syntax

SELECT {* | column| column_expression [,…]} FROM table_name;

Example:

Staff(StaffNo, fname,lname,position,sex,dob,salary, bno)Retrieve all staff information

SELECT StaffNo,fname,lname,position,sex,dob,salary, bno FROM staff;

OR

SELECT * FROM staff;

6

Page 7: SQL-99: Schema Definition, Constraints, Queries, and Views

Retrieve all columns & rows

SL21

SG37

SG14

SA9

SG5

StaffNo

John

Ann

David

Mary

Susan

FName

White

Beech

Ford

Howe

Brand

LName position

Manager

Assistant

Supervisor

Assistant

Manager

Sex

M

F

M

F

F

DOB

1-Oct-45

10-Nov-60

24-Mar-58

19-Feb-70

13-Jun-40

Salary

30000

12000

18000

9000

24000

BrnNo

B005

B003

B003

B007

B003

7

Page 8: SQL-99: Schema Definition, Constraints, Queries, and Views

SL21

SG37

SG14

SA9

SG5

StaffNo

John

Ann

David

Mary

Susan

fName

White

Beech

Ford

Howe

Brand

lName salary

30000

12000

18000

9000

24000

Retrieve specific columns & all rows

Example: List salaries of all staff, showing only the staff number, the first and last name, and salary.

SELECT StaffNo, fname, lname, salary FROM staff;

8

Page 9: SQL-99: Schema Definition, Constraints, Queries, and Views

Use of DISTINCT

DISTINCT eliminates duplicated tuples. SQL does not treat a relation as a set; the result of the query may contain

duplicate tuples.

Syntax

SELECT [DISTINCT|ALL] {* | column |column_expression [,…]}

FROM table_name;

Example: List the available positions for staff

SELECT DISTINCT position FROM staff;

9

Page 10: SQL-99: Schema Definition, Constraints, Queries, and Views

Use of DISTINCT

position

Manager

Assistant

Supervisor

Assistant

Manager

position

Manager

Assistant

Supervisor

SELECT position FROM staff;

SELECT DISTINCT position FROM staff;

10

Page 11: SQL-99: Schema Definition, Constraints, Queries, and Views

Use of DISTINCT

Examples:

SELECT ALL Salary

FROM EMPLOYEE;

SELECT DISTINCT SalaryFROM EMPLOYEE;

11

Page 12: SQL-99: Schema Definition, Constraints, Queries, and Views

Calculated fields

The SQL expression in the SELECT list specifies a derived field. Columns referenced in the arithmetic expression must have a numeric type SQL expression can involve +, - , * , / , ( , ) AS caluse is used to name the derived column

Syntax

SELECT {* | column| column_expression [AS new_name] [,…]} FROM table_name;

12

Page 13: SQL-99: Schema Definition, Constraints, Queries, and Views

Calculated fields

SL21

SG37

SG14

SA9

SG5

StaffNo

John

Ann

David

Mary

Susan

FName

White

Beech

Ford

Howe

Brand

LName MonthlySalary

2500

1000

1500

750

200013

Example: List the monthly salaries for all staff, showing the staff number, the first and last names and salary.

SELECT sno, fname, lname, salary/12 AS monthlySalary FROM staff;

Page 14: SQL-99: Schema Definition, Constraints, Queries, and Views

Calculated fields

Example: Show the effect of giving all employees who work on the

'ProductX' project a 10% raise.

SELECT Fname, Lname, 1.1*Salary AS Increased_sal

FROM EMPLOYEE, WORKS_ON, PROJECT

WHERE Ssn=Essn AND Pno=Pnumber AND Pname='ProductX’;

14

Page 15: SQL-99: Schema Definition, Constraints, Queries, and Views

Row selection (WHERE clause)

Basic form of the SQL SELECT statement is called a mapping or a

SELECT-FROM-WHERE block

SELECT <attribute list>

FROM <table list>

WHERE <condition> ;

– <attribute list> is a list of attribute names whose values are to be

retrieved by the query

– <table list> is a list of the relation names required to process the query

– <condition> is a conditional (Boolean) expression that identifies the

tuples to be retrieved by the query.

15

Page 16: SQL-99: Schema Definition, Constraints, Queries, and Views

Row selection (WHERE clause)

WHERE clause consists of five basic search conditions:

Comparison: Compare the value of one expression to the value of another

expression (= , <, >, <=, >=, <>).

Range: Test whether the value of an expression falls within a specified range of

values (BETWEEN/ NOT BETWEEN).

Set membership: Test whether the value of an expression equals one of a set of

values (IN/ NOT IN).

Pattern match: Test whether a string matches a specified pattern (LIKE/ NOT LIKE).

NULL: Test whether a column has null value (IS NULL/ IS NOT NULL).

16

Page 17: SQL-99: Schema Definition, Constraints, Queries, and Views

WHERE clause Comparison search condition

Comparison operators: = , <, >, <=, >=, <>

Syntax

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

FROM table_name

[WHERE condition];

Example: List all staff with a salary greater than 10,000.

SELECT sno, fname, lname, position, salary FROM staff WHERE salary > 10000;

17

Page 18: SQL-99: Schema Definition, Constraints, Queries, and Views

WHERE clause Comparison search condition

18

SL21

SG37

SG14

SG5

StaffNo

John

Ann

David

Susan

FName

White

Beech

Ford

Brand

LName Salary

30000

12000

18000

24000

Example: List all staff with a salary greater than 10,000.

SELECT sno, fname, lname, position, salary FROM staff WHERE salary > 10000;

Page 19: SQL-99: Schema Definition, Constraints, Queries, and Views

WHERE clause Compound comparison search condition

Compound comparison operators: AND, OR, NOT,( )

Order of evaluation:– Expression is evaluated left to right– Between brackets– NOT– AND– OR

19

Page 20: SQL-99: Schema Definition, Constraints, Queries, and Views

WHERE clause Compound comparison search condition

SL21

SG37

SA9

SG5

StaffNo

John

Ann

Mary

Susan

FName

White

Beech

Howe

Brand

LName position

Manager

Assistant

Assistant

Manager

20

Example: List all staff who work as managers or assistants.

SELECT sno, fname, lname, position FROM staff WHERE position = ‘Manager’ OR position =‘Assistant’;

Page 21: SQL-99: Schema Definition, Constraints, Queries, and Views

WHERE clause Compound comparison search condition

Example: Retrieve the birth date and address of the employee

whose name is 'John B. Smith'.

SELECT Bdate, Address

FROM EMPLOYEE

WHERE Fname='John' AND Minit='B’ AND Lname='Smith’;

21

Page 22: SQL-99: Schema Definition, Constraints, Queries, and Views

WHERE clause BETWEEN/ NOT BETWEEN

BETWEEN checks if a value is within a range. NOT BETWEEN checks if a value is outside a range.

Example: List all staff with a salary between 20000 and 30000.

SELECT sno, fname, lname, salary

FROM staff

WHERE salary BETWEEN 20000 AND 30000;

This would be expressed as:

SELECT sno, fname, lname, salary

FROM staff

WHERE salary >= 20000 AND salary <= 30000;

22

SL21

SG5

StaffNo

John

Susan

FName

White

Brand

LName Salary

30000

24000

Page 23: SQL-99: Schema Definition, Constraints, Queries, and Views

WHERE clause BETWEEN/ NOT BETWEEN

Example: Retrieve all employee in department 5 whose salary is

between $30,000 and $40,000

SELECT *

FROM EMPLOYEE

WHERE (Salary BETWEEN 30,000 AND 40,000) AND Dno=5;

23

Page 24: SQL-99: Schema Definition, Constraints, Queries, and Views

WHERE clause EXPLICIT SETS (IN/ NOT IN )

IN tests whether a data value matches one of a list values. NOT IN checks for data values that do not lie in a specific list of values.

Example: List all Managers and Assistants.

SELECT sno, fname, lname, position

FROM staff

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

This would be expressed as:

SELECT sno, fname, lname, position

FROM staff

WHERE position = ‘Manager’ OR position = ’Assistant’;24

Page 25: SQL-99: Schema Definition, Constraints, Queries, and Views

SL21

SG37

SA9

SG5

StaffNo

John

Ann

Mary

Susan

FName

White

Beech

Howe

Brand

LName position

Manager

Assistant

Assistant

Manager

WHERE clause IN/ NOT IN

25

Page 26: SQL-99: Schema Definition, Constraints, Queries, and Views

WHERE clause IN/ NOT IN

Example: Retrieve the social security numbers of all

employees who work on project number 1, 2, or 3.

SELECT DISTINCT ESSN

FROM WORKS_ON

WHERE PNO IN (1, 2, 3) ;

26

Page 27: SQL-99: Schema Definition, Constraints, Queries, and Views

WHERE clause SUBSTRING COMPARISON (LIKE/ NOT LIKE)

SQL has special pattern matching symbol:– % represents any sequence of zero or more character (wildcard)– _ represents any single character

Example

Address LIKE ‘H%’ means that the first character must be H, but the rest can

be anything

Address LIKE ‘H_ _ _’ means that there must be exactly four characters in the

string, the first of which must be H

Address LIKE ‘%e’ means any sequence of characters, of length at least 1, with

the last character an e

Address LIKE ‘%Glasgow%’ means a sequence of characters of any length

containing Glasgow

Address NOT LIKE ‘H%’ means the first character can not be H27

Page 28: SQL-99: Schema Definition, Constraints, Queries, and Views

WHERE clause LIKE/ NOT LIKE

Example: List all staff with the string ‘Glasgow’ in their address

SELECT sno, fname, lname, address FROM staff WHERE address LIKE ‘%Glasgow%’ ;

28

SL21

SG37

StaffNo

John

Ann

White

Beech

LName address

Achray St,Glasgow G32 9DX

Well St, Glasgow G42

FName

Page 29: SQL-99: Schema Definition, Constraints, Queries, and Views

WHERE clause LIKE/ NOT LIKE

Example: Retrieve all employees who were born during the 1950s.

Here, '5' must be the third character of the string (according to our format for date), so the BDATE value is ‘_ _ 5_ _ _ _ _ _ _ ', with each underscore as a place holder for a single arbitrary character.

SELECT Fname, LnameFROM EMPLOYEEWHERE Bdate LIKE ‘_ _ 5_ _ _ _ _ _ _ ‘;

29

Page 30: SQL-99: Schema Definition, Constraints, Queries, and Views

WHERE clause IS NULL/ IS NOT NULL

A NULL value can not be tested with = or<> to another string. We have to test for NULL explicitly.

Example: Retrieve the names of all employees who do not have

supervisors.

SELECT Fname, Lname

FROM EMPLOYEE

WHERE Super_ssn IS NULL;

30

Page 31: SQL-99: Schema Definition, Constraints, Queries, and Views

Ordering of Query Results

Allows the retrieved records to be ordered in ascending (ASC) or descending order (DESC) on any column or combination of columns.

Syntax

SELECT {* | [column_expression] [,…]} FROM table_name [ORDER BY column_list [ASC|DESC]]

ORDER BY Dname DESC, Lname ASC, Fname ASC;

31

Page 32: SQL-99: Schema Definition, Constraints, Queries, and Views

Ordering of Query Results

Single Column ordering: Produce a list of salaries for all staff, arranged in descending order of salary.

SELECT sno, fname, lname, salary FROM staff ORDER BY salary DESC;

Multiple columns ordering: Produce a list arranged in order of property type

SELECT propertyNo, type, rooms, rent FROM property ORDER BY type, rent DESC;

32

Page 33: SQL-99: Schema Definition, Constraints, Queries, and Views

Ordering of Query Results

PG16

PL94

PG36

PG4

PropertNo

Flat

Flat

Flat

House

Type

4

4

3

3

Rooms

400

370

650

PA14 House 6 600

Rent

450

33

SELECT propertyNo, type, rooms, rent FROM property ORDER BY type, rent DESC;