mysql資料庫增刪改查學習筆記
阿新 • • 發佈:2021-01-07
技術標籤:mysql相關
mysql資料庫增刪改查學習筆記
對資料的增刪改查學習記錄
準備一個空表(student),有三個欄位,id,name,age
mysql> select * from student; Empty set (0.00 sec) mysql> desc student; +-------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(128) | YES | | NULL | | | age | int(11) | YES | | NULL | | +-------+--------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql> show create table student; +---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+ | student | CREATE TABLE `student` ( `id` int(11) DEFAULT NULL, `name` varchar(128) DEFAULT NULL, `age` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 | +---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
增加insert
插入資料
INSERT INTO table_name ( 欄位1, 欄位2,…欄位N ) VALUES( 值1, 值2,…值N );
如果資料是字元型,必須使用單引號或者雙引號,如:“value”
例:插入id為1,姓名zhangsan,年齡18
插入id為2,姓名lisi,年齡19
插入id為空,姓名tom,年齡20
三條資料
mysql> insert into student (id,name,age) values (1,'zhangsan',18); Query OK, 1 row affected (0.00 sec) 也可以不加欄位,但是後面的資料要和欄位對應上 mysql> insert into student values (2,'lisi',19); Query OK, 1 row affected (0.00 sec) 如果少了會報錯 mysql> insert into student values (2,'lisi'); ERROR 1136 (21S01): Column count doesn't match value count at row 1 可以有值不填,只要不填的值在設定中不是非空的就可以 mysql> insert into student (name,age) values ('tom',20); Query OK, 1 row affected (0.00 sec)
查詢select
SELECT column_name,column_name
FROM table_name
[WHERE Clause]
[LIMIT N][ OFFSET M]
- 查詢語句中你可以使用一個或者多個表,表之間使用逗號(,)分割,並使用WHERE語句來設定查詢條件。
- SELECT 命令可以讀取一條或者多條記錄。
- 可以使用星號(*)來代替其他欄位,SELECT語句會返回表的所有欄位資料
- 可以使用 WHERE 語句來包含任何條件。
- 可以使用 LIMIT 屬性來設定返回的記錄數。
- 可以通過OFFSET指定SELECT語句開始查詢的資料偏移量。預設情況下偏移量為0。
最簡單的查詢就是直接查全部
mysql> select * from student;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 1 | zhangsan | 18 |
| 2 | lisi | 19 |
| NULL | tom | 20 |
+------+----------+------+
3 rows in set (0.00 sec)
查詢指定欄位
mysql> select name,age from student;
+----------+------+
| name | age |
+----------+------+
| zhangsan | 18 |
| lisi | 19 |
| tom | 20 |
+----------+------+
3 rows in set (0.00 sec)
指定條件查詢(where)
mysql> select * from student where age=18;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 1 | zhangsan | 18 |
+------+----------+------+
1 row in set (0.00 sec)
只返回查詢到的1行(limit)
mysql> select * from student limit 1;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 1 | zhangsan | 18 |
+------+----------+------+
1 row in set (0.01 sec)
偏移量的使用
取兩個資料,偏移量為1,就是說不要第一個資料,從第二個開始順序取2個數據
mysql> select * from student limit 2 offset 1;
+------+------+------+
| id | name | age |
+------+------+------+
| 2 | lisi | 19 |
| NULL | tom | 20 |
+------+------+------+
2 rows in set (0.00 sec)
offset和limit連用
取一條資料,排查掉前2條資料,順序取
mysql> select * from student limit 2,1;
+------+------+------+
| id | name | age |
+------+------+------+
| NULL | tom | 20 |
+------+------+------+
1 row in set (0.00 sec)
修改update
UPDATE table_name SET field1=new-value1, field2=new-value2
[WHERE Clause]
- 可以同時更新一個或多個欄位。
- 可以在 WHERE 子句中指定任何條件。
- 可以在一個單獨表中同時更新資料
還是用剛剛的那個表,在插入幾個資料
mysql> select * from student;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 1 | zhangsan | 18 |
| 2 | lisi | 19 |
| NULL | tom | 20 |
| 2 | aaa | 19 |
| 3 | bbb | 20 |
+------+----------+------+
5 rows in set (0.00 sec)
將id為null的資料變成5
修改的話要給一個條件,不然會修改所有的資料
mysql> update student set id=5 where name='tom';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 2 | aaa | 19 |
| 5 | tom | 20 |
| 2 | lisi | 19 |
| 3 | bbb | 20 |
| 1 | zhangsan | 18 |
+------+----------+------+
5 rows in set (0.00 sec)
補充小知識:
修改密碼
使用update修改資料庫使用者密碼
update user set authentication_string=password("新密碼") where user='root';
刪除delete
DELETE FROM table_name [WHERE Clause]
- 如果沒有指定 WHERE 子句,MySQL 表中的所有記錄將被刪除。
- 可以在 WHERE 子句中指定任何條件
- 可以在單個表中一次性刪除記錄。
mysql> select * from student;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 2 | aaa | 19 |
| 5 | tom | 20 |
| 2 | lisi | 19 |
| 3 | bbb | 20 |
| 1 | zhangsan | 18 |
+------+----------+------+
5 rows in set (0.00 sec)
刪除年齡為20的資料
mysql> delete from student where age=20;
Query OK, 2 rows affected (0.00 sec)
mysql> select * from student;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 2 | aaa | 19 |
| 2 | lisi | 19 |
| 1 | zhangsan | 18 |
+------+----------+------+
3 rows in set (0.00 sec)
刪除全部資料
mysql> delete from student ;
Query OK, 3 rows affected (0.00 sec)
mysql> select * from student;
Empty set (0.00 sec)
補充:
刪除全表資料還可以使用truncate table table_name;