Primary Keys And Referential Integrity- M1
We use primary keys for two main functions which are as a means of ensuring that records
in a table are unique and to link records in a table to records in another table. There are
many situations where you will want to ensure that records are unique. For example if you
have a database of names, you may want to ensure that duplicate names cannot be
entered. you might then create a compound primary key of First Name and Last Name. That
way, if you already had a record for John Smith, then an attempt to enter a second John
Smith would fail with an error message about duplicate keys.
You also use a primary key to create one-to-many links between two tables. When using a
primary key to link records, using a numeric primary key is often the most handy way to do
this. In Access, an Autonumber field is automatically filled in with an integer number that is
unique in the database. For example, in a table with an AutoNumber field, the very first
record entered will have 1 in the AutoNumber field, the second record will have 2 entered in
that field, and the third record will have 3 entered in that field.
For example, if you have a Customers table, you might create an AutoNumber field for
Customer ID in the table, to be the primary key. You could then use the CustomerID field as
the key to link data contained in other tables to the Customers table.
MS Access really wants users to have a primary key in all tables, and if you create a new
table without also creating a primary key, Access will offer to create a primary key for you.
When creating a primary key, Access adds an AutoNumber field to your table.
Referential integrity is a relational database concept, which states that table relationships
must always be consistent. In other words, any foreign key field must agree with the primary
key that is referenced by the foreign key. Therefore, any primary key field changes must be
applied to all foreign keys, or not at all. The same restriction also applies to foreign keys in
that any updates must be propagated to the primary parent key.
Using a relational database management system with referential integrity offers several
advantages. These include the prevention of data duplication which can lead to a database
being filled with useless data; as well as prevention of one table from pointing to nonexistent
fields in other tables; the guaranteed consistency of linked tables; the deletion of records that
contain a value referring to a foreign key in another table; and the prevention of records
being added to a table that contains a foreign key unless there is a primary key in the linked
table.
Evaluation Of Database Issues- D1