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.

About UNIQUE KEY:

When you specify a unique key on a column, no two distinct rows in a table can have the same value.

时间: 2024-10-11 17:21:40

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_name varchar(100) default NULL, operation_time datetime default NULL, logrecord_operation varchar(100) default NULL, PRIMARY KEY (logrecord_id), KEY wh

Primary Key and Unique Key difference in SQL Server

posted from http://www.sqlservergeeks.com/primary-key-and-unique-key-difference-in-sql-server/ Primary and Unique Key both enforce uniqueness of columns on which they are defined. Then where do they differ? They differ in following way -          A p

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

一.key与primary key区别 CREATE TABLE wh_logrecord ( logrecord_id int(11) NOT NULL auto_increment, user_name varchar(100) default NULL, operation_time datetime default NULL, logrecord_operation varchar(100) default NULL, PRIMARY KEY (logrecord_id), KEY wh

[Z]mysql中key 、primary key 、unique key 与index区别

一.key与primary key区别 CREATE TABLE wh_logrecord ( logrecord_id int(11) NOT NULL auto_increment, user_name varchar(100) default NULL, operation_time datetime default NULL, logrecord_operation varchar(100) default NULL, PRIMARY KEY (logrecord_id), KEY wh

oracle约束总结(not null/unique/primary key/foreign key/check)

约束(constraint):对创建的表的列属性.字段进行的限制.诸如:not null/unique/primary key/foreign key/check 作用范围: ①列级约束只能作用在一个列上 ②表级约束可以作用在多个列上(当然表级约束也可以作用在一个列上) 定义方式:列约束必须跟在列的定义后面,表约束不与列一起,而是单独定义. - -NOT NULL:不为空约束,只能定义在列级 CREATE TABLE employees( employee_id NUMBER(6), --<sp

the difference between primary key and unique key

primary key:主键约束 unique key:唯一键约束 相同点:唯一,既值都不能重复 不同点: 1.一个表中只能有一个主键约束,但是唯一键约束可以有多个: 2.主键约束可以由一个或多个键组成,而唯一键约束只作用在一个键上: 3.主键作用的字段值不能为空,而唯一键作用的字段值可以为空. 主键本质是约束,值不为空,一个表只能建一个,其目的是检查数据的正确性:唯一索引本质是索引,值可为空,一个表能建一多个,其目的是实现数据查询的优化:

Primary key和Unique Key的区别

Primary key 与Unique Key都是唯一性约束.但二者有很大的区别: 1.Primary key的1个或多个列必须为NOT NULL,如果列为NULL,在增加PRIMARY KEY时,列自动更改为NOT NULL.而UNIQUE KEY 对列没有此要求. 2.一个表只能有一个PRIMARY KEY,但可以有多个UNIQUE KEY. 下面以测试说明: SQL> create table t (a int,b int,c int,d int); Table created. SQL>

MySQL的UNIQUE KEY对数据中字母的大小写不敏感

今天遇到一个坑,对于下列这样一个表: CREATE TABLE `test3` ( `id` int(11) NOT NULL, `name` char(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8: 它的数据如下: +----+------+ | id | name | +----+------+ | 1 | aaa | | 2 | Aaa | +----+------+ 现在我想在这个表上的

[MySQL Tips]:如何删除unique key约束

[场景]: 假设最初创建了一个表bank,在street属性上添加了unique约束. create table branch( branch_name char(30) not null primary key, city varchar(20), street varchar(20) unique ); 表结构如下: [问题] 后来发现在同一个street上可以出现多个支行,也就是说street不应该是unique的.此时怎样删除unique约束呢? [方法] alter table bra