Presentation is loading. Please wait.

Presentation is loading. Please wait.

INFO/CSE 100, Spring 2005 Fluency in Information Technology

Similar presentations


Presentation on theme: "INFO/CSE 100, Spring 2005 Fluency in Information Technology"— Presentation transcript:

1 INFO/CSE 100, Spring 2005 Fluency in Information Technology
More Views INFO/CSE 100, Spring 2005 Fluency in Information Technology 11/12/2018 fit views © 2005 University of Washington

2 Readings and References
Fluency with Information Technology Chapter 14, Database Queries References Access Database: Design and Programming by Steve Roman, published by O'Reilly 11/12/2018 fit views © 2005 University of Washington

3 entity-relationship diagram for Library database
Price Name ISBN Title ID Phone WrittenBy Books Authors PublisherOf 1 Publishers entity-relationship diagram for Library database Books, Publishers, Authors are tables Price, ISBN, Title are attributes of the table Books There is a relationship between Books and Publishers 1 Publisher can have many Books ID Phone Name

4 fit100-22-views © 2005 University of Washington
Create a new database We are going to be using MS Access. Its available on the Lab machines and in MS Office if you have that installed on your home computer already. Step 1: create a new database , with a .mdb file extension 11/12/2018 fit views © 2005 University of Washington

5 Create a new table in the database
MS Access tries to be friendly and offer you some choices! Select "Create table in Design View". Do not use the Wizard! 11/12/2018 fit views © 2005 University of Washington

6 Creating a table in Design view
Aha! Now you've got to enter in the attributes {fields} of the table. Attributes have a field name, data type, and a description. In this example, three fields are being created: ISBN, Title, and Price. ISBN field is specified as the key (see key icon). Remember that the key field must be unique! 11/12/2018 fit views © 2005 University of Washington

7 fit100-22-views © 2005 University of Washington
Entering Table Data Once the table has been added you must view leave the Design View to enter data into the table. 11/12/2018 fit views © 2005 University of Washington

8 fit100-22-views © 2005 University of Washington
Build another table 11/12/2018 fit views © 2005 University of Washington

9 Add publisher ID to books
Field properties allow you to add more details about each attribute {field}, including things like size, format (for dates), number of decimal places, required, etc… 11/12/2018 fit views © 2005 University of Washington

10 Create the link between the tables
From the Relationships view. We can create a link by dragging a line between a field from one table to a field on another table. 11/12/2018 fit views © 2005 University of Washington

11 Hey presto, we have a database!
Price Name ISBN Title ID Phone WrittenBy Books Authors PublisherOf 1 Publishers Hey presto, we have a database! ID Phone Name

12 Two tables with a relationship
11/12/2018 fit views © 2005 University of Washington

13 fit100-22-views © 2005 University of Washington
Create a query Create a query in Design View. Step 1: select query from the object lists. Step 2: select the tables you want to query from. Step 3: select the fields you want to query from. 11/12/2018 fit views © 2005 University of Washington

14 The query produces a new (virtual) table
A query produces a new table, of which we see the results. 11/12/2018 fit views © 2005 University of Washington

15 Project (select particular columns)
11/12/2018 fit views © 2005 University of Washington

16 Select particular rows
11/12/2018 fit views © 2005 University of Washington

17 fit100-22-views © 2005 University of Washington
SQL behind the scenes Books.ISBN == books table and ISBN field Books.* all fields from the books table!! 11/12/2018 fit views © 2005 University of Washington

18 Recall: Structure of the database
A database contains one or more tables Tables include entities with attributes There are relationships defined between the entities in the various tables Retrieve information from the tables using queries We designed and partially implemented a simple library database in the previous lecture 11/12/2018 fit views © 2005 University of Washington

19 Books Authors Publishers Price Name ISBN Title ID Phone ∞ WrittenBy ∞
PublisherOf 1 Publishers ID Phone Name

20 What is the relationship?
This relationship is 1-to-many: One publisher is responsible for many books. Each book has only one publisher. Books PublisherOf The two tables are joined using the publisher ID number. The publisher ID is the primary key for each entry in the publishers table. Therefore, each publisher must have a unique publisher ID. The publisher ID is a foreign key for each entry in the books table and we have requested referential integrity Therefore, the given publisher ID must exist in the publishers table. 1 Publishers 11/12/2018 fit views © 2005 University of Washington

21 Referential Integrity
11/12/2018 fit views © 2005 University of Washington

22 PubID must reference an actual publisher
11/12/2018 fit views © 2005 University of Washington

23 What is the relationship?
Price Name ISBN Title ID Phone WrittenBy Books Authors This relationship is many-to-many: One book may have several authors. One author may have written several books. We need a unique identifier for each book. We already selected the ISBN as the primary key and asked Access to make sure that there are no duplicates We need a unique identifier for each author We will define an author table with a unique ID for each author 11/12/2018 fit views © 2005 University of Washington

24 fit100-22-views © 2005 University of Washington
authors table 11/12/2018 fit views © 2005 University of Washington

25 Link one book with many authors?
We DO want: to link each book to one or more authors We DON'T want to specify extra fields (author1, author2, author3,…) this is wasteful and limits the max number of authors to specify each book entry several times, naming a different author in each row this duplicates all the other information about the book 11/12/2018 fit views © 2005 University of Washington

26 Add a cross-reference table!
Refine the design so that it includes another table that is a book-author cross reference Each entity in the table is a single cross reference Attribute: ISBN Attribute: Author ID No primary key Now we can break the many-to-many relationship into two 1-to-many relationships that we already know how to implement 11/12/2018 fit views © 2005 University of Washington

27 Define new cross-reference entities
Price Name ISBN Title ID Phone 1 1 Books Book/Author Authors ISBN AuthorID 11/12/2018 fit views © 2005 University of Washington

28 fit100-22-views © 2005 University of Washington
book-author table 11/12/2018 fit views © 2005 University of Washington

29 Define the new relationships
11/12/2018 fit views © 2005 University of Washington

30 Define a query that uses the relationship
Query By Example actual SQL

31 Get the new view of the data
Notice that this view has redundant data That's okay, because we are not storing it this way, just presenting it The redundant items (Alex, Another Press) came from a single entry in a table – they are guaranteed to be identical 11/12/2018 fit views © 2005 University of Washington

32 Now we've implemented this entire schema.
Price Name ISBN Title ID Phone WrittenBy Books Authors PublisherOf 1 Now we've implemented this entire schema. Publishers ID Phone Name

33 View: All Books from “Another Press”

34 fit100-22-views © 2005 University of Washington
View: All Books by Alex 11/12/2018 fit views © 2005 University of Washington

35 View: All info about a given ISBN


Download ppt "INFO/CSE 100, Spring 2005 Fluency in Information Technology"

Similar presentations


Ads by Google