MySQL 索引、數據導入、數據導出
阿新 • • 發佈:2018-08-02
優點 查詢 inf load 文件編碼 選擇 進行 nes tro
5、再執行查詢語句
select name from t1 where name="lucy88888";
6、查看執行時間
show profiles;
5、索引
1、普通索引(index)
1、使用規則
1、可設置多個字段
2、字段值無約束
3、key標誌 :MUL
2、創建index
1、創建表時
create table 表名(...
index(字段名),index(字段名));
2、已有表
create index 索引名 on 表名(字段名);
create index name on t3(name);
3、查看索引
1、desc 表名; --> KEY標誌為:MUL
2、show index from 表名\G;
4、刪除索引
drop index 索引名 on 表名;
2、唯一索引(unique)
1、使用規則
1、可設置多個字段
2、約束 :字段值不允許重復,但可為 NULL
3、KEY標誌 :UNI
2、創建
1、創建表時創建
unique(字段名),
unique(字段名)
2、已有表
create unique index 索引名 on 表名(字段名);
3、查看、刪除 同 普通索引
3、主鍵索引(primary key)
自增長屬性(auto_increment,配合主鍵一起使用)
1、使用規則
1、只能有一個主鍵字段
2、約束 :不允許重復,且不能為NULL
3、KEY標誌 :PRI
4、通常設置記錄編號字段id,能唯一鎖定一條記錄
2、創建
1、創建表時
(id int primary key auto_increment,
)auto_increment=10000;##設置自增長起始值
已有表添加自增長屬性:
alter table 表名 modify id int auto_increment;
已有表重新指定起始值:
alter table 表名 auto_increment=20000;
2、已有表
alter table 表名 add primary key(id);
3、刪除
1、刪除自增長屬性(modify)
alter table 表名 modify id int;
2、刪除主鍵索引
alter table 表名 drop primary key;
4、外鍵索引
4、數據導入
1、作用 :把文件系統的內容導入到數據庫中
2、語法
load data infile "/var/lib/mysql-files/文件名"
into table 表名
fields terminated by "分隔符"
lines terminated by "\n";
3、將scoretable.csv文件導入到數據庫的表中
1、在數據庫中創建對應的表
create table scoretab(
id int,
name varchar(15),
score float(5,2),
number bigint,
class char(7)
);
2、把文件拷貝到數據庫的默認搜索路徑中
1、查看默認搜索路徑
show variables like "secure_file_priv";
/var/lib/mysql-files/
2、拷貝文件
sudo cp ~/scoretable.csv /var/lib/mysql-files/
3、執行數據導入語句
load data infile "/var/lib/mysql-files/scoretable.csv"
into table scoretab
fields terminated by ","
lines terminated by "\n";
4、文件權限
rwxrw-rw- 1 tarena tarena scoretable.csv
所有者 所屬組
rwx : tarena用戶
rw- : 同組其他用戶
rw- : 其他組的其他用戶(mysql用戶)
r -> 4
w -> 2
x -> 1
chmod 644 文件名 rw-r--r--
5、Excel表格如何轉化為CSV文件
1、打開Excel文件 -> 另存為 -> CSV(逗號分隔)
6、更改文件編碼格式
1、用記事本/編輯器 打開,文件->另存為->選擇編碼
5、數據導出
1、作用
將數據庫中表的記錄導出到系統文件裏
2、語法格式
select ... from 表名
into outfile "/var/lib/mysql-files/文件名"
fields terminated by "分隔符"
lines terminated by "\n";
3、把MOSHOU庫下的sanguo表英雄的姓名、攻擊值、國家導出來,sanguo.txt
select name,gongji,country from MOSHOU.sanguo
into outfile "/var/lib/mysql-files/sanguo.txt"
fields terminated by " "
lines terminated by "\n";
$ sudo -i
$ cd /var/lib/mysql-files/
$ ls
$ cat sanguo.txt
4、將mysql庫下的user表中 user、host兩個字段的值導出到 user.txt
select user,host from mysql.user
into outfile "/var/lib/mysql-files/user.txt" fields terminated by " "
lines terminated by "\n";
索引
1、定義:對數據庫表的一列或多列的值進行排序的一種結構(Btree方式)
2、優點:加快數據檢索速度
3、缺點:占用物理存儲空間;當對表中數據更新時,索引需要動態維護,降低數據維護速度
4、索引示例
1、開啟運行時間檢測 :set profiling=1;
2、執行查詢語句
select name from t1 where name="lucy99999";
3、查看執行時間
show profiles;
4、在name字段創建索引
create index name on t1(name);
5、再執行查詢語句
select name from t1 where name="lucy88888";
6、查看執行時間
show profiles;
5、索引
1、普通索引(index)
1、使用規則
1、可設置多個字段
2、字段值無約束
3、key標誌 :MUL
2、創建index
1、創建表時
create table 表名(...
index(字段名),index(字段名));
2、已有表
create index 索引名 on 表名(字段名);
create index name on t3(name);
3、查看索引
1、desc 表名; --> KEY標誌為:MUL
2、show index from 表名\G;
4、刪除索引
drop index 索引名 on 表名;
2、唯一索引(unique)
1、使用規則
1、可設置多個字段
2、約束 :字段值不允許重復,但可為 NULL
3、KEY標誌 :UNI
2、創建
1、創建表時創建
unique(字段名),
unique(字段名)
2、已有表
create unique index 索引名 on 表名(字段名);
3、查看、刪除 同 普通索引
3、主鍵索引(primary key)
自增長屬性(auto_increment,配合主鍵一起使用)
1、使用規則
1、只能有一個主鍵字段
2、約束 :不允許重復,且不能為NULL
3、KEY標誌 :PRI
4、通常設置記錄編號字段id,能唯一鎖定一條記錄
2、創建
1、創建表時
(id int primary key auto_increment,
)auto_increment=10000;##設置自增長起始值
已有表添加自增長屬性:
alter table 表名 modify id int auto_increment;
已有表重新指定起始值:
alter table 表名 auto_increment=20000;
2、已有表
alter table 表名 add primary key(id);
3、刪除
1、刪除自增長屬性(modify)
alter table 表名 modify id int;
2、刪除主鍵索引
alter table 表名 drop primary key;
4、外鍵索引
4、數據導入
1、作用 :把文件系統的內容導入到數據庫中
2、語法
load data infile "/var/lib/mysql-files/文件名"
into table 表名
fields terminated by "分隔符"
lines terminated by "\n";
3、將scoretable.csv文件導入到數據庫的表中
1、在數據庫中創建對應的表
create table scoretab(
id int,
name varchar(15),
score float(5,2),
number bigint,
class char(7)
);
2、把文件拷貝到數據庫的默認搜索路徑中
1、查看默認搜索路徑
show variables like "secure_file_priv";
/var/lib/mysql-files/
2、拷貝文件
sudo cp ~/scoretable.csv /var/lib/mysql-files/
3、執行數據導入語句
load data infile "/var/lib/mysql-files/scoretable.csv"
into table scoretab
fields terminated by ","
lines terminated by "\n";
4、文件權限
rwxrw-rw- 1 tarena tarena scoretable.csv
所有者 所屬組
rwx : tarena用戶
rw- : 同組其他用戶
rw- : 其他組的其他用戶(mysql用戶)
r -> 4
w -> 2
x -> 1
chmod 644 文件名 rw-r--r--
5、Excel表格如何轉化為CSV文件
1、打開Excel文件 -> 另存為 -> CSV(逗號分隔)
6、更改文件編碼格式
1、用記事本/編輯器 打開,文件->另存為->選擇編碼
5、數據導出
1、作用
將數據庫中表的記錄導出到系統文件裏
2、語法格式
select ... from 表名
into outfile "/var/lib/mysql-files/文件名"
fields terminated by "分隔符"
lines terminated by "\n";
3、把MOSHOU庫下的sanguo表英雄的姓名、攻擊值、國家導出來,sanguo.txt
select name,gongji,country from MOSHOU.sanguo
into outfile "/var/lib/mysql-files/sanguo.txt"
fields terminated by " "
lines terminated by "\n";
$ sudo -i
$ cd /var/lib/mysql-files/
$ ls
$ cat sanguo.txt
4、將mysql庫下的user表中 user、host兩個字段的值導出到 user.txt
select user,host from mysql.user
into outfile "/var/lib/mysql-files/user.txt" fields terminated by " "
lines terminated by "\n";
MySQL 索引、數據導入、數據導出