mysql表的字段怎么设置唯一性

mysql表的本家儿键是独一性,但不是本家儿键的字段怎么设置独一性呢,我和大师分享一下设置的方式,我写的经验对你进修mysql有帮忙的话,给我投票、点赞或者保藏!

1mysql怎么点窜锁记实超时期待时候

1mysql数据库CURRENT_TIMESTAMP时候戳

1mysql备份恢复表中的数据

工具/原料

  • mysql

方式/步骤

  1. 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. 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. 3

    把chengshi字段设置为独一性:

    ALTER TABLE `test`.`chengshi`  ADD  UNIQUE INDEX `chengshi` (`chengshi`);

  4. 4

    查询chengshi表的数据:

    SELECT * FROM chengshi;

  5. 5

    把id=1的城市点窜为郑州,犯错有反复:

    UPDATE chengshi SET chengshi='郑州' WHERE id=1;

  6. 6

    增添一个城市安阳,呈现反复:

    INSERT INTO chengshi(shengfen,chengshi)VALUES('河南','安阳');

  • 发表于 2019-03-11 20:01
  • 阅读 ( 185 )
  • 分类:其他类型

相关问题

0 条评论

请先 登录 后评论