1 Chapter 6 Structured Query Language (SQL) DATABASE MANAGEMENT SYSTEM.

Slides:



Advertisements
Similar presentations
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Advertisements

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.
3 3 Chapter 3 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
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.
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
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
Advanced SQL Advanced SQL Complex Queries, Joining Tables.
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)
SQL Basics. 5/27/2016Chapter 32 of 19 Naming SQL commands are NOT case sensitive SQL commands are NOT case sensitive But user identifier names ARE case.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
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.
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.
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
SQL Structured Query Language. Aims  To introduce the implementation of a Physical design using SQL.  To introduce SQL Data Definition Language (DDL).
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.
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.
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
Structured Query Language (SQL) How to build something useful
ORACLE SQL Developer & SQLPLUS Statements
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
SQL (Structured Query Language)
Presentation transcript:

1 Chapter 6 Structured Query Language (SQL) DATABASE MANAGEMENT SYSTEM

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

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

4 The Tables and Their Components –The VENDOR table contains vendors who are not referenced in the PRODUCT table. PRODUCT is optional to VENDOR. –Existing V_CODE values in the PRODUCT table must have a match in the VENDOR table. –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.

5

6 Creating the Database Structure CREATE DATABASE ; –Example: CREATE DATABASE CH3;

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

8 Creating Table Structures CREATE TABLE ( );

9 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));

10 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);

11 SQL Integrity Constraints –Entity Integrity PRIMARY KEY NOT NULL and UNIQUE –Referential Integrity FOREIGN KEY ON DELETE ON UPDATE

12 SQL Command Coverage

13 Basic Data Management Data 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);

14 Saving the Table Contents COMMIT ; COMMIT PRODUCT; Listing 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;

15 Making 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’; Restoring the Table Contents ROLLBACK

16 Deleting Table Rows DELETE FROM PRODUCT WHERE P_CODE = ‘2238/QPD’; DELETE FROM PRODUCT WHERE P_MIN = 5;

17 Queries Partial Listing of Table Contents SELECT FROM WHERE ; SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE FROM PRODUCT WHERE V_CODE = 21344;

18 Mathematical Operators

19 SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE FROM PRODUCT WHERE V_CODE <> 21344;

20 SELECT P_DESCRIPT, P_ONHAND, P_MIN, P_PRICE FROM PRODUCT WHERE P_PRICE <= 10;

21 Using Mathematical Operators on Character Attributes SELECT P_DESCRIPT, P_ONHAND, P_MIN, P_PRICE FROM PRODUCT WHERE P_CODE < ‘1558-QWI’;

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

23 Logical Operators: AND, OR, and NOT SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE FROM PRODUCT WHERE V_CODE = OR V_CODE = 24288;

24 SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE FROM PRODUCT WHERE P_PRICE ‘07/15/1999’;

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

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

27 BETWEEN is used to define range limits. SELECT * FROM PRODUCT WHERE P_PRICE BETWEEN AND ; SELECT * FROM PRODUCT WHERE P_PRICE > AND P_PRICE < ;

28 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;

29 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%’;

30 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;

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

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

33 The Arithmetic Operators

34 Deleting a Table from the Database –DROP TABLE ; DROP TABLE PART;