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

Slides:



Advertisements
Similar presentations
Introduction to Structured Query Language (SQL)
Advertisements

7 7 SQL Data Definition 4Spread throughout chapter 7.
Database Systems: Design, Implementation, and Management Tenth Edition
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Introduction to Structured Query Language (SQL)
5 Chapter 5 Structured Query Language (SQL2) Revision.
Introduction to Structured Query Language (SQL)
Structured Query Language (SQL)
3 3 Chapter 3 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
5 Chapter 5 Structured Query Language (SQL1) Revision.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Introduction to Structured Query Language (SQL)
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Concepts of Database Management Sixth Edition
Microsoft Access 2010 Chapter 7 Using SQL.
Introduction to SQL J.-S. Chou Assistant Professor.
ITEC 3220A Using and Designing Database Systems Instructor: Gordon Turpin Course Website: Office: CSEB3020.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
Chapter 7 Introduction to Structured Query Language (SQL)
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Chapter 3 Single-Table Queries
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
Concepts of Database Management Seventh Edition
Database Systems: Design, Implementation, and Management Tenth Edition
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
3 3 Chapter 3 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Chapter 6 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management Peter Rob & Carlos Coronel.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
1 Chapter 6 Structured Query Language (SQL) DATABASE MANAGEMENT SYSTEM.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
SQL – Structured Query Language
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
IS 380 Introduction to SQL This lectures covers material from: database textbook chapter 3 Oracle chapter: 3,14,17.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
Week 4 Lecture Part 2 of 3 Structured Query Language (SQL) Samuel ConnSamuel Conn, Faculty Suggestions for using the Lecture Slides.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 4: Intermediate.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
CSC314 DAY 8 Introduction to SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall SQL OVERVIEW  Structured Query Language  The.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
SQL Query Getting to the data ……..
Chapter 5 Introduction to SQL.
Advanced SQL Advanced SQL Week 8 Comparison Operators
ATS Application Programming: Java Programming
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Chapter 7 Introduction to Structured Query Language (SQL)
Contents Preface I Introduction Lesson Objectives I-2
Chapter # 7 Introduction to Structured Query Language (SQL) Part I.
Presentation transcript:

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

6 2 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel In this chapter, you will learn: The basic commands and functions of SQL How to use SQL for data administration (to create 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 extract useful information

6 3 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel 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

6 4 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Introduction to SQL ( continued ) SQL is relatively easy to learn Basic command set has a vocabulary of less than 100 words Nonprocedural language American National Standards Institute (ANSI) prescribes a standard SQL Several SQL dialects exist

6 5 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel SQL Data Definition Commands

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

6 7 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Data Definition Commands Examine the simple database model and the database tables that will form the basis for the many SQL examples Understand the data environment

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

6 9 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Creating the Database Two tasks must be completed –create the database structure –create the tables that will hold the end-user data First task –RDBMS creates the physical files that will hold the database –Tends to differ substantially from one RDBMS to another

6 10 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel The Database Schema Authentication –Process through which the DBMS verifies that only registered users are able to access the database –Log on to the RDBMS using a user ID and a password created by the database administrator Schema –Group of database objects—such as tables and indexes—that are related to each other

6 11 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Data Types Data type selection is usually dictated by the nature of the data and by the intended use Pay close attention to the expected use of attributes for sorting and data retrieval purposes

6 12 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Some Common SQL Data Types

6 13 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel 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

6 14 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel 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 is added to a table CHECK constraint –Validates data when an attribute value is entered

6 15 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel CREATE TABLE Product (p_code CHAR(10), p_descript VARCHAR2(35), p_indate DATE, p_onhand SMALLINT, p_min SMALLINT, p_price NUMBER(8,2), p_discount NUMBER(4,2), v_code INTEGER CONSTRAINT v_code_fk REFERENCES Vendor(v_code), CONSTRAINT p_code_pk PRIMARY KEY (p_code));

6 16 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel SQL Indexes When a primary key is declared, DBMS automatically creates a unique index Often need additional indexes Using the CREATE INDEX command, SQL indexes can be created on the basis of any selected attribute Composite index –Index based on two or more attributes –Often used to prevent data duplication

6 17 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel A Duplicated TEST Record

6 18 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel 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

6 19 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Common SQL Data Manipulation Commands

6 20 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel A Data View and Entry Form

6 21 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Saving Table Changes Changes made to table contents are not physically saved on disk until –Database is closed –Program is closed –COMMIT command is used Syntax –COMMIT [WORK] Will permanently save any changes made to any table in the database

6 22 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel 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 to list all attributes

6 23 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Updating Table Rows UPDATE –Modify data in a table Syntax –UPDATE tablename SET columnname = expression [, columname = expression] [WHERE conditionlist]; If more than one attribute is to be updated in the row, separate corrections with commas

6 24 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Restoring Table Contents ROLLBACK –Used restore the database to its previous condition –Only applicable if COMMIT command has not been used to permanently store the changes in the database Syntax –ROLLBACK; COMMIT and ROLLBACK only work with data manipulation commands that are used to add, modify, or delete table rows

6 25 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel 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 rows from the specified table will be deleted

6 26 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Inserting Table Rows with a Select Subquery INSERT –Inserts multiple rows from another table (source) –Uses SELECT subquery Query that is embedded (or nested) inside another query Executed first Syntax –INSERT INTO tablename SELECT columnlist FROM tablename

6 27 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Selecting Rows with Conditional Restrictions Select partial table contents by placing restrictions on rows to be included in output –Add conditional restrictions to the SELECT statement, using WHERE clause Syntax –SELECT columnlist FROM tablelist [ WHERE conditionlist ] ;

6 28 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Selected PRODUCT Table Attributes for VENDOR Code 21344

6 29 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel The Microsoft Access QBE and its SQL

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

6 31 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Selected PRODUCT Table Attributes for VENDOR Codes Other than 21344

6 32 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Selected PRODUCT Table Attributes with a P_PRICE Restriction

6 33 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Selected PRODUCT Table Attributes: The ASCII Code Effect

6 34 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Selected PRODUCT Table Attributes: Date Restriction

6 35 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel SELECT Statement with a Computed Column

6 36 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel SELECT Statement with a Computed Column and an Alias

6 37 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Arithmetic Operators: The Rule of Precedence Perform operations within parentheses Perform power operations Perform multiplications and divisions Perform additions and subtractions

6 38 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Selected PRODUCT Table Attributes: The Logical OR

6 39 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Selected PRODUCT Table Attributes: The Logical AND

6 40 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Selected PRODUCT Table Attributes: The Logical AND and OR

6 41 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel 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 a given 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

6 42 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Advanced Data Definition Commands All changes in the table structure are made by using the ALTER command –Followed by a keyword that produces specific change –Three options are available ADD MODIFY DROP

6 43 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Changing a Column’s Data Type ALTER can be used to change data type Some RDBMSs (such as Oracle) do not permit changes to data types unless the column to be changed is empty

6 44 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Changing a Column’s Data Characteristics Use ALTER to change data characteristics If the column to be changed already contains data, changes in the column’s characteristics are permitted if those changes do not alter the data type

6 45 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Adding or Dropping a Column Use ALTER to add a column –Do not include the NOT NULL clause for new column Use ALTER to drop a column –Some RDBMSs impose restrictions on the deletion of an attribute

6 46 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel The Effect of Data Entry into the New P_SALECODE Column

6 47 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Update of the P_SALECODE Column in Multiple Data Rows

6 48 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel The Effect of Multiple Data Updates in the PRODUCT Table (MS Access)

6 49 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Copying Parts of Tables SQL permits copying contents of selected table columns so that the data need not be reentered manually into newly created table(s) First create the PART table structure Next add rows to new PART table using PRODUCT table rows

6 50 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel PART Attributes Copied from the PRODUCT Table

6 51 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Advanced Select Queries SQL provides useful functions –Count –Find minimum and maximum values –Calculate averages SQL allows the user to limit queries to only those entries having no duplicates or entries whose duplicates may be grouped

6 52 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Selected PRODUCT Table Attributes: Ordered by (Ascending) P_PRICE

6 53 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Partial Listing of EMPLOYEE Table Contents

6 54 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Telephone List Query Results

6 55 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel A Query Based on Multiple Restrictions

6 56 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel A Listing of Distinct (Different) V_CODE Values in the PRODUCT Table

6 57 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Some Basic SQL Aggregate Functions

6 58 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel COUNT Function Output Examples

6 59 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel MAX and MIN Function Output Examples

6 60 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel The Total Value of All Items in the PRODUCT Table

6 61 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel AVG Function Output Examples

6 62 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel GROUP BY Clause Output Examples

6 63 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Incorrect and Correct Use of the GROUP BY Clause

6 64 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel An Application of the HAVING Clause

6 65 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Virtual Tables: Creating a View View is a virtual table based on a SELECT query –Can contain columns, computed columns, aliases, and aggregate functions from one or more tables Base tables are tables on which the view is based Create a view by using the CREATE VIEW command

6 66 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Creating a Virtual Table with the CREATE VIEW Command

6 67 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Joining Database Tables Ability to combine (join) tables on common attributes is most important distinction between a relational database and other databases Join is performed when data are retrieved from more than one table at a time Join is generally composed of an equality comparison between the foreign key and the primary key of related tables

6 68 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Creating Links Through Foreign Keys

6 69 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel The Results of a Join

6 70 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel An Ordered and Limited Listing After a JOIN

6 71 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel The Contents of the EMP Table

6 72 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Using an Alias to Join a Table to Itself

6 73 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel The Left Outer Join Results

6 74 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel The Right Outer Join Results

6 75 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Grant and Revoke Privileges on Tables Security is an issue in the access of a table from a foreign account. The creator of the table has no restriction on access and may or may not pass along manipulation privileges to others. GRANT {priv | ALL} ON table-name TO {specific user | PUBLIC};

6 76 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Grant and Revoke Privileges on Tables GRANT SELECT, INSERT ON Customer TO fred, mary, joe; GRANT ALL GRANT SELECT ON Customer ON Customer TO fred; TO PUBLIC;

6 77 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Grant and Revoke Privileges on Tables SELECT * FROM mary.Customer; REVOKE SELECT ON Customer FROM PUBLIC;

6 78 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Exercises 1. Student(stno, sname) Grade_report(student_number, course_no,grade) (1) List the names of students who have earned A’s or B’s in any course? (2)List the names and grades of all students who have earned A’s and B’s in any course?

6 79 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Exercises course(course_number, credit_hours, offering_dept) department(dept_code, dept_name) Find the names of all departments that offer a course with “INTRO” in the title)

6 80 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Exercises Student(stno, sname, major, class) Grade_report(student_number, section_id, grade) Section(section_id, course_num, semester, year, instructor, bldg, room) List the student name, student major code, and section identifier of students who earned C’s in courses taught by Professor Johns (JOHNS)?

6 81 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Exercises Room(room_id, capacity) List all room have a below-average capacity?

6 82 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Nulls Revisited Name Salary Joe 1000 Sam 2000 Bill 3000 Dave SELECT count(*), avg(salary), sum(salary), max(salary), min(salary) FROM

6 83 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Nulls Revisited Name Salary Joe 1000 Sam 2000 Bill 3000 Dave SELECT count(*), avg(salary), sum(salary), max(salary), min(salary) FROM Sal;

6 84 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Nulls Revisited count(*) avg sum min max Select avg(NVL(salary, 0)) from sal; AVG(NVL(SALARY, 0)) 1500

6 85 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Delete and Referential Constraint On Delete Restrict –Default referential integrity delete option, which is not explicitly allowed in Oracle. On Delete Cascade (dangerous!) Create table employee ( empno number (3) primary key, empname varchar2(20), dept number(3) references department(deptno) on delete cascade);

6 86 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Delete and Referential Constraint On Delete Set NULL Create table employee ( empno number (3) primary key, empname varchar2(20), dept number(3) references department(deptno) on delete set NULL);

6 87 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Converting an ER Model into a Database Structure Requires following specific rules that govern such a conversion Decisions made by the designer to govern data integrity are reflected in the foreign key rules Implementation decisions vary according to the problem being addressed

6 88 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel The Ch06_Artist Database ERD and Tables

6 89 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel A Data Dictionary for the Ch06_Artist Database

6 90 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel A Summary of Foreign Key Rules

6 91 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel Summary SQL commands can be divided into two overall categories: –Data definition language commands –Data manipulation language commands Basic data definition commands allow you to create tables, indexes, and views Many SQL constraints can be used with columns Aggregate functions –Special functions that perform arithmetic computations over a set of rows

6 92 Database Systems: Design, Implementation, & Management, 6 th Edition, Rob & Coronel 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 SELECT statement Natural join uses join condition to match only rows with equal values in specified columns Right outer join and left outer join used to select rows that have no matching values in other related table