MYSQL数据库知识详解(1)

目录

  1. 基本DDL语句—建表语句CREATE TABLE
  2. 基本DML语句-INSERT/UPDATE/DELECT
  3. 基本DQL语句-SELECT

DDL语句—建表语句CREATE TABLE

1、数据库
查看所有数据库
show databases;

2、使用数据库
use 数据库名;

3、查看当前使用的数据库
select database();

4、创建数据库
create database 数据库名 charset=utf-8;
例:
create database python charset=utf-8;

5、删除数据库
drop database 数据库名;
例:
drop database python;

6、数据表
1)查看当前数据库中的所有表
show tables;

2)查看表结构
desc 表名;

3)创建表
auto_increment 表示自动增长

CREATE TABLE table_name(
column1 datatype contrai,
column2 datatype,
column3 datatype,

columnN datatype,
PRIMARY KEY(one or more columns));

基本DML语句-INSERT/UPDATE/DELECT

练习:创建班级表
create table classes(
id int unsigned auto_increment primary key not null,
name varchar(10));

创建学生表
create table students(
id int unsigned primary key auto_increment not null,
name varchar(20) default 0,
height decimal(5,2),
gender enum(‘男’,‘女’’,‘保密’),
cls_id int unsigned default 0)

修改表—添加字段
alter table 表名 add 列名 类型;
例:
alter table students change birthday birth datetime not null;

修改表—修改字段:不重名版
alter table 表名 modify 列名 类型及约束;
例:
alter table students modify birth date not null;

修改表—删除字段
alter table 表名 drop 列名;
例:
alter table students drop birthday;

删除表
drop table 表名;
例:
drop table students;

查看表的创建语句
show create table 表名;
例:
show create table classes;

基本DQL语句-SELECT

1、where子句对行记录进行过滤
1.查询学生表中性别为女,体重超高60公斤的学生的所有信息
select * from stu where sex=‘女’ and weight>60;

2.查询学生表中1班或者2班中,身高超过190的学生
select * from stu where (cno=1 or cno=2) and height>190;
等效写法
select * from stu where cno in(1,2) and height>190;

3.查询学生表中3班学生里面考分在400和505.5之间的女生
select * from stu where cno=3 and sex=‘女’ and score between 400 and 505.5;
等效写法
select * from stu where cno=3 and sex=‘女’ and (score>=400and score<=505.5);

4.查询学生表中没有分配班级而且是女生的学生
select * from stu where cno is null and sex=‘女’;

5.在学生表体重低于40公斤且身高低于1.65米的学生,列出其姓名,身高,体重,总分以及总分占750分满分的百分比
select sname,height,weight,score,score/750*100 from stu where height/100<1.65;

6.在学生表中查找学生姓名,第二个字是‘候’,或者第一个字是‘张’且名字只有两个字的学生
select * from stu where sname like’候%’ or sname like '张’;

2、mysql 函数
1.char_length(str) 计算字符串长度
学生表中姓名为三个字的学生有哪些?
select sname,char_length(sname) from stu where char_length(sname)=3;

2.concat(str1,str2) 拼接字符串
学生表打印已分班的学生姓名和班级,以xxx是x班的形式打印结果
select concat(sname,‘是’,cno,‘班’) from stu where cno is not null;

3.substring 截取字符串
二班的同学都有什么姓氏?
select sname,substring(sname,1,1) from stu where cno=2;

4.round(num,n) 保留小数点后几位
计算肥胖学生许褚的BMI值,四舍五入保留2位小数,体重/身高^2
select round(weight/(height/100*height/100),2) from stu where sname=‘许褚’;

5.year(date1) 获取日期date1的年份
学生表中哪些同学是1990年出生的?
select sname,birth,year(birth) from stu where year(birth)=1990;
学生表中哪些同学是8月份出生的?
select sname,birth,year(birth),month(birth) from stu where month(birth)=8;

6.curdate() 获取当前日期
curtime() 获取当前时间
now() 获取当前的日期和时间
datediff(date1,date2) 返回date1和date2期间隔的天数

计算2018年6月1号号2018年情人节之间间隔的天数
sele datediff(‘2018-6-1’,‘2018-2-14’);

7.if(expr,v1,v2) 如果表达式expr成立,返回v1值,否则,返回v2值
如果学生高考分大于等于520,其为统招生,否则其为委培生,从学生表中查找,显示姓名,考分,类型(统招或委培)
select sname,score,if(score>=520,‘统招’,‘委培’)类型 from stu;

8.case when expr then v1 [when expr2 v2 then]…else vn end
case运算符为SQL语句增加了多重条件判断比if函数更加灵活
如果考分700以上,优秀,600以上,良好,520以上,中等,否则,较差,按照这个原则列出学生表中的学生,显示姓名,考分,和评判等级
select sname,score(case when score>=700 then ‘优秀’
when score>=600 then ‘良好’
when score>=520 then ‘中等’
else '较差’end) 等级 from stu;

9.空值问题
使用in时,值列表中有null值,不会返回null相关结果
select * from stu where cno in (2,null)
使用not in 列表中有空值时,无查询结果返回
select * from stu where cno not in (2,null);
有空值会造成意想不到的问题,需要用空值函数先对空值进行处理
常用空值处理函数
ifnull(v1,v2)–如果v1为空返回v2,否则返回v1
isnull(expr)- 如果表达式为null空值,返回1,表达式非空则返回0

3、聚合函数
max(列名)
统计出列中所有行中的最大值,列的数据类型可以是数值、字符、日期型
min(列名)
统计出列中所有行中的最小值,列的数据类型可以是数值、字符、日期型
sum(列名)
统计出列中所有行中数值的总和,列的数据类型不能是字符型
avg(列名)
统计出列中所有行中数值的平均值,列的数据类型不能是字符型
count(列名)
统计出列中所有行的数量,列中如果有空值不会被统计技术

1.找出学生中最高的身高、最轻的体重、平均高考分数、学生的总数
select max(height),min(weight),sum(score)/count(),
avg(score),count(
),count(sno),count(cno) from stu;

2.用一个查询语句找出学生中最低的身高、再显示所有人的名字
select min(height),sname from stu;

4、select语句 distinct关键字
disinct关键字查询出指定的1个或多个字段的不重复记录
查询出学生表中都有哪些班级号,要求取出重复班级号
select distinct cno from stu;
列出学生表中不重复的班号和性别
select distinct cno,sex from stu;

5、列的别名
给列起个额外的名称用来代替列原来的名称
select sname [as] 姓名 from stu; ‘as’ 可写可不写
列出2班的学生姓名,性别、生日。表头用对应的中文显示
select sname as 学生姓名,sex 性别,birth 生日 from stu where cno=2;

6.select-order by子句
队查询结果集按照order by 后面指定的1列或多列排序
分为增序和降序
增序ASC,默认可以不写
降序DESC
对于数值,增序是从小到大,降序是从大到小
对于日期和时间,增序是从远到近,降序是从近到远
对于英文字符,增序是从a到z,降序是z到a
对于中文字符,增序是按照字符集编码从小到大,降序相反,如果选择gbk编码可以按照拼音来排序
对于序列的名称,可以用以下方式
列名、列别名、列的序号、函数、表达式等

按照体重增序列出学生信息
select * from stu order by weight;

展示已分班学生信息,先按照班号增序,再按照考分降序排序
select * from stu where cno id not null order by cno,score desc;

按照拼音降序排列所有学生
select * from stu order by convert(sname using gbk)desc;

显示学生性别,姓名,生日3列信息,先按第1列,再按3列降序排序
select sex,sname,birth from stu order by 1,3 desc;

7、select-limit子句
select语句的limit子句最后执行
作用:仅显示查询结果集中部分内容
显示stu表中考分最高的3名学生
select * from stu order by score desc limit 3;

select * from stu order by score desc limit 0,3;

8、select-group by 子句
group by子句按照指定的列column_name对标数据进行分组
group by后面跟得列也叫分组特性列
使用group by后,能选择的列通常只能包括分组特性列和聚合函数

按照班号分组,列出学生表中的班号
select cno from stu group by cno;

按照班号分组,列出学生表中的班号,还要列出学生姓名
select sname,cno from stu group by cno;
查询报错
注意:学生有20人,姓名一共20行记录,班号分组去重后有4行记录,20行无法与4行拼接在一起
使用group by后,能选择的列通常只能包括分组特性列和聚合函数

按照班号分组,列出学生表中的班号,统计每一个班的平均身高,平均体重,人数,最高分,不包括没分班的那些同学
select cno 班号,avg(height)平均身高,avg(weight)平均体重,count(*)人数,
max(score)最高分 from stu where cno is not null group by cno;

按照学生出生年份分组,统计出所有学生每个年份的人数,最高分,最低分,按照年份排序
select year(birth) 出生年份,count(sno) 人数,max(score) 最高分, min(score) 最低分 from stu group by year(birth) order by 1;

找出已分班的学生中,哪些班学生的平均身高超过175,列出其班号和人数
select cno 班号,count(*) 人数 from stu where cno is not null group by cno having avg(height)>175 order by 1;

找出已分班的学生中,哪些班的学生每个人的身高都超过165,列出其班号和人数
select cno 班号,count(*) 人数 from stu where cno is not null group by cno having min(height)>165 order by 1;

统计1班的学生人数,列出班号和人数
方法1:
select cno 班号,count() 人数 from stu group by cno having cno=1;
方法2:
select 1 班号,count(
) 人数 from stu where cno=1;

猜你喜欢

转载自blog.csdn.net/weixin_43870646/article/details/85214246
今日推荐