MySQL学习笔记之数据的增、删、改实现方法
作者:hbiao68 时间:2024-01-27 04:07:41
本文实例讲述了MySQL学习笔记之数据的增、删、改实现方法。分享给大家供大家参考,具体如下:
一、增加数据
插入代码格式:
insert into 表明 [列名…] values (值…)
create table test21(name varchar(32));
insert into test21 (name) values ('huangbiao');
插入原则:
1、插入的数据应与字段的数据类型相同
2、数据的大小应该在列的规定范围内
3、在values中列出的数据位置必须与被加入的列的排列位置对应
例子:
create table test22(id int,name varchar(32));
mysql> insert into test22 (id,name) values (3,'huangbiao');
mysql> insert into test22 (name,id) values ('huangbiao2',5);
mysql> insert into test22 (name,id) values ('',51);
mysql> insert into test22 (name,id) values (NULL,555);
mysql> insert into test22 (id) values (15);
二、更新数据
更新数据的语法格式:
update 表明 set 列名=表达式 … where 条件
说明: 如果where 后面没有条件,则相当于对整个表进行操作。
例子数据:
create table employee(
id int,
name varchar(20),
sex bit,
birthday date,
salary float,
entry_date date,
resume text
);
insert into employee values(1,'aaa',0,'1977-11-11',56.8,now(),'hello word');
insert into employee values(2,'bbb',0,'1977-11-11',57.8,now(),'hello word');
insert into employee values(3,'ccc',0,'1977-11-11',56.3,now(),'hello word');
将employee表的sal字段全部改为2000
update employee set sal=2000;
将名字为zs的用户的sal字段设置为3000
update employee set sal=3000 where name='zs'
将名字为wu的用户sal字段在原来的基础上加100
update employee set sal=sal+100 where name='wu'
三、删除数据
删除数据语法:
delete from 表明 where 条件
删除数据原则:
1、 如果不使用where 子句,将删除表中所有数据
2、 delete语句不能删除某一列的值(可使用update)
3、 delete仅仅删除记录,不删除表本身,如要删除表,使用drop table语句
4、 同insert和update一样,从一个表中删除一条记录将引起其他表的参照完整性问题
5、 删除表中的数据也可以使用truncate table语句
mysql 事务
1、 mysql控制台是默认自动提交事务(dml)
2、 如果我们要在控制台中使用事务,请看下面:
mysql 删除数据是自动提交的
mysql> set autocommit=false;
Query OK, 0 rows affected (0.00 sec)
mysql> savepoint aaa;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from employee;
Query OK, 3 rows affected (0.05 sec)
mysql> select * from employee;
Empty set (0.00 sec)
mysql> rollback to aaa;
Query OK, 0 rows affected (0.06 sec)
mysql> select * from employee;
+------+------+------+------------+--------+------------+------------+
| id | name | sex | birthday | salary | entry_date | resume |
+------+------+------+------------+--------+------------+------------+
| 1 | aaa | | 1977-11-11 | 56.8 | 2014-11-10 | hello word |
| 2 | bbb | | 1977-11-11 | 57.8 | 2014-11-10 | hello word |
| 3 | ccc | | 1977-11-11 | 56.3 | 2014-11-10 | hello word |
+------+------+------+------------+--------+------------+------------+
3 rows in set (0.00 sec)
希望本文所述对大家MySQL数据库计有所帮助。
标签:MySQL,数据,增,删,改
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解决90%的常见问题的8个python NumPy函数
2021-12-06 23:33:39
![](https://img.aspxhome.com/file/2023/3/131393_0s.png)
减少用户的思考
2010-09-07 12:14:00
![](https://img.aspxhome.com/file/UploadPic/20109/7/01-50s.jpg)
python+selenium实现登录账户后自动点击的示例
2021-07-26 05:40:21
python 批量修改/替换数据的实例
2021-03-15 14:18:36
vue3缓存页面keep-alive及路由统一处理详解
2024-05-02 16:34:12
![](https://img.aspxhome.com/file/2023/4/130234_0s.gif)
如何使用Git实现切换分支开发过程解析
2022-07-03 20:57:06
![](https://img.aspxhome.com/file/2023/2/132262_0s.png)
MySQL查询性能优化索引下推
2024-01-24 15:43:01
![](https://img.aspxhome.com/file/2023/4/75644_0s.png)
mysql 8.0.15 winx64安装配置方法图文教程
2024-01-13 04:44:24
![](https://img.aspxhome.com/file/2023/9/109819_0s.jpg)
Python的collections模块中的OrderedDict有序字典
2023-06-21 00:13:52
详解PHP设计模式之桥接模式
2023-05-30 10:29:02
Sql Server中存储过程中输入和输出参数(简单实例 一看就懂)
2012-11-30 20:09:36
pytorch dataloader 取batch_size时候出现bug的解决方式
2023-08-12 01:27:45
Python趣味挑战之给幼儿园弟弟生成1000道算术题
2021-08-13 07:18:56
![](https://img.aspxhome.com/file/2023/0/127660_0s.png)
Python fire模块(最简化命令行生成工具)的使用教程详解
2022-06-10 15:25:00
![](https://img.aspxhome.com/file/2023/0/105280_0s.png)
一篇文章彻底弄懂Python中的if __name__ == __main__
2023-04-27 08:42:14
CSS样式表中SPAN和DIV的区别
2007-10-21 08:47:00
![](https://img.aspxhome.com/file/UploadPic/200710/21/2007102185219219s.jpg)
在VScode中创建你的代码模板的方法
2022-08-28 10:52:09
![](https://img.aspxhome.com/file/2023/0/132390_0s.jpg)
MySQL优化之数据类型的使用
2009-03-16 17:12:00
sqlserver中with(nolock)深入分析
2023-07-20 21:28:33
FCKEditor v2.6 编辑器配置图解教程
2024-01-04 22:16:05
![](https://img.aspxhome.com/file/2023/8/70638_0s.jpg)