Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the difference between unique and primary key in mysql

Shulou Source: shulou.com Published: 2022-05-31 10:54:31 09月23日 Update

This article mainly explains "what is the difference between unique and primary key in mysql". the explanation in this article is simple and clear, easy to learn and understand. please follow the idea of Xiaobian and go deep into it slowly to study and learn "what is the difference between unique and primary key in mysql" together!

Unique constraints ensure that there are no duplicate values in a column, and unique and primary key constraints both provide guarantees for the uniqueness of a column of values.

Unique can appear multiple times in each table, while primary key can only appear one time.

examples

mysql> create table user (name varchar(255),constraint name_un unique(name));Query OK, 0 rows affected (0.07 sec) mysql> insert user values("");Query OK, 1 row affected (0.02 sec) mysql> insert user values("zhang san");ERROR 1062 (23000): Duplicate entry 'zhang san' for key 'user.name_un'mysql> alter table user drop index name_un; Query OK, 0 rows affected (0.03 sec)Records: 0 Duplicates: 0 Warnings: 0 mysql> insert user values("ZHANG SAN"); Query OK, 1 row affected (0.02 sec) Thank you for reading, the above is "what is the difference between unique and primary key in mysql" content, after the study of this article, I believe that we have a deeper understanding of what is the difference between unique and primary key in mysql, the specific use of the situation still needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Tags: Zhang San learning content uniqueness examples that is ideas situations articles more knowledge knowledge points articles follow questions security practice push research verification Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Information macOS Apple Docker