MySQL的常用簡單操作
寫在前面 不要輕易刪除資料庫檔案,否則解除安裝重灌很麻煩
mysql的安裝、初始化以及環境變數配置等不再重複,本文用以記錄對mysql的常用操作。
1 . 連線資料庫,開啟cmd進入命令列下,在進入到資料庫安裝的目錄的bin目錄下,輸入mysql -uroot -p
回車,輸入密碼,再回車即可連線到資料庫。
2. 檢視都有哪些資料庫,輸入命令
show databases;
顯示出資料庫都有哪些,第一次使用的時候只有如下四個
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
3.建立資料庫。
create database 資料庫名字;
注意:建立資料庫時,資料庫名字你不帶分號。再次執行show databases
會顯示出剛建立的資料庫。
4. 刪除資料庫。執行 drop database 資料庫名稱
即可刪除此資料庫。
5. 選擇資料庫。即進入到特定的某個資料庫裡面進行操作,只有進入到了資料庫中才能完成資料庫中表的建立刪除等操作。執行use 資料庫名稱
mysql> use test01;
Database changed
6.對資料庫進行操作,資料庫是由許多表組成的,所以對資料庫的操作可以認為是對錶的操作。
建立資料表。建立資料表需要:表明、表字段名、定義每個表字段,
如果你不想欄位為 null 可以設定欄位的屬性為not null, 在操作資料庫時如果輸入該欄位的資料為null ,就會報錯。
auto_increment定義列為自增的屬性,一般用於主鍵,數值會自動加1。
primary key 關鍵字用於定義列為主鍵
engine 設定儲存引擎,charset 設定編碼。示例:
mysql> create table t1
- > (t1_id int not null auto_increment,
-> t1_name varchar(40) not null,
-> t1_author varchar(40) not null,
-> t1_price int not null,
-> primary key (t1_id)
-> )engine=innodb default charset=utf8;
Query OK, 0 rows affected (2.43 sec)
mysql> show tables;
+------------------+
| Tables_in_test02 |
+------------------+
| t1 |
+------------------+
1 row in set (0.00 sec)
以上實現了建立一個數據表,並驗證是否建立成功。
7.刪除資料表,和刪除資料庫語法相似。輸入drop table 資料表名
即可。
8.往資料表插入資料.現在資料表已經建立好了但裡面還沒有資料,向資料表中插入資料方法很簡單:(注意:如果資料是字元型,必須使用單引號或者引號,如:“value”。)
向資料表中新增資料可以一行一行新增,如下
mysql> insert into t1
-> (t1_name,t1_author,t1_price)
-> values
-> ("西遊記","吳承恩",75);
Query OK, 1 row affected (2.24 sec)
多行資料一次性新增,如下
mysql> insert into t1
-> (t1_name,t1_author,t1_price)
-> values
-> ("水滸傳","施耐庵",78),
-> ("三國演義","羅貫中",80),
-> ("紅樓夢","曹雪芹",85);
Query OK, 3 rows affected (2.17 sec)
Records: 3 Duplicates: 0 Warnings: 0
9.查詢資料表。對於以上的向資料表中插入資料如何得知成功與否呢,執行select * from 資料表名稱
即可,對於之前的插入操作驗證如下
mysql> select * from t1;
+-------+--------------+-----------+----------+
| t1_id | t1_name | t1_author | t1_price |
+-------+--------------+-----------+----------+
| 1 | 西遊記 | 吳承恩 | 75 |
| 2 | 水滸傳 | 施耐庵 | 78 |
| 3 | 三國演義 | 羅貫中 | 80 |
| 4 | 紅樓夢 | 曹雪芹 | 85 |
+-------+--------------+-----------+----------+
10.更新資料。也即是把表中的資料更改,比如書的價格改變了就需要變動price
所以只需執行 類似update t1set t1_price=82 where t1_name='紅樓夢' ;
步驟即可,結果如下:
mysql> update t1 set t1_price=82 where t1_name='紅樓夢';
Query OK, 1 row affected (2.20 sec)
Rows matched: 1 Changed: 1 Warnings:
mysql> select * from t1;
+-------+--------------+-----------+----------+
| t1_id | t1_name | t1_author | t1_price |
+-------+--------------+-----------+----------+
| 1 | 西遊記 | 吳承恩 | 75 |
| 2 | 水滸傳 | 施耐庵 | 78 |
| 3 | 三國演義 | 羅貫中 | 80 |
| 4 | 紅樓夢 | 曹雪芹 | 82 |
+-------+--------------+-----------+----------+
11.刪除資料表中的指定資料:delete from t1 where t1_id=1;
重新查詢一下就會發現 t1_id=1 的那一行被刪除。
mysql> delete from t1 where t1_id=1;
Query OK, 1 row affected (2.23 sec)
mysql> select * from t1;
+-------+--------------+-----------+----------+
| t1_id | t1_name | t1_author | t1_price |
+-------+--------------+-----------+----------+
| 2 | 水滸傳 | 施耐庵 | 78 |
| 3 | 三國演義 | 羅貫中 | 80 |
| 4 | 紅樓夢 | 曹雪芹 | 82 |
+-------+--------------+-----------+----------+
3 rows in set (0.00 sec)
12.like 用法:向 t1 表中加入幾個資料,可以使用like子句代替等號 =,like 通常與 % 一同使用,類似於一個元字元的搜尋。
示例:select * from t1 where t1_author like ('%xml');
mysql> select * from t1 where t1_author like('%.xml');
+-------+---------+-----------+----------+
| t1_id | t1_name | t1_author | t1_price |
+-------+---------+-----------+----------+
| 5 | 1.name | 1.xml | 100 |
| 6 | 3.name | 3.xml | 300 |
| 7 | 2.name | 2.xml | 200 |
+-------+---------+-----------+----------+
3 rows in set (0.00 sec)
13.order by 可以用來排序。例如:t1_price 按升序排列:select * from t1 order by t1_price asc;
mysql> select * from t1 order by t1_price asc;
+-------+--------------+-----------+----------+
| t1_id | t1_name | t1_author | t1_price |
+-------+--------------+-----------+----------+
| 2 | 水滸傳 | 施耐庵 | 78 |
| 3 | 三國演義 | 羅貫中 | 80 |
| 4 | 紅樓夢 | 曹雪芹 | 82 |
| 5 | 1.name | 1.xml | 100 |
| 7 | 2.name | 2.xml | 200 |
| 6 | 3.name | 3.xml | 300 |
+-------+--------------+-----------+----------+
6 rows in set (0.00 sec)
按t1_id降序排列,如下
mysql> select * from t1 order by t1_id desc;
+-------+--------------+-----------+----------+
| t1_id | t1_name | t1_author | t1_price |
+-------+--------------+-----------+----------+
| 7 | 2.name | 2.xml | 200 |
| 6 | 3.name | 3.xml | 300 |
| 5 | 1.name | 1.xml | 100 |
| 4 | 紅樓夢 | 曹雪芹 | 82 |
| 3 | 三國演義 | 羅貫中 | 80 |
| 2 | 水滸傳 | 施耐庵 | 78 |
+-------+--------------+-----------+----------+
6 rows in set (0.00 sec)
14.改變資料表名:一般不會更改,如果要改,則執行alter table t1 rename t11;
即可
mysql> alter table t1 rename t11;
Query OK, 0 rows affected (2.38 sec)
mysql> show tables;
+------------------+
| Tables_in_test02 |
+------------------+
| t11 |
+------------------+
1 row in set (0.00 sec)
15.新增列表。執行alter table t2 add column t1_time varchar(15);
mysql> alter table t11 add column t1_time varchar(50);
Query OK, 0 rows affected (2.71 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from t11;
+-------+--------------+-----------+----------+---------+
| t1_id | t1_name | t1_author | t1_price | t1_time |
+-------+--------------+-----------+----------+---------+
| 2 | 水滸傳 | 施耐庵 | 78 | NULL |
| 3 | 三國演義 | 羅貫中 | 80 | NULL |
| 4 | 紅樓夢 | 曹雪芹 | 82 | NULL |
| 5 | 1.name | 1.xml | 100 | NULL |
| 6 | 3.name | 3.xml | 300 | NULL |
| 7 | 2.name | 2.xml | 200 | NULL |
+-------+--------------+-----------+----------+---------+
6 rows in set (0.00 sec)
mysql> update t11 set t1_time="這本書是2015年出版的" where t1_name='三國演義';
Query OK, 1 row affected (2.19 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from t11;
+-------+--------------+-----------+----------+------------------------------+
| t1_id | t1_name | t1_author | t1_price | t1_time |
+-------+--------------+-----------+----------+------------------------------+
| 2 | 水滸傳 | 施耐庵 | 78 | NULL |
| 3 | 三國演義 | 羅貫中 | 80 | 這本書是2015年出版的 |
| 4 | 紅樓夢 | 曹雪芹 | 82 | NULL |
| 5 | 1.name | 1.xml | 100 | NULL |
| 6 | 3.name | 3.xml | 300 | NULL |
| 7 | 2.name | 2.xml | 200 | NULL |
+-------+--------------+-----------+----------+------------------------------+
6 rows in set (0.00 sec)