mysql表的本家儿键是独一性,但不是本家儿键的字段怎么设置独一性呢,我和大师分享一下设置的方式,我写的经验对你进修mysql有帮忙的话,给我投票、点赞或者保藏!
1mysql怎么点窜锁记实超时期待时候
1mysql数据库CURRENT_TIMESTAMP时候戳
1mysql备份恢复表中的数据
需要这些哦
mysql
方式/
1登录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
文章插图
2在表中增添一些数据:
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');
文章插图
3把chengshi字段设置为独一性:
ALTER TABLE `test`.`chengshi` ADD UNIQUE INDEX `chengshi` (`chengshi`);
文章插图
4查询chengshi表的数据:
SELECT * FROM chengshi;
文章插图
5把id=1的城市点窜为郑州,犯错有反复:
UPDATE chengshi SET chengshi='郑州' WHERE id=1;
文章插图
6增添一个城市安阳,呈现反复:
【mysql表的字段怎么设置唯一性】INSERT INTO chengshi(shengfen,chengshi)VALUES('河南','安阳');
推荐阅读
- 怎样隐藏手机百度贴吧我关注的吧
- 晨起口臭的原因
- 爱吃肉的危害有哪些
- 被子不常晒的危害
- 烤肠里面有放肉的吗
- 枸杞泡水喝的好处有哪些
- 高盐、腌制食物是怎么伤胃的
- 不同材质的被子该怎么晒
- 烤肠的味道为什么那么香
- 肝癌的预防建议