Summary of the differences between Mysql primary key and unique key

Summary of the differences between Mysql primary key and unique key

What is a primary key?

A primary key is a column in a table that uniquely identifies each tuple (row) in that table. The primary key enforces integrity constraints on the table. Only one primary key is allowed in a table. The primary key does not accept any duplicate values ​​or null values. The primary key values ​​in a table rarely change, so you need to be careful when choosing a primary key and choose one that rarely changes. The primary key of one table can be referenced by the foreign key of another table.

To understand the primary key better, let us create a table called Student having attributes like roll_number, name, batch, phone_number, citizen_id, etc.

In the above example, the roll_number attribute can never have the same NULL value because students enrolled in each university have a unique Roll_number, so two students cannot have the same Roll_number, and each row in the table can be uniquely identified by the student's roll_number attribute. So, in this case, we can make the Roll_number attribute as the primary key.

What is a unique key?

A unique key constraint uniquely identifies a single tuple in a relation or table. Unlike a primary key, a table can have multiple unique keys. A unique key constraint can accept only one null value for a column; the unique constraint is also referenced by a foreign key from another table. It can be used when one wants to enforce unique constraints on columns and groups of columns that are not primary keys.

To understand unique key better, let us use Student table with Roll_number, Name, Batch, Phone_number and Citizen_ID attributes; where Roll_number attribute has been given primary key.

In this example, a unique constraint can be assigned to Citizen_ID where each entry in the Citizen_ID column should be unique and not duplicated because every citizen of a country must have his or her unique identification number. However, if the student migrated from another country, in that case he or she will not have a Citizen_ID and the entry may have a NULL value since one NULL is allowed in a unique constraint.

Key differences between primary key and unique key:

1. When an attribute is declared as a primary key, it will not accept NULL values. On the other hand, when a property is declared as Unique, it can accept a NULL value.

2. A table can have only one primary key, but can have multiple unique keys.

3. Automatically create a clustered index when defining the primary key. In contrast, a Unique key generates a nonclustered index.

The above is all about the difference between Mysql primary key and unique key. Thank you for your support to 123WORDPRESS.COM.

You may also be interested in:
  • What to do if the auto-increment primary key in MySQL is used up
  • Solution to running out of MySQL's auto-increment ID (primary key)
  • In-depth analysis of why MySQL does not recommend using uuid or snowflake id as primary key
  • Detailed explanation of non-primary key column overflow monitoring in MySQL tables
  • Use prometheus to count the remaining available percentage of MySQL auto-increment primary keys
  • Python3 operates MySQL to insert a data and return the primary key id example
  • Example analysis of mysql non-primary key self-increment usage
  • Summary of the pitfalls of using primary keys and rowids in MySQL
  • Spring boot integrates mybatis to use MySQL to implement primary key UUID
  • MySQL primary key naming strategy related

<<:  vue-router history mode server-side configuration process record

>>:  Detailed explanation of how to install PHP7 on Linux

Recommend

Detailed explanation of server-id example in MySQL master-slave synchronization

Preface When we build a MySQL cluster, we natural...

Detailed explanation of adding dotted lines to Vue element tree controls

Table of contents 1. Achieve results 2. Implement...

Solution to mysql error when modifying sql_mode

Table of contents A murder caused by ERR 1067 The...

How to handle images in Vue forms

question: I have a form in Vue for uploading blog...

Let's talk about the difference between MyISAM and InnoDB

The main differences are as follows: 1. MySQL use...

A Preliminary Study on Vue Unit Testing

Table of contents Preface Why introduce unit test...

Detailed tutorial on installing Mysql5.7.19 on Centos7 under Linux

1. Download MySQL URL: https://dev.mysql.com/down...

CSS sets Overflow to hide the scroll bar while allowing scrolling

CSS sets Overflow to hide the scroll bar while al...

XHTML Getting Started Tutorial: What is XHTML?

What is HTML? To put it simply: HTML is used to m...

Nginx load balancing algorithm and failover analysis

Overview Nginx load balancing provides upstream s...

Why developers must understand database locks in detail

1.Lock? 1.1 What is a lock? The real meaning of a...

MySQL 8.0.21 free installation version configuration method graphic tutorial

Six steps to install MySQL (only the installation...

Detailed installation steps for MySQL 8.0.11

This article shares the installation steps of MyS...

What you need to know about responsive design

Responsive design is to perform corresponding ope...