如果不设置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. win10取消开机密码

    WIN10开机以后点击一下[开始]然后面它的搜索栏处输入[netplwiz]就可以看到如下图所示提示.   进入到用户长户界面,把[要使用本计算机,用户必须输入用户名和密码]前面的那个勾勾取消了,再点 ...

  2. OpenCV学习:改变图像的对比度和亮度

    本实例演示简单地改变图像的对比度和亮度,使用了如下线性变换来实现像素值的遍历操作: The parameters α > 0 and β often called the gain and bi ...

  3. How do I commit all deleted files in Git?

    Try this: $ git add -u This tells git to automatically stage tracked files -- including deleting the ...

  4. 第四章 Spring.Net 如何管理您的类___对象、对象工厂和应用程序上下文

    在前面一章我们介绍了依赖注入,控制反转的概念,以及自己动手搭建了一下Spring.Net的环境.通过这些操作,我们知道了Spring.Net 的核心是使用依赖注入或控制反转这种思想来管理业务对象,降低 ...

  5. Java精选笔记_Servlet事件监听器

    Servlet事件监听器 概述 在程序开发中,经常需要对某些事件进行监听,如监听鼠标点击事件.监听键盘按下事件等,此时就需要使用事件监听器. 事件监听器用于对程序中发生的事件进行监听,在监听的过程中会 ...

  6. org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Can

    org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Can ...

  7. day05<Java语言基础--数组>

    Java语言基础(数组概述和定义格式说明) Java语言基础(数组的初始化动态初始化) Java语言基础(Java中的内存分配以及栈和堆的区别) Java语言基础(数组的内存图解1一个数组) Java ...

  8. jquery 获取当前时间加180天

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  9. 【PHP】 解决报错:Error: php71w-common conflicts with php-common-5.4.16-43.el7_4.x86_64

    背景: 手动安装的PHP7 环境 问题:在安装扩展的时候.无论输入 php-*  来安装任何扩展.都会报错 Error: php71w-common conflicts with php-common ...

  10. LNMP ftp 可以登录无权限操作?

    服务器环境: LNMP ftp : LNMP ftp一键安装 嘛卖批啊! 解决办法:  登录服务器.执行以下命令 chattr -i /home/wwwroot/default/.user.ini c ...