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

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

WBOY
WBOYOriginal
2016-05-31 08:48:371198browse

在使用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 约束

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn