LSP 121 Normalization Queries (contd). * Normalization Normalization is the process of efficiently...
-
date post
20-Dec-2015 -
Category
Documents
-
view
233 -
download
2
Transcript of LSP 121 Normalization Queries (contd). * Normalization Normalization is the process of efficiently...
![Page 1: LSP 121 Normalization Queries (contd). * Normalization Normalization is the process of efficiently organizing your data Normalizing your database can.](https://reader031.fdocuments.us/reader031/viewer/2022031914/56649d435503460f94a1f12c/html5/thumbnails/1.jpg)
LSP 121
NormalizationQueries (contd)
![Page 2: LSP 121 Normalization Queries (contd). * Normalization Normalization is the process of efficiently organizing your data Normalizing your database can.](https://reader031.fdocuments.us/reader031/viewer/2022031914/56649d435503460f94a1f12c/html5/thumbnails/2.jpg)
* Normalization• Normalization is the process of efficiently organizing
your data• Normalizing your database can drastically improve its
performance– This becomes especially important with very large databases
• Normalization techniques include:– Avoiding data redundancy (ie avoid repetition of data)– Ensuring proper data dependencies
• See example…
![Page 3: LSP 121 Normalization Queries (contd). * Normalization Normalization is the process of efficiently organizing your data Normalizing your database can.](https://reader031.fdocuments.us/reader031/viewer/2022031914/56649d435503460f94a1f12c/html5/thumbnails/3.jpg)
Normalization• Let’s create a database for a car club• What if one person owns multiple cars? (One owner
can have many cars, so this is 1:M relationship) – Table: Members: In this table, we would have to repeat all
of the person’s info (MemberID, Name, Phone, etc, etc for EVERY car they own)
• This is called data redundancy and is very BAD!
– Instead, let’s create an additional separate table just for the cars. We’ll call this table Cars
• We can still connect the car with its owner by including a field in the Cars table MemberID field from the Owners table
• This is called establishing a “relationship” between the tables
![Page 4: LSP 121 Normalization Queries (contd). * Normalization Normalization is the process of efficiently organizing your data Normalizing your database can.](https://reader031.fdocuments.us/reader031/viewer/2022031914/56649d435503460f94a1f12c/html5/thumbnails/4.jpg)
* Data Redundancy
• A major no-no in database design.• Can lead to all kinds of problems down the road.
– For example, suppose you had a database in which you store a person’s phone number in three different tables. Now, suppose you updated a member’s phone number in one table, but forgot to do it in another? This is a very common and can really mess things up.
– You should try to limit fields to one place only.– However, you can “associate” fields between two tables.– Eg: In the ‘Cars’ table, you can refer to the MemberID from
the ‘Members’ table.
![Page 5: LSP 121 Normalization Queries (contd). * Normalization Normalization is the process of efficiently organizing your data Normalizing your database can.](https://reader031.fdocuments.us/reader031/viewer/2022031914/56649d435503460f94a1f12c/html5/thumbnails/5.jpg)
Normalization ExampleCar Club (original table)Member ID (primary key)Member NameMember AddressMember CityMember StateMember ZipMember PhoneDues Paid?National Member?Model of CarMake of CarYear of Car
Note:If a member has >1 car, the first 9fields will be repeated multiple timesThis is pointless – and dangerous!
MemberInfoMember ID (primary key)NameAddressCityStateZipPhoneDues Paid?National Member?
Cars
Model of CarMake of CarYear of CarMember ID (nota primary key here!)
Primary Key in MemberInfo table“Foreign Key” in Cars table?1:M (“one-to-many”) relationship
Relationship
Why isn’t MemberID a primary key in the CarTable table?
![Page 6: LSP 121 Normalization Queries (contd). * Normalization Normalization is the process of efficiently organizing your data Normalizing your database can.](https://reader031.fdocuments.us/reader031/viewer/2022031914/56649d435503460f94a1f12c/html5/thumbnails/6.jpg)
Data Redundancy• Data Redundancy: A bad idea!
– Key Point: Do every thing you can to avoid repeating data.– Database people do everythng they can to avoid data redundancy.
• For example, each time you add a new car for a user, you should not have to repeat all of the user’s personal info (name, address, phone, etc, etc) all over again.
• Instead, place the user’s personal info in one table (e.g. “Members”) and have a separate table for cars owned by the members of the club.
• We then create a relationship between the two tables.
![Page 7: LSP 121 Normalization Queries (contd). * Normalization Normalization is the process of efficiently organizing your data Normalizing your database can.](https://reader031.fdocuments.us/reader031/viewer/2022031914/56649d435503460f94a1f12c/html5/thumbnails/7.jpg)
Another Normalization ExampleStudent ClassRecords
Student ID (primary key)NameAddressCityStateZipPhoneMajorMinorDegree SoughtClass NameGradeNumber Credits The first 10 fields are repeated for each course
Student Info
Student IDNameAddressCityStateZipPhoneMajorMinorDegree Sought
Courses
Class NameGradeNumber CreditsStudent ID
Before
After
• StudentID is a primary key in the StudentInfo table and becomes a “foreign key” in the Courses table.• There is a 1:M (one-to-many) relationship between students
and courses. That is, one student can have many courses.
![Page 8: LSP 121 Normalization Queries (contd). * Normalization Normalization is the process of efficiently organizing your data Normalizing your database can.](https://reader031.fdocuments.us/reader031/viewer/2022031914/56649d435503460f94a1f12c/html5/thumbnails/8.jpg)
Practice Example
Customer IDCustomer Last NameCustomer PhoneCustomer AddressCustomer CityCustomer StateCustomer ZipSales Transaction DateSales AmountItemClearance Item?
Imagine a table of your customers and all of your sales to them.Would you change the design of your database?If so, how many tables would you want in this case? Primary keys? Foreign keys?
![Page 9: LSP 121 Normalization Queries (contd). * Normalization Normalization is the process of efficiently organizing your data Normalizing your database can.](https://reader031.fdocuments.us/reader031/viewer/2022031914/56649d435503460f94a1f12c/html5/thumbnails/9.jpg)
Practice Example
CustomerIDCustomer Last NameCustomer PhoneCustomer AddressCustomer CityCustomer StateCustomer ZipSales Transaction DateSales AmountItemClearance Item?
Table: Customers
CustomerID (primary key)LastNamePhoneAddressCityStateZip
Table: Sales
InvoiceID (primary key)CustomerID (foreign key)SalesTransactionDateSalesAmountItemClearanceItem?
![Page 10: LSP 121 Normalization Queries (contd). * Normalization Normalization is the process of efficiently organizing your data Normalizing your database can.](https://reader031.fdocuments.us/reader031/viewer/2022031914/56649d435503460f94a1f12c/html5/thumbnails/10.jpg)
Relationships
• You can create a relationship between any tables by hand
• Click Tools Relationships– Add the two tables to the view, click on one of the
fields (e.g. StudentID) drag it over to the other table’s identical field (StudentID) and un-click
– Check Enforce Referential Integrity (you don’t want children records without parents)
![Page 11: LSP 121 Normalization Queries (contd). * Normalization Normalization is the process of efficiently organizing your data Normalizing your database can.](https://reader031.fdocuments.us/reader031/viewer/2022031914/56649d435503460f94a1f12c/html5/thumbnails/11.jpg)
Relationships
• Access automatically creates a relationship between the two tables if:a. you create two tablesb. the first table has a primary keyc. you carry that primary key over to the second
table as a foreign keyd. the primary key and the foreign key are spelled
the same and have the same type
![Page 12: LSP 121 Normalization Queries (contd). * Normalization Normalization is the process of efficiently organizing your data Normalizing your database can.](https://reader031.fdocuments.us/reader031/viewer/2022031914/56649d435503460f94a1f12c/html5/thumbnails/12.jpg)
Data 1 Smith 555-5555 Palos Heights 2 Chen 666-6666 LaGrange 3 Wilson 777-7777 Chicago
3/3/09 20.45 Shirt N 1 3/3/09 5.99 Scarf N 1 3/4/09 29.99 Jeans Y 3
Important: Note the last column in the second table references the primary key (e.g. customerID) from the first table.
Let’s do the first part of today’s activity.
![Page 13: LSP 121 Normalization Queries (contd). * Normalization Normalization is the process of efficiently organizing your data Normalizing your database can.](https://reader031.fdocuments.us/reader031/viewer/2022031914/56649d435503460f94a1f12c/html5/thumbnails/13.jpg)
Simple Queries - contd
• To create an Access query, don’t use the query wizard. Instead, create query in Design view
• Let’s see how Access does it• Copy the Pets database from the Basic
Information page to your desktop (or My Documents)
• Then open the Pets database– If you don’t see any tables/queries etc on the left, click on
the down-arrow and choose ‘All Access Objects’
![Page 14: LSP 121 Normalization Queries (contd). * Normalization Normalization is the process of efficiently organizing your data Normalizing your database can.](https://reader031.fdocuments.us/reader031/viewer/2022031914/56649d435503460f94a1f12c/html5/thumbnails/14.jpg)
* Query on Dates• You can query based on dates, but only if the data was
stored as date/time– E.g. to search for dates after Jan 1 2004, you would type: >1/1/2004
• In a query, dates should be entered with # before and after the date. Note that dates can be in written many different formats, ie #1/1/2004#, #January 1, 2004#, #1-Jan-2004#– Access typically puts these in FOR you
• Different databases have different ways of dealing with dates.
![Page 15: LSP 121 Normalization Queries (contd). * Normalization Normalization is the process of efficiently organizing your data Normalizing your database can.](https://reader031.fdocuments.us/reader031/viewer/2022031914/56649d435503460f94a1f12c/html5/thumbnails/15.jpg)
** Queries – Using ‘OR’• In Access, put each criterion in a different row
– If you put criteria in the same row, the query will work as an AND query (discussed next)
– Customers with City=“Chicago” OR first name = “Jack”
![Page 16: LSP 121 Normalization Queries (contd). * Normalization Normalization is the process of efficiently organizing your data Normalizing your database can.](https://reader031.fdocuments.us/reader031/viewer/2022031914/56649d435503460f94a1f12c/html5/thumbnails/16.jpg)
• Another way of using ‘OR’• If you are looking for different values within a single field (e.g.
State), you can simply type the word ‘OR’ between each value:– E.g. You can look for records in the state of Indiana or
Tennessee or Ohio by saying “IL” OR “TN” OR “OH”
– E.g. Show all pets that are birds or snakes:
![Page 17: LSP 121 Normalization Queries (contd). * Normalization Normalization is the process of efficiently organizing your data Normalizing your database can.](https://reader031.fdocuments.us/reader031/viewer/2022031914/56649d435503460f94a1f12c/html5/thumbnails/17.jpg)
** Queries – Using ‘AND’• Logical AND - you can make multiple entries in the
query boxes. – E.g. In the Type field enter “Dog” and in the Color field
enter Brown
• In Access, putting criteria in the same row is how you accomplish an AND effect– Recall that this is different from an OR query, where the
different values must be on separate rows– E.g. Show all brown dogs:
![Page 18: LSP 121 Normalization Queries (contd). * Normalization Normalization is the process of efficiently organizing your data Normalizing your database can.](https://reader031.fdocuments.us/reader031/viewer/2022031914/56649d435503460f94a1f12c/html5/thumbnails/18.jpg)
** Queries – Using ‘AND’• In Access, putting criteria in the SAME row is a way of
accomplishing an AND effect– This example shows customers with city = “Chicago” AND
first name = “Jack”
![Page 19: LSP 121 Normalization Queries (contd). * Normalization Normalization is the process of efficiently organizing your data Normalizing your database can.](https://reader031.fdocuments.us/reader031/viewer/2022031914/56649d435503460f94a1f12c/html5/thumbnails/19.jpg)
AND Queries contd• Logical AND - You can also use an AND in one
field. – For example, in the Size field you can enter
>=3 AND <=9
• Possible operators include =, <>, <, >, <=, >=
![Page 20: LSP 121 Normalization Queries (contd). * Normalization Normalization is the process of efficiently organizing your data Normalizing your database can.](https://reader031.fdocuments.us/reader031/viewer/2022031914/56649d435503460f94a1f12c/html5/thumbnails/20.jpg)
Queries That Calculate• When performing a query, you can aggregate data
together from a series of records– E.g. Find all customers born before 1970 and calculate their
average sales.
• You can do various basic statistical calculations such as: Count, Sum, Avg, Max, Min, Standard deviation, etc
• Certain calculations can be performed only on certain datatypes. – E.g. You can not calculate the average of FirstName
![Page 21: LSP 121 Normalization Queries (contd). * Normalization Normalization is the process of efficiently organizing your data Normalizing your database can.](https://reader031.fdocuments.us/reader031/viewer/2022031914/56649d435503460f94a1f12c/html5/thumbnails/21.jpg)
Viewing Totals• To see these values, you need to click on Design ‘Totals’
– Totals can be found in the ‘Show/Hide’ box
• You will now see an additional row called ‘Total’ in your query design view• Under Total, the ‘Group By’ criteria is important…
![Page 22: LSP 121 Normalization Queries (contd). * Normalization Normalization is the process of efficiently organizing your data Normalizing your database can.](https://reader031.fdocuments.us/reader031/viewer/2022031914/56649d435503460f94a1f12c/html5/thumbnails/22.jpg)
Example• Say you have a database for a vet that treats dogs.
Each dog treated has an entry including ID, weight, and height
• If you want to find the average weight and height of all pets– * IMPORTANT: Note that under ‘Total’, we set ‘Group By’ ‘Count’ for type (it will
show how many pets there were) and to ‘Avg’ for weight (shows the average)
![Page 23: LSP 121 Normalization Queries (contd). * Normalization Normalization is the process of efficiently organizing your data Normalizing your database can.](https://reader031.fdocuments.us/reader031/viewer/2022031914/56649d435503460f94a1f12c/html5/thumbnails/23.jpg)
Example
• What if you want to find the average height and weight for all dogs?
![Page 24: LSP 121 Normalization Queries (contd). * Normalization Normalization is the process of efficiently organizing your data Normalizing your database can.](https://reader031.fdocuments.us/reader031/viewer/2022031914/56649d435503460f94a1f12c/html5/thumbnails/24.jpg)
Example
• What if you want to find the minimum and maximum weight for all dogs?
Note that you have to include the Weight field twice.
![Page 25: LSP 121 Normalization Queries (contd). * Normalization Normalization is the process of efficiently organizing your data Normalizing your database can.](https://reader031.fdocuments.us/reader031/viewer/2022031914/56649d435503460f94a1f12c/html5/thumbnails/25.jpg)
More Examples
• You can also perform totals on groups of records.• For example, suppose you want to count how many
different types of pets the vet has on record
This example is not as intuitive as the others. For example, you would not know that the ‘Type’ field should not be grouped. Learning to skillfuly query a database takes some reading and practice.
![Page 26: LSP 121 Normalization Queries (contd). * Normalization Normalization is the process of efficiently organizing your data Normalizing your database can.](https://reader031.fdocuments.us/reader031/viewer/2022031914/56649d435503460f94a1f12c/html5/thumbnails/26.jpg)
Further Examples?
• Experiment with more queries