SQL分类之DML:增删改表中的数据

DML:增删改表中的数据

1.添加数据:

  • 语法:

insert into 表名(列名1,列名2,...列名n) values(值1,值2,...值n);
  • 注意:

1.列名和值要一一对应。

2.如果表名后,不定义列名,则默认给所有列添加值。

insert into 表名 values(值1,值2,...值n);
mysql> create table abc003(id int,name varchar(37),age int); --创建表

mysql> show tables;
+------------------+
| Tables_in_abc001 |
+------------------+
| ab001            |
| abc003           |
| abc004           |
+------------------+

mysql> desc abc003;  --查询表结构
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(37) | YES  |     | NULL    |       |
| age   | int(11)     | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+

mysql> insert into abc003 values(1,'cai',18),(2,'chuan',20),(3,'qi',30);  --增加列

mysql> select * from abc003;  --查询列
+------+-------+------+
| id   | name  | age  |
+------+-------+------+
|    1 | cai   |   18 |
|    2 | chuan |   20 |
|    3 | qi    |   30 |
+------+-------+------+

3.除了数字类型,其他类型需要使用引号(单双都可以)引起来

2.删除数据:

  • 语法:
delete from 表名 [where 条件]
mysql> show tables;
+------------------+
| Tables_in_abc001 |
+------------------+
| abc003           |
| abc004           |
+------------------+
2 rows in set (0.00 sec)

mysql> desc abc003;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(37) | YES  |     | NULL    |       |
| age   | int(11)     | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> select * from abc003;
+------+-------+------+
| id   | name  | age  |
+------+-------+------+
|    1 | cai   |   18 |
|    2 | chuan |   20 |
|    3 | qi    |   30 |
+------+-------+------+
3 rows in set (0.00 sec)

mysql> delete from abc003 where id=1;  --删除列中的值
Query OK, 1 row affected (0.45 sec)

mysql> select * from abc003;  --查询列
+------+-------+------+
| id   | name  | age  |
+------+-------+------+
|    2 | chuan |   20 |
|    3 | qi    |   30 |
+------+-------+------+
  • 注意:
  1. 如果不加条件,则删除表中所有记录。
  2. 如果要删除所有记录
  • delete from 表名;-- 不推荐使用。有多少条记录就会执行多少次删除操作。
  • TRUNCATE TABLE 表名; -- 推荐使用,效率更高 先删除表,然后再创建一张一样的表。

3.修改数据

  • *语法:

update 表名 set 列名1 = 值1,列名2 = 值2,...[where 条件];
mysql> update abc003 set age=99 where id=3;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    215
Current database: abc001

Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from abc003;
+------+-------+------+
| id   | name  | age  |
+------+-------+------+
|    2 | chuan |   20 |
|    3 | qi    |   99 |
+------+-------+------+
2 rows in set (0.00 sec)

注意:

  • 如果不加任何条件,则会将表中所有记录全部修改。
posted @ 2021-02-18 23:18  奔跑的snail  阅读(38)  评论(0编辑  收藏  举报