WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

MySQL · Basic · question 5 of 100

What is a primary key, and why is it important in a database?

📕 Buy this interview preparation book: 100 MySQL questions & answers — PDF + EPUB for $5

In a relational database, a primary key is a column or set of columns that uniquely identifies each record in a table. The primary key serves as a reference point for other tables that need to establish a relationship with the table containing the primary key.

The importance of the primary key lies in its ability to ensure data integrity in the database. By establishing a unique identifier for each record, the primary key helps prevent duplicate or inconsistent data. It also allows for efficient querying and sorting of data in the table.

For example, let’s consider a table called “employees” that contains the following columns:

| employee_id | first_name | last_name | department_id | hire_date   |
|-------------|------------|-----------|---------------|-------------|
| 1           | John       | Smith     | 1             | 2021-01-01  |
| 2           | Jane       | Doe       | 2             | 2021-02-01  |
| 3           | Bob        | Brown     | 1             | 2020-12-01  |

In this table, the primary key could be the “employee_id” column, as each employee has a unique identifier. This means that each employee record can be uniquely identified by their “employee_id”. If we tried to insert a record with the same “employee_id” as an existing record, we would receive a constraint violation error, since the “employee_id” is the primary key and must be unique.

Overall, the primary key is a crucial component of a well-designed database, as it ensures data accuracy and maintains the integrity of the overall data model.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic MySQL interview — then scores it.
📞 Practice MySQL — free 15 min
📕 Buy this interview preparation book: 100 MySQL questions & answers — PDF + EPUB for $5

All 100 MySQL questions · All topics