What is the primary key in a database? – Knowligent
What is the primary key in a database?

What is the primary key in a database?

HomeHow to, TechWhat is the primary key in a database?

What is a primary key? In the world of databases, the primary key of a relational table uniquely identifies each record in the table. Databases use keys to compare, sort, and store records, and to create relationships between records.

Primary key and foreign key in database

Choosing the primary key in a database is one of the most important steps in the process. It can be a normal attribute that is guaranteed to be unique, such as a social security number in a table with no more than one record per person, or — preferably — it can be generated by the database management system, such as a globally unique identifier, or GUID, in Microsoft SQL Server. Primary keys can consist of a single attribute or multiple attributes in combination.

Primary keys are the unique links to related information in other tables where the primary key is used. It must be entered when a record is created and should never be changed. Each table in the database has a column or two specifically for the primary key.

Imagine that you have a STUDENTS table with a record for each student at a university. The student's unique student ID number is a good choice for a primary key in the STUDENTS table. The student's first and last names are not good choices, because there is always a chance that more than one student will have the same name.