Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Booktopia Database

Similar presentations


Presentation on theme: "The Booktopia Database"— Presentation transcript:

1 The Booktopia Database
Designed By: Romana Chandra High Distinction Assignment- Autumn, 2007

2 Introduction to the Booktopia Database
Booktopia is an online bookstore, similar to amazon. The contents of the database would include information regarding books, authors, customers and their purchases. Booktopia hopes to be able to expand its business from selling only books, to selling a variety of items, through the development of a better structured database and improved technologies.

3 The ERD for the Booktopia Database…

4 Many to Many Relationship (1)
Many customers can purchase many books. The table BDbasket below illustrates this many to many relationship Orderno BookID Price Total quantity customer ID PMT status 231 B9 17.95 89.75 5 C1 P 232 B8 20.66 1 C3 233 B1 35.9 2 C5 Null 234 B3 26.95 C4 235 B4 22.45

5 Many to Many Relationship (2)
Many authors exist and each author can write more than one book, many authors write many books. This is illustrated by the table: BDbookauth RecordNo authorID BookID 1 A1 B1 2 B2 3 A2 B3 4 A3 B4 5 A4 6 A5 7 A6 B5 8 A7 9 A8 10 A9 B6 11 A10 B7 12 A11 B8 13 A12 14 A13 B9 15 A14 B10 16 A15

6 Queries…

7 A simple query of a single table…
List all of the books which are available. Select * from BDinfo where Availability ='Y'; isbn | bookid | title | format | price | availability | B1 | Vegetarian Soups | Paperback | | Y | B3 | Vegetarian Food For Friends | Paperback | | Y | B4 | Cocktail Therapy | Hardcover | | Y | B5 | Art of Terrior | Hardcover | | Y | B7 | 100 Great Risottos | Hardcover | | Y | B8 | One Pot Meals For People With Diabetes | Paperback | | Y | B9 | Welsh Heritage Food and Cooking | Hardcover | | Y | B10 | Blue Ribbon USA | Hardcover | | Y (8 rows)

8 A query which uses the words ‘natural join’
List all the BookIDs’ with their corresponding Author’s. Select * from BDbookauth natural join BDauthor; authorid | recordno | bookid | authorfname | authorlname A | | B1 | Anne | Sheasby A | | B2 | Anne | Sheasby A | | B3 | Jane | Noraika A | | B4 | Leanne | Shear A | | B4 | Tracey | Toomey A | | B4 | Neryl | Walker A | | B5 | George | Rose A | | B5 | Rod | Smith A | | B5 | Jess | Jackson A | | B6 | Mitchell | Vollstedt A | | B7 | Valentina | Harris A | | B8 | Nancy | Baggett A | | B8 | Ruth | Glick A | | B9 | Annette | Yates A | | B10 | John | Margolies A | | B10 | Georgia | Orcutt (16 rows)

9 The cross product equivalent to the ‘natural join’ query above…
For the previous query, generate the same table using the cross product method. Select BDauthor.authorid ,RecordNo, BookId,AuthorFname, AuthorLname from BDbookauth, BDauthor where BDbookauth.authorid = BDauthor.authorid; authorid | recordno | bookid | authorfname | authorlname A | | B1 | Anne | Sheasby A | | B2 | Anne | Sheasby A | | B3 | Jane | Noraika A | | B4 | Leanne | Shear A | | B4 | Tracey | Toomey A | | B4 | Neryl | Walker A | | B5 | George | Rose A | | B5 | Rod | Smith A | | B5 | Jess | Jackson A | | B6 | Mitchell | Vollstedt A | | B7 | Valentina | Harris A | | B8 | Nancy | Baggett A | | B8 | Ruth | Glick A | | B9 | Annette | Yates A | | B10 | John | Margolies A | | B10 | Georgia | Orcutt (16 rows)

10 A query involving a “Group by”, perhaps also with a “HAVING”…
Report the total value of books for customers that have paid for more than one book Select customerid, SUM(total) as paidtotal from BDbasket where pmtstatus='P' group by customerid having count(customerid)>1; customerid | paidtotal C | (1 row)

11 A query which uses a sub query…
Report all book titles which have a price that is greater than the average price for all books. Select title, price from BDinfo where price> (Select AVG(price) from BDinfo); title | price Vegetarian Food For Friends | 26.95 Art of Terrior | 35.96 Blue Ribbon USA | 25.16 (3 rows)

12 A cross product which cannot be implemented using the words “natural join” (e.g. self join)…
Show the Author ID of all the authors that wrote B10 with A14, inclusive. Select auth1.authorid, auth2.authorid, auth2.bookid from BDbookauth auth1, BDbookauth auth2 where auth1.authorid = 'A14' and auth1.bookid = auth2.bookid; authorid | authorid | bookid A | A | B10 A | A | B10 (2 rows)

13 Examples of CHECK statements…
ISBN is a unique 10 digit number. Need to make sure the database does not accept anything over 10 digits. Used the following check statement as a constraint: CHECK ((ISBN> ) AND (ISBN< )),

14 Another CHECK statement…
Book Availability Can be either ‘Y’ for Yes Or ‘N’ for No Database needs to make sure that no other code is accepted as a result. Check statement will be: CHECK ((Availability='Y') or (Availability='N'))

15 The use of SQL action statements
CREATE TABLE BDbasket ( Orderno INTEGER NOT NULL UNIQUE, BookID VARCHAR(3) NOT NULL UNIQUE, Price DECIMAL(5,2) NOT NULL, Total DECIMAL(5,2) NOT NULL CHECK ((Total>=Price)), Quantity INTEGER NOT NULL, CustomerID VARCHAR(3) NOT NULL, PmtStatus CHAR(1) CHECK ((PmtStatus='P') or (PmtStatus=Null)), CONSTRAINT BDbasketPK_Invalid_Orderno PRIMARY KEY (Orderno), CONSTRAINT BDbasket1_FK FOREIGN KEY (BookID) REFERENCES BDinfo ON UPDATE CASCADE ON DELETE CASCADE, CONSTRAINT BDbasket2_FK FOREIGN KEY (CustomerID) REFERENCES BDcustomer ON DELETE CASCADE );

16 Using a View in SQL… Suppose we want to obtain a comprehensive report such as the following: Show the Book ID, Title and Author ID of all the available books from the first 9 records inclusive. The query would be: Select bookid, title, authorid from BDinfo natural join BDbookauth where availability=‘Y’ and recordno>10;

17 Views Continued… The following output is obtained:
bookid | title | authorid B7 | 100 Great Risottos | A10 B8 | One Pot Meals For People With Diabetes | A11 B8 | One Pot Meals For People With Diabetes | A12 B9 | Welsh Heritage Food and Cooking | A13 B10 | Blue Ribbon USA | A14 B10 | Blue Ribbon USA | A15 (6 rows)

18 Views continued… Suppose this query is required again at a later time, It would be rather inconvenient to type out such a long query all the time. Here is where the use of views come in. A view is basically saving a particular query on the database as a particular name.

19 Views Continued… To create the view: Applying this to our query,
The syntax to create a view is CREATE VIEW name AS query. This will save the query string query to the database under the name name. Applying this to our query, Create view booktopiaview as Select bookid, title, authorid from BDinfo natural join BDbookauth where availability='Y' and recordno>10;

20 Views Continued… Now the view has been created, we can check using \dv that it exists List of relations Name | Type | Owner booktopiaview | view | rochandr

21 Select * from Booktopiaview;
Views continued… We can now use the view at any time simply by using a normal select query: Select * from Booktopiaview; bookid | title | authorid B7 | 100 Great Risottos | A10 B8 | One Pot Meals For People With Diabetes | A11 B8 | One Pot Meals For People With Diabetes | A12 B9 | Welsh Heritage Food and Cooking | A13 B10 | Blue Ribbon USA | A14 B10 | Blue Ribbon USA | A15 (6 rows)

22 The END!!!!! Thank you for watching Any Questions?


Download ppt "The Booktopia Database"

Similar presentations


Ads by Google