Upload
owen-berry
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 06, Slide Day 06, Slide 11
CSE 103CSE 103
103 students:103 students:Do not log in yet. Review Day 6 in your Do not log in yet. Review Day 6 in your textbook. Think about why you might want textbook. Think about why you might want to import data into a database.to import data into a database.
Others:Others:Please save your work and log out by Please save your work and log out by 10:15.10:15.
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 06, Slide Day 06, Slide 22
HousekeepingHousekeeping
BT FeedbackBT Feedback– Most of your BTs are graded. Please save comments Most of your BTs are graded. Please save comments
or questions about your BTs until after class.or questions about your BTs until after class.
Office hoursOffice hours– Carl: Carl: by appointment onlyby appointment only
Suggested times: M, W 11-12; T, R: 1-2Suggested times: M, W 11-12; T, R: 1-2
– Someone qualified: MW 3-5; TR 2-4Someone qualified: MW 3-5; TR 2-4
HelproomsHelprooms– 120 CC, Monday and Tuesday nights, 9-11120 CC, Monday and Tuesday nights, 9-11
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 06, Slide Day 06, Slide 33
ImportingImporting Data Data
Read Day 06 in your textbooks, then Read Day 06 in your textbooks, then discuss the following question in your discuss the following question in your groups:groups:– Why would you need or want to import Why would you need or want to import
data into a database?data into a database?
(The answer is not going to be spelled (The answer is not going to be spelled out in the book; you’ll have to think out in the book; you’ll have to think about it.)about it.)
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 06, Slide Day 06, Slide 44
Importing into a DatabaseImporting into a Database
Two formats of text files can be used:Two formats of text files can be used:– Fixed widthFixed width
Each field has a constant widthEach field has a constant width
– DelimitedDelimitedFields are separated by delimitersFields are separated by delimiters
Example delimiters: commas, tabs, spacesExample delimiters: commas, tabs, spaces
Open files Open files Schools-1-06.txtSchools-1-06.txt and and Schools-2-06.txtSchools-2-06.txt in the Day06 class folder. Assess their formats.in the Day06 class folder. Assess their formats.
What are the advantages of each format?What are the advantages of each format?
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 06, Slide Day 06, Slide 55
Colleges & Universities dbColleges & Universities db
Copy the c&u database from the Day 06 Copy the c&u database from the Day 06 AFS space to your personal spaceAFS space to your personal spaceOpen it in Access and explore the tables.Open it in Access and explore the tables.– What information is stored in each table?What information is stored in each table?– What information is stored in two places?What information is stored in two places?
In one spot, it will be a primary key of the table In one spot, it will be a primary key of the table (check Design View), in another, it will be used to (check Design View), in another, it will be used to represent the record in the other table (this is represent the record in the other table (this is called a foreign key, we'll study it more later)called a foreign key, we'll study it more later)How will this affect our import?How will this affect our import?
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 06, Slide Day 06, Slide 66
Importing into tbl_SchoolsImporting into tbl_Schools
From file, choose From file, choose Get External DataGet External Data, then , then choose choose Text FileText File, navigate to the , navigate to the Day06Day06 folder, folder, and select and select Schools-1-06.txtSchools-1-06.txt..
Select Select Fixed WidthFixed Width and then click and then click NextNext..
Adjust the breaks if needed, then click Adjust the breaks if needed, then click NextNext..
Select an existing table like Select an existing table like tbl_Schoolstbl_Schools and and click click Next Next again. again.
Click Click FinishFinish. Check the table contents. Make . Check the table contents. Make sure the data has ended up in the right fields!sure the data has ended up in the right fields!
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 06, Slide Day 06, Slide 77
Second Import ExerciseSecond Import ExerciseImport the data from Import the data from Schools-2-06.txtSchools-2-06.txt into a into a newnew table in the table in the C&UC&U databasedatabase. . Use HELP if Use HELP if you need it. Most of the steps are the same as you need it. Most of the steps are the same as before.before.In a new table, you can name the fields, and you In a new table, you can name the fields, and you can select data typescan select data types. . Click Click NextNext when done. If when done. If you cannot select a primary key, you can let you cannot select a primary key, you can let Access add one. Click Access add one. Click NextNext to advance and to advance and name the new table. name the new table. Check the new table and be sure that the Check the new table and be sure that the information ends up in appropriate fields.information ends up in appropriate fields.
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 06, Slide Day 06, Slide 88
Import Exercise QuestionsImport Exercise Questions
In your import, pay attention to the In your import, pay attention to the following questions:following questions:– What format style is used in this text file?What format style is used in this text file?– What is the role of the first line in the file?What is the role of the first line in the file?– What data types and key are appropriate?What data types and key are appropriate?– Is the order of the fields the same between Is the order of the fields the same between
the table the table tbl_Schoolstbl_Schools and the text file and the text file Schools-2-06.txtSchools-2-06.txt??
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 06, Slide Day 06, Slide 99
Import ProblemsImport Problems
What problems did your group run into What problems did your group run into when trying to import? when trying to import?
How did you solve them? How did you solve them?
Which format style of text file was easiest Which format style of text file was easiest to import from?to import from?
What good does it do to have the data What good does it do to have the data from Schools-2 in a new table? Can we from Schools-2 in a new table? Can we move the data over somehow?move the data over somehow?
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 06, Slide Day 06, Slide 1010
Action QueriesAction Queries
What are action queries?What are action queries?
What new SQL commands are needed What new SQL commands are needed to perform action queries?to perform action queries?
How do you create action queries in How do you create action queries in Access’ Query Design View?Access’ Query Design View?
What happens if you make a mistake in What happens if you make a mistake in running an action query?running an action query?
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 06, Slide Day 06, Slide 1111
Favorite musicFavorite musicOpen your musicdemo databaseOpen your musicdemo databaseWe want to label some of our tracks as We want to label some of our tracks as "favorites," so using design view on that table, "favorites," so using design view on that table, add a new field called Favoriteadd a new field called Favorite– What data type should we use if we just want to What data type should we use if we just want to
indicate whether or not the track is a favorite?indicate whether or not the track is a favorite?
We really like long songs, but we don't like live We really like long songs, but we don't like live recordings, so construct a select query to list all recordings, so construct a select query to list all tracks over 8 minutes long that don't have LIVE tracks over 8 minutes long that don't have LIVE in their titles in their titles – (qry_day06_long_nonlive_tracks) [15](qry_day06_long_nonlive_tracks) [15]
Test your query! Test your query!
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 06, Slide Day 06, Slide 1212
Update QueryUpdate Query
Switch to SQL or DesignView Switch to SQL or DesignView
Change the SELECT query to an UPDATE Change the SELECT query to an UPDATE query to mark these tracks as favoritesquery to mark these tracks as favorites– See pages See pages 6-3 and 6-6 6-3 and 6-6 of the textbookof the textbook
Save the query as Save the query as qry_day06_favoritesqry_day06_favorites
Run the query to update the records Run the query to update the records
Check the results of the update by looking Check the results of the update by looking at the table!at the table!
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 06, Slide Day 06, Slide 1313
More on importing, moving dataMore on importing, moving data
Import the file Import the file mp3s_06.txt mp3s_06.txt from the Day 6 from the Day 6 AFS space to a new (temporary) table in AFS space to a new (temporary) table in your musicdemo databaseyour musicdemo database
What would we have to do if we wanted to What would we have to do if we wanted to get this data completely into our database get this data completely into our database in the right spots?in the right spots?
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 06, Slide Day 06, Slide 1414
Append queries (1)Append queries (1)
Different parts of this file need to go to Different parts of this file need to go to different places in our database, and some different places in our database, and some are unnecessary or redundant are unnecessary or redundant
We can construct an We can construct an append queryappend query to to copy data from one table to anothercopy data from one table to another
We'll practice with Artists, since it's a We'll practice with Artists, since it's a simple tablesimple table
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 06, Slide Day 06, Slide 1515
Append queries (2)Append queries (2)
Start a new query in design view, close the add tables Start a new query in design view, close the add tables dialog, and change the query dialog, and change the query typetype to an to an appendappend query query– Asked where to append (copy) the data Asked where to append (copy) the data TOTO, select tbl_Artists, select tbl_Artists
Now right-click to Show Table…Now right-click to Show Table…– We have to select where to get the data We have to select where to get the data FROMFROM, select your , select your
temporary tabletemporary table
Select each field you want to copySelect each field you want to copy– Under each, use the Under each, use the Append To Append To row to tell Access what field in row to tell Access what field in
the destination table to copy intothe destination table to copy into
Preview your query by switching to datasheet viewPreview your query by switching to datasheet view– Do not actually run the query yetDo not actually run the query yet
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 06, Slide Day 06, Slide 1616
Removing duplicate records:Removing duplicate records:SQL SQL DISTINCTDISTINCT
Notice any problems here?Notice any problems here?– Scroll down for more problemsScroll down for more problems
We have a lot of duplicate records (Artist name We have a lot of duplicate records (Artist name is repeated with each track) and a lot of blank is repeated with each track) and a lot of blank (no artist known, bad tags on the MP3s, etc.)(no artist known, bad tags on the MP3s, etc.)– Switch to design and filter out blank ArtistsSwitch to design and filter out blank Artists
To remove duplicate records, we use SQL To remove duplicate records, we use SQL keyword keyword DISTINCTDISTINCT right after right after SELECTSELECT– Enter in SQL viewEnter in SQL view– Removes all records where all selected fields are an Removes all records where all selected fields are an
exact duplicate of another recordexact duplicate of another record
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 06, Slide Day 06, Slide 1717
Append queries (3)Append queries (3)SQL SQL INSERT INTO…SELECTINSERT INTO…SELECT
Go to SQL view to add the word DISTINCT (it Go to SQL view to add the word DISTINCT (it goes right after SELECT)goes right after SELECT)Notice the new keywords: INSERT INTONotice the new keywords: INSERT INTO– These are used in all INSERT and APPEND queries, These are used in all INSERT and APPEND queries,
for for adding new recordsadding new records to a table to a table– Syntax is: INSERT INTO Syntax is: INSERT INTO tabletable ( (field1, field2…field1, field2…))– The rest is a normal SELECT queryThe rest is a normal SELECT query
Preview the query in Preview the query in datasheetdatasheet view viewSave & run the query! Then open tbl_Artists to Save & run the query! Then open tbl_Artists to check your results.check your results.
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 06, Slide Day 06, Slide 1818
Next steps: AlbumsNext steps: Albums
Look at the table albums in Design viewLook at the table albums in Design view
Then look at it in datasheet viewThen look at it in datasheet view
Compare this to mp3s_06 imported dataCompare this to mp3s_06 imported data
How would you construct an append query How would you construct an append query to move data from mp3s_06 to tbl_Albumsto move data from mp3s_06 to tbl_Albums– What can we do about artists?What can we do about artists?
Bottom line: You can't… yet. So don't!Bottom line: You can't… yet. So don't!
http://www.cse.msu.edu/~cse103http://www.cse.msu.edu/~cse103U:/msu/course/cse/103U:/msu/course/cse/103 Day 06, Slide Day 06, Slide 1919
HomeworkHomework
Do Do NOTNOT finish importing data from mp3s.txt (we finish importing data from mp3s.txt (we have not covered all skills you would need)have not covered all skills you would need)
Check the link on today’s classwork page for Check the link on today’s classwork page for homework to do in advance of Day 07homework to do in advance of Day 07
Read Days Read Days 7 and 87 and 8 in the textbook, covering in the textbook, covering relationships and the JOIN operatorrelationships and the JOIN operator– We will discuss relationships, and use JOIN to do our We will discuss relationships, and use JOIN to do our
first multiple-table SELECT queriesfirst multiple-table SELECT queries