Presentation on theme: "Database Relationships in Access As you recall, the data in a database is stored in tables. In a relational database like Access, you can have multiple."— Presentation transcript:
Database Relationships in Access As you recall, the data in a database is stored in tables. In a relational database like Access, you can have multiple tables and you can relate fields in one table to fields in another. For example, a country in the West Africa table could be related to the nationality of the people in the Famous Africans table. Then if you search for information about the country Ghana, you could also see the Famous Africans from Ghana
As another example, a book thats been checked out of the library could be related to the student who checked it out. When a librarian looks up information about the book in one table, it is linked to the students information (address, phone number, etc) in the other table.
Creating relationships is important because it avoids unnecessary duplication of data. For example, the library need not keep its own list of students addresses. Creating relationships is also more efficient. For example, if a students address changes, it only needs to be updated in one table, and the other tables that are linked to it will have access to the revised data.
Relationship Review When working with databases, you may want to access info from several tables at once. To do so, you must create relationships between the tables. Such relationships tell access how to match the info from separate tables.
Relationship Review Cont When creating relationships, you always work with two tables at a time. One table is called the primary table and the other is called the related.
Types of Relationships Relationships may be one-to-one, one-to many, or many-many. One-to-many relationships are the most common. One table (the parent) can have many matching records in another table (the child).
Note Before you create a relationship between tables, it must meet these requirements: Both tables need to be in the same database file. If they are in separate files, you need to import a table from one database file to another (File, Get External Data, Import). Both tables need to have a primary key defined (open the Table, choose view, design view, then click in the field and click the key icon or choose Edit then Primary KEY)
Note Cont The two related fields must be of the same data type (number, text, date/time, etc) The data in the two related fields must match. That is, dont enter Ghana in the Country field and Ghanaian in the Nationality field. Use Ghana in both cases.
When you fail to join two or more tables, you will produce a query results datasheet in which every record in say table A is joined with every record in say table B. This is called a cross or cartesian product. If you create join fields that do not contain common data, the query results datasheet will show unexpected results.
Types of Joins There are three basic types of joins in Access: 1.Inner joins, which are also known as equi-joins 2.Outer joins 3.Self joins
Types of Joins Cont An inner join is the default join in Access. Access select only those records from both tables that have a matching value in the fields that joined. If a record in say table A has a value in the join field that is not found in the corresponding field in any of the records in say table B, then that record is not included in the query results.
Types of Joins Cont Inner Join Type Table A Table B
Types of Joins Cont An outer join includes all the records from one table and only those records in the second table that have matching values in the joined field. We have the Left Outer Join and Right Outer Join.
Types of Joins Cont Left Outer Join Type Table B Table A
Types of Joins Cont Right Outer Join Type Table A Table B
Types of Joins Cont In Access the first Join Type is the Inner Join and the second and third Join Types are Left and Right Outer Joins Respectively.
Types of Joins Cont A self join relates the data between records in a single table. For example, if your table records measurements overtime, and each record represent a sampling, you may want to compare the measurement between records.
Types of Joins Cont To create a self join, you must add the table to the query twice and join the related fields. By joining the two copies of the same table, you create a self join that combines records from the same table when there are matching values in the joined tables.
Types of Joins Cont You can use a self join in any situation in which you need to compare records in the same table to one another.