当前位置:网站首页>【MySQL】操作表DML相关语句
【MySQL】操作表DML相关语句
2022-08-01 07:10:00 【梦想new的出来】
引言
铭记于心 | ||
---|---|---|
我唯一知道的,便是我一无所知 |
DML操作表记录-增删改【重点】
- 准备工作: 创建一张商品表(商品id,商品名称,商品价格,商品数量.)
create table product(
pid int primary key auto_increment,#
pname varchar(40),
price double,
num int
);
6.1 插入记录
6.1.1 语法
- 方式一: 插入指定列, 如果没有把这个列进行列出来, 以null进行自动赋值了.
eg: 只想插入pname, price , insert into t_product(pname, price) values(‘mac’,18000);
insert into 表名(列,列..) values(值,值..);
注意: 如果没有插入了列设置了非空约束, 会报错的
- 方式二: 插入所有的列,如果哪列不想插入值,则需要赋值为null
insert into 表名 values(值,值....);
eg:
insert into product values(null,'苹果电脑',18000.0,10);
insert into product values(null,'华为5G手机',30000,20);
insert into product values(null,'小米手机',1800,30);
insert into product values(null,'iPhonex',8000,10);
insert into product values(null,'iPhone7',6000,200);
insert into product values(null,'iPhone6s',4000,1000);
insert into product values(null,'iPhone6',3500,100);
insert into product values(null,'iPhone5s',3000,100);
insert into product values(null,'方便面',4.5,1000);
insert into product values(null,'咖啡',11,200);
insert into product values(null,'矿泉水',3,500);
6.2 更新记录
6.2.1语法
update 表名 set 列 =值, 列 =值 [where 条件]
6.2.2练习 有 , .
6.2.2练习
6.2.2练习
- 将所有商品的价格修改为5000元
update product set price = 5000;
- 将商品名是苹果电脑的价格修改为18000元
UPDATE product set price = 18000 WHERE pname = '苹果电脑';
- 将商品名是苹果电脑的价格修改为17000,数量修改为5
UPDATE product set price = 17000,num = 5 WHERE pname = '苹果电脑';
- 将商品名是方便面的商品的价格在原有基础上增加2元
UPDATE product set price = price+2 WHERE pname = '方便面';
6.3 删除记录
6.3.1delete
根据条件,一条一条数据进行删除(可以回滚找回数据)
如果要清空表,那么不要使用delete,直接使用truncate清空表(销毁原表再自动创建一个,不能找回),两者都是物理删除数据
删除后
插入后id会直接从5开始,说明这还是之前那个表
但如果用truncate进行删除后再插入,如右图所示,是一张新表
- 但在实际工作中两种方法都不使用,那么如何“删除”数据呢? — — —** 逻辑删除**(新加字段,1启用,0禁用等方式)
- 语法
delete from 表名 [where 条件] 注意: 删除数据用delete,不用truncate
- 类型
删除表中名称为’苹果电脑’的记录
delete from product where pname = '苹果电脑';
删除价格小于5001的商品记录
delete from product where price < 5001;
删除表中的所有记录
delete from product;
6.3.2truncate
把表直接DROP掉,然后再创建一个同样的新表。删除的数据不能找回。执行速度比DELETE快
truncate table 表;
6.3.3 工作中删除数据
- 物理删除: 真正的删除了, 数据不在, 使用delete就属于物理删除
- 逻辑删除: 没有真正的删除, 数据还在. 搞一个标记, 其实逻辑删除是更新 eg: state 1 启用 0禁用
写在最后:
路漫漫其修远兮,吾将上下而求索!伙伴们,再见!
边栏推荐
- JSON 与 JS 对象的区别
- Win任务栏图标异常解决
- POJ2031空间站题解
- Image lossless compression software which works: try completely free JPG - C image batch finishing compression reduces weight tools | latest JPG batch dressing tools download
- 【HDLBits 刷题】Circuits(1)Combinational Logic
- Information system project managers must recite the work of the core test site (56) Configuration Control Board (CCB)
- MATLAB程序设计与应用 2.5 MATLAB运算
- GO错误处理方式
- 对于升级go1.18的goland问题
- Go 支持 OOP: 用 struct 代替 class
猜你喜欢
小程序通过云函数操作数据库【使用get取数据库】
对于升级go1.18的goland问题
"By sharing" northwestern university life service | | bytes a second interview on three sides by HR
first unique character in characters
curl (7) Failed connect to localhost8080; Connection refused
Datagrip error "The specified database userpassword combination is rejected..."Solutions
零代码网站开发利器:WordPress
MySQL row locks and gap locks
MATLAB program design and application of MATLAB 2.5
Explosive 30,000 words, the hardest core丨Mysql knowledge system, complete collection of commands [recommended collection]
随机推荐
Summary of test points about app updates in different ways
拳头游戏免版权音乐下载,英雄联盟无版权音乐,可用于视频创作、直播
日志导致线程Block的这些坑,你不得不防
配置我的kitty
Golang:go开启web服务
安装SQL Server详细教程
数据机构----线性表之单向链表
Classwork (7) - #598. remainder operation (mod)
Golang: go static file processing
响应式织梦模板园林景观类网站
响应式织梦模板园林花卉类网站
More than 2022 cattle guest school game 4 yue
POJ2421道路建设题解
The Bean's life cycle
GO错误处理方式
阿里三面:MQ 消息丢失、重复、积压问题,该如何解决?
I have three degrees, and I have five faces. I was "confessed" by the interviewer, and I got an offer of 33*15.
Golang:go获取url和表单属性值
我说过无数遍了:从来没有一种技术是为灵活组合这个目标而设计的
牛客刷SQL---2