mysql 约束条件 unique key 唯一的键
如果不设置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 唯一的键的更多相关文章
- unique key 唯一约束
#添加唯一约束mysql> alter table tb2 -> add unique key(name) ->;#删除唯一约束mysql> alter table ...
- mysql 约束条件 primary key 主键
primary key字段的值不为空且唯一 约束:not null unique 存储引擎:innodb 对于innodb来说,一张表内必须有一个主键 单列做主键多列做主键(复合主键) 通常都是id字 ...
- mysql 中UNIQUE KEY 到底是约束还是索引?
答案来自:https://zhidao.baidu.com/question/1863373387452612907.html 两者关系 unique索引包含了unique约束,因为unique约束是 ...
- mysql 约束条件目录
mysql 约束条件 mysql 约束条件 not null与default mysql 约束条件 unique key 唯一的键 mysql primary key 主键 mysql auto_in ...
- mysql中key 、primary key 、unique key 与index区别
一.key与primary key区别 CREATE TABLE wh_logrecord ( logrecord_id ) NOT NULL auto_increment, ) default NU ...
- 【Mysql】key 、primary key 、unique key 与index区别
参考:https://blog.csdn.net/nanamasuda/article/details/52543177 总的来说,primary key .unique key 这些key建立的同时 ...
- MySQL 中 key, primary key ,unique key,index的区别
一.key与primary key区别 CREATE TABLE wh_logrecord ( logrecord_id int(11) NOT NULL auto_increment, user_n ...
- mysql中的key primary key 和unique key
mysql 中key就等同于index 所以 key:普通索引 unique key:唯一索引,就是这一列不能重复 primary key:主键索引,就是不能为空,且主键索引不是完全相同时,插入新数据 ...
- Mysql如何修改unique key
link:http://www.netingcn.com/mysql-modifyunique-key.html mysql可以使用unique key来确保数据的准确性,unique key可以是一 ...
随机推荐
- jquery-修改、回退结果集
1.end()方法 使用end方法得到上一个结果集 2.addBack()方法 使用addBack()可以得到原结果集与当前结果的合集,也可传入选择器来过滤原结果集
- MathType如何编辑手写体l
MathType在编辑公式不仅方便而且规范,并且能够根据自己的需要选择不同的字体进行使用,可以是正体也可以是斜体,可以是新罗马体,也可以是花体,这些用word公式编辑器MathType都是可以的.还有 ...
- 第四章 Spring.Net 如何管理您的类___对象的手动装配
前面我们知道了什么是对象,什么是对象工厂,什么是应用程序上下文.这一次我们来看一下对象的装配. Spring.Net 中有多种装配对象的方式,装配这个词可能比较学术化,我们可以理解为对象的创建. Sp ...
- js实现卡号每四位空格分隔
window.onload =function() { document.getElementById("input_num").oninput =function() { })( ...
- CMS3.0——初次邂逅express
前言: 刚接手cms3.0的工作,似乎对一切都那么的不熟悉,于是在开始新需求之前,先做一个简单的登录系统. 项目目录: 1.使用webstroms建expreess项目,非常方便简单,建好的项目目录就 ...
- iOS中UIImage转换为NSData 方法
参考网址:http://blog.csdn.net/lovenjoe/article/details/7484217 天牛 感谢作者的硕果 在Iphone上有两种读取图片数据的简单方法: UIImag ...
- 好用的在线Markdown编辑器
dillinger.io 支持在线编辑,导出为html格式等.
- 实现类似printf这样的函数
来源:http://www.vimer.cn/2009/12/cc%E5%AE%9E%E7%8E%B0%E5%A4%9A%E5%8F%82%E6%95%B0%E5%87%BD%E6%95%B0%E7% ...
- c++ 引用底层实现
红色是我添加的,其他地方是原作者的. 主要是看了上面的这篇“从底层汇编理解 c++ 引用实现机制“的文章之后,觉得不错.就转了过来,同时,对文中的程序都在自己的机器上验证了一下. 使用的G++版本:g ...
- 深刻理解 React (一) ——JSX和虚拟DOM
版权声明:本文由左明原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/155 来源:腾云阁 https://www.qclou ...