3 3 Chapter 3 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.

Slides:



Advertisements
Similar presentations
Data Definition and Integrity Constraints
Advertisements

Introduction to Structured Query Language (SQL)
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
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)
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
5 Chapter 5 Structured Query Language (SQL1) Revision.
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-1 David M. Kroenke Database Processing Chapter 7 Structured Query Language.
Introduction to SQL J.-S. Chou Assistant Professor.
ITEC 3220A Using and Designing Database Systems Instructor: Gordon Turpin Course Website: Office: CSEB3020.
The Relational Model These slides are based on the slides of your text book.
Chapter 7 Introduction to Structured Query Language (SQL)
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
6 1 Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Chapter 5 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management Peter Rob & Carlos Coronel.
Database Systems: Design, Implementation, and Management Tenth Edition
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
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.
Chapter 7 Introduction to Structured Query Language (SQL)
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.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 7 (Part a): Introduction to SQL Modern Database Management 9 th Edition Jeffrey A.
3 3 Chapter 3 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
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 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 7 SQL Data Modification 4Spread throughout chapter 7.
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.
Oracle 11g: SQL Chapter 4 Constraints.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
SQL – Structured Query Language
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 9 Structured Query Language.
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
1 Chapter 1 File Systems and Databases Database Systems: Design, Implementation, and Management, Fifth Edition, Rob and Coronel l 工程資料庫專題 –NTUST/ME-2007Q4.
Week 8-9 SQL-1. SQL Components: DDL, DCL, & DML SQL is a very large and powerful language, but every type of SQL statement falls within one of three main.
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
Week 4 Lecture Part 2 of 3 Structured Query Language (SQL) Samuel ConnSamuel Conn, Faculty Suggestions for using the Lecture Slides.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
CDT/1 Creating data tables and Referential Integrity Objective –To learn about the data constraints supported by SQL2 –To be able to relate tables together.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Insert, Update and the rest…
Advanced SQL Advanced SQL Week 8 Comparison Operators
ORACLE SQL Developer & SQLPLUS Statements
The Relational Model Relational Data Model
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Chapter 7 Introduction to Structured Query Language (SQL)
COP 4610L: Applications in the Enterprise Spring 2005
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Chapter # 7 Introduction to Structured Query Language (SQL) Part I.
COP 4610L: Applications in the Enterprise Spring 2006
Presentation transcript:

3 3 Chapter 3 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel

3 3 Introduction to SQL 4SQL meets ideal database language requirements: u SQL coverage fits into two categories: l Data definition l Data manipulation u SQL is relatively easy to learn. u ANSI prescribes a standard SQL.

3 3 Data Definition Commands 4The Database Model u Simple Database -- PRODUCT and VENDOR tables l Each product is supplied by only a single vendor. l A vendor may supply many products. Figure 3.1

3 3 Data Definition Commands 4The Tables and Their Components u The VENDOR table contains vendors who are not referenced in the PRODUCT table. PRODUCT is optional to VENDOR. u Existing V_CODE values in the PRODUCT table must have a match in the VENDOR table. u A few products are supplied factory-direct, a few are made in-house, and a few may have been bought in a special warehouse sale. That is, a product is not necessarily supplied by a vendor. VENDOR is optional to PRODUCT.

3 3

3 3 Data Definition Commands 4Creating the Database Structure CREATE SCHEMA AUTHORIZATION ;  Example: CREATE SCHEMA AUTHORIZATION JONES; CREATE DATABASE ;  Example: CREATE DATABASE CH3;

3 3 A Data Dictionary for the CH3 Database Table 3.1

3 3 Some Common SQL Data Types NumericNUMBER(L,D) INTEGER SMALLINT DECIMAL(L,D) CharacterCHAR(L) VARCHAR(L) DateDATE Data TypeFormat

3 3 Data Definition Commands 4Creating Table Structures CREATE TABLE ( );

3 3 Data Definition Commands CREATE TABLE VENDOR (V_CODEFCHAR(5)NOT NULLUNIQUE, V_NAMEVCHAR(35)NOT NULL, V_CONTACTVCHAR(15)NOT NULL, V_AREACODEFCHAR(3)NOT NULL, V_PHONEFCHAR(3)NOT NULL, V_STATEFCHAR(2)NOT NULL, V_ORDERFCHAR(1)NOT NULL, PRIMARY KEY (V_CODE));

3 3 Data Definition Commands CREATE TABLE PRODUCT( P_CODEVCHAR(10)NOT NULLUNIQUE, P_DESCRIPTVCHAR(35)NOT NULL, P_INDATEDATENOT NULL, P_ONHANDSMALLINTNOT NULL, P_MINSMALLINTNOT NULL, P_PRICEDECIMAL(8,2)NOT NULL, P_DISCOUNTDECIMAL(4,1)NOT NULL, V_CODESMALLINT, PRIMARY KEY (P_CODE), FOREIGN KEY (V_CODE) REFERENCES VENDOR ON DELETE RESTRICT ON UPDATE CASCADE);

3 3 Data Definition Commands 4SQL Integrity Constraints u Entity Integrity l PRIMARY KEY l NOT NULL and UNIQUE u Referential Integrity l FOREIGN KEY l ON DELETE l ON UPDATE

3 3 SQL Command Coverage Table 3.3

3 3 Basic Data Management 4Data Entry INSERT INTO VALUES (attribute 1 value, attribute 2 value, … etc.); INSERT INTO VENDOR VALUES(‘21225, ’Bryson, Inc.’, ’Smithson’, ’615’,’ ’, ’TN’, ’Y’); INSERT INTO PRODUCT VALUES(‘11 QER/31’, ’Power painter, 15 psi., 3-nozzle’, ’07/02/1999’, 8.5, , 0.00, 25595);

3 3 Figure 3.3 A Data View and Entry Screen

3 3 Basic Data Management 4Saving the Table Contents COMMIT ; COMMIT PRODUCT; 4Listing the Table Contents SELECT * FROM PRODUCT; SELECT P_CODE, P_DESCRIPT, P_INDATE, P_ONHAND, P_MIN, P-PRICE, P_DISCOUNT, V_CODE FROM PRODUCT;

3 3 Figure 3.4 The Contents of the PRODUCT Table

3 3 Basic Data Management 4Making a Correction UPDATE PRODUCT SET P_INDATE = ‘12/11/96’ WHERE P_CODE = ‘13-Q2/P2’; UPDATE PRODUCT SET P_INDATE = ‘12/11/96’, P_PRICE = 15.99, P_MIN=10 WHERE P_CODE = ‘13-Q2/P2’; 4Restoring the Table Contents ROLLBACK

3 3 Basic Data Management 4Deleting Table Rows DELETE FROM PRODUCT WHERE P_CODE = ‘2238/QPD’; DELETE FROM PRODUCT WHERE P_MIN = 5;

3 3 Queries 4Partial Listing of Table Contents SELECT FROM WHERE ; SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE FROM PRODUCT WHERE V_CODE = 21344; Figure 3.5

3 3 Figure 3.6 The Microsoft Access QBE and Its SQL

3 3 Mathematical Operators Table 3.4 Queries

3 3 SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE FROM PRODUCT WHERE V_CODE <> 21344; Figure 3.7 Queries

3 3 SELECT P_DESCRIPT, P_ONHAND, P_MIN, P_PRICE FROM PRODUCT WHERE P_PRICE <= 10; Figure 3.8 Queries

3 3 4Using Mathematical Operators on Character Attributes SELECT P_DESCRIPT, P_ONHAND, P_MIN, P_PRICE FROM PRODUCT WHERE P_CODE < ‘1558-QWI’; Figure 3.9 Queries

3 3 4Using Mathematical Operators on Dates SELECT P_DESCRIPT, P_ONHAND, P_MIN, P_PRICE, P_INDATE FROM PRODUCT WHERE P_INDATE >= ‘08/15/1999’; Figure 3.10 Queries

3 3 4Logical Operators: AND, OR, and NOT SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE FROM PRODUCT WHERE V_CODE = OR V_CODE = 24288; Figure 3.11 Queries

3 3 SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE FROM PRODUCT WHERE P_PRICE ‘07/15/1999’; Figure 3.12 Queries

3 3 SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE FROM PRODUCT WHERE (P_PRICE ‘07/15/1999’) OR V_CODE = 24288; Figure 3.13 Queries

3 3 4Special Operators u BETWEEN - used to define range limits. u IS NULL - used to check whether an attribute value is null u LIKE - used to check for similar character strings. u IN - used to check whether an attribute value matches a value contained within a (sub)set of listed values. u EXISTS - used to check whether an attribute has a value. In effect, EXISTS is the opposite of IS NULL.

3 3 4Special Operators BETWEEN is used to define range limits. SELECT * FROM PRODUCT WHERE P_PRICE BETWEEN AND ; SELECT * FROM PRODUCT WHERE P_PRICE > AND P_PRICE < ; Queries

3 3 4Special Operators IS NULL is used to check whether an attribute value is null. SELECT P_CODE, P_DESCRIPT FROM PRODUCT WHERE P_MIN IS NULL; SELECT P_CODE, P_DESCRIPT FROM PRODUCT WHERE P_INDATE IS NULL; Queries

3 3 4Special Operators LIKE is used to check for similar character strings. SELECT * FROM VENDOR WHERE V_CONTACT LIKE ‘Smith%’; SELECT * FROM VENDOR WHERE V_CONTACT LIKE ‘SMITH%’; Queries

3 3 4Special Operators IN is used to check whether an attribute value matches a value contained within a (sub)set of listed values. SELECT * FROM PRODUCT WHERE V_CODE IN (21344, 24288); EXISTS is used to check whether an attribute has value. DELETE FROM PRODUCT WHERE P_CODE EXISTS; SELECT * FROM PRODUCT WHERE V_CODE EXISTS; Queries

3 3 Advanced Data Management Commands 4Changing Table Structures ALTER TABLE MODIFY ( ); ALTER TABLE ADD ( );

3 3 4Changing a Column’s Data Type ALTER TABLE PRODUCT MODIFY (V_CODE CHAR(5)); 4Changing Attribute Characteristics ALTER TABLE PRODUCT MODIFY (P_PRICE DECIMAL(9,2)); 4Adding a New Column to the Table ALTER TABLE PRODUCT ADD (P_SALECODE CHAR(1)); Advanced Data Management Commands

3 3 UPDATE PRODUCT SET P_SALECODE = ‘2’ WHERE P_CODE = ‘1546-QQ2’; Figure 3.14 Selected PRODUCT Table Attributes: Multiple Data Entry Advanced Data Management Commands

3 3 UPDATE PRODUCT SET P_SALECODE = ‘1’ WHERE P_CODE IN (‘2232/QWE’, ‘2232/QTY’); Figure 3.15 Selected PRODUCT Table Attributes: Multiple Data Entry Advanced Data Management Commands

3 3 UPDATE PRODUCT SET P_SALECODE = ‘2’ WHERE P_INDATE < ‘07/10/1999’; UPDATE PRODUCT SET P_SALECODE = ‘1’ WHERE P_INDATE >= ‘08/15/1999’ AND P_INDATE < ‘08/20/1999’; Advanced Data Management Commands

3 3 Selected PRODUCT Table Attributes: Multiple Update Effect Figure 3.16 Advanced Data Management Commands

3 3 The Arithmetic Operators Table 3.5

3 3 4Copying Parts of Tables CREATE TABLE PART PART_CODECHAR(8) NOT NULL UNIQUE, PART_DESCRIPTCHAR(35), PART_PRICEDECIMAL(8,2), PRIMARY KEY(PART_CODE)); INSERT INTO PART (PART_CODE, PART_DESCRIPT, PART_PRICE) SELECT P_CODE, P_DESCRIPT, P_PRICE FROM PRODUCT; Advanced Data Management Commands

3 3 The Part Attributes Copied from the PRODUCT Table Figure 3.17

3 3 4Deleting a Table from the Database u DROP TABLE ; DROP TABLE PART; Advanced Data Management Commands

3 3 4Primary and Foreign Key Designation ALTER TABLE PRODUCT ADD PRIMARY KEY (P_CODE); ALTER TABLE PRODUCT ADD FOREIGN KEY (V_CODE) REFERENCES VENDOR; ALTER TABLE PRODUCT ADD PRIMARY KEY (P_CODE) ADD FOREIGN KEY (V_CODE) REFERENCES VENDOR; Advanced Data Management Commands