Contents

About SQL Identifiers

   Aug 27, 2024     2 min read

This article examines the identifier of SQL.

Hello!

Today, we’re going to talk about identifiers in Structured Query Language (SQL).

In SQL, identifiers are an important concept used to uniquely identify data within a database.

In this post, we will take a closer look at the types of identifiers and how they are used in SQL.

Primary Key

Overview

The default key is a field used to uniquely identify each row.

Each table can have one default key, which is used to uniquely identify each row of that table.

How to use it

The default key usually has an auto increment value, such as AUTO_INCREMENT or IDENTITY, and it automatically increases with each row added.

CREATE TABLE Students (
    StudentID INT PRIMARY KEY AUTO_INCREMENT,
    Name VARCHAR(50),
    Major VARCHAR(50)
);

Foreign Key

Overview

Foreign keys are fields that refer to the default keys in other tables.

This allows you to establish and maintain a relationship between two tables.

How to use it

Foreign keys are generated by referencing the default keys in another table.

This allows you to establish a relationship between the parent and child tables.

CREATE TABLE Enrollments (
    EnrollmentID INT PRIMARY KEY AUTO_INCREMENT,
    StudentID INT,
    SubjectID INT,
    FOREIGN KEY (StudentID) REFERENCES Students(StudentID),
    FOREIGN KEY (SubjectID) REFERENCES Subjects(SubjectID)
);

Unique Key

Overview

A unique key is a field that does not allow duplication within a table.

Each row must have a unique value, but unlike the default key, it can have a NULL value.

How to use it

A unique key must not have duplicate values for each row and can have a NULL value.

This ensures that certain fields are not duplicated.

CREATE TABLE Users (
    UserID INT PRIMARY KEY AUTO_INCREMENT,
    Username VARCHAR(50) UNIQUE,
    Email VARCHAR(50) UNIQUE
);

at the end of the day

Identifiers in SQL are essential for uniquely identifying data.

You can maintain database accuracy and consistency by appropriately utilizing various types of identifiers such as primary keys, foreign keys, and unique keys.

When designing a database, try to select and utilize identifiers well to ensure efficient data management.

I hope this posting helped me understand the identifiers in SQL.

Thank you!