đ The IDENTITY property in SQL is used to automatically generate unique numeric values for a column whenever a new row is inserted. It is most commonly used in PRIMARY KEY columns to eliminate the need for manually assigning unique IDs.
đ What is IDENTITY?
The IDENTITY property is primarily associated with SQL Server. It automatically generates sequential numeric values based on a specified starting value (seed) and increment. Each new row receives the next available value.
Information
đ¯ Why Use IDENTITY?
The IDENTITY property simplifies database design and ensures that every row receives a unique identifier automatically.
- đ Automatically generate unique IDs.
- đ Eliminate manual key management.
- đ Reduce duplicate key errors.
- đ Simplify data insertion.
- đ Improve relational database design.
đ Basic Syntax
IDENTITY Syntax
CREATE TABLE table_name
(
id INT IDENTITY(seed, increment) PRIMARY KEY,
column_name data_type
);đ Understanding Seed and Increment
The IDENTITY property accepts two values:
| Parameter | Description | Example |
|---|---|---|
| Seed | The first value generated. | 1 |
| Increment | The value added for each new row. | 1 |
đĄ Create a Table with IDENTITY
Create a Students table where StudentID is generated automatically.
Students Table
CREATE TABLE Students
(
StudentID INT IDENTITY(1,1) PRIMARY KEY,
Name VARCHAR(100),
Department VARCHAR(100)
);â Insert Records
Since the StudentID is generated automatically, it is omitted from the INSERT statement.
Insert Data
INSERT INTO Students (Name, Department)
VALUES
('Alice', 'Computer Science'),
('Bob', 'Mathematics'),
('Charlie', 'Physics');đ Result
| StudentID | Name | Department |
|---|---|---|
| 1 | Alice | Computer Science |
| 2 | Bob | Mathematics |
| 3 | Charlie | Physics |
đĸ Custom Seed and Increment
You can specify custom starting and increment values.
Custom IDENTITY Values
CREATE TABLE Employees
(
EmployeeID INT IDENTITY(1000,5) PRIMARY KEY,
EmployeeName VARCHAR(100)
);Generated values will be:
| Insert | Generated EmployeeID |
|---|---|
| First | 1000 |
| Second | 1005 |
| Third | 1010 |
| Fourth | 1015 |
â ī¸ Inserting Explicit Identity Values
By default, SQL Server does not allow explicit values to be inserted into an IDENTITY column. To do so temporarily, enable IDENTITY_INSERT.
Insert Explicit Identity Value
SET IDENTITY_INSERT Students ON;
INSERT INTO Students
(StudentID, Name, Department)
VALUES
(100, 'David', 'Physics');
SET IDENTITY_INSERT Students OFF;Warning
âī¸ IDENTITY vs AUTO_INCREMENT
| Feature | IDENTITY | AUTO_INCREMENT |
|---|---|---|
| Database | SQL Server | MySQL |
| Automatic Numbering | â Yes | â Yes |
| Custom Start Value | â Yes | â Yes |
| Custom Increment | â Yes | Limited to table-level configuration |
đī¸ Equivalent Features in Other Databases
| Database System | Equivalent Feature |
|---|---|
| SQL Server | IDENTITY(seed, increment) |
| MySQL | AUTO_INCREMENT |
| PostgreSQL | GENERATED ... AS IDENTITY or SERIAL. |
| Oracle | GENERATED ... AS IDENTITY. |
| SQLite | INTEGER PRIMARY KEY with optional AUTOINCREMENT. |
đŧ Real-World Example
A hospital management system automatically assigns a unique patient ID each time a new patient registers, ensuring every patient record can be identified without manual numbering.
Patients Table
CREATE TABLE Patients
(
PatientID INT IDENTITY(1,1) PRIMARY KEY,
PatientName VARCHAR(100),
AdmissionDate DATE
);â ī¸ Common Mistakes
- â Assuming identity values are always gap-free.
- â Manually inserting identity values without enabling IDENTITY_INSERT.
- â Using identity values as meaningful business numbers.
- â Expecting identity values to reset automatically after deleting rows.
Warning
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ IDENTITY automatically generates numeric values.
- đ It is primarily a SQL Server feature.
- đ The syntax is IDENTITY(seed, increment).
- đ It is commonly used with PRIMARY KEY columns.
- đ Identity values are unique but may not be consecutive.
- đ Similar functionality exists in other database systems under different names.