MySQL [test]> create table tbl_keyword (
-> id int not null auto_increment primary key,
-> keyword varchar(256) not null
-> )
-> ;
Query OK, 0 rows affected (0.06 sec)
MySQL [test]> insert into tbl_keyword(keyword) values ("aa"),("bb"), ("cc"), ("aa"),("bb");
Query OK, 5 rows affected (0.05 sec)
Records: 5 Duplicates: 0 Warnings: 0 MySQL [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| tbl_keyword |
+----------------+
1 row in set (0.03 sec) MySQL [test]> desc tbl_keyword;
+---------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| keyword | varchar(256) | NO | | NULL | |
+---------+--------------+------+-----+---------+----------------+
2 rows in set (0.03 sec) MySQL [test]> alter table tbl_keyword add idempotent_id char(32);
Query OK, 0 rows affected (0.11 sec)
Records: 0 Duplicates: 0 Warnings: 0 MySQL [test]> alter table tbl_keyword add unique index idempotent_id(idempotent_id);
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0 MySQL [test]> desc tbl_keyword;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| keyword | varchar(256) | NO | | NULL | |
| idempotent_id | char(32) | YES | UNI | NULL | |
+---------------+--------------+------+-----+---------+----------------+
3 rows in set (0.03 sec)
MySQL [test]> select * from tbl_keyword;
+----+---------+---------------+
| id | keyword | idempotent_id |
+----+---------+---------------+
| 1 | aa | NULL |
| 2 | bb | NULL |
| 3 | cc | NULL |
| 4 | aa | NULL |
| 5 | bb | NULL |
+----+---------+---------------+
5 rows in set (0.03 sec) MySQL [test]> insert into tbl_keyword(keyword, idempotent_id) values ("aa", md5("aa")), ("bb", md5("bb")), ("cc", md5("cc"));
Query OK, 3 rows affected (0.03 sec)
Records: 3 Duplicates: 0 Warnings: 0 MySQL [test]> select * from tbl_keyword;
+----+---------+----------------------------------+
| id | keyword | idempotent_id |
+----+---------+----------------------------------+
| 1 | aa | NULL |
| 2 | bb | NULL |
| 3 | cc | NULL |
| 4 | aa | NULL |
| 5 | bb | NULL |
| 11 | aa | 4124bc0a9335c27f086f24ba207a4912 |
| 12 | bb | 21ad0bd836b90d08f4cf640b4c298e7c |
| 13 | cc | e0323a9039add2978bf5b49550572c7c |
+----+---------+----------------------------------+
8 rows in set (0.03 sec) MySQL [test]> insert into tbl_keyword(keyword, idempotent_id) values ("aa", md5("aa")), ("bb", md5("bb"));
ERROR 1062 (23000): Duplicate entry '4124bc0a9335c27f086f24ba207a4912' for key 'idempotent_id'
MySQL [test]> select * from tbl_keyword;
+----+---------+----------------------------------+
| id | keyword | idempotent_id |
+----+---------+----------------------------------+
| 1 | aa | NULL |
| 2 | bb | NULL |
| 3 | cc | NULL |
| 4 | aa | NULL |
| 5 | bb | NULL |
| 11 | aa | 4124bc0a9335c27f086f24ba207a4912 |
| 12 | bb | 21ad0bd836b90d08f4cf640b4c298e7c |
| 13 | cc | e0323a9039add2978bf5b49550572c7c |
+----+---------+----------------------------------+
8 rows in set (0.03 sec)

mysql 延迟添加唯一索引的更多相关文章

  1. mysql多字段唯一索引

    项目中需要用到联合唯一索引: 例如:有以下需求:每个人每一天只有可能产生一条记录:处了程序约定之外,数据库本身也可以设定: 例如:user表中有userID,userName两个字段,如果不希望有2条 ...

  2. mysql 多列唯一索引在事务中select for update是不是行锁?

    在表中有这么一索引 UNIQUE KEY `customer_id` (`customer_id`,`item_id`,`ref_id`) 问1. 这种多列唯一索引在事务中select for upd ...

  3. mysql 允许在唯一索引的字段中出现多个null值

    线上问题:org.springframework.dao.DuplicateKeyException: PreparedStatementCallback; SQL [update fl_table ...

  4. 视图view没有主键,但可以添加唯一索引

    视图没有主键,但可以加上唯一索引 大致可以这样理解:视图是张虚拟的表.视图所对应的数据不进行实际的存储,数据库中只存储视图的定义,对视图的数据进行操作时,系统根据视图的定义去操作与视图相关联的基本表. ...

  5. Mysql中给有记录的表添加唯一索引

    ALTER IGNORE TABLE neeqs ADD UNIQUE KEY `unique` (`seccode`, `enddate`, `f002v`);

  6. MySQL添加唯一索引

    1 语法如下 ALTER TABLE `t_user` ADD unique(`username`);

  7. mysql延迟查询, 覆盖索引使用例子

    引用自 'mysql高性能' 5.3.6章节 不能使用覆盖索引的情况 :  解决办法 : 

  8. MySQL给字段唯一索引的三种方法

    建表时添加 DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `stu_id` ) NOT NULL AUTO_INCREMENT, ` ...

  9. mysql alter 添加索引

    1.添加主键索引 ALTER TABLE `table_name` ADD PRIMARY KEY (`column`) 2.添加唯一索引 ALTER TABLE `table_name` ADD U ...

随机推荐

  1. okvis代码解读

    okvis_timing模块 提供时间转换的功能函数 okvis_util模块 包含 assert_macros.hpp  该文件包含一些有用的断言宏. source_file_pos.hpp 该文件 ...

  2. 关于html中input组件间空隙的去除

    有空隙的时候的代码是这样的: <input type="text" name="search" title="请输入要搜索的内容" s ...

  3. 用户定义的java计数器

    mapreduce 计数器用来做某个信息的统计. 计数器是全局的.mapreduce 框架将跨所有map和reduce聚集这些计数器,并且作业结束时产生一个最终的结果. 语法像 java 的 enum ...

  4. [No000010D]Git6/9-分支管理

    分支就是科幻电影里面的平行宇宙,当你正在电脑前努力学习Git的时候,另一个你正在另一个平行宇宙里努力学习SVN. 如果两个平行宇宙互不干扰,那对现在的你也没啥影响.不过,在某个时间点,两个平行宇宙合并 ...

  5. HTML5:表单提交

    不加CSS.JavaScrips的HTML表单提交简单代码 <!DOCTYPE html> <html lang="en"> <head> &l ...

  6. Type Operators instanceof is used to determine whether a PHP variable is an instantiated object of a certain class/a class that implements an interface

    w 0-instanceof is used to determine whether a PHP variable is an instantiated object of a certain cl ...

  7. PHP之流程控制

    nest 嵌套 the curly braces 花括号 colon syntax 冒号语法 PHP三种if判断的写法 写法一: if(true){ }else if(){ }else if(){ } ...

  8. 完美解决failed to open stream: HTTP request failed!(file_get_contents引起的)

    当使用php5自带的file_get_contents方法来获取远程文件的时候,有时候会出现file_get_contents(): failed to open stream: HTTP reque ...

  9. jq优化

    1.使用链式写法 $('div').find('h3').eq(2).html('Hello');采用链式写法时,jQuery自动缓存每一步的结果,因此比非链式写法要快.根据测试,链式写法比(不使用缓 ...

  10. MySQL数据库改名的三种方法

    前不久去面试,被问到Innodb引擎的表如何改数据库名,当时我也只回答了MyISAM改如何操作,被一些细节问题打败,真是操蛋. 如果表示MyISAM那么可以直接去到数据库目录mv就可以. Innodb ...