Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5...

49
Lecture6:Data Manipulation in SQL , Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1

Transcript of Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5...

Page 1: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6Lecture6:Data Manipulation in SQL , Simple SQL queries

Prepared by L. Nouf Almujally

Ref. Chapter5

1

Page 2: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

The Process of Database Design

Real World Domain

Conceptual model (ERD)

Relational Data Model

Create schema

(DDL)

Load Data(DML)

2

Page 3: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Tables in the Examples

Customer(custNo, custName, custSt, custCity, age)

Product(prodNo, prodName, prodDes, price)

Orders(ordNo, ordDate, custNo, prodNo, quantity)

Where

custName, custSt, custCity, prodName, prodDes are stringsordDate is dateOthers are numbers 3

Page 4: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Sample Data in Customer Table

custNo custName custSt custCity age

1 C1 Olaya St Jeddah 20

2 C2 Mains St Riyadh 30

3 C3 Mains Rd Riyadh 25

4 C4 Mains Rd Dammam

5 C5 Mains Rd Riyadh

4

Page 5: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Sample Data in Product Table

prodNo prodNam

eprodDes price

100 P0 Food 100

101 P1 healthy Food 100

102 P2 200

103 P3 self_raising flour,80%wheat

300

104 P4 network 80x 3005

Page 6: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Sample Data in Orders Table

ordNo ordDate custNo prodNo quantity

1 01-jan-2003 1 100 2

2 02-jan-2003 1 101 1

3 01-jan-2003 2 102 1

4 01-jan-2003 3 100 2

5 03-jan-2003 1 101 1

6 06-mar-2003 2 100 10

6

Page 7: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Data Manipulation (DML)

• DML is used to retrieve and modify data in the tables

• Four basic statements• Insert Into• Select• Update• delete From

7

Page 8: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Insert Statement

• The INSERT statement adds one or more new rows of data to a database table.

• Syntax

• Note:• value list must correspond to column list• If column list is omitted, then a value for every attribute is required• The data types must be correct

8

INSERT INTO table_name (column1,column2,column3,...)VALUES (value1,value2,value3,...);

INSERT INTO table_name VALUES (value1,value2,value3,...);

Page 9: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

• Example: for table Customer,

Insert into Customer(custNo, custName) values ('6', 'John');

Output: 1 row inserted

Insert into Customer values ('7', 'David ', 'St1','City1', 20);

Output: 1 row inserted

Insert Statement Example

9

Page 10: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Simple SELECT Queries

• The SELECT command is used for submitting queries to the DBMS.

• Syntax

10

SELECT expression_list FROM table_list [WHERE condition] [ORDER BY expression_list];

Page 11: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Simple SELECT Queries

Expression in SELECT statement :

Condition in WHERE statement:• an expression that can be evaluated to TRUE or FALSE. • Only rows satisfying the condition will be chosen.• Condition can be simple comparison or compound expression

Expression Examplecolumn names SELECT prodNoarithmetic operators for numbers: +, -, *, / SELECT Price+10Constant SELECT 'The first name is', fname

FROM customer

11

Page 12: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Conditions in the 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).

Note: Basic comparisons can be compounded by AND, OR, NOT• Eg, prodNo=100 and ordDate='01-jan-2003'

SQ

L (

DM

L)

12

Page 13: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Simple Queries : Comparison search condition

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

Example 1: List all products (by prodNo and price) which are priced more than 100.

Select prodNo, price From Product Where price >100;

Example 2: What is the name of the customer whose custNo is 1?

Select custName From customer Where custNo=1;

prodNo price

102 200

103 300

104 300

custNameC1 13

Page 14: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Listing All Data in a Table

• If WHERE clause is omitted, all rows will be listed.

Example: List all data in the customer table

SELECT custNo, custName, custSt, custCity FROM customer;

OR (use * for all columns)

SELECT * FROM CUSTOMER;

custNo custName custSt custCity age

1 C1 Olaya St Jeddah 20

2 C2 Mains St Riyadh 30

3 C3 Mains Rd

Riyadh 25

4 C4 Mains Rd

Dammam

5 C5 Mains Rd

Riyadh

14

Page 15: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Simple Queries : Compound comparison search condition

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

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

15

Page 16: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Examples

Example: Find all orders of product 100 before 02/01/03. SELECT * FROM orders WHERE prodNo = 100 AND ordDate <'02-jan-2003';

Example: Find all products priced less than 200 or greater than 300 SELECT * FROM product WHERE price < 200 OR price >300;

ordNo ordDate custNo prodNo quantity

1 01-jan-2003 1 100 2

4 01-jan-2003 3 100 2

prodNo prodName

prodDes price

100 P0 Food 100

101 P1 healthy food 100

16

Page 17: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

More Examples

Example: Find the customer with name C1 and live in Riyadh or Jeddah

SELECT * FROM customers WHERE custName ='C1‘ AND (custCity='Jeddah' OR custCity='Riyadh');

custNo custName custSt custCity age

1 C1 Olaya St Jeddah 20

17

Page 18: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Simple Queries : BETWEEN / NOT BETWEEN

• The BETWEEN operator is used to select values within a range.• The NOT BETWEEN checks if a value is outside a range.

• Syntax:

18

SELECT column_name(s) FROM table_name WHERE column_name BETWEEN |NOT BETWEEN value1 AND value2;

Page 19: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

BETWEEN ExampleExample: List products priced between 200 and 300.

SELECT * FROM product WHERE price >=200 and price <=300;

or equivalently

SELECT * FROM product

WHERE price between 200 and 300;

prodNo prodName prodDes price102 P2 200

103 P3 self_raising flour,80%wheat

300

104 P4 network 80x 300

19

Page 20: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Simple Queries : 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

• Syntax

20

SELECT column_name(s) FROM table_name WHERE column_name IN| NOT IN (value1,value2,...);

Page 21: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

IN Example

Example: List all customers living in Riyadh, or Dammam, or Jeddah.

SELECT * FROM Customer

WHERE custCity = 'Jeddah' OR custCity = 'Riyadh' ORcustCity = 'Dammam';

or equivalently

SELECT *FROM Customer

WHERE custCity IN (‘Jeddah', ‘Riyadh', ‘Dammam');

21

custNo custName custSt custCity age

1 C1 Olaya St Jeddah 20

2 C2 Mains St Riyadh 30

3 C3 Mains Rd Riyadh 25

4 C4 Mains Rd Dammam

5 C5 Mains Rd Riyadh

Page 22: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Simple Queries : LIKE / NOT LOKE

• LIKE is used to search for a specified pattern in a column.• NOT LIKE allows you to select records that does NOT match the

pattern.

• Syntax

• SQL has special pattern matching symbol• % represents any sequence of zero or more characters• _ represents any single character

22

SELECT column_name(s) FROM table_name WHERE column_name LIKE | NOT LIKE ‘pattern’;

Page 23: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

LIKE Example

Example: List all products whose description contain the string 'Food'.

SELECT * FROM productWHERE prodDes LIKE '%Food%';

prodNo prodName prodDes price

100 P0 Food 100101 P1 healthy food 100

23

Page 24: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

More Examples of LIKE | NOT LIKE

LIKE 'H_' : any string beginning with H and exactly 2 characters long.

NOT LIKE 'H%': any string not beginning with H

LIKE '%y': any string ending with 'y'

24

Page 25: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Simple Queries : IS NULL and IS NOT NULL

• It is not possible to test for NULL values with comparison operators, such as =, <, or <>.

• To test for null values in a query, use IS NULL or IS NOT NULL in the WHERE clause.

• Comparisons between a NULL and any other value, return unknown and the result will not be included in the final results

• Syntax

25

SELECT column_name(s) FROM table_name WHERE column_name IS NULL;

Page 26: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

IS NULL and IS NOT NULL Examples

Example: List all products with a product description.

SELECT * FROM productWHERE prodDes IS NOT NULL;

Similarly, to list products without description, useSELECT * FROM productWHERE prodDes IS NULL;

prodNo prodName prodDes price

100 P0 Food 100

101 P1 healthy Food 100

103 P3 self_raising flour,80%wheat

300

104 P4 network 80x 300

prodNo prodName prodDes price

102 P2 200

26

Page 27: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

27

Page 28: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Simple Queries : Use of DISTINCT

• Use Distinct in the select statement To remove duplicate values

• Syntax

28

SELECT DISTINCT column_name,column_name FROM table_name;

Page 29: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Use of DISTINCT Example

Example: List all customer cities.

SELECT custCity FROM customer;

• A city will be repeated if there are more than one customer in that city. To eliminate the duplicates, use:

SELECT DISTINCT custCity FROM customer;

custCityJeddah

Riyadh

Dammam

custCityJeddah

Riyadh

Riyadh

Dammam

Riyadh

29

Page 30: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Simple Queries : Ordering of Rows

• Rows can be put in ascending or descending order of some columns. To do this, use ORDER BY

• Syntax

• Default order (ie, if desc is not used) is ascending 30

SELECT column_name,column_name FROM table_name WHERE Condition ORDER BY column_name,column_name ASC|DESC

Page 31: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Ordering of Rows Example

• Example: list all products in descending order of price

SELECT * FROM product ORDER BY price desc;

• Can also order by several attributes, eg. ORDER BY price desc, prodName;

prodNo prodName prodDes price

103 P3 self_raising flour,80%wheat

300

104 P4 network 80x 300

102 P2 200

100 P0 Food 100

101 P1 healthy Food 100

31

Page 32: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Ordering of Rows Example

SELECT * FROM Individual ORDER BY FirstName, LastName

32

Page 33: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Operators summary

33

Operator Function

= equal to

< less than

> greater than

<= less than equal to

>= greater than equal to

<> not equal to

LIKE % used as wildcard. eg. LIKE ‘%PRE%’

IN test for in an enumerated list.

BETWEEN used to select values within a range

Page 34: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

34

Page 35: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Update Statement

• The UPDATE statement is used to update records in a table

• Syntax

Notice the WHERE clause in the SQL UPDATE statement!The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!

35

UPDATE table_name SET column1=value1, column2=value2,... WHERE some_column=some_value

Page 36: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Update Statement Example

• Example : Customer C1 has changed his city to Riyadh.

UPDATE Customer SET custCity=‘Riyadh‘ , custSt='12 Mains Rd' WHERE CustName=C1';

Output: 1 row updatedSelect * From Customer ;

36

custNo custName custSt custCity age

1 C1 12 Main Rd Riyadh 20

2 C2 Mains St Riyadh 30

3 C3 Mains Rd Riyadh 25

4 C4 Mains Rd Dammam

5 C5 Mains Rd Riyadh

Page 37: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Update Statement Example

Be careful when updating records. If we had omitted the WHERE clause, in the example before, like this:

UPDATE Customer SET custCity=‘Riyadh‘ , custSt='12 Mains Rd' ; Output: 5 rows updatedSelect * From Customer ;

37

custNo custName custSt custCity age

1 C1 12 Main Rd Riyadh 20

2 C2 12 Main Rd Riyadh 30

3 C3 12 Main Rd Riyadh 25

4 C4 12 Main Rd Riyadh

5 C5 12 Main Rd Riyadh

Page 38: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Delete Statement

• The DELETE statement is used to delete records in a table.

• Syntax

Notice the WHERE clause in the SQL DELETE statement!The WHERE clause specifies which record or records that should be deleted. If you omit the WHERE clause, all records will be deleted!

DELETE does not delete the table itself, only rows in the table.

38

DELETE FROM table_name WHERE some_column=some_value;

Page 39: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Delete Statement Example

• Example : Delete Customer C1

DELETE FROM Customer WHERE CustName=C1';

Output: 1 row deleted

Select * From Customer ;

39

custNo custName custSt custCity age

2 C2 Mains St Riyadh 30

3 C3 Mains Rd Riyadh 25

4 C4 Mains Rd Dammam

5 C5 Mains Rd Riyadh

Page 40: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Delete Statement Example

• Example : Delete all Customers

DELETE FROM Customer Output: 5 row deleted

Select * From Customer ;

40

custNo custName custSt custCity age

Page 41: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Truncate Statement

• TRUNCATE deletes all data in a table and frees storage space for the table rows ( deletes data faster but you cannot rollback)

• Syntax

TRUNCATE get rid of the data but not the table itself (DROP)

41

Trancate table_name;

Page 42: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Truncate Example

• Example : Delete all Products

TRUNCATE TABLE product;

42

prodNo prodName

prodDes price

Page 43: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Extra Example

Employee No. First Name Last Name Dept Number Salary

E1 Mandy Smith D1 50000

E2 Daniel Hodges D2 45000

E3 Shaskia Ramanthan D2 58000

Dept Number Dept Name Location Mail Number

D1 Computer Science Bundoora 39

D2 Information Science Bendigo 30

D3 Physics Bundoora 37

D4 Chemistry Bendigo 35

DEPARTMENT

EMPLOYEE

43

Page 44: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Basic SQL SELECT Queries

SELECT firstName, lastNameFROM Employee WHERE employeeNo = ‘E1’;

44

Page 45: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Compound Comparison

SELECT deptNumberFROM EMPLOYEEWHERE lastName = ‘Smith’OR lastName = ‘Hodges’;

45

SELECT DISTINCT deptNumber

FROM EMPLOYEE;

Duplicate Removal

Page 46: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

Set Membership Search ( IN)

SELECT deptNumber, mailNumberFROM DEPARTMENTWHERE deptName IN ( ‘Computer Science’, ‘Physics’);

Pattern Match Search ( LIKE)

SELECT employeeNo, deptNumberFROM EMPLOYEEWHERE firstName LIKE ‘%an%’;

46

Page 47: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

SELECT employeeNo , lastNameFROM EMPLOYEEORDER BY lastName;

Sorting Output from Queries

47

Page 48: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

48

Page 49: Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.

Lec

ture

6

References

• “Database Systems: A Practical Approach to Design, Implementation and Management.” Thomas Connolly, Carolyn Begg. 5th Edition, Addison-Wesley, 2009.

49