What is Primary key?

A column, or set of columns, used to uniquely identify one row in a table. It carries the constraints that its values must not be duplicated and must not include NULL. The primary key is chosen from among the minimal sets of columns that can identify a row uniquely (candidate keys).

Japanese name: 主キー

Primary key appears in 8 practice questions across 1 topic, including Database Basics.

Field: Database Basics

Try it: questions about Primary key

In a relational database table, which condition must the column (or set of columns) designated as the primary key satisfy?

  1. AIts values must be numbers
  2. BIt must be the first column of the table
  3. CIts values must not be duplicated within the table and must not include NULL
Show answer and explanation

Answer: C. Its values must not be duplicated within the table and must not include NULL

A primary key is a column, or set of columns, used to uniquely identify one row in a table. Its values therefore must not duplicate those of any other row (uniqueness), and NULL, which represents an unknown value, is not allowed either (NOT NULL). When PRIMARY KEY is specified in an SQL CREATE TABLE statement, the DBMS (database management system) enforces these two constraints, and any operation that tries to insert a duplicate value or NULL fails with an error. A primary key may hold numbers such as employee numbers or character strings, and there is no rule about its column position. A minimal set of columns that can uniquely identify a row is called a candidate key, and the primary key is the one chosen from the candidate keys.

An enrollment table (student_no, course_no, grade) records which student took which course and with what grade. One student takes several courses, and one course is taken by several students. If the same student never registers for the same course twice, which is an appropriate primary key for this table?

  1. Astudent_no
  2. Bcourse_no
  3. CThe combination of student_no and course_no
Show answer and explanation

Answer: C. The combination of student_no and course_no

Because one student takes several courses, student_no alone appears in several rows and cannot identify a row uniquely. Likewise, because one course is taken by several students, course_no alone is not unique either. Combining student_no and course_no determines a single row, given that the same student never registers for the same course twice. A primary key made of several columns like this is called a composite key, and in SQL it is specified by listing the columns, as in PRIMARY KEY (student_no, course_no). The student_no and course_no columns of the enrollment table are also foreign keys referring to the primary keys of the student and course tables, and this table represents the many-to-many relationship between students and courses.

Primary key in the explanations

Questions that mention Primary key (8)

  1. In a relational database table, which condition must the column (or set of columns) designated as the primary key satisfy?
  2. An enrollment table (student_no, course_no, grade) records which student took which course and with what grade. One student takes several courses, and one course is taken by several students. If the same student never registers for the same course twice, which is an appropriate primary key for this table?
  3. The primary key of a customer table (cust_id, name) is cust_id, and cust_id in an orders table (order_id, cust_id) is defined as a foreign key referring to cust_id in the customer table. The customer table contains only the rows with cust_id 'C1' and 'C2'. What happens when INSERT INTO orders VALUES ('O9', 'C5') is executed in this state?
  4. The primary key of an order table (order_no, item_no, qty, cust_no, cust_name) is {order_no, item_no}, and its functional dependencies are {order_no, item_no} → qty, order_no → cust_no and cust_no → cust_name. Which is the correct result of normalizing this table up to third normal form (3NF)?
  5. The primary key of an employee table (emp_no, name, dept_code, dept_name) is emp_no, and every column holds a single value. The functional dependencies are emp_no → name, emp_no → dept_code and dept_code → dept_name. Which correctly states the normal form of this table?
  6. In relational database terminology, which term corresponds to one row of a table (relation)?
  7. In an employee table (emp_no, email, name, dept), both emp_no and email are different for every employee and never NULL. Name and dept may be the same as those of other employees. Which lists all the candidate keys of this table?
  8. In an E-R diagram, there is a many-to-many relationship between students and courses: one student takes several courses, and one course is taken by several students. Which is an appropriate way to implement this relationship with relational database tables?

Books on Database Basics Ad

As an Amazon Associate I earn from qualifying purchases.

Semiconductor, Electronics and Computing Calculators

Related terms

Relational database SQL Foreign key Third normal form (3NF) E-R diagram ACID properties Database index Database view Isolation level Normalization

Back to the glossary