© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 9 Structured Query Language.

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.
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 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 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 4-1 David M. Kroenke Database Processing Chapter 7 Structured Query Language.
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)
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.
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
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.
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.
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.
Week 5 Lecture 2 Data Integrity Constraints. Learning Objectives  Learn the types and the uses of constraints  Examine the syntax and options for creating.
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.
DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E) 1.
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.
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
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.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Structured Query Language
Fundamental of Database Systems
SQL: Schema Definition and Constraints Chapter 6 week 6
The Basics of Data Manipulation
Insert, Update and the rest…
CHAPTER 6: INTRODUCTION TO SQL
The Basics of Data Manipulation
Database Processing: David M. Kroenke’s Chapter Six:
SQL-1 Week 8-9.
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:
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Database Processing: David M. Kroenke’s Chapter Six:
SQL (Structured Query Language)
Presentation transcript:

© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 9 Structured Query Language

© 2002 by Prentice Hall 2 Data Definition Language (DDL) Subset of SQL that creates, deletes & modifies tables & columns –Create –Drop –Alter Structure of database is dynamic

© 2002 by Prentice Hall 3 Creating Tables CREATE TABLE name Column(s): name, type Data Integrity Constraint(s): default, required, unique, domain Primary and Foreign keys

© 2002 by Prentice Hall 4 Create Table Example CREATE TABLE ORDERS ( ORDER_NUM INTEGER, ORDER_DATE DATE, CUSTINTEGER, REP INTEGER, MFRCHAR(3), PRODUCTVARCHAR2(5), QTYINTEGER, AMOUNTNUMBER(9,0) );

© 2002 by Prentice Hall 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

© 2002 by Prentice Hall 6 Alter Table: Columns Can make many table & column modifications. Alter Table Orders Add (column type constraints); Modify (column type constraints); Drop (column);

© 2002 by Prentice Hall 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) );

© 2002 by Prentice Hall 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 );

© 2002 by Prentice Hall 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) );

© 2002 by Prentice Hall 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) );

© 2002 by Prentice Hall 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) );

© 2002 by Prentice Hall 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.

© 2002 by Prentice Hall 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!

© 2002 by Prentice Hall 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

© 2002 by Prentice Hall 15 Foreign Key in ORDERS Relation Descriptions: CUSTOMERS (Cust_Num, Company, …) SALESREPS (Empl_Num, Name, …) PRODUCTS (Mfr_ID, Product_ID, Description, …) ORDERS (Order_Num, Order_Date, Cust, Rep, Mfr, Product, Qty, Amount) Referential Integrity Constraints: Cust in Orders must exist in Cust_Num in Cust Rep in Orders must first exist in Empl_Num in Salesreps (Mfr, Product) in Orders must exist in (Mfr_ID, Product_ID) in Products

© 2002 by Prentice Hall 16 Foreign Key in ORDERS FOREIGN KEY (Cust) references Customers(Cust_Num) FOREIGN KEY (Rep) references Salesreps(Empl_Num) FOREIGN KEY (Mfr, Product) references Products(Mfr_ID, Product_ID)

© 2002 by Prentice Hall 17 Alter Table: Keys ALTER TABLE ORDERS ADD CONSTRAINT Cust_fk FOREIGN KEY (Cust) references customers(Cust_Num); Rep_fk FOREIGN KEY (Rep) references Salesreps(Empl_Num); Prod_fk FOREIGN KEY (Mfr, Product) references Products(Mfr_ID, Product_ID);

© 2002 by Prentice Hall 18 On Delete Specifies action to take on delete of a parent record On Delete Cascade – delete child rows automatically Set Null – set child rows to null Restrict – parent can be deleted if no children