As you may already know,databases use tables to organize information. (If you dont have a basic familiarity with database concepts, read What is a Database?) Each table consists of a number ofrows, each of which corresponds to a single database record. So, how do databases keep all of these records straight? Its through the use of keys.
The first type of key well discuss is the primary key . Every database table should have one or more columns designated as the primary key. The value this key holds should be unique for each record in the database. For example, assume we have a table called Employees that contains personnel information for every employee in our firm. Wed need to select an appropriate primary key that would uniquely identify each employee. Your first thought might be to use the employees name.
This wouldnt work out very well because its conceivable that youd hire two employees with the same name. A better choice might be to use a unique employee ID number that you assign to each employee when theyre hired. Some organizations choose to use Social Security Numbers (or similar government identifiers) for this task because each employee already has one and theyre guaranteed to be unique. However, the use of Social Security Numbers for this purpose is highly controversial due to privacy concerns. (If you work for a government organization, the use of a Social Security Number may even be illegal under the Privacy Act of 1974.) For this reason, most organizations have shifted to the use of unique identifiers (employee ID, student ID, etc.) that dont share these privacy concerns.
Once you decide upon a primary key and set it up in the database, the database management system will enforce the uniqueness of the key. If you try to insert a record into a table with a primary key that duplicates an existing record, the insert will fail.
Most databases are also capable of generating their own primary keys. Microsoft Access , for example, may be configured to use the AutoNumber data type to assign a unique ID to each record in the table. While effective, this is a bad design practice because it leaves you with a meaningless value in each record in the table. Why not use that space to store something useful?
The other type of key that well discuss in this course is the foreign key . These keys are used to create relationships between tables. Natural relationships exist between tables in most database structures. Returning to our employees database, lets imagine that we wanted to add a table containing departmental information to the database. This new table might be called Departments and would contain a large amount of information about the department as a whole. Wed also want to include information about the employees in the department, but it would be redundant to have the same information in two tables (Employees and Departments). Instead, we can create a relationship between the two tables.
Lets assume that the Departments table uses the Department Name column as the primary key. To create a relationship between the two tables, we add a new column to the Employees table called Department. We then fill in the name of the department to which each employee belongs. We also inform the database management system that the Department column in the Employees table is a foreign key that references the Departments table. The database will then enforcereferential integrity by ensuring that all of the values in the Departments column of the Employees table have corresponding entries in the Departments table.
Note that there is no uniqueness constraint for a foreign key. We may (and most likely do!) have more than one employee belonging to a single department. Similarly, theres no requirement that an entry in the Departments table have any corresponding entry in the Employees table. It is possible that wed have a department with no employees.
For more on this topic, read Creating Foreign Keys .