MySql基础

sql 完成对表中数据的CRUD 的操作

插入数据

insert into 表名(列名1,列名2,列名3values(值1,2,3;
insert into student(sid,sname,sex,age) values (1,'zhangsang',1,23)
--简单写法: 如果插入是全列名的数据,表名后面的列名可以省略
insert into student values (2,'zhangsang',2,23);
-- 注意:如果是插入部分列的话,列名不能省略
insert into sstudent (sid,sname) values(3,'lisi');
insert into student values3,'lisi';//这种写法是错误的

--批量插入
insert into student values 
(4,'zhangsi',1,23),
(5,'zhangsi',1,23),
(5,'zhangsi',1,23),
(7,'zhangsi',1,23);

--单条插入和批量插入
插入三条数据

--查看表中数据
select * from student;
  • 命令行下插入中文问题: insert into student values(11,‘李四’,1,24);
    • 临时解决方案:set name gbk;相当于是高速mysql服务器软件,我们当前命令行下输入的内容是gbk编码,当命令窗口关闭之后,它再输入中文就会存在问题
    • 永久解决办法:修改my.in配置(在mysql软件安装路径里)
      • 暂停mysql的服务
      • 在mysql安装路径中找到my.in配置文件:C:Program File\MySQL\MySQLService 5.5
      • 将57行的编码改为gbk
      • 保存文件退出
      • 启动mysql

删除表中数据

delete from 表名 [where 条件]

delete from student where sid=1;
delete from student where 如果没有指定条件 会将表中的数据一条一条全部删除掉
--面试问题:请说一下delete删除数据和truncate删除数据有什么差别
清空表中所有数据
delete :DML 一条一条删除表中的数据。
truncate :DDl:先删除表在重建表,
关于哪条执行语句效率高:具体要看表中的数据量
	如果比较少,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. 分类描述
	. 1,2
	create table category (
	cid int primary key auto _increment,
	cname varchar(10),
	cdesc carchar(31)
	);
	insert into category values(null,'手机数码',电子产品);

	select * from category;
	select cname,desc 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,'小米x4'998null1);
--简单查询:
--查询所有的商品:
select * from product;
--查询商品名称和商品价格:
select pname,price from product;
--别名查询,as关键字,as关键字可以省略
	--表别名:select p.name ,p.price  from product p;(主要用于多表查询);
	select p.name,p.price from product as p;
	--列别名:select pname as 商品名称,price as 商品价格 from product;
	select pname as 商品名称,price as 商品价格 from product;
	省略as关键字
	select pname 商品名称,price 商品价格 from product;
--去掉重复的值
	--查询商品的所有价格
	select price from product;
	select distinct price from product;
--select 运算查询:仅仅在查询结果上做了运算+、* /
	select * ,price1.5 from product;	
	select * ,price1.5 as 折后价 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;
	between...and...
	select from product where price between 10 and 100;
	--逻辑运算:and,or,not
	--查询出商品价格 小于100 或商品价格大于900
	select from product where price <100 or price >900;
	--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 desc;
	--1. 查询所有商品,按照价格进行降序排序(asc-升序 desc -降序);
	select from product order by price desc;
	--2.查询名称有 小 的商品
	select from product where pname like '%小% ';
	
--聚合函数
	--1.获得所有商品价格的总和:
	select sum(price)from product ;
	--2.获得商品的平均价格
	select avg(price )from product;
	--3.获得所有商品的个数:
	select count(*from product;
	--注意:where后面不能接聚合函数
	--查出商品价格大于平均价格的所有商品
	查出商品价格
	大于平均价格的所有商品
	select from product where price >avg(price);
	--子查询
	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... O
select...from ...where ... group by... having..order by 
--执行顺序
F..W..G..H..O
from...where ... group by ... having ..select ..order by 
	
--


	
	

2019/09/21

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值