A primary key is a special relational database table column (or combination of columns) designated to uniquely identify all table records. A primary key’s main features are:
A primary key is either an existing table column or a column that is specifically generated by the database according to a defined sequence.
The primary key concept is critical to an efficient relational database. Without primary key and closely-related foreign key concepts, relational databases would not work. Almost all individuals deal with primary keys frequently but unknowingly in everyday life. For example, students are routinely assigned unique identification (ID) numbers, and all adults receive government-assigned and uniquely-identifiable social security numbers. Imagine designing a database to hold all of the data stored by a commercial bank. Two of the database tables include the CUSTOMER_MASTER, which stores basic and static customer data (e.g., name, date of birth, address and social security number, etc) and the ACCOUNTS_MASTER, which stores various bank account data (e.g., account creation date, account type, withdrawal limits or corresponding account information, etc). To uniquely identify customers, a convenient column or combination of columns is selected to guarantee that two customers never have the same unique value. Thus, certain columns are immediately eliminated, e.g., surname and date of birth. A good primary key candidate is the column that designated to hold unique and government-assigned social-security numbers. However, some account holders (e.g., children) may not have social security numbers, and this column’s candidacy is eliminated. The next logical option is to use a combination of columns such as the surname to the date of birth to the email address, resulting in a long and cumbersome primary key. The best option is to create a separate primary key in a new column named CUSTOMER_ID. Then, the database automatically generates a unique number each time a customer is added, guaranteeing unique identification. As this key is created, the column is designated as the primary key within the SQL script that creates the table, and all null values are automatically rejected. The next step is to review the above scenario from the account’s perspective. The account holder in the ACCOUNTS_MASTER table needs to be identified, and this customer must be an existing customer in the CUSTOMER_MASTER table. Logically, the customer should be added before the account is created. So, one of the ACCOUNTS_MASTER table columns is the CUSTOMER_ID column, which references the same CUSTOMER_ID column in the CUSTOMER_MASTER table. The CUSTOMER_ID column in the ACCOUNTS_MASTER table is known as a foreign key. As this occurs, each ACCOUNTS_MASTER table account must be uniquely identifiable. For this purpose, a primary-key column named ACCOUNT_ID is created. As noted above, the primary key and closely-related foreign key concepts are fundamental and vital relational database features that provide a streamlined solution to inefficient data storage issues. The above example may be expanded to illustrate how primary/foreign key pairs function throughout the bank’s database. For example, a table created to hold loan data could be named LOANS_MASTER and contain a LOAN_ID primary key and CUSTOMER_ID foreign key, showing the association between customers and loans. As described in this example, the account number associated with each ACCOUNT_ID allows for the secure handling of customer complaints or queries and also demonstrates why primary keys offer the fastest method of data searching within tables. For example, a customer may be asked to provide his surname when conducting a bank query. A common surname (e.g., Smith) query is likely to return multiple results. When querying data, utilizing the primary key uniqueness feature guarantees one result.
Read More »
Get Techopedia delivered to your inbox!