表的复制:
语法:create table 表名 as select语句;
将查询结果当做表创建出来。
将查询结果插入到一张表中:
insert into dept1 select * from dept;
将一张表中的dept查询结果当做数据插入前面表中
修改数据:update(注意当中标点符号)
语法格式:update 表名 字段名1=值1,字段名2=值2。。。where 条件;
注意没有条件整张表数据全部更新。
mysql> select * from t_student;
+------+------+------+------------+----------+
| no | name | sex | classno | birth |
+------+------+------+------------+----------+
| NULL | zs | 1 | NULL | NULL |
| 1 | jack | 0 | gaosan2ban | 2021-2-5 |
| 2 | rose | 1 | gs2b | 2021-2-5 |
| 3 | abc | 1 | gs2b | 2021-2-5 |
+------+------+------+------------+----------+
4 rows in set (0.00 sec)
mysql> update t_student set name='aaa',sex='0' where no=3;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from t_student;
+------+------+------+------------+----------+
| no | name | sex | classno | birth |
+------+------+------+------------+----------+
| NULL | zs | 1 | NULL | NULL |
| 1 | jack | 0 | gaosan2ban | 2021-2-5 |
| 2 | rose | 1 | gs2b | 2021-2-5 |
| 3 | aaa | 0 | gs2b | 2021-2-5 |
+------+------+------+------------+----------+
更新所有记录:
update t_student set classno=‘da4’,birth=‘2021-2-6’;
mysql> update t_student set classno='da4',birth='2021-2-6';
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> select * from t_student;
+------+------+------+---------+----------+
| no | name | sex | classno | birth |
+------+------+------+---------+----------+
| NULL | zs | 1 | da4 | 2021-2-6 |
| 1 | jack | 0 | da4 | 2021-2-6 |
| 2 | rose | 1 | da4 | 2021-2-6 |
| 3 | aaa | 0 | da4 | 2021-2-6 |
+------+------+------+---------+----------+
删除数据
语法格式:delete from 表名 where 条件;
注意:没有条件全部删除。
mysql> select * from t_student;
+------+------+------+---------+----------+
| no | name | sex | classno | birth |
+------+------+------+---------+----------+
| NULL | zs | 1 | da4 | 2021-2-6 |
| 1 | jack | 0 | da4 | 2021-2-6 |
| 2 | rose | 1 | da4 | 2021-2-6 |
| 3 | aaa | 0 | da4 | 2021-2-6 |
+------+------+------+---------+----------+
4 rows in set (0.00 sec)
mysql> delete from t_student where no=1;
Query OK, 1 row affected (0.01 sec)
mysql> select * from t_student;
+------+------+------+---------+----------+
| no | name | sex | classno | birth |
+------+------+------+---------+----------+
| NULL | zs | 1 | da4 | 2021-2-6 |
| 2 | rose | 1 | da4 | 2021-2-6 |
| 3 | aaa | 0 | da4 | 2021-2-6 |
+------+------+------+---------+----------+
注意:我在删除no=NULL时不成功,但用name='zs’时可删除。
mysql> select * from t_student;
+------+------+------+---------+----------+
| no | name | sex | classno | birth |
+------+------+------+---------+----------+
| NULL | zs | 1 | da4 | 2021-2-6 |
| 2 | rose | 1 | da4 | 2021-2-6 |
| 3 | aaa | 0 | da4 | 2021-2-6 |
+------+------+------+---------+----------+
3 rows in set (0.00 sec)
mysql> delete from t_student where name='zs';
Query OK, 1 row affected (0.01 sec)
mysql> select * from t_student;
+------+------+------+---------+----------+
| no | name | sex | classno | birth |
+------+------+------+---------+----------+
| 2 | rose | 1 | da4 | 2021-2-6 |
| 3 | aaa | 0 | da4 | 2021-2-6 |
+------+------+------+---------+----------+
删除所有记录:
select * from 表名;
怎么删除大表?(重点)
truncate table 表名;//表被截断,不可回滚,永久丢失