Home » Core Java » sql » SQL Primary Key Example

About Yatin

Yatin
The author is graduated in Electronics & Telecommunication. During his studies, he has been involved with a significant number of projects ranging from programming and software engineering to telecommunications analysis. He works as a technical lead in the information technology sector where he is primarily involved with projects based on Java/J2EE technologies platform and novel UI technologies.

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
(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of