mysql表的本家儿键是独一性,但不是本家儿键的字段怎么设置独一性呢,我和大师分享一下设置的方式,我写的经验对你进修mysql有帮忙的话,给我投票、点赞或者保藏!
登录mysql,在test数据库中新建一个表,本家儿键是id,后面把chengshi字段设置为独一性:
CREATE TABLE `chengshi` ( `id` int(11) NOT NULL AUTO_INCREMENT, `shengfen` varchar(10) DEFAULT NULL, `chengshi` varchar(10) DEFAULT NULL, `shijian` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8
在表中增添一些数据:
insert into `chengshi` (`id`, `shengfen`, `chengshi`, `shijian`) values('1','河南','安阳','2019-03-05 10:14:27');
insert into `chengshi` (`id`, `shengfen`, `chengshi`, `shijian`) values('2','河南','郑州','2019-03-05 10:14:27');
insert into `chengshi` (`id`, `shengfen`, `chengshi`, `shijian`) values('3','河南','洛阳','2019-03-05 10:14:27');
insert into `chengshi` (`id`, `shengfen`, `chengshi`, `shijian`) values('4','河南','漯河','2019-03-05 10:14:27');
insert into `chengshi` (`id`, `shengfen`, `chengshi`, `shijian`) values('5','河南','新乡','2019-03-05 10:14:27');
insert into `chengshi` (`id`, `shengfen`, `chengshi`, `shijian`) values('6','河南','濮阳','2019-03-05 10:14:27');
insert into `chengshi` (`id`, `shengfen`, `chengshi`, `shijian`) values('7','河南','焦作','2019-03-05 10:14:27');
insert into `chengshi` (`id`, `shengfen`, `chengshi`, `shijian`) values('8','河南','鹤壁','2019-03-05 10:14:27');
insert into `chengshi` (`id`, `shengfen`, `chengshi`, `shijian`) values('9','河南','开封','2019-03-05 10:14:27');
把chengshi字段设置为独一性:
ALTER TABLE `test`.`chengshi` ADD UNIQUE INDEX `chengshi` (`chengshi`);
查询chengshi表的数据:
SELECT * FROM chengshi;
把id=1的城市点窜为郑州,犯错有反复:
UPDATE chengshi SET chengshi='郑州' WHERE id=1;
增添一个城市安阳,呈现反复:
INSERT INTO chengshi(shengfen,chengshi)VALUES('河南','安阳');
0 篇文章
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!