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的更多相关文章

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

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

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

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

  3. MySQL - primary key PK unique key,key PK index

    primary key PK unique key 总结 primary key = unique + not null 主键不能为空每个字段值都不重复,unique可以为空,非空字段不重复 uniq ...

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

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

  5. MySQL中KEY、PRIMARY KEY、UNIQUE KEY、INDEX 的区别

    参考:MySQL中KEY.PRIMARY KEY.UNIQUE KEY.INDEX 的区别 对于题目中提出的问题,可以拆分来一步步解决.在 MySQL 中 KEY 和 INDEX 是同义.那这个问题就 ...

  6. 待续--mysql中key 、primary key 、unique key 与index区别

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

  7. mysql中 key 、primary key 、unique key 和 index 有什么不同

    mysql中 key .primary key .unique key 和 index 有什么不同 key 是数据库的物理结构,它包含两层意义和作用, 一是约束(偏重于约束和规范数据库的结构完整性), ...

  8. Mysql中的索引()key 、primary key 、unique key 与index区别)

    CREATE TABLE pre_forum_post ( pid int(10) unsigned NOT NULL COMMENT '帖子id', fid mediumint(8) unsigne ...

  9. PRIMARY KEY,key,unique key

    主键索引(必须指定为“PRIMARY KEY”,没有PRIMARY Index). 唯一索引(unique index,一般写成unique key). 普通索引(index,只有这一种才是纯粹的in ...

随机推荐

  1. NOPI Excel 读取公式生成后的数据

    using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; using S ...

  2. AJPFX关于JAVA StringBuffer的用法总结

    StringBuffer类和String一样,也用来代表字符串,只是由于StringBuffer的内部实现方式和String不同,所以StringBuffer在进行字符串处理时,不生成新的对象,在内存 ...

  3. angularjs实现导航菜单切换高亮

    <ul> <li ng-repeat="(index, item) in headerList"> <a ui-sref="{{item.h ...

  4. BLL-IDAL-DAL的关系

    BLL  实现 IDAL  是一个接口 DAL 实现方法 BLL 调用IDAL 的方法 IDAL中的方法   在 DAL中必须实现 使用的方法  调用BLL的方法就可以

  5. HDU 3592 World Exhibition (差分约束,spfa,水)

    题意: 有n个人在排队,按照前后顺序编号为1~n,现在对其中某两人的距离进行约束,有上限和下限,表示dis[a,b]<=c或者dis[a,b]>=c,问第1个人与第n个人的距离最多可能为多 ...

  6. BZOJ 2119: 股市的预测 SA

    2119: 股市的预测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 434  Solved: 200[Submit][Status][Discuss ...

  7. BZOJ 2725: [Violet 6]故乡的梦 最短路+线段树

    2725: [Violet 6]故乡的梦 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 678  Solved: 204[Submit][Status ...

  8. Java 设计模式之中介者模式

    本文继续23种设计模式系列之中介者模式.   定义 用一个中介者对象封装一系列的对象交互,中介者使各对象不需要显示地相互作用,从而使耦合松散,而且可以独立地改变它们之间的交互.   角色 抽象中介者: ...

  9. 认识CoreData—初识CoreData

    http://www.cocoachina.com/ios/20160729/17245.html 这段时间公司一直比较忙,和组里小伙伴一起把公司项目按照之前逻辑重写了一下.由于项目比较大,还要兼顾之 ...

  10. guruguru

    6576: guruguru 时间限制: 1 Sec  内存限制: 128 MB提交: 28  解决: 12[提交] [状态] [讨论版] [命题人:admin] 题目描述 Snuke is buyi ...