Further Consolidation Objectives of the Lecture : To use simple queries to check out a DB. To use insertions, deletions and amendments to maintain a DB.

Slides:



Advertisements
Similar presentations
Data Definition and Integrity Constraints
Advertisements

Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Candidate Keys Objectives of the Lecture : To consider Integrity Constraints; To consider the principles of Candidate Keys; To consider Candidate Keys.
Monday, 08 June 2015Dr. Mohamed Osman1 What is Database Administration A high level function (technical Function) that is responsible for ► physical DB.
Introduction to Structured Query Language (SQL)
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
Database Design Concepts INFO1408 Term 2 week 1 Data validation and Referential integrity.
5 Chapter 5 Structured Query Language (SQL1) Revision.
A Guide to MySQL 7. 2 Objectives Understand, define, and drop views Recognize the benefits of using views Use a view to update data Grant and revoke users’
Introduction to Structured Query Language (SQL)
Data Integrity Constraints
A Guide to SQL, Seventh Edition. Objectives Understand, create, and drop views Recognize the benefits of using views Grant and revoke user’s database.
The Relational Model Codd (1970): based on set theory Relational model: represents the database as a collection of relations (a table of values --> file)
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
Database Systems Relational Model Concepts Toqir Ahmad Rana Database Management Systems 1 Lecture 17.
Chapter 7 Constraints and Triggers Spring 2011 Instructor: Hassan Khosravi.
Logical Database Design Nazife Dimililer. II - Logical Database Design Two stages –Building and validating local logical model –Building and validating.
Chapter 6: Integrity Objective Key Constraints (Chapter 2) Cardinality Constraints (Chapter 2) Domain Constraints Referential Integrity Assertions Triggers.
Objectives of the Lecture :
©Silberschatz, Korth and Sudarshan6.1Database System Concepts Chapter 6: Integrity and Security Domain Constraints Referential Integrity Assertions Triggers.
Retrievals & Projections Objectives of the Lecture : To consider retrieval actions from a DB; To consider using relational algebra for defining relations;
1 Intro to Info Tech Database Management Systems Copyright 2003 by Janson Industries This presentation can be viewed on line at:
Database Management System Lecture 6 The Relational Database Model – Keys, Integrity Rules.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
DAY 15: ACCESS CHAPTER 2 Larry Reaves October 7,
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Triggers, PL/SQL, embedded SQL, JDBC.
INTEGRITY. Integrity constraint Integrity constraints are specified on a database schema and are expected to hold on every valid database state of the.
Concepts of Database Management, Fifth Edition Chapter 4: The Relational Model 3: Advanced Topics.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
DBMS Spring 2014 Database Integrity Sources: Security in Computing, Pfleeger and Pfleeger, Prentice Hall, 2003 Lecture Slides, CSE6243, MSU, Rayford B.
Switch off your Mobiles Phones or Change Profile to Silent Mode.
© Logicalis Group Using DB2/400 effectively. Data integrity facilities Traditional iSeries database usage Applications are responsible for data integrity.
Ad Hoc Constraints Objectives of the Lecture : To consider Ad Hoc Constraints in principle; To consider Ad Hoc Constraints in SQL; To consider other aspects.
Chapter 6 Database Administration
FEN  Data Definition: CREATE TABLE, ALTER TABLE  Data Manipulation: INSERT, UPDATE, DELETE  Queries: SELECT SQL: Structured Query Language.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Amendments & Transactions Objectives of the Lecture : To consider amendments to a relation; To consider transactions in SQL.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
M1G Introduction to Database Development 5. Doing more with queries.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Maintaining a Database Access Project 3. 2 What is Database Maintenance ?  Maintaining a database means modifying the data to keep it up-to-date. This.
INFORMATION TECHNOLOGY DATABASE MANAGEMENT. Adding a new field 1Right click the table name and select design view 2Type the field information at the end.
Fall 2001Database Systems1 Triggers Assertions –Assertions describe rules that should hold for a given database. –An assertion is checked anytime a table.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
Constraints cis 407 Types of Constraints & Naming Key Constraints Unique Constraints Check Constraints Default Constraints Misc Rules and Defaults Triggers.
Introduction to Database System Adisak Intana Lecturer Chapter 7 : Data Integrity.
Week 5 Lecture 2 Data Integrity Constraints. Learning Objectives  Learn the types and the uses of constraints  Examine the syntax and options for creating.
Managing Constraints. 2 home back first prev next last What Will I Learn? Four different functions that the ALTER statement can perform on constraints.
Objectives Database triggers and syntax
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Normalisation Lecture 3 Akhtar Ali 12/16/ Learning Objectives 1.To consider the process of Normalisation 2.To consider the definition and application.
Agenda for Class 2/02/2006 Finish discussing constraints generated with the CREATE TABLE statement. Discuss DROP statement. Discuss INSERT, COMMIT, DELETE,
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Access 2010 by Robert Grauer, Keith Mast, and Mary Anne.
Constraints Lesson 8. Skills Matrix Constraints Domain Integrity: A domain refers to a column in a table. Domain integrity includes data types, rules,
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
MIS 3053 Database Design And Applications The University Of Tulsa Professor: Akhilesh Bajaj Normal Forms Lecture 1 © Akhilesh Bajaj, 2000, 2002, 2003.
Consolidation Objectives of the Lecture : To review a given design of a relational database. To amplify the design by adding Integrity Constraints. To.
Lecture 03 Constraints. Example Schema CONSTRAINTS.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Embedded SQL, JDBC. u Read Sections.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
Getting started with Accurately Storing Data
Fundamental of Database Systems
Lecturer: Mukhtar Mohamed Ali “Hakaale”
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Integrity 5/5/2019 See scm-intranet.
Presentation transcript:

Further Consolidation Objectives of the Lecture : To use simple queries to check out a DB. To use insertions, deletions and amendments to maintain a DB. To review the need for integrity constraints to ensure a DB is maintained correctly.

The ‘Customer Orders’ Database l DB consists of 4 relations : l Product - holds data about products sold. l Customer - holds data about customers. l Order - holds general data about customer orders. l OrderLine - holds data about individual products ordered by customers For this exercise, the relations have no Referential Integrity or Ad Hoc constraints. Infrequent changes. Small regular changes. Continually changed.

ProdCodeDescSizeComment Prod1Nintendo DSn/a Available in pink, white or black Prod2Avatar the legend of Anng n/a DS game Prod3Avatar the legend of Anng n/a DVD region 2 Prod4Screen protector Pack of 3 For DS Prod5PSPn/a Available in white or black ‘Product’ Relation What additional integrity constraints would be worth adding ? Candidate Key

‘Customer’ Relation CustNoSurnameForenameHouseNoPostcode C123JonesJane23NE31 2BX C453RobertsElizabeth15NE54 8EX C765JarvisAndrew-698NE91 1DX C109FletcherNigel55TS1 17DS C125BrownCharlie112SE09 8ER C025GreenGillNE19 8ED Candidate Key What additional integrity constraints would be worth adding ? Check house numbers are reasonable. Check postcodes. (?)

Query Check on ‘Customer’ House Numbers Could retrieve all house numbers Customer Project[ HouseNo ] and ‘eyeball’ them as check. Not helpful if there are many customers. C765JarvisAndrew-698NE91 1DX Pick out tuples with ‘duff’ house numbers. Customer Restrict[ HouseNo NOT NULL And HouseNo <= 0 ] Gives all data for ‘duff’ cases; makes it easier to correct. SQL is : SELECT * FROM Customer WHERE HouseNo IS NOT NULL And HouseNo <= 0 ;

Correct ‘Customer’ Relation Need to : l Amend data in relation to correct it; l Add an integrity constraint to prevent the error recurring. Cannot add an integrity constraint until the relevant data in the relation is valid. Amend Relation SQL is : UPDATE Customer SET HouseNo = 698 WHERE CustNo = ‘C765’ ; And so on if there were other errors. Add Ad Hoc Integrity Constraint SQL is : ALTER TABLE Customer ADD CONSTRAINT Valid_HouseNo CHECK( HouseNo > 0 Or HouseNo IS NULL );

OrdNumDateCustNo Ord123-Nov-2003C123 Ord201-Dec-2007C755 ‘Order’ Relation Candidate Key What additional integrity constraints would be worth adding ? Check dates are reasonable. Check customer exists.

Query Check on ‘Order’ Dates What is a reasonable date ? Can’t be future, or too far in past. Assume a “week up to and including Today” is acceptable. Pick out tuples with ‘duff’ order dates. Order Restrict[ Date Today ] SQL is SELECT * FROM Order WHERE Date SYSDATE ;

Correct ‘Order’ Relation : Dates Need to : l Amend data in relation to correct it; l Add an integrity constraint to prevent the error recurring. Amend Relation SQL is : UPDATE Order SET Date = SYSDATE WHERE Date SYSDATE ; Or set Date to another date if appropriate. Add Ad Hoc Integrity Constraint SQL is : ALTER TABLE Order ADD CONSTRAINT Valid_Date CHECK( Not (Date SYSDATE ) );

To accomplish in SQL, write : SELECT CustNo FROM Customer ; SELECT * FROM Order WHERE CustNo Not In (‘C123’, ‘C453’, ‘C765’, ‘C109’, ‘C125’, ‘C025 ); Query Check on ‘Order’ Customers Exist Check for customers not in Customer relation. Order Restrict[ CustNo Not In ( ) ] Need to get all the ‘CustNos’ in Customer. Customer Project[ CustNo ] CustNos in Customer. Gives data to put in previous query. gives C75501-Dec-2007Ord2

Correct ‘Order’ Relation : Amend Relation SQL is : UPDATE Order SET CustNo = ‘C765’ WHERE CustNo = ‘C755’ ; Or as appropriate. Add Referential Integrity Constraint SQL is : ALTER TABLE Order ADD CONSTRAINT Valid_CustNo FOREIGN KEY( CustNo ) REFERENCES Customer( CustNo ) ; Customers Exist

‘OrderLine’ Relation OrdNumOrdLineProdNoQuantityTotalPrice Ord11Prod Ord12Prod Ord21Prod Candidate Key What additional integrity constraints would be worth adding ? Check order exists. Check product exists. Check quantities are positive. Check prices are positive.

Thus to accomplish in SQL, write : SELECT OrdNum FROM Order ; SELECT * FROM OrderLine WHERE OrdNum Not In (‘Ord1’, ‘Ord2’ ); Query Check on ‘OrderLine’ Orders Exist Check for orders not in OrderLine relation. OrderLine Restrict[ OrdNum Not In ( ) ] Need to get all the ‘OrdNums’ in Order. Order Project[ OrdNum ] OrdNums in Order. Gives data to put in previous query. gives

Correct ‘OrderLine’ Relation : Fortunately all the orders appearing in OrderLine are also in Order. So no amendments are necessary to correct OrderLine. Orders Exist Still need to add a Referential Integrity Constraint. SQL is : ALTER TABLE OrderLine ADD CONSTRAINT Valid_OrdNum FOREIGN KEY( OrdNum ) REFERENCES Order( OrdNum ) ;

‘OrderLine’ : l Check that the ‘ProdNos’ all exist in Product. Correct if necessary. Regardless of any corrections, add a Referential Integrity Constraint to prevent it from occurring. l Check that ‘Quantity’ is always positive. Correct if necessary. Regardless of any corrections, add an Ad Hoc Integrity Constraint to prevent it from occurring. l Check that ‘TotalPrice’ is always positive. Correct if necessary. Regardless of any corrections, add an Ad Hoc Integrity Constraint to prevent it from occurring. Remaining Checks & Corrections

Adding an Order INSERT INTO Order VALUES (‘Ord2’, ’30-Nov-2007’, ‘C453’) ; Ord230-Nov-2007C453 C76501-Dec-2007Ord2 C12323-Nov-2003Ord1 CustNoDateOrdNum ‘Today’ is 02-Dec This will fail due to a duplicate candidate key. (‘Date’ and ‘CustNo’ are valid).

Adding an OrderLine INSERT INTO OrderLine VALUES (‘Ord3’, 1, ‘Prod5’, 2, ) ; This will fail due to a referential integrity constraint - there is no ‘Ord3’ in Order. (All other values are valid). OrdNumOrdLineProdNoQuantityTotalPrice Ord11Prod Ord12Prod Ord21Prod Ord31Prod

Add an Order with OrderLines The following insertions would be OK. INSERT INTO Order VALUES (‘Ord3’, ’30-Nov-2007’, ‘C453’) ; INSERT INTO OrderLine VALUES (‘Ord3’, 1, ‘Prod5’, 2, ) ; ‘Today’ is 02-Dec Another line of the order could also be added as follows ;- INSERT INTO OrderLine VALUES (‘Ord3’, 2, ‘Prod4’, 5, ) ;

Deleting an Order DELETE FROM Order WHERE OrdNum = ‘Ord3’ ; This will fail due to a referential integrity constraint. There is still an order line(s) for ‘Ord3’. C76501-Dec-2007Ord2 C12323-Nov-2003Ord1 CustNoDateOrdNum C45530-Nov-2007Ord3

Delete an Order with OrderLines The following deletion(s) would be OK :- DELETE FROM OrderLine WHERE OrdNum = ‘Ord3’ ; This would remove all lines of the ‘Ord3’ order, however many there are. ‘Ord3’ can now be deleted from Order :- DELETE FROM Order WHERE OrdNum = ‘Ord3’ ;