INFO/CSE 100, Spring 2006 Fluency in Information Technology Database Intro INFO/CSE 100, Spring 2006 Fluency in Information Technology http://www.cs.washington.edu/100 4/7/2019 fit100-23-databases © 2006 University of Washington
Readings and References Fluency with Information Technology Chapter 13, Introduction to Database Concepts References Access Database: Design and Programming by Steve Roman, published by O'Reilly 4/7/2019 fit100-23-databases © 2006 University of Washington
fit100-23-databases © 2006 University of Washington Why Study Databases? Some of us want to compute, but all of us want information … Much of the archived information is in tables Databases enhance applications, e.g. Web Once you know how to create databases, you can use them to personal advantage Databases introduce interesting ideas 4/7/2019 fit100-23-databases © 2006 University of Washington
fit100-23-databases © 2006 University of Washington Relational Databases Information is stored in tables Tables store information about entities Entities have characteristics called attributes Each row in a table represents a single entity Each row is a set of attribute values Every row must be unique, identified by a key Relationships -- associations among the data values are stored Table structure = schema Table contents = instance 4/7/2019 fit100-23-databases © 2006 University of Washington
fit100-23-databases © 2006 University of Washington A Table in a Database Tables have names, attributes {fields}, entities {rows} Schema for Example table: ID number unique number(Key) Last text person’s last name First text person’s first name JobCode number current position Hire date first day on job ... instance Attributes are sometimes referred to as fields, represented by the columns. Entities are sometimes referred to as rows or tuples. The schema is the structure of the table. schema 4/7/2019 fit100-23-databases © 2006 University of Washington
Two tables in a database Tables: Example, JobCodes Example: 6 rows or entities and 9 fields or attributes, ID is key JobCodes: 3 rows and 3 fields, JobID is key 4/7/2019 fit100-23-databases © 2006 University of Washington
Redundancy in a database is Very Bad Not every assembly of tables is a good database Repeating data is a bad idea Replicated data can differ in its different locations, e.g. multiple addresses can differ Inconsistent data is worse than no data Cut down on the typos and mis-keyed entries Keep a single copy of any data Reduces memory and data processing costs if it is needed in multiple places, associate it with a key and store key rather than the data Effort to update is high 4/7/2019 fit100-23-databases © 2006 University of Washington
Relationships between tables Relationship between the Perms table and JobCodes table, They share field JobID. 4/7/2019 fit100-23-databases © 2006 University of Washington
fit100-23-databases © 2006 University of Washington “You can look it up” When looking for information, a single item might be the answer, but a table is more likely Which employees live in Kirkland? Table of employees Who is taking INFO/CSE 100? Table of students Whose mile run time 4:00? Table of runners A query to a table produces a new table as a result! Query to a database (set of tables) produces a new table 4/7/2019 fit100-23-databases © 2006 University of Washington
Relational Algebra: Tables From Tables There are five basic “algebraic” operations on tables: Select -- pick rows from a table Project -- pick columns from a table Union/Join -- combine two tables w/like columns Difference -- remove one table from another Product -- create “all pairs” from two tables From this basis, many more complicated operations can be built up 4/7/2019 fit100-23-databases © 2006 University of Washington
fit100-23-databases © 2006 University of Washington Select Operation Select creates a table from the rows of another table meeting a criterion Select_from Example On Hire < 1993 Structured Query Language (SQL) in red Table in green Condition {test} in blue 4/7/2019 fit100-23-databases © 2006 University of Washington
Project Project creates a table from the columns of another table Project Last, First From Example This is a projection from 9 dimensions to 2 dimensions 4/7/2019 fit100-23-databases © 2006 University of Washington
fit100-23-databases © 2006 University of Washington Union Union combines two tables with same attributes All employees = perms UNION temps 4/7/2019 fit100-23-databases © 2006 University of Washington
fit100-23-databases © 2006 University of Washington Difference Difference (written like subtraction) removes 1 table’s rows from another Eastern = States - WestCoast 4/7/2019 fit100-23-databases © 2006 University of Washington
fit100-23-databases © 2006 University of Washington Product Product (written like multiplication) combines columns and pairs all rows Colors = Blues x Reds = Column Rule: If A has x columns, B has y columns, A x B has x+y columns Row Rule: If A has m rows, B has n rows A x B has mn rows 4/7/2019 fit100-23-databases © 2006 University of Washington
Join Join (written like a bow tie) combines rows if common field matches Employee List = Perms JobCodes >< Join is kind of like a Product,, except only combine rows if common fields match!
fit100-23-databases © 2006 University of Washington DB Operations The five DB Operations can create any table from a given set of tables All modern database systems are built on these relational operations Join is not primitive, but can be built from 5 Join, select and project are used most often The operations are not usually used directly, but are used indirectly from other languages Structured Query Language (SQL) is the language that we talk to the database in SQL, the DB language we learn, is built on basic 5 4/7/2019 fit100-23-databases © 2006 University of Washington
fit100-23-databases © 2006 University of Washington Database Structure 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 Create GUI front ends (forms and reports) for users First, design the database or create the schema What are the entities? What are the attributes of each entity? What are the relationships between tables? Creating a database… Lets consider all of the details about the database schema first!!! 4/7/2019 fit100-23-databases © 2006 University of Washington
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
fit100-23-databases © 2006 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 4/7/2019 fit100-23-databases © 2006 University of Washington
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! 4/7/2019 fit100-23-databases © 2006 University of Washington
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! 4/7/2019 fit100-23-databases © 2006 University of Washington
fit100-23-databases © 2006 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. 4/7/2019 fit100-23-databases © 2006 University of Washington
fit100-23-databases © 2006 University of Washington Build another table 4/7/2019 fit100-23-databases © 2006 University of Washington
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… 4/7/2019 fit100-23-databases © 2006 University of Washington
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. 4/7/2019 fit100-23-databases © 2006 University of Washington
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
Two tables with a relationship 4/7/2019 fit100-23-databases © 2006 University of Washington
fit100-23-databases © 2006 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. 4/7/2019 fit100-23-databases © 2006 University of Washington
The query produces a new (virtual) table A query produces a new table, of which we see the results. 4/7/2019 fit100-23-databases © 2006 University of Washington
Project (select particular columns) 4/7/2019 fit100-23-databases © 2006 University of Washington
Select particular rows 4/7/2019 fit100-23-databases © 2006 University of Washington
fit100-23-databases © 2006 University of Washington SQL behind the scenes 4/7/2019 fit100-23-databases © 2006 University of Washington