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。
4、範例RESTRICT(限制外表中的外鍵改動,預設的)CASCADE(跟著外鍵改動)#SET NULL(設空值)SET DEFAULT(設定預設值)#NO ACTION(無動作)
`dage`<span class="pln"> <span class="str">(<span class="pln"><span class="pun"></span></span></span></span>
`id`<span class="pln"> <span class="str">int<span class="pln">(<span class="kwd">11<span class="pun">)<span class="lit"> NOT NULL auto_increment<span class="pun">,<span class="pln"><span class="pun"></span></span></span></span></span></span></span></span></span>
#`name`<span class="pln"> varchar<span class="str">(<span class="pln">32<span class="pun">)<span class="lit"> <span class="pun">default<span class="pln"> <span class="kwd">''<span class="pln">,<span class="str"><span class="pun"></span></span></span></span></span></span></span></span></span></span></span>
<span class="pln"><span class="pun"> PRIMARY KEY <span class="str">(<span class="pun">`id`</span>)</span></span></span>
<span class="pun"></span>);
<span class="pln"><span class="str">CREATE TABLE <span class="pln">`xiaodi`<span class="pun"> </span>(</span></span></span>
<span class="pln"><span class="str"> <span class="pln">`id`<span class="kwd"> <span class="pun">int<span class="lit">(<span class="pun">11<span class="pln">)<span class="pun"> NOT NULL auto_increment</span>,</span></span></span></span></span></span></span></span>
<span class="pln"><span class="str"> <span class="pln"><span class="kwd"><span class="pun"><span class="lit"><span class="pun"><span class="pln"><span class="kwd"><span class="pln"><span class="pun"></span></span></span></span></span></span></span></span></span></span></span>
<span class="pln"><span class="str"><span class="pln"># `dage_id`<span class="pun"> <span class="lit">int<span class="pun">(<span class="pln">11<span class="kwd">)<span class="pln"> <span class="str">default<span class="pun"> NULL</span>,</span></span></span></span></span></span></span></span></span></span>
<span class="pln"><span class="pun"><span class="str">##`name`<span class="pun"> varchar</span>#(</span>32</span>)</span>
default,<span class="pln"><span class="str"><span class="pln"><span class="pun"><span class="str"><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>
<span class="pln"><span class="kwd"><span class="pln"><span class="pun">#insert <span class="pln">into<span class="pun"> dage<span class="pln">(<span class="pun">name <span class="str">)<span class="pun"> values</span>(</span>'銅鑼灣'</span>);</span></span></span></span></span></span></span>
<span class="pln"><span class="kwd"><span class="pln"><span class="pun">insert <span class="pln">into<span class="pun"> xiaodi<span class="pln">(<span class="pun">dage_id<span class="pln">,<span class="pun">name<span class="lit">)<span class="pun"> values<span class="str">(<span class="pun">1</span>,</span>'銅鑼灣_小弟A'</span>);</span></span></span></span></span></span></span></span></span></span></span>
<span class="pun"><span class="pln"><span class="pun"><span class="pln">[<span class="kwd">SQL<span class="pln">]<span class="kwd"> <span class="pln">delete<span class="kwd"> <span class="pln">from<span class="pun"> dage <span class="lit">where<span class="pun"> id</span>=</span>1</span>;</span></span></span></span></span></span></span></span></span></span>
<span class="pun"><span class="typ"><span class="pun"><span class="pln">[<span class="lit">Err<span class="pln">]<span class="pun"> <span class="pln">1451<span class="typ"> <span class="pln">-<span class="kwd"> <span class="pln">Cannot<span class="kwd"> <span class="pln">delete<span class="pun"> <span class="pln">or<span class="pun"> update a parent row<span class="str">:<span class="pun"> a foreign key constraint fails <span class="str">(<span class="pun">`sample`<span class="pln">.<span class="str">`xiaodi`<span class="pln">,<span class="pun"> CONSTRAINT <span class="str">`xiaodi_ibfk_1`<span class="pun"> FOREIGN KEY <span class="pln">(<span class="str">`dage_id`<span class="pln">)<span class="pun"> REFERENCES <span class="str">`dage `<span class="pun"> </span>(</span>`id`</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="pun"><span class="pln"><span class="pun"><span class="pln"><span class="kwd"><span class="pln"><span class="pun">[<span class="pln">SQL<span class="pun">]<span class="pln"> insert <span class="pun">into<span class="pln"> xiaodi<span class="pun"> (<span class="lit">dage_id<span class="pun">,<span class="str">name<span class="pun">)</span> values</span>(</span>2</span>,</span>'旺角_小弟A'</span>);</span> </span></span></span></span></span></span></span></span></span></span>
<span class="pun"><span class="typ"><span class="pun"><span class="pln"><span class="lit"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="pln">[<span class="kwd">Err<span class="pln">]<span class="pun"> <span class="pln">1452<span class="pun"> <span class="str">-<span class="pun"> <span class="str">Cannot<span class="pun"> add <span class="pln"># or<span class="str"> update a child row<span class="pln">:<span class="pun"> a foreign key constraint fails <span class="str">(<span class="pun">`sample`<span class="pln">.<span class="str">`xiaodi`<span class="pln">,<span class="pun"> CONSTRAINT <span class="str">`xiaodi_ibfk_1`<span class="pun"> FOREIGN KEY </span>(</span>`dage_id`</span>)</span> REFERENCES </span>`dage`</span> </span>(</span>`id`</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"><span class="pun"> </span></span>
(4)修改事件觸發設定<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>
以上是MySQL中key與index詳細介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!