Mysql基礎入門-SQL_DML語句


wKioL1j8ciTxLtlBAAAhxh_ilA0334.jpg


DML語句:

DML操作是指對數據庫中的表進行操作,主要包括記錄的插入(insert),更新(update),刪除(delete),查詢(select)。



記錄插入

創建表完成後就需要給器插入記錄和數據了,插入記錄基本語法如下:

INSERT tablename (fielde1,fielden2....)VALUES(value1,value2....);

下面我給我製作的表名爲class_1填入以下下信息

name分別爲liao,liaoxz,marry  age爲18,18,28 sex 爲man,man,woman

mysql> insert class_1 (name,age,sex)values('liao','18','man');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> insert class_1 (name,age,sex)values('liaoxz','18','man');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> insert class_1 (name,age,sex)values('marry','28','woman');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> select name,age,sex from class_1 ;
+--------+------+------+
| name   | age  | sex  |
+--------+------+------+
| marry  |   28 | w    |
| liaoxz |   18 | m    |
| liao   |   18 | m    |
+--------+------+------+
3 rows in set (0.00 sec)


注:可以不用指定字段名但是values後面的順序必須要和字段排序一樣,加入新的數據記錄時如果只指定其中幾個字段,其他默認爲null值

例如:

mysql> insert class_1 (name,age)values('tom','19');
Query OK, 1 row affected (0.00 sec)
mysql> select name,age,sex from class_1 ;
+--------+------+------+
| name   | age  | sex  |
+--------+------+------+
| marry  |   28 | w    |
| liaoxz |   18 | m    |
| liao   |   18 | m    |
| tom    |   19 | NULL |
+--------+------+------+
4 rows in set (0.00 sec)
mysql> insert class_1 values('jarry','18','woman');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> select name,age,sex from class_1 ;
+--------+------+------+
| name   | age  | sex  |
+--------+------+------+
| marry  |   28 | w    |
| liaoxz |   18 | m    |
| liao   |   18 | m    |
| tom    |   19 | NULL |
| jarry  |   18 | w    |
+--------+------+------+
5 rows in set (0.00 sec)


同時加入多條記錄

mysql> insert class_1 values('wang','19','man'),('li','16','woman'),('liu','17','woman');
Query OK, 3 rows affected, 3 warnings (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 3
mysql> select name,age,sex from class_1 ;
+--------+------+------+
| name   | age  | sex  |
+--------+------+------+
| marry  |   28 | w    |
| liaoxz |   18 | m    |
| liao   |   18 | m    |
| tom    |   19 | NULL |
| jarry  |   18 | w    |
| wang   |   19 | m    |
| li     |   16 | w    |
| liu    |   17 | w    |
+--------+------+------+
8 rows in set (0.00 sec)

記錄更新

表中的值可以通過update來更新語法如下:

UPDATE tablename SET filed1=value1,filed2=value2...[WHERE CONDITION];

例 如將表中的liao 的age更改爲20:

mysql> update test.class_1 set age=20 where name="liao";Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0
mysql> select name,age from class_1;
+--------+------+
| name   | age  |
+--------+------+
| marry  |   28 |
| liaoxz |   18 |
| liao   |   20 |
| tom    |   19 |
| jarry  |   18 |
| wang   |   19 |
| li     |   16 |
| liu    |   17 |
+--------+------+
8 rows in set (0.00 sec)

記錄刪除

當數據不再需要時,可以使用delete將其刪除,例如將class_1中所有name爲liao的所有數據刪除;

語法如下:

DELETE FROM tablename [WHERE CONDITION];


mysql> delete from class_1 where name='liao';
Query OK, 1 row affected (0.00 sec)
mysql> select name,age,sex from class_1;
+--------+------+------+
| name   | age  | sex  |
+--------+------+------+
| marry  |   28 | w    |
| liaoxz |   18 | m    |
| tom    |   19 | NULL |
| jarry  |   18 | w    |
| wang   |   19 | m    |
| li     |   16 | w    |
| liu    |   17 | w    |
+--------+------+------+
7 rows in set (0.00 sec)

記錄查詢


當想要查看數據庫中其中一條數據信息時可以使用select命令查找;基本語法如下;

SELECT*FROM tablename [WHERE CONDITION];

常見用法 

例1

查看錶中class_1中所有數據;

mysql> select * from test.class_1;
+--------+------+------+
| name   | age  | sex  |
+--------+------+------+
| marry  |   28 | w    |
| liaoxz |   18 | m    |
| tom    |   19 | NULL |
| jarry  |   18 | w    |
| wang   |   19 | m    |
| li     |   16 | w    |
| liu    |   17 | w    |
+--------+------+------+
7 rows in set (0.00 sec)

例2

查看class_1中所有age爲19的用戶記錄

mysql> select * from class_1 where age=19;
+------+------+------+
| name | age  | sex  |
+------+------+------+
| tom  |   19 | NULL |
| wang |   19 | m    |
+------+------+------+
2 rows in set (0.00 sec)

根據查詢不同要求可以將查詢分爲以下幾類:

1)去重查詢

使用distinct關鍵字來實現

例如查看錶class_2中age去重後顯示出來

mysql> select distinct age from class_2; 
+------+
| age  |
+------+
|   18 |
|   19 |
|   16 |
+------+
3 rows in set (0.00 sec)

從上面可以看出class_2表中學生age都在16,18,19之間


2)條件查詢

條件查詢同where後的字段進行條件比較,比較符號包括 >,<,=,<=,>=,!= 還可以使用多個條件 使用or ,and等邏輯運算符進行多條件查詢。

例子1 查看class_1表中age大於等於18的數據,

mysql> select name,age,sex from class_1 where age>=18;
+--------+------+------+
| name   | age  | sex  |
+--------+------+------+
| marry  |   28 | w    |
| liaoxz |   18 | m    |
| tom    |   19 | NULL |
| jarry  |   18 | w    |
| wang   |   19 | m    |
+--------+------+------+
5 rows in set (0.00 sec)


例子2 查看class_1表中sex爲man,且年齡大於等於18的數據

mysql> select name,age,sex from class_1 where sex='m' and age>=18;
+--------+------+------+
| name   | age  | sex  |
+--------+------+------+
| liaoxz |   18 | m    |
| wang   |   19 | m    |
+--------+------+------+
2 rows in set (0.00 sec)


3)輸出排序查詢

在查詢過程中常會將數據進行排序後進行查看,在這裏排序查看使用關鍵字 ORDER BY來實現,語法如下:

SELECT * FROM tablename [WHERE CONDITION] [ORDER BY filed1 [DESC|ASC]];

DESC是按照字段進行排序,如果第一個字段值一樣賦值會按照第二個字段進行比較排序,ASC是按照升序排列,不加sac默認也是升序排列

例子 1 將表class_1按照age進行排序,

mysql> select name,age,sex from class_1 order  by age asc;
+--------+------+-------+
| name   | age  | sex   |
+--------+------+-------+
| li     |   16 | woman |
| liu    |   17 | woman |
| liaoxz |   18 | man   |
| jarry  |   18 | woman |
| tom    |   19 | NULL  |
| wang   |   19 | man   |
| marry  |   28 | woman |
+--------+------+-------+
7 rows in set (0.00 sec)


4)限定輸出查詢

當一張表中數據過多時,我們只想查看其中一部分可以使用LIMIT關鍵字實現,LIMIT 語法如下:

SELECT ... [LIMIT offset_start,row_count]

offset_start表示記錄起始偏移量,row_count表示行數;

注意:默認其實偏移量爲0,例;顯示class_1表中按照age排序後的前三條記錄

mysql> select * from class_1 order by age;
+--------+------+-------+
| name   | age  | sex   |
+--------+------+-------+
| li     |   16 | woman |
| liu    |   17 | woman |
| liaoxz |   18 | man   |
| jarry  |   18 | woman |
| tom    |   19 | NULL  |
| wang   |   19 | man   |
| marry  |   28 | woman |
+--------+------+-------+
7 rows in set (0.00 sec)
mysql> select * from class_1 order by age limit 3;
+--------+------+-------+
| name   | age  | sex   |
+--------+------+-------+
| li     |   16 | woman |
| liu    |   17 | woman |
| liaoxz |   18 | man   |
+--------+------+-------+
3 rows in set (0.00 sec)


例2 顯示從第3條開始後的三條記錄

mysql> select * from class_1 order by age limit 2,3;
+--------+------+-------+
| name   | age  | sex   |
+--------+------+-------+
| liaoxz |   18 | man   |
| jarry  |   18 | woman |
| tom    |   19 | NULL  |
+--------+------+-------+
3 rows in set (0.00 sec)

寫在後面:多表操作將在後續文章中陸續貼出

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章