Upload
damian-walton
View
213
Download
0
Embed Size (px)
Citation preview
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 03, Slide Day 03, Slide 11
CSE 103 Day 15: Database DesignCSE 103 Day 15: Database Design
Students:Students:– Read Day 2 in your textbookRead Day 2 in your textbook– Your BT feedback may be (hopefully is!) upYour BT feedback may be (hopefully is!) up
Others:Others:– Please save your work and log out by 10:10.Please save your work and log out by 10:10.
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 03, Slide Day 03, Slide 22
Day 13/14 ReviewDay 13/14 Review
Day 13: Nested queries in MySQLDay 13: Nested queries in MySQL– Write subqueries to find data needed before we can Write subqueries to find data needed before we can
answer the problemanswer the problem– Compare subqueries with exactly one record & field Compare subqueries with exactly one record & field
using = > < etc.using = > < etc.– In FROM clause, subqueries make virtual tables we In FROM clause, subqueries make virtual tables we
name with ASname with AS
Day 14: BT day. Comments/feedback on your Day 14: BT day. Comments/feedback on your BT? BT? – "It was too hard" doesn't count"It was too hard" doesn't count
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 03, Slide Day 03, Slide 33
Database terminology reviewDatabase terminology review
RecordRecord– An item in your database, usually represented by one An item in your database, usually represented by one
rowrow in your database in your database– Also called Also called entity, objectentity, object– TablesTables contain contain recordsrecords about about objectsobjects of the same of the same
typetype
FieldField– A piece of information about an objectA piece of information about an object– Also called attributeAlso called attribute– Stored in columns in your databaseStored in columns in your database
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 03, Slide Day 03, Slide 44
Database DesignDatabase Design
What questions do I want the database to What questions do I want the database to answer?answer?
What data do I need to answer these What data do I need to answer these questions?questions?
How are these data related to one How are these data related to one another?another?
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 03, Slide Day 03, Slide 55
Guiding PrinciplesGuiding PrinciplesControl data RedundancyControl data Redundancy– Don't (redundantly) store data in two places (twice)Don't (redundantly) store data in two places (twice)
Maintain data ConsistencyMaintain data Consistency– Don't store conflicting dataDon't store conflicting data
Provide data IntegrityProvide data Integrity– Only allow legal values; all records must have primary Only allow legal values; all records must have primary
keyskeys
Maintain data IndependenceMaintain data Independence– Changing data in one place does not break DB in other Changing data in one place does not break DB in other
placesplaces
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 03, Slide Day 03, Slide 66
Database Example:Database Example:Electronic RolodexElectronic Rolodex
What information about your friends would What information about your friends would you like a rolodex to provide?you like a rolodex to provide?What data is in the SBC phonebook white What data is in the SBC phonebook white pages?pages?What data isn't available in the SBC phone What data isn't available in the SBC phone book that you would like to have in your book that you would like to have in your personal phonebook?personal phonebook?How would you arrange this data in a How would you arrange this data in a database table? database table?
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 03, Slide Day 03, Slide 77
Database Example: Electronic Database Example: Electronic Rolodex, cont’dRolodex, cont’d
What questions do you want to be able to What questions do you want to be able to answer about your friends?answer about your friends?
What items of data do we need in our contact What items of data do we need in our contact directory in order to answer such questions?directory in order to answer such questions?
What should you do when you can't get some of What should you do when you can't get some of the data? For example, you don't know all of the data? For example, you don't know all of your friends' addresses.your friends' addresses.
What should you do with people who have more What should you do with people who have more than one phone number: a cell phone number, a than one phone number: a cell phone number, a pager number, a fax number?pager number, a fax number?
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 03, Slide Day 03, Slide 88
Data typesData types
Examine the list of data types on page Examine the list of data types on page 2-52-5 of the textof the text
Within your group, reach a consensus on Within your group, reach a consensus on the datatype assigned to each piece of the datatype assigned to each piece of data in your designdata in your design
Note: this data will be made available in Note: this data will be made available in the day 15 AFS space after we have the day 15 AFS space after we have agreed on types as a classagreed on types as a class
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 03, Slide Day 03, Slide 99
Design View for a TableDesign View for a TableStart Access and create a new database called Start Access and create a new database called contacts.mdbcontacts.mdbCreate a new table in design view, and enter the Create a new table in design view, and enter the field names and data types (as agreed) field names and data types (as agreed) Use descriptive field names close to those Use descriptive field names close to those discussed in class so that you don't need any discussed in class so that you don't need any additional information in the Description boxadditional information in the Description box– NEVER USE SPACES IN FIELD NAMES!NEVER USE SPACES IN FIELD NAMES!– Try to follow a consistent convention, e.g., all lower-Try to follow a consistent convention, e.g., all lower-
case, first capital, underscores between words, etc. case, first capital, underscores between words, etc. Pick your own but BE CONSISTENT!Pick your own but BE CONSISTENT!
Don't save the table yetDon't save the table yet
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 03, Slide Day 03, Slide 1010
Saving the TableSaving the TableUse naming convention:Use naming convention:– To tell difference between table and query, we To tell difference between table and query, we
use tbl_ and qry_use tbl_ and qry_– Not required—some people don't use any Not required—some people don't use any
convention, but recommended to use itconvention, but recommended to use it
Following the naming convention, call the Following the naming convention, call the table table tbl_Peopletbl_People..Access will prompt you about a primary Access will prompt you about a primary key. Answer no for now and continue to key. Answer no for now and continue to next slide.next slide.
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 03, Slide Day 03, Slide 1111
Primary KeysPrimary Keys
What does the primary key do? What does the primary key do?
What is a reasonable primary key for this What is a reasonable primary key for this table? (see next slide)table? (see next slide)
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 03, Slide Day 03, Slide 1212
Types of Primary KeysTypes of Primary KeysExisting data:Existing data:– e.g., Social Security #, PID, filename, etc.e.g., Social Security #, PID, filename, etc.– One item that uniquely identifies an objectOne item that uniquely identifies an object
Multiple cell:Multiple cell:– e.g., e.g., ActorIDActorID + + MovieIDMovieID = = RoleRole (list of Actors in Movies); (list of Actors in Movies);
TextbookIDTextbookID + + AuthorIDAuthorID = = BookAuthorBookAuthor (books’ authors) (books’ authors) – The combination of all fields uniquely identifies an object: same The combination of all fields uniquely identifies an object: same
actor can’t be in same movie, but one actor can be in multiple actor can’t be in same movie, but one actor can be in multiple movies, and one movie may have multiple actorsmovies, and one movie may have multiple actors
Autonumber:Autonumber:– Last resort, used when nothing else worksLast resort, used when nothing else works– The DBMS assigns a new unique # when you insert a rowThe DBMS assigns a new unique # when you insert a row– Wastes space if you could find a key among existing dataWastes space if you could find a key among existing data– Doesn't help prevent duplicate entries (can always insert; get new Doesn't help prevent duplicate entries (can always insert; get new
key every time even if all other data is the same)key every time even if all other data is the same)
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 03, Slide Day 03, Slide 1313
Closing & data entryClosing & data entry
Set the primary key, save, and close the Set the primary key, save, and close the tabletableOpen it in datasheet view and add Open it in datasheet view and add information about yourselfinformation about yourselfRemember that we don't ever save the Remember that we don't ever save the database itselfdatabase itself– All data changes are liveAll data changes are live– Changes to table or query designs have to be Changes to table or query designs have to be
savedsaved
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 03, Slide Day 03, Slide 1414
Better design, first steps:Better design, first steps:Properties of dataProperties of data
What pieces of information does every What pieces of information does every person have:person have:– Exactly one ofExactly one of– Exactly zero or one ofExactly zero or one of– Any number between zero and at least twoAny number between zero and at least two
What pieces of information might be What pieces of information might be shared between multiple people?shared between multiple people?
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 03, Slide Day 03, Slide 1515
HomeworkHomeworkCheck the Check the homework linkhomework link..– Enter 15 records into your contact directory. Enter 15 records into your contact directory.
(Suggest you use classmates right after class!)(Suggest you use classmates right after class!)– Practice identifying primary keysPractice identifying primary keys– Read Day 10 on the design of multiple-table Read Day 10 on the design of multiple-table
databasesdatabases– Think about how we could improve the design of the Think about how we could improve the design of the
contacts database (see questions on previous slide)contacts database (see questions on previous slide)