sql完成对数据库表数据的CRUD操作
插⼊数据:
insert into 表名(列名1,列名2,列名3) values (值1,值2,值3);
双老太婆原型是三个女人例⼦:insert into student(sid,sname,sex,age) values (1,'zhangsan',0,21);[写列名]
简单写法: insert into student values(2,'lisi',1,32);[不加列名]
注:简单写法,如果插⼊式全列名的数据,表名后⾯的列名可以省略.
id不要重复,否则会报以下错误:
插⼊其中⼏列:
insert into 数据库表名(列名1,列名2) values (值1,值2);[对的]
例⼦:insert into student(sid,sname) values (3,'wangwu');[对的]
insert into 数据库表名 (sid,sname) values (值1,值2)[错的]
注:如果是插⼊部分列的话,列名不能省略.
批量插⼊:openstack三种网络模式
insert into 数据库表名 (列名1,列名2,列名3) values (值1,值2,值3),(值1,值2,值3),(值1,值2,值3);
例⼦:insert into student(sid,sname,sex,age) values (1,'zhangsan',0,21),(2,'zhangsan',0,21),(3,'zhangsan',0,21);
注:单条插⼊与批量插⼊的效率:
批量插⼊的效率更⾼.假设需插⼊10条数据,单条插⼊:插⼊10次即可.批量插⼊:⼀次性插⼊10条即可,但,如果,中间有数据错了,接下来未插⼊的数据就不会执⾏了.
查看表中数据:
select * from 表名;
例⼦:select * from student;
删除记录:
delete from 表名 [where 条件];
例⼦:delete from student where id = 2;
delete from 表名;
如果没有指定条件,会将表中数据⼀条⼀条全部删除掉.
例⼦:delete from student;
⾯试问题:请说⼀下,delete 删除数据和truncate 删除数据有什么差别?
delete:是⼀个关键字,属于DML,⼀条⼀条删除表中的数据.
truncate:属于DDL,先删除表在重建表.
关于哪条执⾏效率⾼:具体要看表中的数据量
如果数据⽐较少,delete⽐较⾼效.
如果数据⽐较多,truncate⽐较⾼效.
更新表记录:
update 表名 set 列名=列的值,列名2=列的值2 [where 条件];
例⼦:将sid为5的名字改为李四.
如果参数是字符串,⽇期,要加上单引号,如果是数字,直接写即可.
update student set sname='李四' where id=5;[可⾏]
update student set sname='李四',sex=0;[可⾏,不加where条件会将表中sex列的值都改为0]查询记录:
select [distinct] [ *  ] [列名1,列名2] from 表名 [where条件];
distinct:去处重复数据
select:选择显⽰哪些列的内容
商品分类:⼿机数码,鞋靴箱包
1.分类ID
2.分类名称
java环境变量path和classpath的作用分别是什么3.分类描述
create table cayegory(
cid int primary key auto_increment,
cname vatchar(30),
cdesc varchar(50)
);
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(
kotlin入门教程
pid int primary key auto_increment,
pname varchar(20),
price double,
pdate timestamp,
cno int
);
insert into product values(null,'⼩⽶',998,null,1);
insert into product values(null,'⾏李箱',200,null,2);
insert into product values(null,'葡萄酒',666,null,3);
insert into product values(null,'⽠⼦',10,null,4);
简单查询:
查询所有商品:
select * from product;
查询商品名称和商品价格:
select pname,price from product;
别名查询 as关键字 as关键字可省略.
表列名:select p.pname,p.price from product p;(主要是⽤在多表查询)[不使⽤as关键字] select p.pname,p.price from product as p;[使⽤as关键字]
列别名:select pname as 商品名称,price as 商品价格 from product;
select pname,price from product;
select pname as 商品名称,price as 商品价格 from product;[使⽤as关键字]
select pname  商品名称,price  商品价格 from product;[不使⽤as关键字]
去掉重复的值:
查询商品所有的价格:
select price from product;
select distinct price from product;[使⽤distinct关键字去重]
select运算查询:[仅仅在查询结果上做了运算 + - * / ]
select *,price*1.5 from product;
select *,price*1.5 as  折后价 from product;
select *,price*0.8 from product;//打⼋折
条件查询[where关键字]:
where关键字:指定条件,确定要操作的记录.mysql语句的执行顺序
查询商品价格>60元的所有商品信息:
select * from product where price > 60;
where后的条件写法:
关系运算符:> >= < <= = != <>
<>:不等于标准sql语法
!=:不等于⾮标准sql语法[sqlserver]
查询商品价格不等于88的所有商品价格
select * from product where price <> 88;
select * from product where price != 88;
查询商品价格在10到100之间:
select * from product where price > 10 and price < 100; between ...关键字:
select * from product where price between 10 and 100;
逻辑运算 :and ,or ,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 降序
查询所有商品,按照价格进⾏排序
select * from product order by price;[结果默认是升序]
查询所有的商品,按照价格降序排序(asc-升序,desc-降序) select * from product order by price desc;
查询名称有⼩的商品,按照价格降序排序:
1.查询出名称有 ⼩ 的商品
select * from product where pname like '%⼩%';
2.进⾏排序,得出结果
select * from product where pname like '%⼩%' order by 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.条件  ⼤于平均价格网络编程项目源码
select avg(price) from product;
3.两个结果合在⼀块(⼦查询)
select * from product > (select avg(price) from product);[对的] select * from product where price > avg(price);[错的]
分组:group by
1.根据cno字段分组,分组后统计商品的个数
select cno,count(*) from product group by cno;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。