79
6 1 Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel

Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

  • Upload
    others

  • View
    11

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

6

1

Chapter 6

Introduction to Structured QueryLanguage (SQL)

Database Systems:Design, Implementation, and Management,

Sixth Edition, Rob and Coronel

Page 2: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

2

In this chapter, you will learn:

•The basic commands and functions of SQL

•How to use SQL for data administration (tocreate tables, indexes, and views)

•How to use SQL for data manipulation (to add,modify, delete, and retrieve data)

•How to use SQL to query a database to extractuseful information

Page 3: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

3

Introduction to SQL

•SQL functions fit into two broad categories:•Data definition language

–SQL includes commands to create•Database objects such as tables, indexes, and

views•Commands to define access rights to those

database objects

•Data manipulation language–Includes commands to insert, update, delete,

and retrieve data within the database tables

Page 4: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

4

Introduction to SQL (continued)

•SQL is relatively easy to learn

•Basic command set has a vocabulary of lessthan 100 words

•Nonprocedural language

•American National Standards Institute (ANSI)prescribes a standard SQL

•Several SQL dialects exist

Page 5: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

5

SQL Data Definition Commands

Page 6: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

6

Data Manipulation Commands

Page 7: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

7

Data Definition Commands

•Examine the simple database model and thedatabase tables that will form the basis for themany SQL examples

•Understand the data environment

Page 8: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

8

The Database Model

Page 9: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

9

Creating the Database

•Two tasks must be completed–create the database structure

–create the tables that will hold the end-userdata

•First task–RDBMS creates the physical files that will hold

the database

–Tends to differ substantially from one RDBMSto another

Page 10: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

10

The Database Schema

•Authentication–Process through which the DBMS verifies that

only registered users are able to access thedatabase

–Log on to the RDBMS using a user ID and apassword created by the databaseadministrator

•Schema–Group of database objects—such as tables

and indexes—that are related to each other

Page 11: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

11

Data Types

•Data type selection is usually dictated by thenature of the data and by the intended use

•Pay close attention to the expected use ofattributes for sorting and data retrievalpurposes

Page 12: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

12

Some Common SQL Data Types

Page 13: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

13

Creating Table Structures

•Use one line per column (attribute) definition•Use spaces to line up the attribute characteristics and

constraints•Table and attribute names are capitalized•NOT NULL specification•UNIQUE specification•Primary key attributes contain both a NOT NULL and a

UNIQUE specification•RDBMS will automatically enforce referential integrity

for foreign keys•Command sequence ends with a semicolon

Page 14: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

14

Other SQL Constraints

•NOT NULL constraint

–Ensures that a column does not accept nulls

•UNIQUE constraint

–Ensures that all values in a column are unique

•DEFAULT constraint

–Assigns a value to an attribute when a new row isadded to a table

•CHECK constraint

–Validates data when an attribute value is entered

Page 15: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

15

SQL Indexes•When a primary key is declared, DBMS

automatically creates a unique index

•Often need additional indexes

•Using the CREATE INDEX command, SQLindexes can be created on the basis of anyselected attribute

•Composite index

–Index based on two or more attributes

–Often used to prevent data duplication

Page 16: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

16

A Duplicated TEST Record

Page 17: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

17

Data Manipulation Commands

•Adding table rows

•Saving table changes

•Listing table rows

•Updating table rows

•Restoring table contents

•Deleting table rows

•Inserting table rows with a select subquery

Page 18: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

18

Common SQL Data ManipulationCommands

Page 19: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

19

A Data View and Entry Form

Page 20: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

20

Saving Table Changes

•Changes made to table contents are notphysically saved on disk until–Database is closed

–Program is closed

–COMMIT command is used

•Syntax–COMMIT [WORK]

•Will permanently save any changes made toany table in the database

Page 21: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

21

Listing Table Rows

•SELECT–Used to list contents of table

•Syntax–SELECT columnlist

FROM tablename

•Columnlist represents one or more attributes,separated by commas

•Asterisk can be used as wildcard character tolist all attributes

Page 22: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

22

Updating Table Rows

•UPDATE

–Modify data in a table

•Syntax

–UPDATE tablenameSET columnname = expression [, columname= expression][WHERE conditionlist];

•If more than one attribute is to be updated inthe row, separate corrections with commas

Page 23: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

23

Restoring Table Contents•ROLLBACK

–Used restore the database to its previouscondition

–Only applicable if COMMIT command has notbeen used to permanently store the changesin the database

•Syntax–ROLLBACK;

•COMMIT and ROLLBACK only work withdata manipulation commands that are used toadd, modify, or delete table rows

Page 24: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

24

Deleting Table Rows

•DELETE

–Deletes a table row

•Syntax

–DELETE FROM tablename[WHERE conditionlist ];

•WHERE condition is optional

•If WHERE condition is not specified, all rowsfrom the specified table will be deleted

Page 25: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

25

Inserting Table Rows with a SelectSubquery

•INSERT–Inserts multiple rows from another table

(source)–Uses SELECT subquery

•Query that is embedded (or nested) insideanother query

•Executed first

•Syntax–INSERT INTO tablename SELECT columnlist

FROM tablename

Page 26: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

26

Selecting Rows with ConditionalRestrictions

•Select partial table contents by placingrestrictions on rows to be included in output

–Add conditional restrictions to the SELECTstatement, using WHERE clause

•Syntax

–SELECT columnlistFROM tablelist[ WHERE conditionlist ] ;

Page 27: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

27

Selected PRODUCT Table Attributes forVENDOR Code 21344

Page 28: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

28

The Microsoft Access QBE and its SQL

Page 29: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

29

Comparison Operators

Page 30: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

30

Selected PRODUCT Table Attributes forVENDOR Codes Other than 21344

Page 31: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

31

Selected PRODUCT Table Attributeswith a P_PRICE Restriction

Page 32: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

32

Selected PRODUCT Table Attributes:The ASCII Code Effect

Page 33: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

33

Selected PRODUCT Table Attributes:Date Restriction

Page 34: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

34

SELECT Statementwith a Computed Column

Page 35: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

35

SELECT Statement with a ComputedColumn and an Alias

Page 36: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

36

Arithmetic Operators:The Rule of Precedence

•Perform operations within parentheses

•Perform power operations

•Perform multiplications and divisions

•Perform additions and subtractions

Page 37: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

37

Selected PRODUCT Table Attributes:The Logical OR

Page 38: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

38

Selected PRODUCT Table Attributes:The Logical AND

Page 39: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

39

Selected PRODUCT Table Attributes:The Logical AND and OR

Page 40: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

40

Special Operators

•BETWEEN–Used to check whether attribute value is within a

range•IS NULL

–Used to check whether attribute value is null•LIKE

–Used to check whether attribute value matches agiven string pattern

•IN–Used to check whether attribute value matches

any value within a value list•EXISTS

–Used to check if a subquery returns any rows

Page 41: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

41

Advanced Data Definition Commands

•All changes in the table structure are madeby using the ALTER command

–Followed by a keyword that produces specificchange

–Three options are available

•ADD

•MODIFY

•DROP

Page 42: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

42

Changing a Column’s Data Type

•ALTER can be used to change data type

•Some RDBMSs (such as Oracle) do notpermit changes to data types unless thecolumn to be changed is empty

Page 43: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

43

Changing a Column’s DataCharacteristics

•Use ALTER to change data characteristics

•If the column to be changed already containsdata, changes in the column’s characteristicsare permitted if those changes do not alterthe data type

Page 44: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

44

Adding or Dropping a Column

•Use ALTER to add a column

–Do not include the NOT NULL clause for newcolumn

•Use ALTER to drop a column

–Some RDBMSs impose restrictions on thedeletion of an attribute

Page 45: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

45

The Effect of Data Entry into the NewP_SALECODE Column

Page 46: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

46

Update of the P_SALECODE Column inMultiple Data Rows

Page 47: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

47

The Effect of Multiple Data Updates in thePRODUCT Table (MS Access)

Page 48: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

48

Copying Parts of Tables

•SQL permits copying contents of selectedtable columns so that the data need not bereentered manually into newly created table(s)

•First create the PART table structure

•Next add rows to new PART table usingPRODUCT table rows

Page 49: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

49

PART Attributes Copiedfrom the PRODUCT Table

Page 50: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

50

Advanced Select Queries

•SQL provides useful functions

–Count

–Find minimum and maximum values

–Calculate averages

•SQL allows the user to limit queries to onlythose entries having no duplicates or entrieswhose duplicates may be grouped

Page 51: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

51

Selected PRODUCT Table Attributes:Ordered by (Ascending) P_PRICE

Page 52: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

52

Partial Listing ofEMPLOYEE Table Contents

Page 53: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

53

Telephone List Query Results

Page 54: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

54

A Query Based on Multiple Restrictions

Page 55: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

55

A Listing of Distinct (Different) V_CODEValues in the PRODUCT Table

Page 56: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

56

Some Basic SQL Aggregate Functions

Page 57: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

57

COUNT Function Output Examples

Page 58: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

58

MAX and MIN Function Output Examples

Page 59: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

59

The Total Value of All Itemsin the PRODUCT Table

Page 60: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

60

AVG Function Output Examples

Page 61: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

61

GROUP BY Clause Output Examples

Page 62: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

62

Incorrect and Correct Useof the GROUP BY Clause

Page 63: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

63

An Application of the HAVING Clause

Page 64: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

64

Virtual Tables: Creating a View

•View is a virtual table based on a SELECTquery–Can contain columns, computed columns,

aliases, and aggregate functions from one ormore tables

•Base tables are tables on which the view isbased

•Create a view by using the CREATE VIEWcommand

Page 65: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

65

Creating a Virtual Tablewith the CREATE VIEW Command

Page 66: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

66

Joining Database Tables

•Ability to combine (join) tables on commonattributes is most important distinctionbetween a relational database and otherdatabases

•Join is performed when data are retrievedfrom more than one table at a time

•Join is generally composed of an equalitycomparison between the foreign key and theprimary key of related tables

Page 67: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

67

Creating Links Through Foreign Keys

Page 68: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

68

The Results of a Join

Page 69: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

69

An Ordered and Limited ListingAfter a JOIN

Page 70: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

70

The Contents of the EMP Table

Page 71: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

71

Using an Alias to Join a Table to Itself

Page 72: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

72

The Left Outer Join Results

Page 73: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

73

The Right Outer Join Results

Page 74: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

74

Converting an ER Modelinto a Database Structure

•Requires following specific rules that governsuch a conversion

•Decisions made by the designer to governdata integrity are reflected in the foreign keyrules

•Implementation decisions vary according tothe problem being addressed

Page 75: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

75

The Ch06_Artist Database ERDand Tables

Page 76: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

76

A Data Dictionaryfor the Ch06_Artist Database

Page 77: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

77

A Summary of Foreign Key Rules

Page 78: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

78

Summary•SQL commands can be divided into two

overall categories:–Data definition language commands–Data manipulation language commands

•Basic data definition commands allow you tocreate tables, indexes, and views

•Many SQL constraints can be used withcolumns

•Aggregate functions–Special functions that perform arithmetic

computations over a set of rows

Page 79: Chapter 6 - KNREDDY · Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel. Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

6

79

Summary (continued)•ORDER BY clause

–Used to sort output of a SELECT statement–Can sort by one or more columns and use

either an ascending or descending order

•Join output of multiple tables with SELECTstatement

•Natural join uses join condition to match onlyrows with equal values in specified columns

•Right outer join and left outer join used toselect rows that have no matching values inother related table