SQLite is a small, fast, and efficient relational database management system that is widely used in various applications and devices such as smartphones, tablets, and IoT devices. When designing and implementing a database in SQLite, it is essential to understand the concept of UNIQUE and PRIMARY keys, as they play a crucial role in data integrity and consistency.
In this article, we will explore the UNIQUE and PRIMARY keys in SQLite and how they differ from each other.
What is a Unique Key in SQLite?
A constraint known as a unique key makes sure that there are no duplicate values, or unique data, in a given column. It can be applied to one or more columns in a table and can be used for different reasons, such as enforcing business rules, improving performance, and facilitating data management. In SQLite, a unique key can be created using the UNIQUE keyword in the CREATE TABLE statement.
Here’s an example of creating a table with a unique constraint on a single column:
unique_column INTEGER UNIQUE,
other_columns ...
);
In this example, your_table represents the table name, unique_column is the column to be unique, and other_columns are additional columns in the table. The unique constraint ensures that no duplicate values are inserted into the unique_column.
What is a Primary Key in SQLite?
Each row in a table is identified exclusively by a primary key, which is a constraint. It can have one or more columns, and neither null nor duplicate values are allowed. The primary key serves as a reference for other tables and is crucial for maintaining data integrity. The PRIMARY KEY term in the CREATE TABLE statement of SQLite can be used to construct a main key.
Here’s an example of creating a table with a primary key constraint:
id INTEGER PRIMARY KEY,
other_columns ...
);
In this example, your_table is the table name, id is the primary key column, and other_columns represent additional columns in the table. The primary key constraint guarantees the uniqueness of the values in the id column and allows for efficient retrieval of specific rows using their primary key values.
How to Use Unique and Primary Keys in SQLite?
Let’s consider an example that shows the usage of unique and primary keys in SQLite:
First, create a table with unique and primary key constraints.
id INTEGER PRIMARY KEY,
employee_code TEXT UNIQUE,
name TEXT,
position TEXT
);
Now insert data into the table.
VALUES ('E001', 'John Doe', 'Manager');
Then we insert a duplicate employee code.
VALUES ('E001', 'Jane Smith', 'Assistant');
Once it’s done, we then retrieve all employees from the table.
In this example, we create a table named employees with columns for id, employee_code, name, and position. The id column serves as the primary key, ensuring uniqueness and acting as a unique identifier for each employee. The employee_code column has a unique constraint to prevent duplicate employee codes.
We attempt to insert a duplicate employee_code, which violates the unique constraint. Finally, we retrieve all employees from the table to observe the data.
The above example effectively showcases the usage of unique and primary key constraints in SQLite and demonstrates how they help maintain data integrity by preventing duplicates and ensuring uniqueness.
Difference Between Unique and Primary Key
One of the main differences between a unique key and a primary key is its purpose and scope. While a unique key ensures the uniqueness of a column or set of columns, a primary key identifies each row in a table. As a result, whereas a primary key can be used to create connections between tables, a unique key can be used to enforce data integrity constraints. A table may also have more than one unique key, but just a single primary key.
Another difference between a unique key and a primary key is the behavior when updating or deleting data. When updating data in a column that has a unique key constraint, the new values must be unique, or else the update will fail.
However, when updating data in a table that has a primary key, the new values will replace the old values for the same primary key. Similarly, when deleting data from a table that has a primary key, the corresponding rows will be deleted, while deleting data from a column that has a unique key constraint will not affect other rows.
Data Types for Unique and Primary Keys
SQLite provides different types of data types that can be used for primary and unique keys. For primary keys, the INTEGER data type is often used, while for unique keys other data types such as TEXT, REAL, and BLOB can be used. The choice of data type depends on the nature of the data and its usage.
Unique and Primary Key Constraints
In SQLite, the primary key constraint creates a special index on the specified column or set of columns. This index can improve the performance of queries that involve the primary key. Similarly, a unique constraint can create an index that allows for fast lookups of the unique values in a column or set of columns. Therefore, both primary and unique keys can enhance the efficiency of the database.
Conclusion
The unique and primary keys are essential concepts in SQLite that ensure the integrity and consistency of data. While both constraints enforce uniqueness, their purposes and scopes differ. A unique key is used to ensure unique values in a column or set of columns, while a primary key is used to identify each row in a table. Following this guide, you will be able to clearly understand the purpose of both these keys in SQLite.