Mysql學習筆記二

2021-06-23 00:23:55 字數 3854 閱讀 1663

接著上面繼續學習,下面主要是以索引為主。

建立索引

有四種型別的索引:主鍵、唯一索引、全文索引和普通索引

它是值惟一並且沒有值為null的域的索引。

如:create table tablename(filename columntype not null,filedname2....] primary key (fieldname);

注意關鍵字not null在建立主鍵時是強制性的,主鍵不能包含null值。

create table pk_tets(f1 int,primary key(f1));

在乙個已經存在的表上建立主鍵,可以使用alter關鍵字:

alter table tablename add primary key(filedname);

下面是修改列屬性為not null的同時增加主鍵。

alter table customer modify id int not null,add primary key(id);

一般在建表的同時建立索引。

create table tablename(filedname columntype,filedname2 columntype ,index [indexname] (filedname1,filedname2));;

也可以使用下面的**在以後建立索引:

alter table tablename add index [idexname] (filedname); 或

create index indexname on table_name(filedname));

可以在表myisam中針對任意的char、varchar或text域建立全文索引。它是用來對大表中的文字進行索引的。

create table tablename (filedname columntype,fulltext index(filedname)):

alter table tablename add fulltext [indexname] (filedname); 或

create fulltext index indexname on tablename(filedname);

可以使用match()函式匹配域,against()匹配值,來返回全文檢索的結果。如:

查詢單詞master出現的次數:

select * from t2 where match(f1) against('master');

惟一索引除了不容許有重複的記錄以外,其它與普通索引一樣。

create table tablename(fieldname columntype,unique (filedname));

或表已經存在直接增加:

alter table tablename add unique [indexname] (fieldname); 或

create unique index indexname on tablename(fiedldname);

create table u1_test(f1 int,f2 int,unqiue(f1));

可以對域的部分建立索引,如alter table customer add index(name (10));  對錶中的姓氏域建立10個字元的索引。

這個域必須是數字主鍵或數字惟一索引 如:

create table tablename(fieldname int auto_increment, primary key (fieldname));

alter table tablename modify filedname columntype auto_increment;

檢視表列的屬性

show columns from customer;

注意:乙個最重要的特徵是即使記錄被刪除了,mysql的自動增加計數器也會記住最近增加的數,這可以保證新插入的記錄有乙個新的id值並且不和舊的任務記錄衝突。

使用

last_insert_id()

函式來返回最近插入的自動增加值。

select last_insert_id() from customer limit 1;

重置計數器的值:

alter table ai_tets auto_increment=1;

刪除主鍵

alter table tablename drop primary key;

刪除普通、全文索引,只需要指定索引名

alter table tablename drop index idexname;

或drop index indexname on tablename;

顯示索引的名字:

show keys from tablename;

explain select * from aa where name='test';

各列的描述:

table

顯示其餘的行是關於哪張表的

type

這是重要的表,顯示連線使用了何種型別,最好到最差的型別為:const\eq_ref,ref,range,index和all

possible_keys

顯示可能應用在這張表中的索引。如果為空,沒有可能的索引。可以為相關的域從where語句選擇乙個合適的

key實際使用的索引,如果為null,則沒有使用索引。可以select語句使用use index(indexname)來強制使用乙個索引或用ignore index(idexname)來強制忽略索引

key_len

使用的索引的長度。長度越短越好

ref顯示索引的哪一列被使用了,如果可能的話,是乙個常數

rows

mysql認為必須檢查的用來返回請求資料的行數

extra

關於mysql如何解析查詢的額外資訊。如果是using temporary或using filesort,意思是根本不能使用索引,結果是檢查會很慢

extra explain列返回的描述的意義

extra column描述

distinct

一旦找到與行聯合匹配的行,就不再搜尋了

not exists

range checked for each record(index map:#)

沒有找到理想的索引

using filesort

這個需要優化。需要進行額外的排序

using index

列資料是從僅僅使用了索引中的資訊而沒有讀取實際的行的表返回的,

using temporary

需要建立乙個臨時表來儲存結果,需要優化

where used

使用了where 從句來限制哪些行將與下一張表匹配或是返回給使用者。或查詢有問題。

optimize table對錶進行優化和管理。

不同的連線型別

system

表只有一行,這是const連線型別的特殊情況

const

表中的乙個記錄的最大值能夠匹配這個查詢

eq_ref

從前面的表中,對每乙個記錄的聯合都從表中讀取乙個記錄,它在查詢使用了索引為主鍵或惟一鍵的全部時使用

ref這個連線型別只有在查詢使用了不是惟一或主鍵的鍵或是這些型別的部分時發生。對於之前的表的每乙個行聯合,全部記錄都將從表中讀出,這個連線型別嚴重信賴於根據索引匹配的記錄多少,越少越好。

range

使用索引返回乙個範圍中的行,如使用》《或查詢東西時發生的

index

對前面的表中的每乙個記錄聯合進行完全掃瞄比all更好,因為索引一般小於表資料。

all這個連線型別對前面的表中的每乙個記錄聯合進行完全掃瞄,這一般比較糟糕,應該盡量避免。

mysql學習筆記(二)

在具體應用中,需要實現在乙個查詢語句中顯示多張表的資料,這就是所謂的多表資料連線查詢,簡稱連線查詢。1.並 把具有相同字段數目和字段型別的表合併到一起。2.笛卡爾積 這個比較難懂,還是直接上圖 3.內連線 inner join 為了便於使用者操作,mysql專門提供了一種針對資料庫操作的運算 連線。...

Mysql學習筆記(二)

上次學習了mysql安裝,登入和退出等基本操作,以及建立,修改,刪除資料庫的基本指令重點注意mysql基本的語法規範。這篇博文主要介紹mysql幾種資料型別,以及資料表的基本內容。mysql主要有有下面幾種資料型別,包括整型,浮點型,字串型以及日期時間型。具體內容如下表所示 整型資料型別 儲存範圍 ...

MySQL學習筆記(二)

與mysql的零距離接觸 慕課網 第二章筆記 一.資料型別 a 整型 資料型別 儲存範圍 位元組tinyint 有符號 128 127 2 7 2 7 1 無符號 0 255 0 2 8 1 1smallint 有符號 32768 32767 2 15 2 15 1 無符號 0 65535 0到2 ...