標籤:結構 主鍵索引 base char 瞭解 names null rem 方法
一、MySQL當中的索引:
數組當中我們見過索引;它的好處就是能夠快速的通過下標、索引將一個資訊查到;或者說
能夠快速的定位到一個資訊;
1.MySQL中的索引是什嗎?
它是將我們表中具有索引的那個欄位,單獨的儲存到了一張表之中(MyISAM儲存引擎),
當我們重新查詢表中的資料時,如果你搜尋的條件,是具有索引的那個欄位,這會,它
不再遍曆表中的所有資訊了,而是去索引表中,快速的定位到你要搜尋的那條資料,
它有一個指標是指向我們資料表中的源資訊的,由此一來,就可以讓我們能快速的從
一個具有龐大數量級的資料庫中準確的快速的取出某條資訊;
2.MySQL中(MyISAM儲存引擎)儲存表的方式;
1. .frm資料表的結構
2. .MYD 資料表的資料
3. .MYI 資料表中的索引欄位
3.MySQL當中的索引,有哪幾種呢?
索引,在我們定義之後,不用刻意的去使用,當我們在查詢表中具有索引的欄位的時候
索引會自動生效;
1> 普通索引(index)(MUL代表普通索引)
特點:沒有任何限制,當我們定義了普通索引之後,直接搜尋資料即可使用它
① 在建立表的時候,定義一個普通索引
create tabel test1(
id int unsigned not null,
name varchar(32) not null,
sex enum(‘m‘,‘w‘) not null default ‘w‘,
age tinyint not null default 18,
index id(id)索引類型 索引名(欄位名)
);
② 在建表之後,給某個欄位添加普通索引
create index id on test1(id);
create 索引類型 索引名 on 表名(欄位名);
③ 刪除一個普通索引的方法
drop index id on test1;
drop 索引類型 索引名 on 表名;
2> 唯一索引(unique)(UNI代表唯一索引)
特點:具有唯一索引的欄位,它的值只能出現一次,出現重複的值則會報錯!
同時,一個表中可以有多個欄位添加唯一索引
① 在建表時建立唯一索引的方法一
create table test1(
id int unsigned not null,
name varchar(32) not null,
sex enum(‘w‘,‘m‘) not null default ‘m‘,
age tinyint not null default 18,
unique index name(name)//索引類型 索引名(欄位名)
);
② 在建表時建立唯一索引的方法二
create table test1(
id int unsigned not null,
name varchar(32) not null unique,//直接給欄位添加唯一索引
sex enum(‘w‘,‘m‘) not null default ‘w‘,
age tinyint not null default 18
);
③ 在建表之後添加一個唯一索引
create unique index id on test1(id);
create 索引類型 索引名 on 表名(欄位名);
④ 刪除一個表中的唯一索引的方法
drop index id on test1;
drop 索引類型 索引名 on 表名;
3> 主鍵索引(primary key)
特點:它的唯一索引基本上使用方法以及特性一致,唯一的區別是,唯一索引在
一個表中可以多次定義、主鍵索引只能定義一次,而且主鍵索引一般我們
會添加到id欄位當中
① 建表時建立一個主鍵索引的方法
create table test1(
id int unsigned not null auto_increment primary key,//添加主鍵
name varchar(32) not null,
sex enum(‘w‘,‘m‘) not null default ‘m‘,
age tinyint not null default 18
);
② 建表之後,添加一個主鍵索引的方法
1.alter table test1 change id id int unsigned not null auto_increment primary key;
alter table 表名 change 欄位原名 欄位新名 類型 約束條件……;
2.alter table test1 modify id int unsigned not null auto_increment priamry key;
alter table 表名 modify 欄位名 類型 約束條件……;
③ 刪除主鍵索引的方法
因為主鍵索引比較特殊,所以我們在刪除主鍵索引時,必須先來查看錶結構,看錶中
具有主鍵索引的那個欄位,是否同時擁有 auto_increment 這個約束條件,如果有,
先刪除 auto_increment 這個約束條件,之後才能刪除主鍵索引
1.先查看錶結構,查看是否擁有 auto_increment 關鍵字
desc 表名;
2.如果有 auto_increment 關鍵字,則需要先刪除該關鍵字
alter table test1 modify id int unsigned not null;
alter table 表名 modify 欄位名 欄位類型 約束條件;
3.刪除主鍵索引
alter table test1 drop primary key;
alter table 表名 drop 主鍵索引;
4> 全文索引
二、儲存引擎(瞭解):
交易處理:有時,當你執行一個操作的時候,斷電可能會導致一些不必要的麻煩,就比如
電子轉賬操作,如果說此時斷電,所有的事務操作都會有一個復原的效果,恢複到上一次
斷點儲存的位置,避免出現其他的問題
1.MyISAM儲存引擎
對於我們一個表的操作,如果是查詢比較頻繁的表,我們使用MyISAM的儲存引擎來
進行儲存,因為它不支援事務操作
2.InnoDB儲存引擎
因為這種儲存引擎它支援事務的操作,對於一個表的增、刪、改操作比較頻繁,就需要
我們的表支援交易處理,由此一來,就大大降低了表的查詢速度。
3.選擇什麼樣的儲存引擎,關鍵在於你的項目各種功能所需要的表的不同,去選擇一個
更合適的儲存引擎
4.如何來指定一個表的儲存引擎:
create table test1(
id int unsigned not null auto_increment primary key,
name varchar(32) not null unique,
sex enum(‘w‘,‘m‘) not null default ‘m‘
)engine=MyISAM[InnoDB];
5.如何來查看一個表的儲存引擎
show create table 表名;
三、MySQL當中的編碼格式:
1.查看我們能夠設定的編碼格式:
show character set;
2.在MySQL伺服器中的編碼類別型的4個層級
1> 伺服器級
2> 資料庫級
3> 資料表級
4> 資料欄位級
3.編碼層級的一個特性:
它具有一個繼承的特性,當我們設定了伺服器層級的編碼類別型之後,我們在該伺服器
下所建立的所有的資料庫、資料表、資料欄位都是跟隨伺服器層級的編碼類別型了
4.如何來設定一個編碼類別型
1> 設定伺服器層級的編碼類別型
set character_set_server = "utf8";
2> 設定資料庫層級的編碼類別型
① 在建立一個資料庫時設定預設的編碼類別型
create database test default charset="utf8";
create database 資料庫名 預設編碼類別型="utf8";
② 修改一個資料庫的編碼類別型
alter database test default charset="utf8";
alter database 資料庫名 預設編碼類別型="utf8";
3> 設定資料表層級的編碼類別型
① 建立一個資料表時設定預設的編碼類別型
create table test(
id int unsigned not null auto_increment priamry key
)engine=MyISAM default charset="utf8";
② 修改資料表的編碼類別型
alter table test default charset="utf8";
4> 設定資料欄位級的編碼類別型
① 修改一個資料欄位級的編碼
alter table test modify name varchar(32) character set "utf8";
5> 設定DOS命令框的編碼格式
set names utf8;
四、修改表結構
1.添加表欄位:
alter table test1 add name varchar(32) not null unique;//不指定位置,則預設在最後出現
alter table test1 add name varchar(32) not null unique after id;//指定在id後添加name欄位
alter table test1 add name varchar(32) not null unique first;//在表的開頭添加name欄位
2.修改表欄位:
alter table test1 modify 欄位名 欄位類型 約束條件……;
alter table test1 change 原欄位名 新欄位名 欄位類型 約束條件……;
3.刪除表欄位:
alter table test1 drop 欄位名;
4.表的重新命名:
alter table test1 rename test2;
5.刪除多個表的操作:
drop table 表名1,表名2,表名3……;
【資料庫】MySQL資料庫(三)