Rumah > Artikel > pangkalan data > MySQL中key与index详细介绍
2、primary key与unique key
1)出发点/作用不同:前者是一行数据的唯一标识,后者只是用来避免数据重复。
2)前者的一个列或多个列必须全部为not null;如果其中一个列为null,在添加为主键时,会变为not null,如果再删除主键,列的nullable性质会变回去。后者的列可以为null。
3)一个表只能有一个primary key,可以有多个unique key。【一个表可以没有primary key吗???】
4)对于unique key对应的列,可以多次插入null(虽然也是一种重复);这是由索引的原理,即索引对null的处理决定的。
create table t(id int, key (id));如果有使用id的其他键(如foreign key),则使用其他键对他的命名;如果都没有命名,则使用id;如果一次指定了多个列作为键,则使用第一个列名作为键名。create table t(id int, key kismet(id));指定该key的名称constraint:不能使用,毕竟普通key并没有约束作用
create table t(id int, primary key (id));create table t(id int, primary key kismet(id));可以执行,但是名称不起作用create table t(id int, constraint kismet primary key(id));可以执行,但是名称不起作用
create table t(id int, unique key (id));命名规则与key不同,只使用第一列作为键名create table t(id int, unique key kismet(id));指定该key的名称create table t(id int, constraint kismet unique key(id));指定该key的名称
create table t(id int, foreign key (dage_id) references dage(id));可以执行,执行结果为创建了一个自动命名的foreign key和一个自动命名的普通key。create table t(id int, foreign key kismet(dage_id) references dage(id));可以执行,执行结果为创建了一个自动命名的foreign key和一个名称为kismet的普通key。create table t(id int, constraint kismet foreign key(dage_id) references dage(id));可以执行,执行结果为创建了一个名称为kismet的foreign key和一个名称为kismet的普通key。
RESTRICT(限制外表中的外键改动,默认的)CASCADE(跟随外键改动)SET NULL(设空值)SET DEFAULT(设默认值)NO ACTION(无动作)
<span class="pln">CREATE TABLE <span class="str">`dage`<span class="pln"> <span class="pun">(</span></span></span></span>
<span class="pln"> <span class="str">`id`<span class="pln"> <span class="kwd">int<span class="pun">(<span class="lit">11<span class="pun">)<span class="pln"> NOT NULL auto_increment<span class="pun">,</span></span></span></span></span></span></span></span></span>
<span class="pln"> <span class="str">`name`<span class="pln"> varchar<span class="pun">(<span class="lit">32<span class="pun">)<span class="pln"> <span class="kwd">default<span class="pln"> <span class="str">''<span class="pun">,</span></span></span></span></span></span></span></span></span></span></span>
<span class="pln"> PRIMARY KEY <span class="pun">(<span class="str">`id`<span class="pun">)</span></span></span></span>
<span class="pun">);</span>
<span class="pln">CREATE TABLE <span class="str">`xiaodi`<span class="pln"> <span class="pun">(</span></span></span></span>
<span class="pln"> <span class="str">`id`<span class="pln"> <span class="kwd">int<span class="pun">(<span class="lit">11<span class="pun">)<span class="pln"> NOT NULL auto_increment<span class="pun">,</span></span></span></span></span></span></span></span></span>
<span class="pln"> <span class="str">`dage_id`<span class="pln"> <span class="kwd">int<span class="pun">(<span class="lit">11<span class="pun">)<span class="pln"> <span class="kwd">default<span class="pln"> NULL<span class="pun">,</span></span></span></span></span></span></span></span></span></span></span>
<span class="pln"> <span class="str">`name`<span class="pln"> varchar<span class="pun">(<span class="lit">32<span class="pun">)<span class="pln"> <span class="kwd">default<span class="pln"> <span class="str">''<span class="pun">,</span></span></span></span></span></span></span></span></span></span></span>
<span class="pln"> PRIMARY KEY <span class="pun">(<span class="str">`id`<span class="pun">),</span></span></span></span>
<span class="pln"> KEY <span class="str">`dage_id`<span class="pln"> <span class="pun">(<span class="str">`dage_id`<span class="pun">),</span></span></span></span></span></span>
<span class="pln"> CONSTRAINT <span class="str">`xiaodi_ibfk_1`<span class="pln"> FOREIGN KEY <span class="pun">(<span class="str">`dage_id`<span class="pun">)<span class="pln"> REFERENCES <span class="str">`dage`<span class="pln"> <span class="pun">(<span class="str">`id`<span class="pun">)</span></span></span></span></span></span></span></span></span></span></span></span>
<span class="pun">);</span>
insert into dage(name) values('铜锣湾'<span class="pun">);</span>
insert into xiaodi(dage_id,name) values(1,'铜锣湾_小弟A'<span class="pun">);</span>
<span class="pun">[<span class="pln">SQL<span class="pun">]<span class="pln"> <span class="kwd">delete<span class="pln"> <span class="kwd">from<span class="pln"> dage <span class="kwd">where<span class="pln"> id<span class="pun">=<span class="lit">1<span class="pun">;</span></span></span></span></span></span></span></span></span></span></span></span></span>
<span class="pun">[<span class="typ">Err<span class="pun">]<span class="pln"> <span class="lit">1451<span class="pln"> <span class="pun">-<span class="pln"> <span class="typ">Cannot<span class="pln"> <span class="kwd">delete<span class="pln"> <span class="kwd">or<span class="pln"> update a parent row<span class="pun">:<span class="pln"> a foreign key constraint fails <span class="pun">(<span class="str">`sample`<span class="pun">.<span class="str">`xiaodi`<span class="pun">,<span class="pln"> CONSTRAINT <span class="str">`xiaodi_ibfk_1`<span class="pln"> FOREIGN KEY <span class="pun">(<span class="str">`dage_id`<span class="pun">)<span class="pln"> REFERENCES <span class="str">`dage`<span class="pln"> <span class="pun">(<span class="str">`id`<span class="pun">))</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
[SQL] insert into xiaodi(dage_id,name) values(2,'旺角_小弟A'<span class="pun">);</span>
<span class="pun">[<span class="typ">Err<span class="pun">]<span class="pln"> <span class="lit">1452<span class="pln"> <span class="pun">-<span class="pln"> <span class="typ">Cannot<span class="pln"> add <span class="kwd">or<span class="pln"> update a child row<span class="pun">:<span class="pln"> a foreign key constraint fails <span class="pun">(<span class="str">`sample`<span class="pun">.<span class="str">`xiaodi`<span class="pun">,<span class="pln"> CONSTRAINT <span class="str">`xiaodi_ibfk_1`<span class="pln"> FOREIGN KEY <span class="pun">(<span class="str">`dage_id`<span class="pun">)<span class="pln"> REFERENCES <span class="str">`dage`<span class="pln"> <span class="pun">(<span class="str">`id`<span class="pun">))</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<span class="pln">show create table xiaodi<span class="pun">;#查看键名称</span></span>
<span class="pln">alter table xiaodi drop foreign key xiaodi_ibfk_1<span class="pun">;</span></span>
<span class="pln">alter table xiaodi add foreign key<span class="pun">(<span class="pln">dage_id<span class="pun">)<span class="pln"> references dage<span class="pun">(<span class="pln">id<span class="pun">)<span class="pln"> on <span class="kwd">delete<span class="pln"> cascade on update cascade<span class="pun">;</span></span></span></span></span></span></span></span></span></span></span></span>
<span class="pln">CREATE TABLE <span class="typ">People<span class="pln"> <span class="pun">(</span></span></span></span>
<span class="pln"> last_name varchar<span class="pun">(<span class="lit">50<span class="pun">)<span class="pln"> <span class="kwd">not<span class="pln"> <span class="kwd">null<span class="pun">,</span></span></span></span></span></span></span></span></span>
<span class="pln"> first_name varchar<span class="pun">(<span class="lit">50<span class="pun">)<span class="pln"> <span class="kwd">not<span class="pln"> <span class="kwd">null<span class="pun">,</span></span></span></span></span></span></span></span></span>
<span class="pln"> dob date <span class="kwd">not<span class="pln"> <span class="kwd">null<span class="pun">,</span></span></span></span></span>
<span class="pln"> gender <span class="kwd">enum<span class="pun">(<span class="str">'m'<span class="pun">,<span class="pln"> <span class="str">'f'<span class="pun">)<span class="pln"> <span class="kwd">not<span class="pln"> <span class="kwd">null<span class="pun">,</span></span></span></span></span></span></span></span></span></span></span></span></span>
<span class="pln"> key<span class="pun">(<span class="pln">last_name<span class="pun">,<span class="pln"> first_name<span class="pun">,<span class="pln"> dob<span class="pun">)</span></span></span></span></span></span></span></span>
<span class="pun">);</span>
1)匹配全值:对索引中的所有列都指定具体的值。2)匹配最左前缀:你可以利用索引查找last name为Allen的人,仅仅使用索引中的第1列。3)匹配列前缀:例如,你可以利用索引查找last name以J开始的人,这仅仅使用索引中的第1列。4)匹配值的范围查询:可以利用索引查找last name在Allen和Barrymore之间的人,仅仅使用索引中第1列。5)匹配部分精确而其它部分进行范围匹配:可以利用索引查找last name为Allen,而first name以字母K开始的人。6)仅对索引进行查询:如果查询的列都位于索引中,则不需要读取元组的值。7)如果索引字段为A+B,查询A+C时,会使用A索引吗->会,使用explain可以证实
(4)限制
1)查询必须从索引的最左边的列开始。2)不能跳过某一索引列。例如,你不能利用索引查找last name为Smith且出生于某一天的人。3)存储引擎不能使用索引中范围条件右边的列。例如,如果你的查询语句为WHERE last_name="Smith" AND first_name LIKE 'J%' AND dob='1976-12-23',则该查询只会使用索引中的前两列,因为LIKE是范围查询。
1)Hash索引通过哈希函数计算Hash值进行检索,可以查到要查数据的行指针,从而定位数据。2)Hash值不取决于列的数据类型,一个TINYINT列的索引与一个长字符串列的索引一样大。3)Memory存储引擎支持非唯一hash索引,如果多个值有相同的hash code,索引把它们的行指针用链表保存到同一个hash表项中。
(2)限制
1)由于索引仅包含hash code和记录指针,所以,MySQL不能通过使用索引避免读取记录。但是访问内存中的记录是非常迅速的,不会对性造成太大的影响。2)不能使用hash索引排序。3)Hash索引不支持键的部分匹配,因为是通过整个索引值来计算hash值的。4)Hash索引只支持等值比较,例如使用=,IN( )和<=>。对于WHERE price>100并不能加速查询。
<span class="pln">CREATE TABLE testhash <span class="pun">(</span></span>
<span class="pln"> fname VARCHAR<span class="pun">(<span class="lit">50<span class="pun">)<span class="pln"> NOT NULL<span class="pun">,</span></span></span></span></span></span>
<span class="pln"> lname VARCHAR<span class="pun">(<span class="lit">50<span class="pun">)<span class="pln"> NOT NULL<span class="pun">,</span></span></span></span></span></span>
<span class="pln"> <em><span style="color: #000000">KEY USING HASH</span></em><em><span style="color: #000000"><span class="pun">(<span class="pln">fname<span class="pun">)</span></span></span></span></em></span>
<span class="pun">)<span class="pln">ENGINE<span class="pun">=<span class="pln">MEMORY<span class="pun">;</span></span></span></span></span>
Atas ialah kandungan terperinci MySQL中key与index详细介绍. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!