Python全棧 MySQL 資料庫 (索引、資料匯入、匯出)

來源:互聯網
上載者:User

標籤:樣本   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.預設匯出到預設搜尋路徑    檔案許可權:       rwxrw-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 資料庫 (索引、資料匯入、匯出)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.