MySql基础 --- DML操作表

Data Manipulation Language(DML数据操纵语言),如:对表中的记录操作增删改

sql语句示例

-- -----------------------------DML操作表--------------------------
-- ------------------------------创建表----------------------------
create table product(
pid int primary key auto_increment,   -- 只有设置了auto_increment id列才可以赋值为null
pname varchar(40) not null,
price double,
num int
);


-- ----------------------------插入记录-------------------------
-- values(id设置了自增所以可以写为null  pname商品名字  price商品价格  num商品数量)
insert into product values(null,'MAC',18000.0,10),
(null,'某为5G手机',30000,20),
(null,'某米手机',1800,30),
(null,'iPhnex',8000,10),
(null,'某果电脑',8000,100),
(null,'iPh7',6000,200),
(null,'iPh6',4000,1000),
(null,'iPh6',3500,100),
(null,'iPh5s',3000,100),
(null,'方便面',4.5,1000),
(null,'咖啡',11,200),
(null,'MAC',3000,100),
(null,'iph6s',4.5,1000),
(null,'咖啡',11,200),
(null,'矿泉水',3,500);


-- -----------------------更新记录-----------------------
-- 语法:updata 表名 set 列名 = 值,列名 = 值,列名 = 值,......[where 条件];
-- 将所有商品的价格修改为5000元
update product set price = 5000;

-- 将商品名是Mac的价格修改为18000元
update product set price = 18000 where pname = "MAC";

-- 将商品名是Mac的价格修改为17000,数量修改为5
update product set price = 17000, num = 5 where pname = "MAC";

-- 将商品名是方便面的商品的价格在原有基础上增加2元
update product set price = price + 2 where pname = "方便面";


-- ----------------------- 删除记录:-------------------------
-- 方式一 : delete from 表名 where 条件;   可以删除表中所有记录,也可以指定记录删除
-- 方式二 : truncate table 表名;   删除表中所有数据

-- 删除表中有名称为MAC的记录
delete from product where pname = "MAC";

-- 删除价格少于5001的商品记录
delete from product where price < 5001;

-- 删除表中所有记录
delete from product; -- 一条一条删除表中的记录
-- 或者
truncate table product;  -- 直接删除表,然后创建一个和之前结构一模一样的表



-- -------------------DQL操作表记录--查询------------------

-- 基本查询 ---> 语法:
-- 查询所有的列:select * from 表名;
select * FROM product;

-- 查询某张表特定列 :select 列名,列名,...from 表名;
select pname,num from product;

-- 去重查询:select distinct 列名 from 表名;
select distinct pname from product;

-- 别名查询:select 列名 as 别名 from 表名 as 别名; -- as 可以省略不写
select pname as 商品名 from product as 商品名;\
select pname 商品名 from product 商品名;

-- 运算查询 (+-*/ 等) select 列运算 from 表名;
select * from product where price = price + 2;
-- 基本条件查询:select ... from 表名 where 条件;
-- 		比较运算符 : >  、 <  、 <=  、 >=  、 =  、 <>	
-- 1、=表示 等于;<> 表示不等于;(注释:在 SQL 的一些版本中,该操作符可被写成 !=);> 表示大于;< 表示小于;>= 表示大于等于;<= 表示小于等于;BETWEEN表示在某个范围内;LIKE表示搜索某种模式;IN表示指定针对某个列的多个可能值。
-- 		between ... add ... 
-- 		in(值,值,...)
--    like
-- 				_ 匹配一个字符
-- 				% 匹配0到多个字符(大于等于0个)
-- 		逻辑运算符:
-- 				add...
-- 				or...
-- 				not...

--  练习:查询product表中所有的数据
select * from product;

-- 练习:查询pid,pname,price列中的数据
select pid , pname , price from product;

-- 练习:去重查询商品价格
select distinct price from product;
	
-- 练习:查询pid,pname,price列中的数据并对列取别名
select pid as id , pname as 商品名 , price as 商品价格 from product;
select pid id, pname 商品名, price 商品价格 from product;


-- 练习:查询价格大于5000的商品信息
select * from product where price > 5000;


-- 练习:查询价格大于5000的商品并且数量大于10的商品信息
select * from product where price > 5000 and num > 10;

-- 练习: 查询商品名称含有'iPh'的商品信息
select * from product where pname like '%iph%' ;

-- 练习:查询商品价格在3000到8000之间的商品信息,包含3000,8000
select * from product where price >= 3000 and price <= 8000;

-- 练习:查询pid为1,3,5,7,9,19的商品信息
select * from product where pid in(1,3,5,7,9,19)

-- 练习: 查询每种商品需要的总价
select sum(price) from product;

-- 查询商品价格>3000的商品
select * from product where price > 3000;

-- 查询id=1的商品
select * from product where pid = 1;

-- 查询id<>1的商品
select * from product where pid <> 1;

-- 查询价格在3000到6000之间的商品
select * from product where price > 3000 and price < 6000;

-- 查询id在1,5,7,15范围内的商品
select * from product where pid in(1,5,7,15)

-- 查询商品名以iPho开头的商品(iPhone系列)
select * from product where pname like 'iph%';

-- 查询商品价格大于3000并且数量大于20的商品 (条件 and 条件 and...)
select * from product where price > 3000 and num > 20;

-- 查询id=1或者价格小于3000的商品
select * from product where pid = 1 or price < 3000;

小白一枚,喜欢编程,以上内容纯手打,如有错误,请及时指正,谢谢!!!

            欢迎下次再来ヾ(≧▽≦*)o
         
               求三连(❁´◡`❁)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Donne_CN

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值