Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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.

2 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.

3 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

4 ‘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. (?)

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

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

7 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.

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

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

10 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

11 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

12 ‘OrderLine’ Relation OrdNumOrdLineProdNoQuantityTotalPrice Ord11Prod1099.99 Ord12Prod2224.98 Ord21Prod31-19.99 Candidate Key What additional integrity constraints would be worth adding ? Check order exists. Check product exists. Check quantities are positive. Check prices are positive.

13 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

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

15 ‘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

16 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-2007. This will fail due to a duplicate candidate key. (‘Date’ and ‘CustNo’ are valid).

17 Adding an OrderLine INSERT INTO OrderLine VALUES (‘Ord3’, 1, ‘Prod5’, 2, 64.50 ) ; This will fail due to a referential integrity constraint - there is no ‘Ord3’ in Order. (All other values are valid). OrdNumOrdLineProdNoQuantityTotalPrice Ord11Prod1199.99 Ord12Prod2224.98 Ord21Prod3119.99 Ord31Prod5264.50

18 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, 64.50 ) ; ‘Today’ is 02-Dec-2007. Another line of the order could also be added as follows ;- INSERT INTO OrderLine VALUES (‘Ord3’, 2, ‘Prod4’, 5, 54.49 ) ;

19 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

20 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’ ;


Download ppt "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."

Similar presentations


Ads by Google