INDEX && PRIMARY KEY && UNIQUE KEY
When I have do some sql tody, some confusion come up to me.
Its about the index && PRIMARY KEY && UNIQUE KEY in MySQL. So I google it for the answers. There is a clearly answer on the
StackOverflow. So I share it on this BLOG.
About INDEX:
Firstly, we should understand KEY and INDEX are synonyms in MySQL. They mean the same thing.
You may have the quersion : what is exactly the INDEX mean ?
Here is a shortcut about the INDEX:
In databases you would use indexes to improve the speed of data retrieve. An index is typically created on columns used in
JOIN, WHERE, and ORDER BY clauses.
There is an example about INDEX so you can understand it better :
Imagine you have a table called users
and you want to search for all the users which have the last name 'Smith'. Without an index, the database would have to go through all the records of the table: this is slow, because the more records you have in your database, the more work it has to do to find the result. On the other hand, an index will help the database skip quickly to the relevant pages where the 'Smith' records are held. This is very similar to how we, humans, go through a phone book directory to find someone by the last name: We don't start searching through the directory from cover to cover, as long we inserted the information in some order that we can use to skip quickly to the 'S' pages.
About PRIMARY KEY :
Primary keys and unique keys are similar. A primary key is a column, or a combination of columns, that can uniquely identify a row.
the five rules of primary key :
1. The data in a primary key can't be repeated. (Because primary key is a special kind of unique key)
2. A primary key must have a value.
3. The primary key must be set when a new row is inserted.
4. A primary key must be as efficient as possible.
5. The value of a primary key can't be changed
About UNIQUE KEY:
When you specify a unique key on a column, no two distinct rows in a table can have the same value.
The columns in which no two rows are similar .
Also note that columns defined as primary keys or unique keys are automatically indexed in MySQL.
Primary key does not allow null value but unique key allows null value.
INDEX && PRIMARY KEY && UNIQUE 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 与index区别
参考:https://blog.csdn.net/nanamasuda/article/details/52543177 总的来说,primary key .unique key 这些key建立的同时 ...
- MySQL - primary key PK unique key,key PK index
primary key PK unique key 总结 primary key = unique + not null 主键不能为空每个字段值都不重复,unique可以为空,非空字段不重复 uniq ...
- 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 的区别
参考:MySQL中KEY.PRIMARY KEY.UNIQUE KEY.INDEX 的区别 对于题目中提出的问题,可以拆分来一步步解决.在 MySQL 中 KEY 和 INDEX 是同义.那这个问题就 ...
- 待续--mysql中key 、primary key 、unique key 与index区别
mysql中key .primary key .unique key 与index区别
- mysql中 key 、primary key 、unique key 和 index 有什么不同
mysql中 key .primary key .unique key 和 index 有什么不同 key 是数据库的物理结构,它包含两层意义和作用, 一是约束(偏重于约束和规范数据库的结构完整性), ...
- Mysql中的索引()key 、primary key 、unique key 与index区别)
CREATE TABLE pre_forum_post ( pid int(10) unsigned NOT NULL COMMENT '帖子id', fid mediumint(8) unsigne ...
- PRIMARY KEY,key,unique key
主键索引(必须指定为“PRIMARY KEY”,没有PRIMARY Index). 唯一索引(unique index,一般写成unique key). 普通索引(index,只有这一种才是纯粹的in ...
随机推荐
- csrf攻击实例
CSRF 攻击可以在受害者毫不知情的情况下以受害者名义伪造请求发送给受攻击站点,从而在并未授权的情况下执行在权限保护之下的操作.比如说,受害者 Bob 在银行有一笔存款,通过对银行的网站发送请求 ht ...
- Bootcamp Win10蓝牙鼠标的问题
运行services.msc找到Bluetooth support service 把启动类型从手动改为自动 重新连接蓝牙鼠标
- nodejs 实践:express 最佳实践(六) express 自省获得所有的路由
nodejs 实践:express 最佳实践(六) express 自省获得所有的路由 某些情况下,你需要知道你的应用有多少路由,这在 express 中没有方法可以.因此我这边曲线了一下,做成了一个 ...
- Java中的构造函数——通过示例学习Java编程(14)
作者:CHAITANYA SINGH 来源:https://www.koofun.com//pro/kfpostsdetail?kfpostsid=25 构造函数是用来初始化新创建的对象的代码块. ...
- iOS NSDecimalNumber 使用
在iOS开发中,经常遇到和货币价格计算相关的,这时就需要注意计算精度的问题.使用float类型运算,经常出现误差.为了解决这种问题我们使用NSDecimalNumber,下面将通过例子的形式给大家展示 ...
- Web端 session cookies Application viewstate
URL传值/QueryString1.不占用服务器内存2.保密性差,传递值的长度有限 因为 上篇文章 保密性差,长度有限 传值有限只能传string类型的值 这篇文章学的知识是 session ...
- 洛谷 P1057 传球游戏
题目描述 上体育课的时候,小蛮的老师经常带着同学们一起做游戏.这次,老师带着同学们一起做传球游戏. 游戏规则是这样的:n个同学站成一个圆圈,其中的一个同学手里拿着一个球,当老师吹哨子时开始传球,每个同 ...
- CF Gym 100637J Superfactorial numeral system (构造)
题意:给一个式子,ak,k>2时,0<=ak<k:ai都是整数,给你p,q让你求一组ak. 题解:构造,每次除掉q取整得到ai,然后减一减 #include<cstdio> ...
- HDU 4738 Caocao's Bridges taijan (求割边,神坑)
神坑题.这题的坑点有1.判断连通,2.有重边,3.至少要有一个人背*** 因为有重边,tarjan的时候不能用子结点和父节点来判断是不是树边的二次访问,所以我的采用用前向星存边编号的奇偶性关系,用^1 ...
- Hbase 完全分布式 高可用 集群搭建
1.准备 Hadoop 版本:2.7.7 ZooKeeper 版本:3.4.14 Hbase 版本:2.0.5 四台主机: s0, s1, s2, s3 搭建目标如下: HMaster:s0,s1(备 ...