Sql完成对表数据的CRUD操作

一、插入数据

insert into 表名(列名1,列名2...) values(值1,值2,...);

insert into student (sid,sname,sex,age) values (1,'zhangsan',1,18);

 

-->如果插入所有字段,则可以简写为:

insert into student  values (1,'zhangsan',1,18);

 

-->如果插入部分字段,则必须写列名

insert into student (sid,sname) values (3,'lisi');

 

-->批量插入

insert into student  values (4,'zhangsan',1,18),(5,'zhangsan',1,18),(6,'zhangsan',1,18),(7,'zhangsan',1,18);

 

插入中文字段出现乱码:

临时解决:set names gbk;

永久解决:暂停MySql服务--》MySql 文件夹下my.ini配置--》将57行的编码改成gbk--》保存文件并退出,启动MySql服务

查看表中数据:

select * from student;

 

二、删除数据

delete from 表名 [where 条件];

delete from student where sid=10;

delete from student;将表中的数据都被删除掉

--》delete删除数据和truncate删除数据有什么区别?

delete:一条一条的删除数据

truncate:先删除表再重建表

关于哪条执行的效率高:具体要看表中的数据量

如果数据比较少,delete效率比较高效

如果数据比较多,truncate效率比较高

 

三、更新数据

update 表名 set 列名=列的值,列名2=列的值2 [where 条件];

--将sid为5的名字改成李四

--如果参数是字符串,日期要加上单引号

update student set sname='李四' where sid=5; 

 

四、查询记录

select [distinct] [*] [列名,列名2] from 表名 [where 条件]

distinct:去除重复的数据

--商品分类:手机数码,鞋靴箱包,

1.分类的ID

2.分类的名称

3.分类描述

create table category(

cid int primary key auto_increment,

cname varchar(10),

cdesc varchar(31)

);

insert into category values(null,'手机数码','电子产品,黑马生产');

insert into category values(null,'鞋靴箱包','江南皮革厂倾情打造');

insert into category values(null,'香烟酒水','黄鹤楼,茅台,二锅头');

insert into category values(null,'酸奶饼干','哇哈哈,蒙牛酸奶乳');

insert into category values(null,'馋嘴零食','瓜子花生,八宝粥,辣条');

select * from category;

select cname,cdesc from category;

--所有商品

1.商品ID

2.商品名称

3.商品的价格

4.生产日期

5.商品分类ID

商品和商品分类:所属关系

create table product(

pid int primary key auto_increment,

pname varchar (10),

price double,

pdate timestamp,

cno int

);

insert into product values(null,'小米mix4',998,null,1);

insert into product values(null,'锤子',2888,null,1);

insert into product values(null,'阿迪王',99,null,2);

insert into product values(null,'老村长',88,null,3);

insert into product values(null,'劲酒',35,null,3);

insert into product values(null,'小熊饼干',1,null,4);

insert into product values(null,'卫龙辣条',1,null,5);

insert into product values(null,'旺旺大饼',1,null,5);

--简单查询

---查询所有商品

select * from product;

---查询商品名称和商品价格

select pname,price from product;

---别名查询,as关键字,as关键字是可以省略的

表别名:select p.pname,p.price from product as p;select p.pname,p.price from product  p;

列别名:select pname as 商品名称,price as 商品价格 from product;select pname  商品名称,price  商品价格 from product;

---去掉重复的值

    --查询所有商品的价格

select price from product;

select distinct price from product;

--select 运算查询:仅仅在查询结果上做了运算

select *,price*1.5 as 折后价 from product;

select *,price*0.8  from product;

--条件查询[where 关键字]

指定条件,确定要操作的记录

--查询商品价格>60的所有商品信息

select * from product where price>60;

--where后的条件语法

         --关系运算符:>,>=,<,<=,=,!=,<>

         <>:不等于:标准sql语法

         !=:不等于:非标准sql语法

        --查询商品价格不等于88的所有商品信息

select * from product where price <> 88;

select * from product where price != 88;

         --查询商品价格在10~100之间的所有的商品信息

select * from product where price > 10 and price < 100;

select * from product where price between 10 and 100;

         -- 逻辑运算符 or , and , not

         --查询出商品小于35或大于900

select * from product where price < 35 or price > 900;

         --like 模糊查询

_:代表一个字符

%:代表多个字符

              --查询出名字中带有饼的所有商品 '%饼%'

select * from product where pname like '%饼%';

              --查询第二名字是熊的所有商品信息 '_熊%'

select * from product where pname like '_熊%';

             --in 在某个范围中获得值

                 --查询出商品分类ID 在1,4,5里面的所有商品

select * from product where cno in (1,4,5);

 

--排序查询:order by 关键字

asc:ascend 升序

desc:descend 降序

--0查询所有商品,按照价格进行排序

select * from product order by price;

--1查询所有商品,按照价格进行降序

select * from product order by price desc;

--2查询商品名称带有 小 的商品,按照价格升序排序

 1)查询商品有 小 的商品

select * from product where pname like '%小%';

 2)   进行排序

select * from product where pname like '%小%' order by  price asc;

--聚合函数

sum():求和

avg():求平均值

count():统计数量

max():最大值

min():最小值

--1.获取所有商品的价格总和

select sum(price) from product;

--2.获取所有商品的价格平均值

select avg(price) from product;

--3.获取所有商品的数量

select count(*) from product;

 

--注意:where条件后面不能接聚合函数

--查询出所有商品价格大于平均值的商品

1.查询出所有的商品

select * from product;

2.大于

3.平均值

select avg(price) from product;

select * from product where price > (select avg(price) from product);

 

--分组:group by

     --1.根据cno字段分组,分组后统计商品的个数

select cno,count(*) from product group by cno;

     --2.根据cno分组,分组统计每组商品的平均价格 并且商品平均价格 > 60

select cno,avg(price)

from product group by cno

having avg(price) > 60;

       --having 关键字 可以接聚合函数的 出现在分组之后

       --where关键字 它是不可以接聚合函数,出现在分组之前

--编写顺序

--S--F--W--G--H--O

select -- from -- where -- group by -- having -- order by

--执行顺序

F--W--G--H--S--O

 

              

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值