SQLite – 5 – Primary Keys and Constraints

In SQLite, primary keys and constraints play a vital role in ensuring data integrity and maintaining the reliability of a database. They help define rules and relationships within the database tables. This discussion will delve into the concepts of primary keys and constraints in SQLite, their significance, and how to use them effectively.

Primary Key in SQLite:

A primary key is a unique identifier for a record in a table. It serves as a crucial reference point for data retrieval and ensures that each record is distinct within the table. SQLite allows you to define primary keys when creating tables using the CREATE TABLE statement.

Syntax for Creating a Table with a Primary Key:

CREATE TABLE table_name ( column1_name datatype PRIMARY KEY, column2_name datatype, ... );

In this syntax:

  • table_name is the name of the table you are creating.
  • column1_name is the name of the column that will serve as the primary key.
  • datatype specifies the data type for the primary key column.

Example:

Let’s say you want to create a table called Students with a primary key on the StudentID column:

CREATE TABLE Students ( StudentID INTEGER PRIMARY KEY, FirstName TEXT, LastName TEXT, Age INTEGER );

In this example, the StudentID column is designated as the primary key. This ensures that each student’s ID is unique within the Students table, preventing any duplicate IDs.

Constraints in SQLite:

Constraints are rules that you can apply to the columns in a table to enforce data integrity. SQLite supports various constraints, including:

  1. NOT NULL: This constraint ensures that a column cannot have a NULL value, meaning it must contain a valid data value.
  2. UNIQUE: The UNIQUE constraint ensures that all values in a column are distinct. No two rows can have the same value in a column with this constraint.
  3. DEFAULT: The DEFAULT constraint allows you to specify a default value for a column. If no value is provided during an INSERT operation, the default value is used.
  4. CHECK: The CHECK constraint defines conditions that data values must meet. It restricts the range of acceptable values for a column.
  5. FOREIGN KEY: The FOREIGN KEY constraint establishes a relationship between tables, ensuring referential integrity. It enforces that values in a column match values in another table’s primary key.

Example with Constraints:

Consider a scenario where you want to create a table called Orders with the following constraints:

  • The OrderID column must be a primary key.
  • The CustomerID column cannot contain NULL values.
  • The TotalAmount column has a default value of 0.00.
  • The Status column must contain one of three values: ‘Pending,’ ‘Processing,’ or ‘Shipped.’

CREATE TABLE Orders ( OrderID INTEGER PRIMARY KEY, CustomerID INTEGER NOT NULL, OrderDate DATE, TotalAmount REAL DEFAULT 0.00, Status TEXT CHECK (Status IN ('Pending', 'Processing', 'Shipped')) );

In this example:

  • OrderID serves as the primary key, ensuring each order has a unique identifier.
  • CustomerID cannot contain NULL values, ensuring that every order is associated with a customer.
  • TotalAmount has a default value of 0.00, which will be used if no value is provided.
  • Status is constrained by a CHECK constraint, ensuring that it only contains specific values (‘Pending,’ ‘Processing,’ or ‘Shipped’).

These constraints collectively enhance data integrity and define the rules that govern the Orders table.

Conclusion:

Primary keys and constraints are essential components of database design in SQLite. They help maintain data integrity, enforce rules for data storage, and establish relationships between tables. By understanding how to use primary keys and various constraints effectively, you can create robust and reliable database structures in SQLite that accurately reflect your data requirements. These features are crucial for building efficient and trustworthy database systems.