1. 程式人生 > >【數據庫】MySQL數據庫(三)

【數據庫】MySQL數據庫(三)

沒有 特殊 進行 主鍵索引 rst ble 刪除 索引 可能

一、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數據庫(三)