数据库08表的复制,改,删

表的复制:
语法: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 表名;//表被截断,不可回滚,永久丢失

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值