Home > Database > Mysql Tutorial > How to set unique field in mysql

How to set unique field in mysql

coldplay.xixi
Release: 2020-10-13 17:37:02
Original
8906 people have browsed it

Mysql method to set unique fields: 1. Add uniqueness constraints when creating the table, the code is [`Id` int(11) NOT NULL AUTO_INCREMENT]; 2. Add uniqueness to the already built table Sexual constraints, the code is [ALTER TABLE `t_user` ADD].

How to set unique field in mysql

Mysql method of setting unique fields:

1. Add uniqueness constraints when creating the table

CREATE TABLE `t_user` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(18) NOT NULL unique,
`password` varchar(18) NOT NULL,
PRIMARY KEY (`Id`)  www.2cto.com  
) ENGINE=InnoDB AUTO_INCREMENT=1018 DEFAULT CHARSET=gbk;
Copy after login

2. Add unique constraints to the already created table

ALTER TABLE `t_user` ADD unique(`username`);
Copy after login

More related free learning recommendations:mysql tutorial(video)

The above is the detailed content of How to set unique field in mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template