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 ...
随机推荐
- SpringMVC的基础配置及视图定位
概要 记录一下搭建SpringMVC框架的步骤 视图定位也就是改变jsp在项目中的路径 一.新建javaweb项目springmvc1,在lib中导入jar包 此项目上传了GitHub,方便去下载ja ...
- 如何检查linux 下是否安装java(jdk)环境
大家可通过下面五条命令来查看linux 系统是否安装了java 环境 1.java -version 2.which java 3.rpm -qa |grep java 4.echo $PATH 5. ...
- 小米OJ刷题日志
虽然这OJ上的题比较水,但还是挺有意思的.关键是能赚钱 特别是提交方式 居然不支持C++,垃圾OJ 4. 最长连续数列 排序后dp 5. 找出旋转有序数列的中间值 写个排序就做完了. 6. 交叉队列 ...
- angularjs实现导航菜单切换高亮
<ul> <li ng-repeat="(index, item) in headerList"> <a ui-sref="{{item.h ...
- Windows及Linux环境搭建Redis集群
一.Windows环境搭建Redis集群 参考资料:Windows 环境搭建Redis集群 二.Linux环境搭建Redis集群 参考资料:Redis Cluster的搭建与部署,实现redis的分布 ...
- 实现memcopy函数
实现memcopy函数: void * memcpy(void *dest, const void *src, unsigned int count); { if ((src == NULL) || ...
- C#调用SAP S4/HANA Gateway Service
公司使用SAP,并且实施公司做了一些提供报表数据的接口供调用,首先说明一下我对SAP不熟悉 但SAP用到的接口信息提供大家参考一下,这些Gateway Service使用的是DCP方式写的,SAP提供 ...
- servlet forword服务器端跳转
web.xml中配置servlet的映射和访问路径 <?xml version="1.0" encoding="UTF-8"?><web-ap ...
- HDU - 5096 ACM Rank (Treap)
平衡树的题,Treap破之,比较难搞的出现相同题数罚时的情况,解决方法是在每个结点用一个set, 保证结点值的时候可以把题数和罚时保存到一个int里,令v = n*MaxPenaltySum-pena ...
- 用python Image读图
https://www.cnblogs.com/kongzhagen/p/6295925.html import os name = [] with open('/media/hdc/xing/Dee ...