We will study the many kinds of SQL keys today. In essence, SQL has the following kinds of keys.
Keys: Primary, Foreign, Unique, Composite, Alternate, Surrogate, and Composite
1. Principal Key
This is the SQL table's fundamental key. It designates a certain table row. A primary key must have unique values when defined, meaning that data must be taken into consideration when putting it into the table. Additionally, it cannot be null, meaning that null values cannot be added to the main key column.
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
EmployeeNo VARCHAR(100) NOT NULL,
Email VARCHAR(200) NOT NULL,
Active BIT NOT NULL,
Phone VARCHAR(20) NULL,
CreatedDate DATETIME NOT NULL
);
2. Foreign Key
This key is related to the Primary key. A foreign key is basically the primary key in another table. It is building a connection among tables.
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
EmployeeNo VARCHAR(100) NOT NULL,
Email VARCHAR(200) NOT NULL,
Active BIT NOT NULL,
Phone VARCHAR(20) NULL,
CreatedDate DATETIME NOT NULL
);
CREATE TABLE EmployeeFiles (
EmployeeFileID INT PRIMARY KEY,
EmployeeID INT,
CreatedDate DATETIME NOT NULL,
FOREIGN KEY (EmployeeID) REFERENCES Employees(EmployeeID)
);
3. Unique Key
A unique key in the table ensures that the value in the row is unique overall. It does almost the same as the primary key, but it allows a null value for the column. For example, in the table below, the email will be unique for all records no duplicate email can be inserted in this table.
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
EmployeeNo VARCHAR(100) NOT NULL,
Email VARCHAR(200) NOT NULL UNIQUE,
Active BIT NOT NULL,
Phone VARCHAR(20) NULL,
CreatedDate DATETIME NOT NULL
);
4. Composite Key
When there is a requirement of uniqueness on the basis of 2 columns, then a Composite key is used. It is basically a primary key on multiple columns. For example, in the below query data will be inserted on the basis of the uniqueness of EmployeeNo and Email.
CREATE TABLE Employees (
EmployeeID INT IDENTITY(1,1),
Name VARCHAR(100) NOT NULL,
EmployeeNo VARCHAR(100) NOT NULL,
Email VARCHAR(200) NOT NULL,
Active BIT NOT NULL,
Phone VARCHAR(20) NULL,
CreatedDate DATETIME NOT NULL,
PRIMARY KEY (EmployeeNo, Email)
);
5. Alternate Key
When multiple keys are added to a table except the Primary key, then all keys (except the Primary Key) are identified as Alternate Keys. In the following query, EmployeeNo and Email are alternate keys.
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
EmployeeNo VARCHAR(100) NOT NULL UNIQUE,
Email VARCHAR(200) NOT NULL UNIQUE,
Active BIT NOT NULL,
Phone VARCHAR(20) NULL,
CreatedDate DATETIME NOT NULL
);
6. Surrogate Key
When the primary key of the table serves as a unique identifier, it is known as a Surrogate key. It may be database generated (as in the below query) or by another application (not supplied by the user). It is more often a database generated.
CREATE TABLE Employees (
EmployeeID INT IDENTITY(1,1) PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
EmployeeNo VARCHAR(100) NOT NULL UNIQUE,
Email VARCHAR(200) NOT NULL UNIQUE,
Active BIT NOT NULL,
Phone VARCHAR(20) NULL,
CreatedDate DATETIME NOT NULL
);
I hope you find it simple and helpful. Thank you!
HostForLIFEASP.NET SQL Server 2022 Hosting