Keys identify rows and link tables. Constraints are rules that stop wrong data from entering. Together they keep a relational database correct.

Keys

Use this student table as the running example: student(roll_no, email, aadhaar_no, name, dept_id).

  • Super key: any set of columns that identifies a row uniquely, for example {roll_no}, {roll_no, name} or {email}.
  • Candidate key: a minimal super key, with no unnecessary column. roll_no and email are both candidate keys.
  • Primary key: the candidate key chosen to identify rows. It is unique and never NULL. Here, roll_no.
  • Alternate key: a candidate key that was not chosen. Here, email.
  • Unique key: a column (or set) whose values must not repeat, but which may allow one NULL, depending on the DBMS.
  • Composite key: a key made of two or more columns, for example (student_id, course_id) in an enrolment table.
  • Foreign key: a column that refers to the primary key of another table. dept_id in student refers to department(dept_id).
  • Surrogate key: an artificial key, usually an auto-number, used when no natural key is suitable.

Tip: Every primary key is a candidate key, and every candidate key is a super key, but not the other way round.

Constraints in a table

Constraint What it enforces
PRIMARY KEY Unique and NOT NULL identity of a row
FOREIGN KEY Value must exist in the referenced table (or be NULL)
UNIQUE No two rows share the same value
NOT NULL The column must have a value
CHECK The value must satisfy a condition
CREATE TABLE department (
    dept_id   INT PRIMARY KEY,
    dept_name VARCHAR(40) NOT NULL UNIQUE
);

CREATE TABLE student (
    roll_no INT PRIMARY KEY,
    email   VARCHAR(60) UNIQUE NOT NULL,
    name    VARCHAR(40) NOT NULL,
    marks   INT CHECK (marks BETWEEN 0 AND 100),
    dept_id INT,
    FOREIGN KEY (dept_id) REFERENCES department(dept_id)
);

Trying to insert a student with marks = 120, or with a dept_id that does not exist in department, makes the DBMS reject the row.

Worked example

In enrolment(student_id, course_id, grade), one student may take many courses and one course has many students, so neither column alone is unique. The pair (student_id, course_id) is a composite primary key, and each column is also a foreign key.

Common mistakes

  • Calling any unique column the primary key. A table has one primary key but may have several candidate keys.
  • Allowing NULL in a primary key. It is never allowed.
  • Forgetting that a foreign key may be NULL unless NOT NULL is also given.

Practice questions

  1. Differentiate super key and candidate key.
  2. What is a foreign key? Give an example.
  3. Differentiate primary key and unique key.
  4. Write a CREATE TABLE statement with a CHECK constraint.
  5. What is a surrogate key and when is it used?

Answers to the practice questions

  1. A super key is any set of columns that is unique; a candidate key is a minimal super key.
  2. A column referring to the primary key of another table, such as dept_id in student.
  3. Primary key: only one per table, never NULL. Unique key: several per table, a NULL may be allowed.
  4. CREATE TABLE emp (id INT PRIMARY KEY, salary INT CHECK (salary > 0));
  5. An artificial key, such as an auto-number, used when no natural key identifies rows well.

Sources

  • Official BCA syllabus Database Management Systems, course code CC202, Unit I
  • Raghu Ramakrishnan and Johannes Gehrke, Database Management Systems, 3rd edition, McGraw-Hill