DEV Community

Pranav Bakare
Pranav Bakare

Posted on

Keys in SQL

In Oracle SQL and relational database management systems, keys are essential elements that help maintain data integrity and facilitate efficient data retrieval. Here’s an overview of different types of keys, including the Primary Key, as well as additional types of keys commonly used in Oracle databases.

  1. Primary Key

A Primary Key is a field (or a combination of fields) that uniquely identifies each record in a table. The main characteristics of a primary key are:

Uniqueness: Each value in the primary key must be unique across all records in the table.

Non-null: A primary key cannot contain null values; every record must have a valid primary key value.

Immutable: The value of a primary key should not change over time to maintain the integrity of the relationship between tables.

Example:

CREATE TABLE Employees (
EmployeeID NUMBER PRIMARY KEY,
FirstName VARCHAR2(50),
LastName VARCHAR2(50),
HireDate DATE
);

  1. Candidate Key

A Candidate Key is a set of one or more columns that can uniquely identify a record in a table. There can be multiple candidate keys in a table, but only one can be chosen as the primary key. Candidate keys also must meet the uniqueness and non-null requirements.

Example:

In the Employees table, both EmployeeID and a combination of FirstName and LastName could be considered candidate keys, provided they are unique.

  1. Foreign Key

A Foreign Key is a field (or a collection of fields) in one table that uniquely identifies a row in another table. It establishes a relationship between two tables and is used to enforce referential integrity.

Example:

CREATE TABLE Departments (
DepartmentID NUMBER PRIMARY KEY,
DepartmentName VARCHAR2(50)
);

CREATE TABLE Employees (
EmployeeID NUMBER PRIMARY KEY,
FirstName VARCHAR2(50),
LastName VARCHAR2(50),
DepartmentID NUMBER,
FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID)
);

  1. Composite Key

A Composite Key is a primary key that consists of two or more columns. This is used when a single column is not sufficient to uniquely identify a record.

Example:

CREATE TABLE CourseEnrollments (
StudentID NUMBER,
CourseID NUMBER,
PRIMARY KEY (StudentID, CourseID) -- Composite Key
);

  1. Unique Key

A Unique Key constraint ensures that all values in a column (or a combination of columns) are distinct from one another. Unlike a primary key, a unique key can accept null values (but only one null value per unique key column).

Example:

CREATE TABLE Users (
UserID NUMBER PRIMARY KEY,
Email VARCHAR2(100) UNIQUE -- Unique Key
);

  1. Surrogate Key

A Surrogate Key is an artificial key that is used as a substitute for the natural primary key. Surrogate keys are typically numeric and are generated by the database (e.g., using sequences). They are used to maintain uniqueness without relying on the natural data.

Example:

CREATE TABLE Products (
ProductID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
ProductName VARCHAR2(100),
Price NUMBER
);

  1. Alternate Key

An Alternate Key is any candidate key that is not selected as the primary key. It can still enforce uniqueness but is not the primary means of identifying records in a table.

Summary

In Oracle SQL, understanding the different types of keys is essential for designing a robust and efficient database schema. Each key serves a specific purpose:

Primary Key: Uniquely identifies a record; cannot be null.

Candidate Key: A potential primary key; must be unique and non-null.

Foreign Key: Establishes a relationship between two tables.

Composite Key: A primary key composed of multiple columns.

Unique Key: Ensures unique values, allowing one null value.

Surrogate Key: An artificial key for uniqueness; not derived from the actual data.

Alternate Key: Any candidate key not chosen as the primary key.

Using these keys effectively helps maintain data integrity, supports relationships between tables, and ensures efficient querying in Oracle databases.

Top comments (0)