9
Create table relationships (Access basics, part 2) then press F5 or click Slide Show > From Beginning to start the course. In the message bar, click Enable Editing, If the videos in this course don’t play, you may need to download QuickTime or just switch to PowerPoint 2013 .

Microsoft Office Access 2013 Tutorial -Creating Table Relationships Part-2

Embed Size (px)

Citation preview

PowerPoint Presentation

Create table relationships (Access basics, part 2)

then press F5 or click Slide Show > From Beginning to start the course. In the message bar, click Enable Editing,

If the videos in this course dont play, you may need to download QuickTime or just switch to PowerPoint 2013.

1

Create table relationships (Access basics, part 2) Closed captions

Press F5 to start, Esc to stopSummaryFeedback Help Lookup WizardUse the paneMany-to-manyOne-to-one3:191:313:253:065:07Intro & types1/5 videos

Now that we have a set of tables, we can build a relationship between them.Relationships are critical because they link your tables back together after you divide your data,and also because they have a big impact on how you design your queries and get answers from your data.Any relational database uses three types of relationships between its tables: one-to-many, many-to-many, and one-to-one.The relationship you use depends on your data, and heres what I mean by that.You have a one-to-many relationship when a single record in one tableis related to one or more records in another table.And our sample database has this kind of relationship.One customer can have one or more phone numbers, and each number is only related to one customer.You have a many-to-many relationshipwhen a single record in table A can be related to one or more records in table B,and a single record in B can also be related to one or more records in table A.You typically see many-to-many relationships in order tracking databases,where an order can contain many products, and a product can be part of multiple orders.Youll also see this type of relationship in student databases, where many students can occupy many classrooms.You have a one-to-one relationship when a single record in one tableis related to just a single record in another table.For example, a single employee may drive one company car.But you wont see this very often, because one-to-one relationships arent very common.So now I want to select Database Tools, then Relationships.That starts the Relationships pane, and youll use this tool quite a bit.This is where you build and manage most of your relationships,and this is a typical relationshiptwo tables with a line between them.If you dont see a line, the tables arent related.Whats more, this is a one-to-many relationship,and you can tell it is because the relationship line here has a 1 and an infinity symbol.And those symbols point to a couple concepts you need to understand.First, the table on the one side is a parent table, and the table on the many side is a child table.Second, you can think of most of your relationships as having sides.In this case, you have a table on the one side and a table on the many side.Sides are important because they have a big impact on how you design your queries,and well see how that works in the next course.For now, just keep the idea of sides in mind.So back to our relationship, and Im going to select the relationship line,then Edit Relationships here on the ribbon.This dialog controls three settings that you need to understand in order to use relationships properly.Referential Integrity, Cascading Updates, and Cascading Deletes can have a big impact on your data, and heres what they do.First, Referential Integrity synchronizes the records in both tables.This is a critical part of keeping your data accurate, so you almost always want to set it.For example, with referential integrity enforced, I cant add a phone number unless I have a name in the Customers table.That prevents me from creating what are called orphans, data with no parent records, because orphans can cause problems.For example, just imagine a sales figure with no order attached,and you can see why Im harping about setting referential integrity.Cascading Updates allows any changes on the parent sideto cascade through to any related records on the child or many side.So if a customer changes his name, for example, you only have to enter that change in the Customers table,and any related records will change.Cascading deletes control whether you can delete data. If you dont select the option, you cant delete.But if you do, then when you delete a record, Access deletes everything related to that record.For example, delete a customer and the phone numbers go, too.That can be a good thing because it prevents orphans, numbers with no customer.But remember your business rules may prevent you from deleting data, so keep them in mind.They can be an key part of your database design.And finally, what weve covered here may seem like a lot to absorb,but for the rest of this course, well put it all to use and create each type of relationship, so keep going.

5761234Course summary8Help

Create table relationships (Access basics, part 2) Closed captions

Press F5 to start, Esc to stopSummaryFeedback Help 2/5 videos

Lookup WizardUse the paneMany-to-manyOne-to-one3:191:313:253:065:07Intro & types

One of the easiest ways to create a one-to-many relationship is to use a tool called the Lookup Wizard.The wizard actually does two jobs for you. It creates the relationship, and when youre done,the foreign key field lets you choose values from the table on the one side of the relationship.That makes it easier to enter data.In our example, all I have to do is select a namethats the value on the one sideand enter a phone number.But remember one thing: When you use the wizard,you dont need a foreign key field in your child table because the wizard creates it for you.If you already have a foreign key, thats okay, go ahead and use it, but if not, just let the wizard create one for you.So heres our sample database, without the foreign key that we added in the previous course.To start, click the header on a blank field, or click the header in your foreign key,if you have one, choose Lookup & Relationship, and the wizard starts.Use the first option, looking up values from another table, and choose Next.The parent table is Customers, so make sure thats selected and choose Next.We need to look up the values in the Customer Name field, so move that over.This is the calculated field we created in the first course.You saw me using this field at the start of this video, and notice that it shows you the entire name.That takes the guesswork out of choosing the right person, or more generically, the right data.Data entry is faster and more accurate that way, and thats why we create that type of field.So choose next, and a sort order is optional, so I wont bother with one, just choose Next again.As a rule, you want to hide the key column because it just clutters up your database if its visible, so choose Next.And here Ill name the field Customer IDFK.That tells me the parent field is Customer ID, and FK reminds me that this is a foreign key.So now Ill select Enable Data Integrity and Cascade Delete.This is how you enable referential integrity and cascading deletes.Select Finish, and theres your field.Finally, I want to make a change to the relationship, and in order to do that, I need to close the Phone Numbers table.You cant change a relationship if one of the tables involved in that relationship is open.So after the table is closed, I go to the Database tools tab and select Relationships.Then I select the relationship line, then Edit Relationships here on the ribbon,and you can see that referential integrity and cascading deletes are both selected.Ill also enable the middle option, Cascade Update Related Fields, select OK, and were done.And up next, were going to do this all again, but were going to do it with the Relationships pane, so keep going.

5761234Course summary8Help

Create table relationships (Access basics, part 2) Closed captions

Press F5 to start, Esc to stopSummaryFeedback Help

3/5 videos

Another way to create a relationship is with the Relationships pane, and heres what it looks like.These are the tables involved in a relationship, and the line tells you whether its a one-to-many relationship or not.The thing to remember about this tool is it doesnt create a lookup list for you,unlike the Lookup Wizard, but thats not all bad.If you use queries to extract and process your data, you need the relationship,but not the lookup list, because the query provides the data you want to see.So to use the pane, select Database Tools, then Relationship.If the pane is blank, you can drag your tables from the Navigation Pane, like this.Now, unlike the Lookup Wizard, your child table has to have a foreign key with the same data type as the primary key,or a compatible data type.So theres my foreign key, and to create the relationship, I just drag the primary key over to the foreign key and let go.That starts the Edit Relationships dialog,and I enable referential integrity, cascading updates and deletes, and select Create.There you go, a one-to-many relationship.And up next, well create a many-to-many relationship because you find them in any order tracking database.

Lookup WizardUse the paneMany-to-manyOne-to-one3:191:313:253:065:07Intro & types

5761234Course summary8Help

Create table relationships (Access basics, part 2) Closed captions

Press F5 to start, Esc to stopSummaryFeedback Help

4/5 videos

Lookup WizardUse the paneMany-to-manyOne-to-one3:191:313:253:065:07Intro & types

Now that youve seen how to create a one-to-many relationship, lets create a many-to-many relationship.And to do that, well grow our database into a rudimentary order tracking system, with Products and Orders tables.And as a review, we have a many-to-many relationship between these tablesbecause a product may appear in many orders, and an order can contain many products.So the first step in building a many-to-many relationship is to create a new table.This is called an intermediate table, and it will store the data from the other tables involved in the relationship.You may also hear this new table referred to as a linking or junction table.The next thing you do is create a pair of one-to-many relationshipsbetween the junction table and the other tables involved.As part of that, you also need to make both of these fields into the junction tables primary key.This is called a composite key, and the thing to remember is you have to build them in Design view.So, Create, Table Design, and theres a new, blank table.Ill also open the Products and Orders tables this way, too, and the first thing Ill do is look at the primary keys for each.The Products table uses an AutoNumber field called ProductID,and over to Orders, and I see its called OrderID, and its also an AutoNumber field.So back to my junction table, and I enter ProductID, select AutoNumber,then enter OrderID, only this time I select the Number data type.Im using Number because Access only allows one AutoNumber field in a table, so I need to use something thats compatible.Now Ill select both fields, and to do that, you put the cursor in the gray box next to either field and drag.And with both fields selected, Ill click Primary Key here on the ribbon.We need both fields to ensure that each order, and everything in each order, is uniquely identified.So Ill use Ctrl + S to save the table, and call it Order Details.So now we can create the relationships.Start by closing all your open tables, and then select Database Tools, and Relationships.Select Show Table, here on the Design tab, add the Order Details, Orders, and Products tables, and close the dialog.Arrange the tables so Order Details is in the middle,and then Ill drag the OrderID field from Orders over to the junction table.In the Edit Relationships dialog, select Enforce Referential Integrity and create the relationship.Do the same with the ProductID field, enforce referential integrity, choose Create, and thats it.Youve just created a many-to-many relationship.So next, Ill show you how to create a one-to-one relationship, so keep going.

5761234Course summary8Help

Create table relationships (Access basics, part 2) Closed captions

Press F5 to start, Esc to stopSummaryFeedback Help 5/5 videos

Lets create a one-to-one relationship. You wont do this very often, but you need to know how.And for this video, Ive added two tables to the database, one for a set of company cars,and another for the employees who drive them.You can see the company uses one type of car, so the way to identify who drives what is to use the license number field.Now for this video, were going to pretend that each employee is assigned to a single car,but before I start, you need to know another new term: Index.Access indexes the fields in your databases, and it uses them like you use the index in a bookto find data quickly.Indexes are on by default, but you can change their behavior or turn them off.Thats important, because you create a one-to-one relationship by first creating a one-to-many relationshipand then changing the indexes for the key fields in both tables.So Ill start by creating the relationship, and this time Ill use the Lookup Wizard.Ill get values from another table, and thats the Company Cars table, I want the license number field,and I wont bother with a sort order because Im dealing with a small number of values.Ill hide the key column, call the field Company Car, and I wont enable data integrity.This is probably the only time you wont turn referential integrity on.That way, if I delete an employee, I dont accidentally delete a car.Click Finish, and now I have a foreign key, and an easy way to pick license numbers.So next, make sure you have the field selected, unlike me, then select Fields, then Indexed, and Unique.Now go to the Company Cars table and select the primary key, if it isnt already.You can see its also indexed and unique because its an AutoNumber field, so were done.If youre in Design view, you can see the Indexed property down here in the Field Properties pane.And if you open the list for the property, you see three values.You can use two of these values in a one-to-one relationship, either No, or Yes, No Duplicates.And, if we start the Relationships pane and look at the relationship,you can see its just a plain line with no 1 or infinity symbol.So now our database has all three types of relationships.Our tables are linked together, but more than that, weve also made it possible to write queries.Your table relationships have a big impact on query design, and well see how that works in the next course, so keep going.

Lookup WizardUse the paneMany-to-manyOne-to-one3:191:313:253:065:07Intro & types

5761234Course summary8Help

HelpCourse summary

Press F5 to start, Esc to stop

SummaryFeedback Help

57612348

Course summaryCreate table relationships (Access basics, part 2)

Lookup WizardUse the paneMany-to-manyOne-to-one3:191:313:253:065:07Intro & typesWhy relationshipsThey link your tables, so theyre a critical part of your database, and they inform your queries.Relationship typesOne-to-one, one-to-many, and many-to-many. One-to-many is most common, many-to-many are part of most transaction and student databases, 1:1 is rare.Create a one-to-manyIn datasheet view, select your foreign key or a blank field.Foreign key: Select the field, then Fields > More Fields > Lookup & Relationship, and follow the wizard. Blank field: Select the header, then Lookup & Relationship and follow the wizard. Use the Relationships paneChild table must have a foreign key. Select Database Tools > Relationships, then drag the primary key from the parent table to the foreign key in the child table. Use Edit Relationships to set referential integrity, and cascading updates/deletes as needed.Many-to-manyCreate > Table Design, create a junction table. In that table, duplicate the primary keys from the tables on both sides of the relationship, select both fields and set them as a composite key. One-to-oneCreate a one-to-many relationship, then: Design view: Set the Indexed property for both keys to No, or Yes, No Duplicates.Ribbon, set both keys to Indexed and Unique.

See alsoGuide to table relationshipsCreate a relationshipEdit a relationshipDelete a relationshipMore training coursesOffice Compatibility Pack

Check out more coursesHelpCourse summary

Press F5 to start, Esc to stop

SummaryFeedback Help

57612348

Rating and commentsThank you for viewing this course!

How did we do? Please tell us what you think

Lookup WizardUse the paneMany-to-manyOne-to-one3:191:313:253:065:07Intro & types

HelpCourse summary

Press F5 to start, Esc to stop

SummaryFeedback Help

57612348

Help

If you download a course and the videos dont playClick Enable Editing if you see that button. If that doesnt work, you may have PowerPoint 2007 or earlier. If you do, you need to get the PowerPoint Viewer. If you have PowerPoint 2010, you need the QuickTime player, or you can upgrade to PowerPoint 2013.Using PowerPoints video controlsPoint at the bottom edge of any video to start, stop, pause, or rewind. You drag to rewind. Going placesYou can go to any part of a course by clicking the thumbnails (light or shaded) below the video. You can also click the forward and back arrows, or press Page Up or Page Down. Stopping a courseIf youre viewing online, click your browsers Back button. If youre viewing offline, press Esc. If youre watching a video, press Esc once to stop the video, again to stop the course.

Lookup WizardUse the paneMany-to-manyOne-to-one3:191:313:253:065:07Intro & types