SQL語句之DML資料操作語言(insert、delete、update)
阿新 • • 發佈:2020-07-15
1.insert 命令
1.查看錶結構
mysql> desc student; +----------+---------------------+------+-----+-------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------------+------+-----+-------------------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(12) | NO | | NULL | | | age | tinyint(3) unsigned | NO | | NULL | | | gender | enum('m','f') | YES | | f | | | cometime | datetime | YES | | CURRENT_TIMESTAMP | | | birthday | datetime | YES | | NULL | | +----------+---------------------+------+-----+-------------------+----------------+ 6 rows in set (0.00 sec)
2.插入資料(不規範寫法)
mysql> insert into student values(1,'邱導',78,'f',now(),'1942-07-14'); Query OK, 1 row affected (0.00 sec) mysql> select * from student; +----+--------+-----+--------+---------------------+---------------------+ | id | name | age | gender | cometime | birthday | +----+--------+-----+--------+---------------------+---------------------+ | 1 | 邱導 | 78 | f | 2020-07-15 09:21:12 | 1942-07-14 00:00:00 | +----+--------+-----+--------+---------------------+---------------------+ 1 row in set (0.00 sec)
3.插入資料(規範寫法)
#1.插入指定列資料 mysql> insert into student(name,age) values('曾導','84'); Query OK, 1 row affected (0.00 sec) mysql> select * from student; +----+--------+-----+--------+---------------------+---------------------+ | id | name | age | gender | cometime | birthday | +----+--------+-----+--------+---------------------+---------------------+ | 1 | 邱導 | 78 | f | 2020-07-15 09:21:12 | 1942-07-14 00:00:00 | | 2 | 邱導 | 78 | f | 2020-07-15 09:22:27 | 1942-07-14 00:00:00 | | 3 | 曾導 | 84 | f | 2020-07-15 09:24:17 | NULL | +----+--------+-----+--------+---------------------+---------------------+ 3 rows in set (0.00 sec) #2.插入指定列資料 mysql> insert into student(name,age,birthday) values('曾導','84','1936-02-20'); Query OK, 1 row affected (0.00 sec) mysql> select * from student; +----+--------+-----+--------+---------------------+---------------------+ | id | name | age | gender | cometime | birthday | +----+--------+-----+--------+---------------------+---------------------+ | 1 | 邱導 | 78 | f | 2020-07-15 09:21:12 | 1942-07-14 00:00:00 | | 2 | 邱導 | 78 | f | 2020-07-15 09:22:27 | 1942-07-14 00:00:00 | | 3 | 曾導 | 84 | f | 2020-07-15 09:24:17 | NULL | | 4 | 曾導 | 84 | f | 2020-07-15 09:25:22 | 1936-02-20 00:00:00 | +----+--------+-----+--------+---------------------+---------------------+ 4 rows in set (0.00 sec)
4.插入多條資料
mysql> insert into student(name,age,birthday) values('好大','18',1936-02-21),('好小','28','1992-01-01');
Query OK, 2 rows affected, 1 warning (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 1
mysql> select * from student;
+----+--------+-----+--------+---------------------+---------------------+
| id | name | age | gender | cometime | birthday |
+----+--------+-----+--------+---------------------+---------------------+
| 1 | 邱導 | 78 | f | 2020-07-15 09:21:12 | 1942-07-14 00:00:00 |
| 2 | 邱導 | 78 | f | 2020-07-15 09:22:27 | 1942-07-14 00:00:00 |
| 3 | 曾導 | 84 | f | 2020-07-15 09:24:17 | NULL |
| 4 | 曾導 | 84 | f | 2020-07-15 09:25:22 | 1936-02-20 00:00:00 |
| 5 | 曾導 | 84 | f | 2020-07-15 09:28:11 | 1936-02-21 00:00:00 |
| 6 | 好大 | 18 | f | 2020-07-15 09:29:18 | 0000-00-00 00:00:00 |
| 7 | 好大 | 18 | f | 2020-07-15 09:31:07 | 0000-00-00 00:00:00 |
| 8 | 好小 | 28 | f | 2020-07-15 09:31:07 | 1992-01-01 00:00:00 |
+----+--------+-----+--------+---------------------+---------------------+
8 rows in set (0.00 sec)
2.update命令
1.檢視資料
mysql> select * from student;
+----+--------+-----+--------+---------------------+---------------------+
| id | name | age | gender | cometime | birthday |
+----+--------+-----+--------+---------------------+---------------------+
| 1 | 邱導 | 78 | f | 2020-07-15 09:21:12 | 1942-07-14 00:00:00 |
| 2 | 邱導 | 78 | f | 2020-07-15 09:22:27 | 1942-07-14 00:00:00 |
| 3 | 曾導 | 84 | f | 2020-07-15 09:24:17 | NULL |
| 4 | 曾導 | 84 | f | 2020-07-15 09:25:22 | 1936-02-20 00:00:00 |
| 5 | 曾導 | 84 | f | 2020-07-15 09:28:11 | 1936-02-21 00:00:00 |
| 6 | 好大 | 18 | f | 2020-07-15 09:29:18 | 0000-00-00 00:00:00 |
| 7 | 好大 | 18 | f | 2020-07-15 09:31:07 | 0000-00-00 00:00:00 |
| 8 | 好小 | 28 | f | 2020-07-15 09:31:07 | 1992-01-01 00:00:00 |
+----+--------+-----+--------+---------------------+---------------------+
8 rows in set (0.00 sec)
2.修改資料
#使用update語句必須要加where條件
mysql> update student set age=18 where name='邱導';
Query OK, 2 rows affected (0.01 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> select * from student;
+----+--------+-----+--------+---------------------+---------------------+
| id | name | age | gender | cometime | birthday |
+----+--------+-----+--------+---------------------+---------------------+
| 1 | 邱導 | 18 | f | 2020-07-15 09:21:12 | 1942-07-14 00:00:00 |
| 2 | 邱導 | 18 | f | 2020-07-15 09:22:27 | 1942-07-14 00:00:00 |
| 3 | 曾導 | 84 | f | 2020-07-15 09:24:17 | NULL |
| 4 | 曾導 | 84 | f | 2020-07-15 09:25:22 | 1936-02-20 00:00:00 |
| 5 | 曾導 | 84 | f | 2020-07-15 09:28:11 | 1936-02-21 00:00:00 |
| 6 | 好大 | 18 | f | 2020-07-15 09:29:18 | 0000-00-00 00:00:00 |
| 7 | 好大 | 18 | f | 2020-07-15 09:31:07 | 0000-00-00 00:00:00 |
| 8 | 好小 | 28 | f | 2020-07-15 09:31:07 | 1992-01-01 00:00:00 |
+----+--------+-----+--------+---------------------+---------------------+
8 rows in set (0.00 sec)
3.指點修改一條資料
#如果資料庫有主鍵,一定使用主鍵
mysql> update student set age=88 where name='邱導' and cometime='2020-07-15 09:21:12';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update student set age=88 where id=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student;
+----+--------+-----+--------+---------------------+---------------------+
| id | name | age | gender | cometime | birthday |
+----+--------+-----+--------+---------------------+---------------------+
| 1 | 邱導 | 88 | f | 2020-07-15 09:21:12 | 1942-07-14 00:00:00 |
| 2 | 邱導 | 88 | f | 2020-07-15 09:22:27 | 1942-07-14 00:00:00 |
| 3 | 曾導 | 84 | f | 2020-07-15 09:24:17 | NULL |
| 4 | 曾導 | 84 | f | 2020-07-15 09:25:22 | 1936-02-20 00:00:00 |
| 5 | 曾導 | 84 | f | 2020-07-15 09:28:11 | 1936-02-21 00:00:00 |
| 6 | 好大 | 18 | f | 2020-07-15 09:29:18 | 0000-00-00 00:00:00 |
| 7 | 好大 | 18 | f | 2020-07-15 09:31:07 | 0000-00-00 00:00:00 |
| 8 | 好小 | 28 | f | 2020-07-15 09:31:07 | 1992-01-01 00:00:00 |
+----+--------+-----+--------+---------------------+---------------------+
8 rows in set (0.00 sec)
3.delete語句
#1.先檢視資料,確認要刪除的資料,怎麼確定唯一
#2.使用delete語句也一定要加where條件
mysql> delete from student where id=8;
Query OK, 1 row affected (0.01 sec)
mysql> select * from student;
+----+--------+-----+--------+---------------------+---------------------+
| id | name | age | gender | cometime | birthday |
+----+--------+-----+--------+---------------------+---------------------+
| 1 | 邱導 | 88 | f | 2020-07-15 09:21:12 | 1942-07-14 00:00:00 |
| 2 | 邱導 | 88 | f | 2020-07-15 09:22:27 | 1942-07-14 00:00:00 |
| 3 | 曾導 | 84 | f | 2020-07-15 09:24:17 | NULL |
| 4 | 曾導 | 84 | f | 2020-07-15 09:25:22 | 1936-02-20 00:00:00 |
| 5 | 曾導 | 84 | f | 2020-07-15 09:28:11 | 1936-02-21 00:00:00 |
| 6 | 好大 | 18 | f | 2020-07-15 09:29:18 | 0000-00-00 00:00:00 |
| 7 | 好大 | 18 | f | 2020-07-15 09:31:07 | 0000-00-00 00:00:00 |
+----+--------+-----+--------+---------------------+---------------------+
7 rows in set (0.00 sec)
#3.如果就是要清空表
mysql> delete from student where 1=1;
Query OK, 1 row affected (0.01 sec)
#(危險)
truncate table student;
drop table student
4.使用update代替delete
1.新增狀態欄位
mysql> alter table student add status enum('1','0') default 1;
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from student;
+----+--------+-----+--------+---------------------+---------------------+--------+
| id | name | age | gender | cometime | birthday | status |
+----+--------+-----+--------+---------------------+---------------------+--------+
| 1 | 邱導 | 88 | f | 2020-07-15 09:21:12 | 1942-07-14 00:00:00 | 1 |
| 2 | 邱導 | 88 | f | 2020-07-15 09:22:27 | 1942-07-14 00:00:00 | 1 |
| 3 | 曾導 | 84 | f | 2020-07-15 09:24:17 | NULL | 1 |
| 4 | 曾導 | 84 | f | 2020-07-15 09:25:22 | 1936-02-20 00:00:00 | 1 |
| 5 | 曾導 | 84 | f | 2020-07-15 09:28:11 | 1936-02-21 00:00:00 | 1 |
| 6 | 好大 | 18 | f | 2020-07-15 09:29:18 | 0000-00-00 00:00:00 | 1 |
| 7 | 好大 | 18 | f | 2020-07-15 09:31:07 | 0000-00-00 00:00:00 | 1 |
+----+--------+-----+--------+---------------------+---------------------+--------+
7 rows in set (0.00 sec)
2.使用update代替delete
#相當於刪除學生
mysql> update student set status='0' where id =2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student;
+----+--------+-----+--------+---------------------+---------------------+--------+
| id | name | age | gender | cometime | birthday | status |
+----+--------+-----+--------+---------------------+---------------------+--------+
| 2 | 邱導 | 88 | f | 2020-07-15 09:22:27 | 1942-07-14 00:00:00 | 0 |
| 3 | 曾導 | 84 | f | 2020-07-15 09:24:17 | NULL | 1 |
| 4 | 曾導 | 84 | f | 2020-07-15 09:25:22 | 1936-02-20 00:00:00 | 1 |
| 5 | 曾導 | 84 | f | 2020-07-15 09:28:11 | 1936-02-21 00:00:00 | 1 |
| 6 | 好大 | 18 | f | 2020-07-15 09:29:18 | 0000-00-00 00:00:00 | 1 |
| 7 | 好大 | 18 | f | 2020-07-15 09:31:07 | 0000-00-00 00:00:00 | 1 |
+----+--------+-----+--------+---------------------+---------------------+--------+
6 rows in set (0.00 sec)
#相當於學生回來
mysql> update student set status='1' where id =2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student;
+----+--------+-----+--------+---------------------+---------------------+--------+
| id | name | age | gender | cometime | birthday | status |
+----+--------+-----+--------+---------------------+---------------------+--------+
| 2 | 邱導 | 88 | f | 2020-07-15 09:22:27 | 1942-07-14 00:00:00 | 1 |
| 3 | 曾導 | 84 | f | 2020-07-15 09:24:17 | NULL | 1 |
| 4 | 曾導 | 84 | f | 2020-07-15 09:25:22 | 1936-02-20 00:00:00 | 1 |
| 5 | 曾導 | 84 | f | 2020-07-15 09:28:11 | 1936-02-21 00:00:00 | 1 |
| 6 | 好大 | 18 | f | 2020-07-15 09:29:18 | 0000-00-00 00:00:00 | 1 |
| 7 | 好大 | 18 | f | 2020-07-15 09:31:07 | 0000-00-00 00:00:00 | 1 |
+----+--------+-----+--------+---------------------+---------------------+--------+
6 rows in set (0.00 sec)