MySQL 增删改
MySQL的基本操作————增 删 1.向表中增加数据 insert into 表名 (字段1,字段2……) values (值1,值2……); mysql> select * from linlin; Empty set (0.00 sec) mysql> desc linlin; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | score | float | YES | | NULL | | | name | varchar(20) | YES | | NULL | | | id | int(20) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql> insert into linlin (score, name, id) values (99, 'A', 1); Query OK, 1 row affected (0.01 sec) mysql> select * from linlin; +-------+------+------+ | score | name | id | +-------+------+------+ | 99 | A | 1 | +-------+------+------+ 1 row in set (0.00 sec) 也可以一次增加多条数据 insert into 表名 (字段1,字段2……) values (值1,值2……),(值1,值2……),……; mysql> insert into linlin (score, name, id) values (98, 'B', 2), (97, 'C', 3); Query OK, 2 rows affected (0.01 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from linlin; +-------+------+------+ | score | name | id | +-------+------+------+ | 99 | A | 1 | | 98 | B | 2 | | 97 | C | 3 | +-------+------+------+ 3 rows in set (0.00 sec) 当插入语句中(字段1,字段2……)省略时,系统会按照表中字段的排列顺序插入 insert into 表名 values (值1,值2……); mysql> insert into linlin values (96, 'D', 4); Query OK, 1 row affected (0.02 sec) mysql> select * from linlin; +-------+------+------+ | score | name | id | +-------+------+------+ | 99 | A | 1 | | 98 | B | 2 | | 97 | C | 3 | | 96 | D | 4 | +-------+------+------+ 4 rows in set (0.00 sec) insert语句的另一种写法: insert into 表名 set 字段1 = 值1,字段2 = 值2,……; mysql> insert into linlin set score = 95, name = 'E', id = 5; Query OK, 1 row affected (0.02 sec) mysql> select * from linlin; +-------+------+------+ | score | name | id | +-------+------+------+ | 99 | A | 1 | | 98 | B | 2 | | 97 | C | 3 | | 96 | D | 4 | | 95 | E | 5 | +-------+------+------+ 5 rows in set (0.00 sec) 2.对表中已存在的数据进行修改 update 表名 set 字段1 = 值1,字段2 = 值2,…… where 条件; mysql> select * from linlin where id = 1; +-------+------+------+ | score | name | id | +-------+------+------+ | 100 | A | 1 | +-------+------+------+ 1 row in set (0.00 sec) mysql> update linlin set score = 99 where id = 1; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from linlin where id = 1; +-------+------+------+ | score | name | id | +-------+------+------+ | 99 | A | 1 | +-------+------+------+ 1 row in set (0.00 sec) 如果需要更新全部数据时,则不需要where条件 3. 删除表中的记录 delete from 表名 where 表达式; mysql> select * from linlin; +-------+------+------+ | score | name | id | +-------+------+------+ | 99 | A | 1 | | 98 | B | 2 | | 97 | C | 3 | | 96 | D | 4 | | 95 | E | 5 | +-------+------+------+ 5 rows in set (0.00 sec) mysql> delete from linlin where id = 5; Query OK, 1 row affected (0.01 sec) mysql> select * from linlin; +-------+------+------+ | score | name | id | +-------+------+------+ | 99 | A | 1 | | 98 | B | 2 | | 97 | C | 3 | | 96 | D | 4 | +-------+------+------+ 4 rows in set (0.00 sec) 如果需要删除全部数据,则不必加上where条件 另外,还可以使用 truncate 表名 删除全部数据 truncate 表名 删除全部数据 与 delete from 表名 删除全部数据的异同 它们都可以删除全部数据,但对于自动增加字段的值,truncate 表名 删除 全部数据之后再向表中添加数据时,自动增加字段的默认值时从1开始,而 对于 delete from 表名 删除全部数据,自动增加字段的默认值是从未删除 时该字段的值加1开始。
转载请注明出处
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现