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. CentOS 6.4 中yum命令安装php5.2.17

    最近给公司部署服务器的时候发现他们提供的服务器是centos6.4系统的,装好系统和相关服务httpd,mysql,php,一跑代码,发现php5.3中的zend加密不能用,安装Zend Guard ...

  2. nodejs 实践:express 最佳实践 (一) 项目结构

    express 最佳实践 (一) 第二篇: express 最佳实践(二):中间件 最近,一直在使用 nodejs 做项目,对 nodejs 开发可以说深有体会. 先说说 nodejs 在业务中的脚色 ...

  3. spring data jpa自定义baseRepository

    在一些特殊时候,我们会设计到对Spring Data JPA中的方法进行重新实现,这将会面临一个问题,如果我们新创建一个实现类.如果这个实现类实现了JpaRepository接口,这样我们不得不实现该 ...

  4. 1g免费空间永久使用

    云邦互联免费空间(免备案,无广告) [1G免费全能空间,免备案,无广告] 1G全能空间 + 100M数据库(Mysql 5.5 / SQL Server 2005) 支持的脚本:ASP.PHP(5.2 ...

  5. JavaScirpt 的垃圾(garbage collection)回收机制

    一.垃圾回收机制—GC Javascript具有自动垃圾回收机制(GC:Garbage Collecation),也就是说,执行环境会负责管理代码执行过程中使用的内存. 原理:垃圾收集器会定期(周期性 ...

  6. .net core 的跨域

    .net core 的跨域问题花了 我很长时间 接下来我简单的描述下解决过程 首先我在前端用jquery的ajax去调用自己的本地接口大致如下 $.ajax({ type:"POST&quo ...

  7. [20190625]记录npm的一些常用命令

    1. 安装依赖包 npm install -g packageName //全局安装npm install packageName --save //安装在项目下并写入package.json文件中n ...

  8. Object-C反射读取实体属性和值

    举例: 首先定义TestModel如下: @interface TestModel : NSObject @property (nonatomic, strong) NSString *name; @ ...

  9. vue搭建骨架屏步骤配置

    1.什么是骨架屏幕? 在页面加载数据之前,有一段空白时间,要么用loading加载,要么就用骨架屏. 在开发webapp的时候总是会受到首屏加载时间过长的影响,主流的解决方法是在载入完成之前显示loa ...

  10. codevs 4093 EZ的间谍网络

    时间限制: 10 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 由于外国间谍的大量渗入,学校安全正处于高度的危机之中.YJY决定挺身而作出反抗 ...