sql

SQL Primary Key Example

Welcome readers, in this tutorial, we will understand the SQL Primary Key column that uniquely identifies a tuple in the database table.

1. Introduction

SQL Primary Key is a column or multiple columns that uniquely identify a tuple in the database table.

  • It contains unique values but cannot have null or duplicate
  • It consists of multiple columns known as a composite primary key. In a composite primary key, the values may be duplicated in one column, but the value combination from all columns must be unique
  • It offers fast access to data
  • It contains a maximum of 16 columns in MySQL and MariaDB and 32 columns in Oracle

To start with this tutorial, we are hoping that users at present have their preferred database installed on their machines. For easy usage, I am using MySQL on a Windows operating system. If someone needs to go through the MySQL installation, please watch this video.

2. SQL Primary Key Example

Here is a systematic guide and we are using MySQL Database and Workbench.

2.1 Creating a Primary Key for one column

The following command creates a table called employees having a single column acting as a primary key.

1
2
3
4
5
6
7
8
/* Query #1 :: Sql to create primary key for one column */
CREATE TABLE IF NOT EXISTS employees (
    id INT NOT NULL,
    firstname VARCHAR(255) NOT NULL,
    lastname VARCHAR(255) NULL,
    age INT NOT NULL,
    PRIMARY KEY (id)
);

If everything goes well, the table with a primary key will be created.

SQL Primary Key
Fig. 1: Primary Key

2.2 Creating a Primary Key for multiple columns

The following command creates a table called address having multiple columns acting as a primary key.

/* Query #2 :: Sql to create primary key for mulitple columns */
CREATE TABLE IF NOT EXISTS address (
    id INT NOT NULL,
    address_1 VARCHAR(255) NOT NULL,
    address_2 VARCHAR(255) NULL,
    city VARCHAR(255) NOT NULL,
    state VARCHAR(255) NOT NULL,
    country VARCHAR(255) NULL,
    CONSTRAINT pk_address PRIMARY KEY (id , address_1 , city , state)
);

If everything goes well, the table with a composite primary key will be created.

SQL Primary Key - Composite
Fig. 2: Composite Primary Key

2.3 Dropping a Primary Key

The following command drops the primary key constraint from a table.

/* Query #3 :: Sql to drop a primary key constraint from the table */
ALTER TABLE employees DROP PRIMARY KEY;

If everything goes well, the primary key constraint will be deleted from the table.

SQL Primary Key - Dropping a Primary Key
Fig. 3: Dropping a Primary Key

2.4 Adding a Primary Key on an existing table

The following command creates a primary key constraint on an existing table.

/* Query #4 :: Sql to create a primary key constraint on the existing table */
/* Note= Primary Key is created on the column or columns defined as not null */
ALTER TABLE employees ADD PRIMARY KEY(id);

If everything goes well, the primary key constraint will be added to the existing table.

SQL Primary Key - add Primary Key
Fig. 4: Altering table to add Primary Key

Developers can follow the same approach to create a primary key constraint on multiple columns of an existing table.

1
2
3
/* Query #5 :: Sql to create a primary key constraint on multiple columns of an existing table */
/* Note= Primary Key is created on the column or columns defined as not null */
ALTER TABLE address ADD PRIMARY KEY (id , address_1 , city , state);

That is all for this tutorial and I hope the article served you whatever you were looking for. Happy Learning and do not forget to share!

3. Conclusion

In this section, developers learned how to create a Primary Key in a database table. Developers can download the sample scripts in the Downloads section.

4. Download the SQL Script

This was an example of creating a Primary Key in a database table.

  • Each tuple in a table can have only one primary key
  • It contains unique values but cannot have null or duplicate
  • A primary key column will throw an error while inserting a duplicate value
  • A non-primary key column can have a duplicate value
Download
You can download the full source code of this example here: SQL Primary Key Example

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button