Home >Database >Mysql Tutorial >How to set unique field in mysql
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].
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!