mysql常見操作
說明:本文旨在記錄常見資料庫操作
資料庫相關
-
增加資料庫;
create database 資料庫的名字 charset=字元編碼;
create database testdb charset=utf8;
-
查詢資料庫
show databases;
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mock_db | | mysql | | performance_schema | | sys | | testdb | +--------------------+ 6 rows in set (0.01 sec)
-
選擇資料庫
use testdb;
mysql> use testdb; Database changed mysql>
-
檢視正在使用的資料庫
select database();
mysql> select database(); +------------+ | database() | +------------+ | testdb | +------------+ 1 row in set (0.00 sec) mysql>
-
檢視使用的資料庫的所有表
show tables;
mysql> show tables; Empty set (0.00 sec) mysql>
-
刪除資料庫
drop database 資料庫名稱;
mysql> drop database testdb; Query OK, 0 rows affected (0.00 sec) mysql>
-
資料庫備份
這裡用到的是終端的一個重定向
>
的命令Mac
下資料庫的資料儲存的路徑/usr/local/mysql/data
進入上面目錄
sudo cd /usr/local/mysql/data
使用
mysqldump
命令將資料備份到桌面下~/Desktop/kkk.sql
➜ ~ mysqldump -u root -p testdb > ~/Desktop/kkk.sql; Enter password:
資料密碼就可以在桌面看到kkk.sql了
我們先把舊的
testdb
刪除掉drop database testdb;
檢視一下
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mock_db | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql>
我們來建立一個空的名為
testdb
的資料庫create database testdb2 charset=utf8;
我們檢視下
testdb
```
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mock_db |
| mysql |
| performance_schema |
| sys |
| testdb |
+--------------------+
6 rows in set (0.00 sec)
mysql> use testdb;
Database changed
mysql> show tables;
Empty set (0.00 sec)
```
恢復資料
```
➜ ~ mysql -u root -p testdb < ~/Desktop/kkk.sql;
Enter password:
```
再查一次
```
mysql> show tables;
+------------------+
| Tables_in_testdb |
+------------------+
| person |
+------------------+
1 row in set (0.00 sec)
mysql> select *from person;
+----+----------------+------+
| id | name | city |
+----+----------------+------+
| 1 | zhangsanfeng | |
| 3 | zhaomin | |
| 4 | xiaozhao | |
| 5 | jinnmaoshiwang | |
| 6 | baimei | |
+----+----------------+------+
5 rows in set (0.00 sec)
mysql>
```
表相關
-
增加表
create table 表名(列及型別);
mysql> create table person( > id int auto_increment primary key, > name varchar(20) not null); Query OK, 0 rows affected (0.02 sec)
通過
show tables
檢視mysql> show tables; +------------------+ | Tables_in_testdb | +------------------+ | person | +------------------+ 1 row in set (0.00 sec) mysql>
-
檢視錶屬性資訊
desc 表名稱;
mysql> desc person; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(20) | NO | | NULL | | +-------+-------------+------+-----+---------+----------------+ 2 rows in set (0.00 sec) mysql>
-
刪除表
drop table 表名;
現在有2張表
mysql> show tables; +------------------+ | Tables_in_testdb | +------------------+ | person | | test2 | +------------------+ 2 rows in set (0.00 sec)
刪除
test2
表mysql> drop table test2; Query OK, 0 rows affected (0.00 sec) mysql> show tables; +------------------+ | Tables_in_testdb | +------------------+ | person | +------------------+ 1 row in set (0.00 sec)
-
修改表
alter table 表名 add | change | drop 列名 型別;
比如:
將
person
表的gender 改成gender2;alter table person change gender gender2 bit;
給person增加
city
列alter table person add city varchar(20);
刪除person的gender2
alter table person drop gender2;
資料相關
-
查詢表資料
select * from 表名;
-
新增資料
(1). 全列增加單個:
insert into 表名 values(...);
mysql> insert into person values(0,'zhangsanfeng'); Query OK, 1 row affected (0.01 sec) mysql>
查詢結果
mysql> select * from person; +----+--------------+ | id | name | +----+--------------+ | 1 | zhangsanfeng | +----+--------------+ 1 row in set (0.00 sec) mysql>
(2). 全列增加多個:
insert into 表名 values(...),(...);
mysql> insert into person values(0,'zhangwuji'),(0,'zhaomin'); Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0
檢視一下
mysql> select * from person; +----+--------------+ | id | name | +----+--------------+ | 1 | zhangsanfeng | | 2 | zhangwuji | | 3 | zhaomin | +----+--------------+ 3 rows in set (0.00 sec) mysql>
(3). 單列增加單個:
insert into 表名(列名) values(...);
mysql> insert into person(name) values('xiaozhao'); Query OK, 1 row affected (0.01 sec)
檢視
mysql> select * from person; +----+--------------+ | id | name | +----+--------------+ | 1 | zhangsanfeng | | 2 | zhangwuji | | 3 | zhaomin | | 4 | xiaozhao | +----+--------------+ 4 rows in set (0.00 sec) mysql>
(4). 單列增加多個:
insert into 表名(列名) values(...),values(...);
mysql> insert into person(name) values('jinnmaoshiwang'),('zhangcuishan'); Query OK, 2 rows affected (0.01 sec) Records: 2 Duplicates: 0 Warnings: 0
檢視
```
mysql> select * from person;
+----+----------------+
| id | name |
+----+----------------+
| 1 | zhangsanfeng |
| 2 | zhangwuji |
| 3 | zhaomin |
| 4 | xiaozhao |
| 5 | jinnmaoshiwang |
| 6 | zhangcuishan |
+----+----------------+
6 rows in set (0.00 sec)
mysql>
```
-
更新資料
update 表名 set 列名=值 where 條件
mysql> update person set name='baimei' where id=6; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0
檢視
mysql> select *from person; +----+----------------+ | id | name | +----+----------------+ | 1 | zhangsanfeng | | 2 | zhangwuji | | 3 | zhaomin | | 4 | xiaozhao | | 5 | jinnmaoshiwang | | 6 | baimei | +----+----------------+ 6 rows in set (0.00 sec) mysql>
-
刪除資料
delete from 表名 where 條件
在person表中刪除id為2的資料
mysql> delete from person where id=2; Query OK, 1 row affected (0.01 sec)
檢視
mysql> select *from person; +----+----------------+ | id | name | +----+----------------+ | 1 | zhangsanfeng | | 3 | zhaomin | | 4 | xiaozhao | | 5 | jinnmaoshiwang | | 6 | baimei | +----+----------------+ 5 rows in set (0.00 sec) mysql>
常見資料查詢操作
1. 模糊查詢
- like
- %表示任意多個,也即是可有可無
- _表示一個任意字元,至少一個
person
表內容
mysql> select * from person;
+----+----------------+----------+
| id | name | city |
+----+----------------+----------+
| 1 | zhangsanfeng | Shanghai |
| 3 | zhaomin | 北京 |
| 4 | xiaozhao | |
| 5 | jinnmaoshiwang | 北京 |
| 6 | baimei | |
+----+----------------+----------+
5 rows in set (0.00 sec)
mysql>
查詢城市是北京的人的名字
mysql> select name from person where city like '北京';
+----------------+
| name |
+----------------+
| zhaomin |
| jinnmaoshiwang |
+----------------+
2 rows in set (0.01 sec)
mysql>
查詢以北
開頭的人的名字,使用%
和_
都可以
mysql> select name from person where city like '北%';
+----------------+
| name |
+----------------+
| zhaomin |
| jinnmaoshiwang |
+----------------+
2 rows in set (0.00 sec)
mysql> select name from person where city like '北_';
+----------------+
| name |
+----------------+
| zhaomin |
| jinnmaoshiwang |
+----------------+
2 rows in set (0.00 sec)
修改一下zhaomin
的city區分一下%
和_
;
mysql> update person set city='北' where id=3;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from person;
+----+----------------+----------+
| id | name | city |
+----+----------------+----------+
| 1 | zhangsanfeng | Shanghai |
| 3 | zhaomin | 北 |
| 4 | xiaozhao | |
| 5 | jinnmaoshiwang | 北京 |
| 6 | baimei | |
+----+----------------+----------+
5 rows in set (0.00 sec)
再執行下上面的
mysql> select name from person where city like '北%';
+----------------+
| name |
+----------------+
| zhaomin |
| jinnmaoshiwang |
+----------------+
2 rows in set (0.00 sec)
mysql>
mysql> select name from person where city like '北_';
+----------------+
| name |
+----------------+
| jinnmaoshiwang |
+----------------+
1 row in set (0.00 sec)
2. 聚合查詢
簡寫 | 含義 |
---|---|
min | 最小值 |
max | 最大值 |
avg | 平均值 |
sum | 求和 |
count | 數量 |
mysql> select *from person;
+----+----------------+--------+
| id | name | city |
+----+----------------+--------+
| 1 | zhangsanfeng | 上海 |
| 3 | zhaomin | 北 |
| 4 | xiaozhao | 上海 |
| 5 | jinnmaoshiwang | 北京 |
| 6 | baimei | 上海 |
+----+----------------+--------+
5 rows in set (0.00 sec)
求city是上海的人的總和
mysql> select count(*) from person where city='上海';
+----------+
| count(*) |
+----------+
| 3 |
+----------+
1 row in set (0.01 sec)
求city是上海的人中id最大的的人
mysql> select max(id) from person where city='上海';
+---------+
| max(id) |
+---------+
| 6 |
+---------+
1 row in set (0.01 sec)
其他的和上面的類似
3. 分組查詢
查詢每個city的人的總數
mysql> select city ,count(*) from person group by city;
+--------+----------+
| city | count(*) |
+--------+----------+
| 上海 | 3 |
| 北 | 1 |
| 北京 | 1 |
+--------+----------+
對結果進行排序,使用order by
將上面的結果按照總數進行,升序排序
mysql> select city ,count(*) as 總數 from person group by city order by 總數 asc;
+--------+--------+
| city | 總數 |
+--------+--------+
| 北 | 1 |
| 北京 | 1 |
| 上海 | 3 |
+--------+--------+
3 rows in set (0.00 sec)
上面的as
是取別名
單獨看上海的有多少人,使用having
mysql> select city ,count(*) from person group by city having city='上海';
+--------+----------+
| city | count(*) |
+--------+----------+
| 上海 | 3 |
+--------+----------+
1 row in set (0.00 sec)
分頁查詢,使用limit
select * from 表名 limit start,count
start
,表示從第幾個元素開始;count
,查詢幾個
mysql> select * from person;
+----+----------------+--------+
| id | name | city |
+----+----------------+--------+
| 1 | zhangsanfeng | 上海 |
| 3 | zhaomin | 北 |
| 4 | xiaozhao | 上海 |
| 5 | jinnmaoshiwang | 北京 |
| 6 | baimei | 上海 |
| 7 | zhangsan1 | 海南 |
| 8 | zhangsan2 | 海南 |
| 9 | zhangsan3 | 海南 |
| 10 | zhangsan4 | 海南 |
| 11 | zhangsan5 | 海南 |
| 12 | zhangsan6 | 海南 |
| 13 | zhangsan7 | 海南 |
| 14 | zhangsan8 | 海南 |
+----+----------------+--------+
13 rows in set (0.00 sec)
需求:分頁查詢,每頁2個,從0開始;
第一頁
mysql> select * from person limit 0,3;
+----+--------------+--------+
| id | name | city |
+----+--------------+--------+
| 1 | zhangsanfeng | 上海 |
| 3 | zhaomin | 北 |
| 4 | xiaozhao | 上海 |
+----+--------------+--------+
3 rows in set (0.00 sec)
第二頁
mysql> select * from person limit 3,3;
+----+----------------+--------+
| id | name | city |
+----+----------------+--------+
| 5 | jinnmaoshiwang | 北京 |
| 6 | baimei | 上海 |
| 7 | zhangsan1 | 海南 |
+----+----------------+--------+
3 rows in set (0.00 sec)
第三頁
mysql> select * from person limit 6,3;
+----+-----------+--------+
| id | name | city |
+----+-----------+--------+
| 8 | zhangsan2 | 海南 |
| 9 | zhangsan3 | 海南 |
| 10 | zhangsan4 | 海南 |
+----+-----------+--------+
3 rows in set (0.00 sec)
having
和where
區別
where
是對原始資料進行篩選;having
是對篩選的結果進行篩選。
該文會持續補充。。。
相關文章
- Centos 常見操作CentOS
- Mysql系列第四講 DDL常見操作彙總MySql
- 常見的DOM操作
- JS常見的字串操作JS字串
- Linux 常見高危操作Linux
- UE常見快捷鍵操作
- Oracle redo log 常見操作Oracle Redo
- adb常見操作命令
- 常見的查詢操作
- MySQL 常見錯誤MySql
- Mysql 常見面試題MySql面試題
- MySQL常見索引概念MySql索引
- mysql常見問題MySql
- MySQL 常見問題MySql
- Mysql:常見問題MySql
- 【MySQL】常見的mysql 程式stateMySql
- JavaScript 陣列常見操作(一)JavaScript陣列
- JavaScript 陣列常見操作 (二)JavaScript陣列
- Linux 下 常見操作命令Linux
- selenium定位與常見操作
- Linux常見操作小結Linux
- 華為交換機常見QinQ操作
- 表空間常見的操作
- linux常見的操作(轉)Linux
- Qt QTreeView 常見節點操作QTView
- Java Map和List常見操作Java
- mysql面試常見題目MySql面試
- mysql replication常見錯誤MySql
- Ceph常規操作及常見問題梳理
- 華為交換機常見STP/RSTP操作
- Python常見資料框操作①Python
- mysql常見資料型別MySql資料型別
- MySQL 安裝常見錯誤MySql
- mysql8 常見錯誤MySql
- mysql常見問題總結MySql
- MySQL 之索引常見內容MySql索引
- MySQL Replication常見錯誤整理MySql
- MYSQL常見出錯程式碼MySql