Home  >  Article  >  Database  >  How to set unique field in mysql

How to set unique field in mysql

coldplay.xixi
coldplay.xixiOriginal
2020-10-13 17:37:028869browse

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;

2. Add unique constraints to the already created table

ALTER TABLE `t_user` ADD unique(`username`);

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!

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