MySQL資料庫命令下的常用操作方法示範與介紹~,mysql資料庫
資料庫mysql
安裝mysql請移步百度教程,此文章不再詳述。
啟動資料庫
cmd下 mysql -u root -p自己設定的密碼
命令
show databases;看mysql資料庫管理系統的資料庫列表
use 資料庫名;選擇要操作的mysql資料庫,使用該命令所有mysql命令都只針對該資料庫
show columns from 資料表; 顯示資料表的屬性,屬性類型,主鍵資訊,是否為Null(空),預設值等其他資訊
create database testdb charset "utf8"; 建立一個叫testdb的資料庫,且讓其支援中文。
drop database testdb;刪除資料庫testdb
show index from 資料表; 顯示資料表的詳細索 引資訊,包括PRIMARY(主鍵)
建立一個資料表
create table table_name(column_name【列名】 column_type【列類型】); #資料庫中分號很重要
例如:
mysql> create table student(
-> id int auto_increment,#【auto_increment表示每插入一條資料自增1】
-> name char(32) not null,
-> age int not null,
-> register_date date not null,
-> primary key(id));
如此便建立了一個student的資料表
MySQL資料表中插入新資料
insert into table_name(field1,field2,...fieldN)values(value1,value2,...valueN)
使用上面建立的表例子
mysql> insert into student(name,age,register_date)values("zhangyang",3,"2018-1-1")
mysql> insert into student(name,age,register_date)values('zhangyang',3,'2018-5-5')# 單引號和雙引號也無區別
如此便於資料表中插入了資料
mysql查詢資料
select命令可以讀取一條或者多條資料
*號代替其他欄位
select * from student;代表看資料表所有欄位的詳情
運行結果如下
+----+-----------+-----+---------------+
| id | name | age | register_date |
+----+-----------+-----+---------------+
| 1 | zhangyang | 3 | 2018-01-01 |
| 2 | zhangyang | 3 | 2018-05-05 |
+----+-----------+-----+---------------+
一般文法:
select column_name,column_name from table_name;
可以使用offset n來設定位移量,說白了就是從第n+1條資料讀取,必須配合limit使用,預設為0
limit n代表唯讀取n條資料的意思。如select * from student limit 2 offset 3;#offset 必須在limit後面
where子句:
可以使用where語句來包含任何條件:
如:select * from student where id>3;輸出是第三條以後的資料
select * from student where id=4;輸出第四條資料
也可以多條件查詢:
select * from student where id>3 and age>5;查詢並輸出第三條後年齡大於5的資料。
還能模糊查詢:
select * from student where register_date like '2018-05%'; 記得要用like和%
mysql修改資料
使用update語句
文法:update table_name set field1 = new-value1,field2 = new-value2 where clause;
例如:
update student set name='韓梅梅' where name='zhangyang';#只改一個列名中的東西,還可以改多個
mysql刪除資料
delete語句
delete from table_name where clause; 設定刪除條件後,精準刪除資料
例如:
delete from student where name = '韓梅梅'; 名字為韓梅梅的資料全部刪除。
資料排序:
select from table_name order by fieldn;
select from table_name order by fieldn desc;
按第n列來排序,預設為升序,加一個desc就是降序。
group by 用來分組統計
直接上例子
select name,count(*) from student group by name;
此時會用name來計數
mysql> select * from student;#之前使用過delete 刪了前面兩條,id數字增加到哪裡還是哪裡
+----+------+-----+---------------+
| id | name | age | register_date |
+----+------+-----+---------------+
| 3 | 陸毅 | 31 | 2018-11-05 |
| 4 | x毅 | 31 | 2048-01-05 |
| 5 | x毅 | 31 | 2017-05-05 |
+----+------+-----+---------------+
3 rows in set (0.00 sec)
mysql> select name,count(*) from student group by name;
+------+----------+
| name | count(*) | #count(*)也能賦值出去換一個新的名稱,也是使用as **。
+------+----------+
| x毅 | 2 |
| 陸毅 | 1 |
+------+----------+
2 rows in set (0.07 sec)
-----------------------我是分割符------------------------------------------
mysql> select name,count(*) as count_name from student group by name;
+------+------------+
| name | count_name |
+------+------------+
| x毅 | 2 |
| 陸毅 | 1 |
+------+------------+
2 rows in set (0.27 sec)
注意 前面的name 是顯示name列,後面的name 是顯示[by]name計數的結果!
group by的合計功能:
mysql> select name,sum(age) from student group by name;
+--------+----------+
| name | sum(age) |
+--------+----------+
| x毅 | 62 |
| 張嘉譯 | 45 |
| 草清華 | 15 |
| 陸毅 | 31 |
+--------+----------+
4 rows in set (0.04 sec)
最後統計所有人年齡相加(這裡可以是別的東西計數相加,我只是舉例子),只需要在分號前加入with rollup既可
mysql> select name,sum(age)as age_count from student group by name with rollup;
+--------+-----------+
| name | age_count |
+--------+-----------+
| x毅 | 62 |
| 張嘉譯 | 45 |
| 草清華 | 15 |
| 陸毅 | 31 |
| NULL | 153 |
+--------+-----------+
5 rows in set (0.00 sec)
不想要NULL的話,也能使用coalesce文法來給NULL換成想要的名字
mysql> select coalesce(name,'合計'),sum(age)as age_count from student group by name with rollup;
+-----------------------+-----------+
| coalesce(name,'合計') | age_count |
+-----------------------+-----------+
| x毅 | 62 |
| 張嘉譯 | 45 |
| 草清華 | 15 |
| 陸毅 | 31 |
| 合計 | 153 |
+-----------------------+-----------+
5 rows in set (0.00 sec)
修改資料表本身而不是資料,比如修改name age這個表頭
mysql alter語句
alter table student add sex enum('M','W') not null;增加sex這個欄位,欄位類型設定為enum('M','W')
alter table student drop age;從student中刪除age整個欄位(age對應的整列)
修改欄位類型及名稱 需要用change子句
mysql> alter table student change sex gender char(32) default 'X'; #change後接舊欄位名,馬上跟新欄位名,然後新欄位類型既可,預設值看情況是否需要設定。
Query OK, 5 rows affected (0.84 sec)
Records: 5 Duplicates: 0 Warnings: 0
查詢兩個表之間的交集並集差集
innerjoin 交集 left join 差集(左對右) right join也是差集(右對左)
full join 並集:mysql並不直接支援full join,可以聯合left和right一起輸出就成。
文法如下:
create table A (a int not null);
create table B (b int not null);
insert into A (a)values(i);#這裡簡寫,i為1-5
insert into B (b)values(i);#這裡簡寫,i為3-7
select * from A inner join B on A.a=B.b;此時輸出A與B的交集
+---+---+
| a | b |
+---+---+
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
+---+---+
select * from A left join B on A.a=B.b;此時輸出 B 於 A中的差集,先顯示交集,後面出現的是差集
+---+------+
| a | b |
+---+------+
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
| 1 | NULL |
| 2 | NULL |
+---+------+
根據字面意思不難看出 right join其實就是把結果翻過來顯示。
並集由於mysql不直接支援,可以使用union語句把left join 和right join放一起輸出摺合顯示:
select * from A left join B on A.a=B.b union select * from A right join B on A.a=B.b;
mysql事務:
其實就是一開始輸入begin;#開始事務,
然後各種騷操作,如果程式這個時候蹦了,那麼無論是插入資料還是修改或者幹啥,不會記錄下來,但是如果那個id序號是自增的,這個序號其實還是會變的,就如操作後再輸入rollback;的效果是一樣的
如果輸入了commit; 那麼操作過的資料都會保留下來
mysql索引
如果說加了索引的mysql是飛機的話,那麼沒加索引的mysql就是一輛共用單車~,速度的差異很大,當然資料量很小的話體現不出來,但是資料量大了,就有非常大的區別,但索引也有缺點,表的查詢速度提升了,但是如果對錶進行update,insert,和delete等操作,mysql不僅要儲存資料還要儲存索引,建立索引會佔用磁碟空間的索引檔案。
建立索引
create index index_name on table_name(x(length));#index_name索引名(看自己喜歡寫什麼),table_name要索引的表,x要成為索引的欄位,length該欄位的長度,不超過欄位本身的長度,一般欄位多少,他就填多少
、、、
注意:主鍵自動就是索引。
、、、
刪除索引:
drop index index_name on table_name;#刪除table_name表中的index_name索引。
關於python如何串連資料庫以及各種操作正在整理中。。。