SQL PRIMARY KEY


SQL PRIMARY KEY Constraint

The SQL PRIMARY KEY constraint is used to uniquely identify each record in a database table. This command is essential for maintaining data integrity and ensuring that each record can be uniquely referenced. A primary key column must contain unique values and cannot contain NULL values.


Syntax

-- To create a primary key when creating a table
CREATE TABLE table_name (
    column1 datatype PRIMARY KEY,
    column2 datatype,
    ...
);

-- To add a primary key to an existing table
ALTER TABLE table_name
ADD PRIMARY KEY (column_name);
  • CREATE TABLE: This is the SQL keyword used to create a new table.
  • ALTER TABLE: This is the SQL keyword used to modify an existing table.
  • PRIMARY KEY: This is the SQL keyword used to define a primary key.
  • column1, column2, ...: These specify the names of the columns in the table.
  • datatype: This specifies the type of data the column can hold, such as INTEGER, VARCHAR, DATE, etc.
  • table_name: This specifies the name of the table to create or modify.
  • column_name: This specifies the name of the column to set as the primary key.

Example

Let's go through a complete example that includes creating a database, creating a table with a primary key, and inserting data into the table.

Step 1: Creating a Database

This step involves creating a new database named example_db.

CREATE DATABASE example_db;

In this example, we create a database named example_db.

Step 2: Creating a Table with a Primary Key

In this step, we create a table named employees within the previously created database, defining the id column as the primary key.

USE example_db;

CREATE TABLE employees (
    id INT AUTO_INCREMENT PRIMARY KEY,
    first_name VARCHAR(50) NOT NULL,
    last_name VARCHAR(50) NOT NULL,
    email VARCHAR(100),
    hire_date DATE
);

Here, we define the employees table with columns for id, first_name, last_name, email, and hire_date. The id column is set as the primary key, ensuring each record is uniquely identified.

Step 3: Inserting Data into the Table

This step involves inserting some sample data into the employees table.

INSERT INTO employees (first_name, last_name, email, hire_date) VALUES ('John', 'Doe', 'john.doe@example.com', '2023-01-01');
INSERT INTO employees (first_name, last_name, email, hire_date) VALUES ('Jane', 'Smith', 'jane.smith@example.com', '2023-02-01');
INSERT INTO employees (first_name, last_name, email, hire_date) VALUES ('Alice', 'Johnson', 'alice.johnson@example.com', '2023-03-01');
INSERT INTO employees (first_name, last_name, email, hire_date) VALUES ('Bob', 'Brown', 'bob.brown@example.com', '2023-04-01');

Here, we insert four rows of data into the employees table. Each record is uniquely identified by the id column, which is the primary key.

Step 4: Adding a Primary Key to an Existing Table

This step involves adding a primary key to an existing table named departments that does not have a primary key defined.

ALTER TABLE departments
ADD PRIMARY KEY (department_id);

This command adds a primary key to the department_id column of the departments table, ensuring each record in the table is uniquely identified.