標籤:樣本 distinct 注意 所有者 結構 根據 已耗用時間 mysq 使用者
ParisGabriel 每天堅持手寫 一天一篇 決定堅持幾年 為了夢想為了信仰 開局一張圖
表欄位重新命名(change)
alter table 表名 change 原名 新名 資料類型;
SQL查詢
執行順序: 3. select ...彙總函式 from 表名 1. where ... 2. group by... 4. having... 5. order by... 6. limit...
group by 語句
作用:給查詢結果進行
分組
注意: 1.
group by之後的
欄位必須要為
select之後的
欄位名 2.
如果select之後的欄位和group by 之後的
欄位不一致,則
必須對 欄位
進行彙總處理(彙總函式)
having 語句
作用:對查詢結果
進一步篩選
注意: 1.
having語句通常和
group by聯合使用,
過濾由
group by語句
返回的記錄集 2.
where只能操作表中
實際存在的
欄位,
having by可操作由彙總函式產生的顯示列
distinct
作用:不顯示欄位
重複值
注意: 1.distinct和from之間
所有欄位都相同
才會去重 2.distinct
不能對任何欄位做彙總
處理
運算子:
+ - * / %
## sudo apt-get install python3-pip 安裝pip3
## sudo pip3 install pymysql 安裝mysql
約束:
1.作用:
保證資料的
完整性、
一致性、
有效性
2.約束分類
1.預設約束(default)
插入欄位時
不給該欄位
賦值 則
使用預設值
2.非空約束(not NULL)
不允許該欄位值有NULL記錄
sex enum(“M”,"F","S") not null defaulf "S"
索引
定義:
對資料庫表色一列或多列的
值進行排序的一種
結構
(Btree方式)
優點:
加快資料的
檢索速度
缺點: 1.需要
佔用實體儲存體空間 2.當對錶中資料更新時,索引需要
動態維護。
降低 資料
維護速度
佔用系統資源
已耗用時間檢測:
開啟:set profiling=1;
關閉:set profiling=0;
查詢MySQL變數:show variables like profiling;
查詢執行記錄:show profilings;
欄位建立索引:
create index name on t1(欄位名);
索引的分類:
1.普通索引(index)
使用規則: 1.可設定多個欄位 2.欄位值
無約束 3.key標誌:
MUL
建立index
建立表時建立 create table(.... ... index(欄位名), index(欄位名2)...)
已有表添加index
create index 索引名 on 表名(欄位名);
查看索引:
1.desc 表名; key:MUL
2.show index from 表名
3.show index from\G;
刪除索引:
drop index 索引名 on 表名;
2.唯一索引(unique)
使用規則: 1.可以設定多個欄位
2.約束:欄位值不允許重複,但
可以為NULL 3.key標誌:
UNI
建立unique: 1.建立表時建立
unique(欄位名),.. 2.已有表
create unique index 索引名 on 表名(欄位名); 查看、刪除
和普通索引一致
3.主鍵索引(primary key)
自增屬性(auto_increment,
配合主鍵一起
使用)
使用規則: 1.
只能有一個主鍵欄位 2.
約束:不允許重複,且
不能為NULL 3.key標誌:
PRI 4.
通常設定記錄編號欄位
id,能
唯一鎖定一條
記錄
建立primary key 建立表時: 1.
id int primary key auto_increment, 2.
起始值:表()
auto_inctement=10000; 已有表:
alter table 表名 add primary key(id);
添加:alter table 表名 modify id int auto_inctement;
刪除:
1.刪除自增屬性(modify)
alter table 表名 modify id int;
2.刪除主鍵索引
alter table 表名 drop primary key; 4.外鍵索引........演算法全是
btree 節省時間都一樣 不同的是約束不同 這裡btree 演算法 有人說btree就是btree 不是二叉樹 但是我覺得就是二叉樹 沒什麼區別根據資料量的大小 提升速度 快能達到幾百倍的提速
資料匯入:
作用:
把檔案系統的內容匯入到資料庫
文法:
load data infile “檔案名稱”
into table 表名
fields terminated by “分隔字元”
lines terminated by “\n”;
步驟: 1.
資料庫中
建立對應的
表 2.
把檔案拷貝到資料庫的
預設搜尋路徑中
1.查看預設路徑
show variables like “secure_file_priv”;
/var/lib/mysql-files/
2.拷貝檔案
sudo cp ~/scoretable.csv /var/lib/mysql-files/
3.把表匯入到資料庫
資料匯出:
作用:
資料庫中表的記錄匯出到系統檔案裡
文法:
select ... from 表名
into outfile “/var/lib/mysql-files/檔案名稱”
fields terminated by “分隔字元”
lines terminated by “\n”;
步驟:
1.直接執行匯出命令
2.自動建立檔案
3.預設匯出到預設搜尋路徑
檔案許可權:
rwx
rw-
rw- 1 tarena tarena 所有者 所屬組 rwx:tarena使用者 rw-:同組其他使用者 rw-:其他組的使用者(mysql) r: 4 w: 2 x: 1 最高許可權:7
查看許可權:ls -l 檔案名稱 修改檔案許可權:
chmod 644 檔案名稱
Excel表格如何
轉化為
CSV檔案
開啟Excel檔案 -> 另存新檔 -> CSV(逗號分隔)變更檔
編碼格式
用記事本/編輯器 開啟,檔案->另存新檔->選擇編碼
匯入樣本:
將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";
匯出樣本:
把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"; 將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";查詢 $ sudo -i $ cd /var/lib/mysql-files/ $ ls $ cat sanguo.txt
Python全棧 MySQL 資料庫 (索引、資料匯入、匯出)