sql语句

创建数据库

create database [IF NOT EXISTS] 数据库名;

创建一个名称为mydb1的数据库;

create database mydb1;

查看所有数据库

show databases;

创建一个使用utf-8字符集的mydb2数据库

create database mydb2 character set utf8;

创建一个使用utf-8字符集的mydb2数据库,并带校对规则的mydb3数据库

 create database mydb2 character set utf8 collate utf8_general_ci;

查看库的创建信息

show create database mydb3;

删除指定数据库

drop database 数据库名;

查看数据库中的数据库,并把其中一个库的字符集改为gb2312

alter database mydb2 character set gb2312;

备份库

1.准备数据库

create database    mydb3;

use mydb3;

create table test(

id int                                                                                                                       

);

insert into test(id) values(1);

2.备份库

 2.1 退出mysql客户端:quit

2.2 在windows命令行窗口中下执行:mysqldump -uroot -p mydb1>c:\test.sql

3.删除库:drop database mydb3;

4.恢复库

4.1创建库:create databse mydb3;

4.2 source c:\test.sql(通过执行脚本文件实现)

5.恢复库(2)mysql -u root -p mydb3<c:\test.sql


创建表

创建一个员工表
create table employee
(
id int,
name varchar(20),
gender varchar(4),
birthdat date,
entry_date date,
job varchar(40),
salary double,
resume text
)character set utf8 collate utf8_general_ci;

修改表
alter table 表名
add
(
column_name datatype,
...
);
在上面的员工表的基本上增加一个Image列
alter table employee add image blob;

修改job列,使其长度为60
alter table employee modify job varchar(60);

删除sex列
alter  table employee drop sex;

表名改为user
alter table employeee rename to user;

修改表的字符集为utf-8
alter table user character set gb2312;
show create table;

列名name改为username;
alter table user change name username varchar(20);

Insert

字符和日期型数据应包含在单引号中


使用Insert语句想表中插入一个员工信息
insert into table employee(id,username,birthday,entry_date,job,salary,resume) values(1,'aaa','1980-08-08','xxx','10000','asdsada');

update

将所有员工的薪水修改为5000
update employee set salary =5000;

将姓名为‘aaa'的员工薪水修修改为3000
update employee set salary =3000 where  username='aaa';

将姓名为‘aaa'的员工薪水修修改为4000,job该为ccc
update employee set salary=4000,job=ccc where username ='aaa';

将aaa的薪水在原有的基础上增加1000
update employee set salary=salary+1000 where username='aaa';

Delete

delete不能删除某一列的值(可使用update)
删除表中数据也可以使用truncate table语句

删除表中名称为'zs'的记录
delete from employee where username='小李子';

删除表中所有记录
delete from employee;

使用truncate删除表中记录
truncate table employee;

Select

查询表中所有学生的信息
select id,name,chinaese,english,math,from student;
select * from student;

查询表中所有学生的姓名和对应的英语成绩
select name,english from student;

过滤表中重复数据
select distinct name, english from student;

在所有学生英语分数上加十分特长分
select  name, english+10 from student;

统计每个学生的总分
select name ,(english+math+chinese) from student;

使用别名表示学生分数
select name as 姓名,(english+math+chinese)  as 总分 from student;

使用where字句过滤查询
select * from student where name='王五';

查询英语成绩大于90分的学生
select * from  student where english >90;

查询总分大于200分的所有学生
select * frrom student where (english+math+chinese)>200;


在where字句中使用运算符

查询英语分数在80-90之间的同学
select * from student where english>80 and english<90;
select * from student where english between 80 and 90;

查询数学分数为89,90,91的同学
select * from student where manth =89 or math =90 or math =91;
select * from student where math in(90,90,91);

查询所有姓李的学生成绩
slect * from student where name =like '李%’;

使用order by字句排序查询
1.order by指定排序的列,排序的列即可是表中的列,也可以是select 语句后指定的列名
2.Asc是升序,Desc是降序
3.order by 字句应该位于select 语句的结尾

对数学成绩排序后输出
select name,math from student  order by  math;

对总分排序后输出,然后再按从高到低的顺序输出
select name from student order by (emglish +chinese+math) desc;

对姓李的学生成绩排序输出
select name 姓名 ,(english+chinese+math) 总分 from student  where name='李%' order by(english+chinese+math) desc;

合计函数

统计一个班有多少学生?
select count(* ) from student ;
select count(name) from student;

统计数学成绩大于90的学生有多少个
select count(*) from student where math >90;

统计总分大于250的人数
select count(*) from student where (english+math+chinese)>250;

统计一个班级的数学总成绩
select sum(math) from student;

统计一个班级语文、数学、英语各科的总成绩
select sum(math),sum(english),sum(math) from student;

统计一个班级语文、数学、英语的成绩综合
select sum(english+math+chinese) from student;

统计一个班级语文成绩平均分
select sum(chinese)/count(chinese) from student;

注意:sum函数仅对数值起作用,否则会报错
注意:对多列求和,‘,’不能少

求一个班级数学平均分
select avg(math) from studentl;

求一个班级总分平均分
select avg(english+math+chinese) from student;

合计函数MAX/MIN

求班级最高分和最低分
select  max(english+chinese+math),min(english+chinese+math) from student ;

idproductprice
1电视900
2洗衣机100
3洗衣粉90
4橘子9
5洗衣粉90
对订单表中商品归类后,显示每一类商品的总价
select product,sum(price) from orders group by product;

使用Havding字句过滤where
查询购买了几类商品,并且每类总价大于100的商品
select product from orders group by product having(where后面不能跟合计函数) sum(price) >100;

注意:having 和where均可实现过滤,但在having可以使用合计函数,having通常跟在group by 后,它作用于组。
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值