DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-1 David M. Kroenke Database Processing Chapter 7 Structured Query Language.

Slides:



Advertisements
Similar presentations
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Advertisements

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 8-1 David M. Kroenke’s Chapter Eight: Database Redesign Database Processing:
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Chapter 4 5 6_ SQL SQL Is: Structured Query Language
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-1 David M. Kroenke Database Processing Chapter 2 Structured Query Language.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 COS 346 Day 11.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 6-1 David M. Kroenke Database Processing Chapter 6 Transforming Data.
1 © Prentice Hall, 2002 Chapter 7: SQL Modern Database Management 6 th Edition Jeffrey A. Hoffer, Mary B. Prescott, Fred R. McFadden.
1 IS 4420 Database Fundamentals Chapter 7: Introduction to SQL Leon Chen.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 8-1 COS 346 Day 17.
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.
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
5 Chapter 5 Structured Query Language (SQL1) Revision.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
Chapter Extension 6 Using Microsoft Access © 2008 Pearson Prentice Hall, Experiencing MIS, David Kroenke.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
Oracle Data Definition Language (DDL)
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
Working with Queries in Microsoft Access The Access Workbench: Section Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 3 rd Edition.
Database Design lecture 3_1 1 Database Design Lecture 3_1 Data definition in SQL.
SQL Data Definition (CB Chapter 6) CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems by Connolly & Begg, © Addison Wesley.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Introduction to SQL.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 UNIT 6: Chapter 7: Introduction to SQL Modern Database Management 9 th Edition Jeffrey A.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Chapter 5: Part 1: DDL STRUCTURED QUERY LANGUAGE (SQL)
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
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.
Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 7 (Part a): Introduction to SQL Modern Database Management 9 th Edition Jeffrey A.
Lec 3- B Database Integrity 1. Overview Define a database using SQL data definition language Work with Views Write single table queries Establish referential.
Database Processing with Microsoft Access Appendix DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
Tables and Constraints Oracle PL/SQL. Datatypes The SQL Data Definition Language Commands (or DDL) enable us to create, modify and remove database data.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Oracle 11g: SQL Chapter 4 Constraints.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
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.
© 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.
DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E) 1.
SQL Overview Structured Query Language
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Chapter 3: Relational Databases
Basic SQL*Plus edit and execute commands SQL*Plus buffer and built-in editor holds the last SQL statement Statements are created in free-flow style and.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 6-1 David M. Kroenke’s Chapter Six: Transforming Data Models into Database.
CSC314 DAY 8 Introduction to SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall SQL OVERVIEW  Structured Query Language  The.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Fundamental of Database Systems
SQL: Schema Definition and Constraints Chapter 6 week 6
Insert, Update and the rest…
CHAPTER 6: INTRODUCTION TO SQL
The Basics of Data Manipulation
Database Processing: David M. Kroenke’s Chapter Six:
Oracle Data Definition Language (DDL)
Database Processing: David M. Kroenke’s Chapter Seven:
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Database Processing: David M. Kroenke’s Chapter Six:
Database Processing: David M. Kroenke’s Chapter Six:
SQL (Structured Query Language)
Presentation transcript:

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-1 David M. Kroenke Database Processing Chapter 7 Structured Query Language II

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-2 Data Definition Language (DDL) Subset of SQL that creates, deletes & modifies tables & columns –Create –Drop –Alter Structure of database is dynamic

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-3 Creating Tables CREATE TABLE name Column(s): name, type Data Integrity Constraint(s): default, required, unique, domain Primary and Foreign keys

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-4 Create Table Example CREATE TABLE ORDERS ( ORDER_NUM INTEGER, ORDER_DATE DATE, CUSTINTEGER, REPINTEGER, MFRCHAR(3), PRODUCTVARCHAR2(5), QTYINTEGER, AMOUNTNUMBER(9,0) );

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-5 Data Types CHAR (len)fixed length character strings VARCHAR2 (len)variable length character strings INTinteger numbers NUMBER (w, d)decimal numbers DATEcalendar date BLOBbinary object, up to 4 GB CLOBtext object, up to 4 GB

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-6 Alter Table: Columns Can make many table & column modifications. Alter Table Orders Add (column type constraints); Modify (column type constraints); Drop (column);

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-7 Defaults Uses the DEFAULT when insert doesn’t specify a value CREATE TABLE ORDERS ( ORDER_NUM integer, ORDER_DATE date DEFAULT sysdate, CUSTinteger, REPinteger, MFRchar(3), PRODUCTvarchar2(5), QTYinteger, AMOUNTnumber(9,0) );

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-8 Required Data If data is required, then NULL values NOT allowed. CREATE TABLE ORDERS ( ORDER_NUM integer NOT NULL, ORDER_DATE date default sysdate NOT NULL, CUSTinteger NOT NULL, REPinteger NOT NULL, MFRchar(3) NOT NULL, PRODUCTvarchar2(5) NOT NULL, QTYinteger NOT NULL, AMOUNTnumber(9,0) NOT NULL );

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-9 Uniqueness Whether or not the column is required to be UNIQUE. CREATE TABLE ORDERS ( ORDER_NUM integer UNIQUE, ORDER_DATE date default sysdate, CUSTinteger not null, REPinteger not null, MFRchar(3) not null, PRODUCTvarchar2(5) not null, QTYinteger, AMOUNTnumber(9,0) );

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-10 Domain Use the CHECK command to enforce a domain… CREATE TABLE ORDERS ( ORDER_NUM integer unique, ORDER_DATE date default sysdate, CUSTinteger not null, REPinteger not null, MFRchar(3) not null, PRODUCTvarchar2(5) not null, QTYinteger CHECK (QTY <> 0), AMOUNTnumber(9,0) );

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-11 Primary Key The unique identifier(s) for each row use the PRIMARY KEY command… CREATE TABLE ORDERS ( ORDER_NUM integer, ORDER_DATE date default sysdate, CUSTinteger not null, REPinteger not null, MFRchar(3) not null, PRODUCTvarchar2(5) not null, QTYinteger check (qty <> 0), AMOUNTnumber(9,0), CONSTRAINT order_pk PRIMARY KEY (order_num) );

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-12 Unique vs. Primary Key If the primary key is one identifier, then use of UNIQUE command will suffice. If the primary key is composite, then must use PRIMARY KEY command.

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-13 Composite Primary Key CREATE TABLE ORDERS ( ORDER_NUM integer unique, ORDER_DATE date default sysdate, CUSTinteger not null, REPinteger not null, MFRchar(3) not null, PRODUCTvarchar2(5) not null, QTYinteger check (qty <> 0), AMOUNTnumber(9,0), CONSTRAINT order_pk PRIMARY KEY (order_date, cust) ); Using UNIQUE with order_date, cust will cause problems!

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-14 Foreign Key Specifies the relationship to another parent table(s). –Column(s) that form the foreign key –Table that is referenced by the foreign key –Name of the relationship (optional) –Optional delete, update & check constraint rules

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-15 Foreign Key in ORDERS Relation Descriptions: PRODUCTS (Mfr_ID, Product_ID, Description, …) CUSTOMERS (Cust_Num, Company, …) ORDERS (Order_Num, Order_Date, Cust, Rep, Mfr, Product, Qty, Amount) Referential Integrity Constraints: Orders.Cust must first exist in Customers.Cust_Num Orders.(Mfr, Product) must first exist in Products.(Mfr_ID,Product_ID)

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-16 Foreign Key in ORDERS FOREIGN KEY (Cust) references Customers(Cust_Num) FOREIGN KEY (Mfr, Product) references Products(Mfr_ID, Product_ID)

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-17 Foreign Key in ORDERS CREATE TABLE ORDERS ( ORDER_NUM integer unique, ORDER_DATE date default sysdate, CUSTinteger not null, REPinteger not null, MFRchar(3) not null, PRODUCTvarchar2(5) not null, QTYinteger check (qty <> 0), AMOUNTnumber(9,0), CONSTRAINT order_pk PRIMARY KEY (order_date, cust), FOREIGN KEY (Cust) references Customers(Cust_Num), FOREIGN KEY (Mfr, Product) references Products(Mfr_ID, Product_ID) );

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-18 Alter Table: Keys ALTER TABLE ORDERS ADD CONSTRAINT Cust_fk FOREIGN KEY (Cust) references customers(Cust_Num); Prod_fk FOREIGN KEY (Mfr, Product) references Products(Mfr_ID, Product_ID);

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-19 On Delete Specifies action to take on delete of a parent record On Delete Cascade – delete child rows automatically Restrict – parent can be deleted if no children (In Oracle, this is the default) Set Null – set child rows to null

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-20 Order of Drops and Creates When Dropping tables – Drop child tables first, then parent(s). When Creating tables – Create parent tables first, then child(ren).