39
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

Embed Size (px)

DESCRIPTION

© 2002 by Prentice Hall3 Structured Query Language Structured Query Language is known as either: –Its acronym, SQL, or –SEQUEL, the name of the original version of SQL SEQUEL was developed by IBM in the mid- 1970s.

Citation preview

Page 1: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 1

Structured Query Language

Dav

id M

. Kro

enke

Database Concepts 1e Chapter 3

3

Page 2: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 2

Chapter Objectives• Learn basic SQL statements for creating

database constructs• Learn basic SQL SELECT statements and

options for processing a single table• Learn basic SQL SELECT statements for

processing multiple tables with subqueries• Learn basic SQL SELECT statements for

processing multiple tables with joins• Learn SQL statements to add, modify, and

delete data

Page 3: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 3

Structured Query Language• Structured Query Language is

known as either:– Its acronym, SQL, or– SEQUEL, the name of the original

version of SQL• SEQUEL was developed by IBM in the

mid-1970s.

Page 4: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 4

SQL Defined• SQL is not a programming language,

but rather a data sub-language• SQL has several functions

– Data Definition– Data Retrieval (Queries)– Data Manipulation/Updates– And others (not covered in this chapter)

• Data control• Transaction control

Page 5: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 5

SQL for Data Definition• The SQL data definition statements

include– CREATE

• To create database objects– ALTER

• To modify the structure and/or characteristics of database objects

– DROP• To delete database objects

Page 6: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 6

SQL for Data Definition: CREATE

• Creating database tables– The SQL CREATE TABLE statement

CREATE TABLE Employee (EmpID Integer Not Null,EmpName Char(25));

Page 7: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 7

Primary Key Constraint: ALTER• Adding primary key constraints to

an existing table– The SQL ALTER statement

ALTER TABLE EmployeeADD CONSTRAINT EmpPK PRIMARY KEY(EmpID);

Page 8: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 8

Composite Primary Key Constraints: ALTER• The SQL ALTER statement may also be used

to create a composite primary key constraint

CREATE TABLE Empl_Skill (EmpID Integer Not Null,SkillID Integer Not Null,SkillLevel Integer);

ALTER TABLE Empl_SkillADD CONSTRAINT EmpSkillPK PRIMARY KEY(EmpID, SkillID);

Page 9: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 9

Foreign Key Constraint: ALTER• Adding foreign key constraints to

an existing table– The SQL ALTER statement

ALTER TABLE EmployeeADD CONSTRAINT EmpFK FOREIGN KEY(DeptID)

REFERENCES Dept;

Page 10: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 10

Defining the Cascade Rules: ALTER• Using the SQL ALTER statement to

apply cascading rules

ALTER TABLE EmployeeADD CONSTRAINT EmpFK FOREIGN KEY(DeptID)

REFERENCES Dept ON DELETE CASCADE;

Page 11: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 11

Deleting Database Objects: DROP• To remove unwanted database

objects from the database, use the SQL DROP statement

• Warning… The DROP statement will permanently remove the object and all data

DROP TABLE Employee;

Page 12: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 12

Removing a Constraint: ALTER & DROP• To change the constraints on

existing tables, you may need to remove the existing constraints before adding new constraints

ALTER TABLE Employee DROP CONSTRAINT EmpFK;

Page 13: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 13

SQL for Data Retrieval (Queries)• The most well known SQL

statement…SELECT

• SELECT will retrieve information from the database that matches the specified criteria

SELECT EmpName FROM Emp;

Page 14: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 14

The Results of a Query is a Relation• A query pulls information from

several relations and creates (temporarily) a new relation

• This allows for a query to: – Create a new relation– Feed information to another query

(sub-queries)

Page 15: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 15

Showing a Row Only Once: DISTINCT• A qualifier may be added to the

SELECT statement to inhibit duplicate rows from displaying

SELECT DISTINCT DeptID from Emp;

Page 16: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 16

Specifying Search Criteria: WHERE• The WHERE clause stipulates the

matching criteria for the record that are to be displayed

SELECT EmpName FROM EmpWHERE DeptID = 15;

Page 17: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 17

Match Criteria• The WHERE clause match criteria

may include– Equals “=“– Not Equals “<>”– Greater than “>”– Less than “<“– Greater than or Equal to “>=“– Less than or Equal to “<=“

Page 18: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 18

Match Operators• Multiple matching criteria may be

specified using– AND

• Representing an intersection of the data sets

– OR• Representing a union of the data sets

Page 19: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 19

Operator ExamplesSELECT EmpName FROM EmpWHERE DeptID < 7 ORDeptID > 12;

SELECT EmpName FROM EmpWHERE DeptID = 9 ANDSalaryCode <= 23;

Page 20: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 20

A List of Values

• The WHERE clause may specify that a particular column value must be included in a list of values

SELECT EmpName FROM EmpWHERE DeptID IN (4, 8, 9);

Page 21: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 21

The Logical NOT Operator• Any criteria statement may be

preceded by a NOT operator which is to say that all information will be shown except that information matching the specified criteria

SELECT EmpName FROM EmpWHERE DeptID NOT IN (4, 8, 9);

Page 22: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 22

Finding Data Matching a Range of Values: BETWEEN

• SQL provides a BETWEEN statement that allows a user to specify a minimum and maximum value on one line

SELECT EmpName FROM EmpWHERE SalaryCode BETWEEN 10 AND 45;

Page 23: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 23

Allowing for Wildcard Searches: LIKE• Sometimes it may be

advantageous to find rows matching a string value using wildcards– Single character wildcard character is

an underscore (_)– Multiple character wildcard character

is a percent sign (%)

Page 24: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 24

Wildcard Search ExamplesSELECT EmpID FROM EmpWHERE EmpName LIKE ‘Kr%’;

SELECT EmpID FROM EmpWHERE Phone LIKE ‘616-___-____’;

Page 25: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 25

Displaying All Columns: *• To show all of the column values

for the rows that match the specified criteria, use an *

SELECT * FROM Emp WHERE EmpName LIKE ‘%land’;

Page 26: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 26

Sorting the Results: ORDER BY

• The results may be sorted using the ORDER BY clause

SELECT * FROM EmpORDER BY EmpName;

Page 27: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 27

Built-in SQL Functions• SQL provides several built-in functions

– COUNT• Counts the number of rows that match the

specified criteria– MIN

• Finds the minimum value for a specific column for those rows matching the criteria

– MAX• Finds the maximum value for a specific

column for those rows matching the criteria

Page 28: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 28

Built-in SQL Functions (continued)• SUM

– Calculates the sum for a specific column for those rows matching the criteria

• AVG– Calculates the numerical average of a

specific column for those rows matching the criteria

Page 29: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 29

Built-in Function Examples

SELECT COUNT(DISTINCT DeptID) FROM Emp;

SELECT MIN(Hours), MAX(Hours), AVG(Hours) FROM Project

WHERE ProjID > 7;

Page 30: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 30

Providing Subtotals: GROUP BY• Subtotals may be calculated by

using the GROUP BY clause

SELECT DeptID, COUNT(*), FROM EmpGROUP BY DeptIDHAVING Count(*) > 3;

Page 31: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 31

Retrieving Information from Multiple Tables• SubQueries

– As stated earlier, the result of a query is a relation. As a result, a query may feed another query. This is called a subquery

• Joins– Another way of combining data is by using a

Join • Equi-Join• Left Outer Join• Right Outer Join

Page 32: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 32

Subquery

SELECT EmpName FROM EmpWHERE DeptID in

(SELECT DeptID from Department WHERE DeptName LIKE ‘Accounts%’);

Page 33: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 33

Equi-JoinSELECT EmpName FROM Emp, DepartmentWHERE Emp.DeptID = Department.DeptID

AND Department.DeptName LIKE ‘Account%’;

Page 34: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 34

Outer Join• Right Outer Join

SELECT EmpName FROM Department RIGHT JOIN Emp WHERE Emp.DeptID = Department.DeptID ANDDepartment.DeptName LIKE ‘Account%’;

• Left Outer JoinSELECT EmpName FROM Emp LEFT JOIN Department WHERE Emp.DeptID = Department.DeptID ANDDepartment.DeptName LIKE ‘Account%’;

Page 35: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 35

Modifying Data using SQL• Insert

– Will add a new row in a table• Update

– Will update the data in a table that matches the specified criteria

• Delete– Will delete the data in a table that

matches the specified criteria

Page 36: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 36

Adding Data: INSERT• To add a row to an existing table,

use the INSERT statement

INSERT INTO Emp VALUES (91, ‘Smither’, 12);

INSERT INTO Emp (EmpID, SalaryCode) VALUES (62, 11);

Page 37: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 37

Changing Data Values: UPDATE• To change the data values in an existing

row (or set of rows) use the Update statement

UPDATE EmpSET Phone ‘791-555-1234’WHERE EmpID = 29;

UPDATE EmpSET DeptID = 44WHERE EmpName LIKE ‘Kr%’;

Page 38: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 38

Deleting Data: DELETE• To delete a row or set of rows from

a table using the DELETE statement

DELETE FROM EmpWHERE EmpID = 29;

DELETE FROM EmpWHERE EmpName LIKE ‘Kr%’;

Page 39: © 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3

© 2002 by Prentice Hall 39

Structured Query Language

Dav

id M

. Kro

enke

Database Concepts 1e Chapter 3

3