Mat*_*ght 5 mysql sql unique primary-key unique-key
目前我在我的数据库中有这个表:
CREATE TABLE `twMCUserDB` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`mc_userName` text NOT NULL,
`mc_userPass` text NOT NULL,
`tw_userName` text NOT NULL,
`tw_userPass` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
Run Code Online (Sandbox Code Playgroud)
我希望列mc_userName也是唯一的(就像id),但mc_userName必须是一个字符串.
我试图使它也成为主键,但这不起作用.
当我向表中添加数据时,首先要检查是否mc_userName已经存在?或者我可以使用MySQL中的任何内置函数(在插入查询中,还是在其他地方)?
加上 UNIQUE
`mc_userName` text NOT NULL UNIQUE,
Run Code Online (Sandbox Code Playgroud)
要么
CREATE TABLE `twMCUserDB` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`mc_userName` text NOT NULL,
`mc_userPass` text NOT NULL,
`tw_userName` text NOT NULL,
`tw_userPass` text NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT tb_uq UNIQUE (mc_userName)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
Run Code Online (Sandbox Code Playgroud)