Maison  >  Article  >  base de données  >  MySQL数据库使某个不是主键的字段唯一_MySQL

MySQL数据库使某个不是主键的字段唯一_MySQL

WBOY
WBOYoriginal
2016-05-31 08:48:371198parcourir

在使用MySQL数据的过程中有时候我们需要某个不是主键的字段不重复,这个时候就要用到SQL的UNIQUE约束了。

下面摘抄自w3school里的一段介绍:

UNIQUE 约束唯一标识数据库表中的每条记录。

UNIQUE 和 PRIMARY KEY 约束均为列或列集合提供了唯一性的保证。

PRIMARY KEY 拥有自动定义的 UNIQUE 约束。

请注意,每个表可以有多个 UNIQUE 约束,但是每个表只能有一个 PRIMARY KEY 约束。

下面在 "Persons" 表创建时在 "Id_P" 列创建 UNIQUE 约束:

MySQL:

CREATE TABLE Persons(Id_P int NOT NULL,LastName varchar(255) NOT NULL,FirstName varchar(255),Address varchar(255),City varchar(255),<code>UNIQUE (Id_P)</code>)

详细内容:SQL UNIQUE 约束

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn