如果不设置unique

会出现两条相同的记录

mysql> create table department1(id int,name varchar(16));
Query OK, 0 rows affected (0.01 sec) mysql> insert into department1 values(1 ,'mike'),(2,'mike');
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from department1;
+------+------+
| id | name |
+------+------+
| 1 | mike |
| 2 | mike |
+------+------+
2 rows in set (0.00 sec)
mysql> desc department1 ;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(16) | YES | UNI | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
============设置唯一约束 UNIQUE===============

不能插入相同记录

方式一:
mysql> create table department1(id int,name varchar(16) unique);
Query OK, 0 rows affected (0.01 sec) mysql> insert into department1 values(1 ,'mike'),(2,'mike');
ERROR 1062 (23000): Duplicate entry 'mike' for key 'name'

方式二:

mysql> create table department1(id int,name varchar(16),unique(id),unique(name));
Query OK, 0 rows affected (0.02 sec) mysql> desc department1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | UNI | NULL | |
| name | varchar(16) | YES | UNI | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
这两种方法都叫单列唯一 针对一个字段设置唯一性

还有一种 联合唯一
几个字段合到一起不重复就可以 联合唯一
unique(ip,port)
desc 看到有 MUL 就是联合唯一
mysql> create table services(id int,ip char(16),port int,unique(id),unique(ip,port));
Query OK, 0 rows affected (0.01 sec) mysql> desc services;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | YES | UNI | NULL | |
| ip | char(16) | YES | MUL | NULL | |
| port | int(11) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)

验证 插入记录

mysql> insert into services values
-> (1,'192.168.10.11',80),
-> (2,'192.168.10.11',81),
-> (3,'192.168.10.10',80);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from services;
+------+---------------+------+
| id | ip | port |
+------+---------------+------+
| 1 | 192.168.10.11 | 80 |
| 2 | 192.168.10.11 | 81 |
| 3 | 192.168.10.10 | 80 |
+------+---------------+------+
3 rows in set (0.00 sec)

再插入一条原本有的记录 报错了

mysql> insert into services values(4,'192.168.10.10',80);
ERROR 1062 (23000): Duplicate entry '192.168.10.10-80' for key 'ip'


mysql 约束条件 unique key 唯一的键的更多相关文章

  1. unique key 唯一约束

    #添加唯一约束mysql> alter table tb2    -> add unique key(name)   ->;#删除唯一约束mysql> alter table ...

  2. mysql 约束条件 primary key 主键

    primary key字段的值不为空且唯一 约束:not null unique 存储引擎:innodb 对于innodb来说,一张表内必须有一个主键 单列做主键多列做主键(复合主键) 通常都是id字 ...

  3. mysql 中UNIQUE KEY 到底是约束还是索引?

    答案来自:https://zhidao.baidu.com/question/1863373387452612907.html 两者关系 unique索引包含了unique约束,因为unique约束是 ...

  4. mysql 约束条件目录

    mysql 约束条件 mysql 约束条件 not null与default mysql 约束条件 unique key 唯一的键 mysql primary key 主键 mysql auto_in ...

  5. mysql中key 、primary key 、unique key 与index区别

    一.key与primary key区别 CREATE TABLE wh_logrecord ( logrecord_id ) NOT NULL auto_increment, ) default NU ...

  6. 【Mysql】key 、primary key 、unique key 与index区别

    参考:https://blog.csdn.net/nanamasuda/article/details/52543177 总的来说,primary key .unique key 这些key建立的同时 ...

  7. MySQL 中 key, primary key ,unique key,index的区别

    一.key与primary key区别 CREATE TABLE wh_logrecord ( logrecord_id int(11) NOT NULL auto_increment, user_n ...

  8. mysql中的key primary key 和unique key

    mysql 中key就等同于index 所以 key:普通索引 unique key:唯一索引,就是这一列不能重复 primary key:主键索引,就是不能为空,且主键索引不是完全相同时,插入新数据 ...

  9. Mysql如何修改unique key

    link:http://www.netingcn.com/mysql-modifyunique-key.html mysql可以使用unique key来确保数据的准确性,unique key可以是一 ...

随机推荐

  1. Java中关于“=”和“==”的分析

    Java中变量分为普通原始变量(int char float等)和对象 一"=" (1)普通原始变量 普通原始变量的声明和赋值语句例如以下 int a=3; int b=a; 此时 ...

  2. c++primer记录(二) 模板

    因为看得源码里有大量的类模板,所以补充下C++中模板的知识 模板:函数模板 类模板 .p- 函数模板的类型参数可由编译器进行推断,也可以由用户显式的指定,类模板的类型参数必须显式的给出: p- 非类型 ...

  3. keepalived双BACKUP加nopreempt失效、手动监控服务脚步。

    keepalived双BACKUP加nopreempt不起作用,两个机器同时拥有vip, 排查几天发现是防火墙问题,啃爹. 打开  vi /etc/sysconfig/iptables 插入一条:-A ...

  4. MathType公式波浪线怎么编辑

    数学公式中有很多符号与数学样式,在用手写时是没有问题的,但是很多论文或者期刊中也是需要用到这些符号或者样式的,比如公式波浪线,那么MathType公式波浪线怎么编辑出来呢? 具体操作步骤如下: 1.打 ...

  5. Win7配置SVN详细步骤(服务器和客户端)

    下载并安装服务器端SVN  VisualSVN Server 下载并安装客户端SVN  TortoiseSVN 创建SVN库 在C盘创建文件夹MySVN(可自由命名),打开文件夹----右键Torto ...

  6. tiny6410SDK制作NFS文件系统

    1.初次编译内核出现问题 解决方法:将uboot的tools目录 下的mkimage拷贝到/bin/下. 2.开机后终端出现mmc0: error -110 whilst initialising S ...

  7. Strut2------获取界面返回的session,application,parameter

    1.Action类下的代码 public class ServletActionDemo extends ActionSupport { @Override public String execute ...

  8. swift--使用 is 和 as 操作符来实现类型检查和转换 / AnyObject与Any的区别

    声明几个类: //动物类 class Animal{ } //陆地动物类 class terricole: Animal { } //海洋动物类 class SeaAnimals: Animal { ...

  9. Python 流程控制:while

    while 语法如下,当条件为假时循环才退出,否则会一直循环下去: while 条件: 执行语句 当条件为假时,才会执行else语句: while 条件: 执行语句 else: 执行语句

  10. c++11实现异步定时器

    c++11提供了丰富的时间和线程操作函数,比如 std::this_thread::sleep, std::chrono::seconds等.可以利用这些来很方便的实现一个定时器.     定时器要求 ...