【无标题】MYSQL: difference Demo on DELETE, Truncate and DROP

mysql> select * from dept;

+--------+------------+--------+
| deptno | dname      | loc    |
+--------+------------+--------+
|      1 | accounting | 一区   |
|      2 | research   | 二区   |
|      3 | operations | 二区   |
+--------+------------+--------+
3 rows in set (0.00 sec)

mysql> SELECT d.dname,e.ename,e.job
    ->  
    -> FROM emp e INNER JOIN dept d
    ->  
    -> ON e.deptno=d.deptno
    ->  
    -> WHERE d.dname='research';
+----------+-------+--------+
| dname    | ename | job    |
+----------+-------+--------+
| research | tony  | 总监   |
| research | hana  | 经理   |
| research | leo   | 员工   |
| research | liu   | 员工   |
+----------+-------+--------+
4 rows in set (0.00 sec)

mysql> SELECT d.dname,d.loc,e.ename,e.job   FROM emp e INNER JOIN dept d   ON e.deptno=d.deptno   WHERE d.dname='research';
+----------+--------+-------+--------+
| dname    | loc    | ename | job    |
+----------+--------+-------+--------+
| research | 二区   | tony  | 总监   |
| research | 二区   | hana  | 经理   |
| research | 二区   | leo   | 员工   |
| research | 二区   | liu   | 员工   |
+----------+--------+-------+--------+
4 rows in set (0.00 sec)

mysql> select * from dept;
+--------+------------+--------+
| deptno | dname      | loc    |
+--------+------------+--------+
|      1 | accounting | 一区   |
|      2 | research   | 二区   |
|      3 | operations | 二区   |
+--------+------------+--------+
3 rows in set (0.00 sec)

mysql> desc dept;
+--------+-------------+------+-----+---------+----------------+
| Field  | Type        | Null | Key | Default | Extra          |
+--------+-------------+------+-----+---------+----------------+
| deptno | int(11)     | NO   | PRI | NULL    | auto_increment |
| dname  | varchar(20) | YES  |     | NULL    |                |
| loc    | varchar(13) | YES  |     | NULL    |                |
+--------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> describe  emp;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| empno    | int(11)      | NO   | PRI | NULL    | auto_increment |
| ename    | varchar(10)  | YES  |     | NULL    |                |
| job      | varchar(10)  | YES  |     | NULL    |                |
| mgr      | int(11)      | YES  |     | NULL    |                |
| hiredate | date         | YES  |     | NULL    |                |
| sal      | double       | YES  |     | NULL    |                |
| comm     | decimal(7,2) | YES  |     | NULL    |                |
| deptno   | int(11)      | YES  | MUL | NULL    |                |
+----------+--------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)

mysql> select * from dept;
+--------+------------+--------+
| deptno | dname      | loc    |
+--------+------------+--------+
|      1 | accounting | 一区   |
|      2 | research   | 二区   |
|      3 | operations | 二区   |
+--------+------------+--------+
3 rows in set (0.00 sec)

mysql> select * from emp;
+-------+-------+--------+------+------------+-------+---------+--------+
| empno | ename | job    | mgr  | hiredate   | sal   | comm    | deptno |
+-------+-------+--------+------+------------+-------+---------+--------+
|   100 | jack  | 副总   | NULL | 2002-05-01 | 90000 |    NULL |      1 |
|   200 | tony  | 总监   |  100 | 2015-02-02 | 10000 | 2000.00 |      2 |
|   300 | hana  | 经理   |  200 | 2017-02-02 |  8000 | 1000.00 |      2 |
|   400 | leo   | 员工   |  300 | 2019-02-22 |  3000 |  200.12 |      2 |
|   500 | liu   | 员工   |  300 | 2019-03-19 |  3500 |  200.58 |      2 |
+-------+-------+--------+------+------------+-------+---------+--------+
5 rows in set (0.00 sec)

mysql> DELETE FROM emp where empno = 500;
Query OK, 1 row affected (0.00 sec)

mysql> select * from emp;
+-------+-------+--------+------+------------+-------+---------+--------+
| empno | ename | job    | mgr  | hiredate   | sal   | comm    | deptno |
+-------+-------+--------+------+------------+-------+---------+--------+
|   100 | jack  | 副总   | NULL | 2002-05-01 | 90000 |    NULL |      1 |
|   200 | tony  | 总监   |  100 | 2015-02-02 | 10000 | 2000.00 |      2 |
|   300 | hana  | 经理   |  200 | 2017-02-02 |  8000 | 1000.00 |      2 |
|   400 | leo   | 员工   |  300 | 2019-02-22 |  3000 |  200.12 |      2 |
+-------+-------+--------+------+------------+-------+---------+--------+
4 rows in set (0.00 sec)

mysql> INSERT INTO emp VALUES(null,'liu','员工',300,'2019-03-19',3500,200.58,2);
Query OK, 1 row affected (0.01 sec)

mysql> select * from emp;
+-------+-------+--------+------+------------+-------+---------+--------+
| empno | ename | job    | mgr  | hiredate   | sal   | comm    | deptno |
+-------+-------+--------+------+------------+-------+---------+--------+
|   100 | jack  | 副总   | NULL | 2002-05-01 | 90000 |    NULL |      1 |
|   200 | tony  | 总监   |  100 | 2015-02-02 | 10000 | 2000.00 |      2 |
|   300 | hana  | 经理   |  200 | 2017-02-02 |  8000 | 1000.00 |      2 |
|   400 | leo   | 员工   |  300 | 2019-02-22 |  3000 |  200.12 |      2 |
|   501 | liu   | 员工   |  300 | 2019-03-19 |  3500 |  200.58 |      2 |
+-------+-------+--------+------+------------+-------+---------+--------+
5 rows in set (0.00 sec)

mysql> truncate table emp;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from emp;
Empty set (0.00 sec)

mysql> INSERT INTO emp VALUES(null,'liu','员工',300,'2019-03-19',3500,200.58,2);
Query OK, 1 row affected (0.01 sec)

mysql> select * from emp;
+-------+-------+--------+------+------------+------+--------+--------+
| empno | ename | job    | mgr  | hiredate   | sal  | comm   | deptno |
+-------+-------+--------+------+------------+------+--------+--------+
|     1 | liu   | 员工   |  300 | 2019-03-19 | 3500 | 200.58 |      2 |
+-------+-------+--------+------+------------+------+--------+--------+
1 row in set (0.00 sec)

mysql> INSERT INTO emp VALUES(null,'liu','员工',300,'2019-03-19',3500,200.58,2);
Query OK, 1 row affected (0.00 sec)

mysql> select * from emp;
+-------+-------+--------+------+------------+------+--------+--------+
| empno | ename | job    | mgr  | hiredate   | sal  | comm   | deptno |
+-------+-------+--------+------+------------+------+--------+--------+
|     1 | liu   | 员工   |  300 | 2019-03-19 | 3500 | 200.58 |      2 |
|     2 | liu   | 员工   |  300 | 2019-03-19 | 3500 | 200.58 |      2 |
+-------+-------+--------+------+------------+------+--------+--------+
2 rows in set (0.00 sec)

mysql> delete table emp;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table emp' at line 1
mysql> delete from emp;
Query OK, 2 rows affected (0.00 sec)

mysql> select * from emp;
Empty set (0.00 sec)

mysql> INSERT INTO emp VALUES(null,'liu','员工',300,'2019-03-19',3500,200.58,2);
Query OK, 1 row affected (0.00 sec)

mysql> select * from emp;
+-------+-------+--------+------+------------+------+--------+--------+
| empno | ename | job    | mgr  | hiredate   | sal  | comm   | deptno |
+-------+-------+--------+------+------------+------+--------+--------+
|     3 | liu   | 员工   |  300 | 2019-03-19 | 3500 | 200.58 |      2 |
+-------+-------+--------+------+------------+------+--------+--------+
1 row in set (0.00 sec)

mysql> drop emp;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'emp' at line 1
mysql> drop from emp;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from emp' at line 1
mysql> drop table emp;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from emp;
ERROR 1146 (42S02): Table 'cgb2022.emp' doesn't exist
mysql> desc emp;
ERROR 1146 (42S02): Table 'cgb2022.emp' doesn't exist
mysql> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值