chapter five data manipulation language (dml) objectives oracle dbms understanding the dml general...

47
Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of Operators String Processing Concept of NULL Conditional Statement

Upload: simon-lester

Post on 18-Jan-2016

233 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

Chapter FiveData Manipulation Language (DML)

Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of Operators String Processing Concept of NULL Conditional Statement

Page 2: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

2

Example:

Student (Name, ID, GPA, Major, B_Date)

Course (C_Num, Dept, Title, Cr)

Student_Course (ID, C_Num, Dept, Grade)

Faculty (ID, Name, Dept, Salary, Area)

Faculty_Course (ID, C_Num, Dept, Semester)

Department (Name, Num_Faculty)

Tables

Page 3: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

3

General Format

SELECT fieldnamesFROM relation[ WHERE condition][ GROUP BY group_field ][ HAVING condition] [ ORDER BY fieldname]

;

Page 4: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

4

Select Attributes: Example:

Show the name and GPA of the students (ALL).

 SELECT name, GPAFROM student;

Page 5: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

5

Select Attributes: Example:

List all the columns in course 

SELECT *FROM course;

Page 6: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

6

General Format

Table display: Default justification:

-Date and characters LEFT-Number RIGHT

Default display:-Uppercase

Page 7: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

7

General Format

NAME GPA------------ ----------MARY 3.1

Page 8: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

8

Practice:

List all columns in customer table

Page 9: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

9

Duplicated Rows Example:

List of the course credits offered at FSU.

SELECT CrFROM Course;

Page 10: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

10

Use of Distinct: Example:

Type of the course credits offered at FSU.

SELECT DISTINCT CrFROM Course;

Page 11: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

11

Practice:

List of cities in customer table. (Unique city name)

Page 12: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

12

Use of Aliases: Example:List of the faculty’s name and their

salary per month.  SELECT name, salary / 12FROM faculty;

Page 13: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

13

Use of Aliases: Rename column heading

Example: List of the faculty salary for next year with 5% increase.

 

SELECT name, salary Pay,

salary+salary*0.05 AS New_Salary

FROM faculty;

Page 14: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

14

Use of Aliases: NAME PAY NEW_SALARY -------- ------ -----------------

Page 15: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

15

Practice:

Produce the following list:Customer_City Customer_State Customer_Zip_Code----------------- ------------------ ------------------------

Page 16: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

16

Use of Arithmetic Operations: () -, *, / +, -

Operation of some priority is evaluated from left to right

5* (2+1)

Page 17: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

17

Use of Arithmetic Operations: Example: List the course numbers and credits

in a quarter system 

SELECT C_Num, Cr * 0.75FROM Course;

Page 18: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

18

Use of Concatenation: Example:

List of faculty and department as a unit

SELECT name || dept “Name:”FROM faculty;

Name:

-----------------------------CHITSAZCOSC

Page 19: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

19

Use of Literal: Example:

List of faculty and department

SELECT name || ‘ ‘ || ‘is in ’ || dept “Department Name:”FROM faculty;

Department Name:-------------------------CHITSAZ is in COSC

Page 20: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

20

Condition statements:

SELECT name, GPAFROM studentWHERE GPA > 2;

Page 21: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

21

Condition Operators: =, >, >=, <=, <, <> != ^= IN BETWEEN ..... AND ..... LIKE IS NULL AND, OR, NOT

Page 22: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

22

String & Date Comparison Example: List the students who born on March 2, 99 

SELECT nameFROM studentWHERE B_Date =’02-MAR-99’ ;

Date Format ‘DD-MON-YY’

Page 23: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

23

Use of Boolean Operations: Example:

List of Student names that have a GPA > 3and majoring in COSC

SELECT nameFROM studentWHERE GPA > 3 AND major = 'COSC';

Character string is case sensitive & should be in a single quotation mark

Page 24: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

24

Question

What kind of information you get if you use this condition:

SELECT name FROM studentWHERE GPA > 3 OR major = 'COSC';

Page 25: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

25

Question

What kind of information you get if you use this condition: SELECT name FROM studentWHERE (GPA > 3 AND major = 'COSC')OR (GPA>2.5 AND major=‘ART ’);

Page 26: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

26

Practice:

List of Last name, First name of customers with a balance > 2000 and their birth date is before March, 01, 1985.

Page 27: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

27

Precedence Rule: >, >=, <>, <=, =, NOT AND OR

Page 28: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

28

Practice:

List Last name of customer which have a balance >100 or credit limit <2000 and live in MD from the customer table.

Page 29: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

29

Null vs. No Value Null value is:

UnavailableUnassignedUnknownInapplicable

Examples: Null is not the same as zero or blank

Page 30: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

30

Null vs. No Value SELECT name, Major

FROM Student;

SELECT name, Num_FacultyFROM Department;

Page 31: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

31

Null Values in Expressions Result of an arithmetic expression

with a null value is null.

SELECT name, GPA*0.75FROM Student;

Page 32: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

32

Null vs. No Value List of students with no major SELECT name

FROM StudentWHERE major IS NULL;

SELECT nameFROM StudentWHERE major IS NOT NULL;

Page 33: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

33

Null vs. No Value NVL Null Value substitution:

We can substitute a value for a NULL value record by using NVL.

List of students and their major:

SELECT name, NVL(major, ‘unknown’)FROM Student;

SELECT name, NVL(GPA, 0.0)FROM Student;

Page 34: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

34

Practice:

List of customer first and last names which have not been assigned a sales rep. number.

Page 35: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

35

Use of Between BETWEEN: Test against a list of values:

(Check the data in a range)

List description of courses with the course number between 200 AND 299

SELECT TitleFROMCourseWHERE C_Num BETWEEN 200 AND 299;

Page 36: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

36

Use of BetweenSELECT TitleFROM CourseWHERE C_Num NOT BETWEEN 200

AND 299;

SELECT TitleFROM CourseWHERE (C_Num >= 200) AND

(C_Num <= 299);

Page 37: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

37

Use of Between List of Faculty’s name from D to E

SELECT nameFROM facultyWHERE name BETWEEN ‘D’ AND ‘E’;

Page 38: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

38

Question

List of names starting with D only!

Page 39: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

39

Practice:

List the order numbers of items with a order quoted price between $100 and $1000

Page 40: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

40

Use of IN IN: Tests against a list of values: (Set of

values used for comparison)

List of students with ID = 1111, ID = 2111 or ID = 3111

SELECT nameFROM StudentWHERE ID IN (1111, 2111, 3111);

Page 41: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

41

Use of INSELECT nameFROM StudentWHERE major IN (‘COSC’, ‘MATH’);

SELECT name

FROM Student

WHERE major NOT IN (‘COSC’, ‘MATH’);

Page 42: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

42

Practice:

List customer number, first and last name of customers with Zip code of 11011 or 12011 or 10012 or 11001

Page 43: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

43

Use of LIKE LIKE: Determines the presence of

a sub string

(_) a single unknown character(%) any number of unknown

characters

Page 44: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

44

Use of LIKE List all the student’s records so

that the student’s name starts with a ‘K’

SELECT *FROM StudentWHERE name LIKE ‘K%’;

Page 45: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

45

Use of LIKE List of students records with the

second character to be ‘K’

SELECT *FROM StudentWHERE name LIKE ‘_K’;

Page 46: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

46

Practice:

List of customers with the last name ending with “SON” like Jackson, Nelson, Larson.

Page 47: Chapter Five Data Manipulation Language (DML) Objectives Oracle DBMS Understanding the DML General format of SQL Capability of SELECT statement Use of

47

Practice:

List of customer Last names who live in a street name which has a character string ‘upper’ like ‘South upper Potomac’, ‘upper Dakota’, ‘Magnolia upper’