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. RHEL/CentOS 7.X 安装笔记

    配置vim # 配置 vim 编辑器 yum install vim rm /usr/bin/vi ln -s /usr/bin/vim /usr/bin/vi vi /etc/vimrc # 开启语 ...

  2. bootstrap fileinput 上传文件

    最近用到文件上传功能, 说实话:以前遇到过一次,COPY了别人的代码 结束! 这次又要用,可是看到别人很酷的文件上传功能,心痒了! 好吧.简单的办法,找控件: bootstrap fileinput ...

  3. 安装ubuntu出现BUG soft lockup的解决方法(16.04 14.04)

    对于16.04而言,当时用的是UtrISO 安装的,导致安装过程用会出现 “not a com32r image” 的错误,解决方法见上文的: boot: live 华硕Z9主板安装16.04以上系统 ...

  4. 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统:2.技术简介之MinaFilter(1)

    欢迎阅读我的开源项目<迷你微信>服务器与<迷你微信>客户端 Filter filter:过滤器?(不知道是不是这么翻译,算了知道意思就好了╮(╯▽╰)╭),这种东西在很多语言中 ...

  5. nmap -sS

    SYN 扫描,半连接,受到syn/ack响应后意味着端口开放,收到rst包意味着端口关闭.

  6. MovieReview—NINE LIVES(九条命)

    Struggle & Family         A successful middle-aged man in the movie became a cat by falling from ...

  7. 《学习CSS布局》学习笔记

    近几天做了一个小的企业展示网站.虽然页面是在模板的基础上改的,但改的多了不熟悉CSS也很麻烦.正好我看到了学习CSS布局这个网站,于是补习了一下CSS知识. CSS的显示 CSS的元素分为两类:块级元 ...

  8. 影响一个UIView是否能正常显示的几个因素

    在使用代码实现UIView及其子类的对象的时候,经常会遇到创建的某个view没有显示在屏幕上.以下总结了本人遇到过的几种情况.可能还有些其它的原因也会导致view不能正常显示,限于个人经历有限,无法全 ...

  9. 使用ImageList组件制作动画图片

    实现效果: 知识运用: Timer组件的Enabled属性  Tick事件 PictureBox控件的Image属性 ImageList组件的Images属性 实现代码: private void F ...

  10. 2018.3.4 Linux and Unix 知识点

    UNIX系统的特点 1.多任务 2.多用户 3.强大的网络功能 4.设备无关性 5.并行处理能力 6.开放性 7.错误处理 Linux系统的特点 1.自由软件 2.良好的兼容性 3.良好的界面 4.丰 ...