学习笔记:MySQL——表查询和常用函数

SQL

SQL全称叫Structured Query Language,结构化的查询语言。

作用:是为了统一/屏蔽不同数据库厂商生产的数据库产品之间的差异。方便于出现项目后期进行数据库更换,那么此时就不需要重写操作数据库的语句了,虽然还有一定的差别,但比全是差别要好多了。

DDL基础

  • 更改表名

     	语法结构
     		alter table 原表名 rename 新表名;
     	示例
     		alter table teacher rename t_teacher;
    
  • 更改字段名

     	语法结构
     		alter table 原表名 change 列名 新列名 数据类型; 
     		更改表的列名 和 数据类型 当然数据类型可以不改,但是必须得写
     	示例
     		alter table t_teacher change name teacher_name varchar(20);
    
  • 添加字段

     	语法结构
     		alter table 表名add 列名     数据类型;
     	示例
     		 alter table t_teacheradd birthday datetime;默认添加到尾部
     		alter table t_teacheradd birthday datetime after teacher_name; 把列添加到指定列的后面
     		alter table t_teacheradd sex2 char(2) first; 添加到第一列
    
  • 删除字段

     	语法结构
     		alter table 原表名 drop 列名;
     	示例
     		 alter table t_teacher drop birthday;
    
  • 更改字段类型

     	语法结构
     		alter table 原表名 modify 列名 新数据类型; 
     	示例
     		alter table t_teacher modify sex2 varchar(20);
     		alter table 表名 modify 列名 数据类型 comment '该列的注释说明';  更改类型的同时,还能添加注释说明
    
  • 展示建表语句

     	show create table 表名;
    

DDL增强版

约束健分类

  • 主键;primary key
  • 外键;foreign key
  • 唯一;unique
  • 非空;not null
  • 自增;auto_increment
  • 默认值;default

主键

主键通常用于唯一确定表中的一条记录,设置为主键的字段是不能为空且唯一(不能重复)。

主键可以设置在一个字段上,也可以设置在多个字段上。(但大多数场景都是设置在一个字段上,这个字段通常是业务主键或者流水号)
创建表语句时,添加主键约束

				create table person(
        id int ,
        name varchar(100),
        income decimal(18,2),
        primary key (id,name) 
    ); 添加两个主键
				 create table person1(
        id int ,
        name varchar(100),
        income decimal(18,2),
        primary key (id) 
    ); 添加一个主键
				 create table person2(
        id int primary key,
        name varchar(100),
        income decimal(18,2)
      );  添加一个主键 也可以直接写在列后面

创建表完成之后,通过alter添加主键约束

create table person3(
        id int ,
        name varchar(100),
        income decimal(18,2)
      ); 创建表
				 alter table person3 add primary key(id); 添加约束

主键自增

auto_increment,自增就是自动增长,不用手动设置,自增的列,必须为主键列
建表时添加

create table person4(
	id int auto_increment ,
	name varchar(200),
	 primary key(id)
);

创建后添加

create table person5(
	id int ,
	name varchar(200),
	 primary key(id)
);
alter table person5 modify id int auto_increment;

设置自增起始值

	语法
		alter table 表名auto_increment=; 
	示例
		alter table person6 auto_increment=10000; 

外键

一个表中的外键列,需要参照另一个表的主键值生成,并且一个表的外键列的值和另一个表的主键值得数据类型必须一致,
创建时添加

create table teacher(
    id int ,
    name varchar(20),
    primary key (id)
);
create table student (
    id int ,
    name varchar(20),
    teacher_id int ,
    primary key (id),
    foreign key (teacher_id) references teacher(id)
);

创建后添加

create table student1 (
    id int ,
    name varchar(20),
    teacher_id int,
    primary key (id)
);
create table teacher1(
    id int ,
    name varchar(20),
    primary key (id)
);
alter table student1 add foreign key (teacher_id) references teacher1 (id);

语法 :  alter table 表名 add foreign key (外键列列名) references 指向的表名 (主键列列名);

注意 !!引用student中添加外键列,指向teacher表,所以必须先创建teacher表才行

唯一约束

unique,唯一约束是指定table的行和行特定的值不能重复,保证数据的唯一性。
注意:唯一约束不允许出现重复的值,但是可以有多个null.
创建表时,添加unique约束

create table temp (
    id int ,
    `name` varchar(20),
    unique(id)
);

创建表之后,添加unique约束

create table temp1 (
    id int ,
    `name` varchar(20)
);
alter table temp1 add unique (id);

非空和默认值

not null,非空;default ,默认值。所有的类型的值都可以是null,包括int、float 等数据类型,
设置为not null的字段,必须填入数据;经常和default一起使用,当不填写数据的时候,默认值为什么

创建时添加

create table temp2(
    id int not null,
    `name` varchar(30) default  'abc',
	sex varchar(10) not null default '男'
);

创建后添加

create table temp3(
    id int,
    `name` varchar(30) ,
	sex varchar(10) 
);
alter table temp3 modify id int not null ;
alter table temp3 modify name  varchar(30)   default  'abc';
alter table temp3 modify sex varchar(10) not null  default '男';

DQL基础

介绍
DQL : Data Query Language,数据查询语言,主要用于查询表。
它通常用来从一张表或者多张表(视图或者子查询等)中按指定的条件筛选出某此记录。涉及到的命令有select。

语法
select 列限定 from 表限定 where 行限定;

示例代码

		create table teacher(
		id int,
		`name` varchar(30)
);
insert into teacher (id,name) values (1,'张老师');
insert into teacher (id,name) values (2,'王老师');
	会查询到teacher表中所有的数据
	select * from teacher;
	查看所有行的name
		select name from teacher;
	查看id为1的讲师姓名
		select name from teacher where id = 1;

条件判断等

介绍
条件判断主要用于where限定,用于确定我们想要操作的数据,比如上面的where id = 1

and

且,和,的意思,一般用于 必须符合两个添加的判断,等同于java中的 &&
语法
select 列限定 from 表限定 where A表达式 and B表达式;
示例

查询学生表中,name是张三且成绩大于90select * from student where name='张三' and score > 90;

or

或的意思,一般用于 符合一个添加判断的情况下,等同于java中的 ||
语法
select 列限定 from 表限定 where A表达式 or B表达式;
示例

查询学生表中,name是张三 或 成绩大于90select * from student where name='张三' or score > 90;
	andor优先算and,有点优先级的意思,不管顺序

关系表达式

  • 大于 >

  • 大于等于 >=

  • <:小于

  • <=:小于等于

  • <>:不等于

  • =:相等

示例

			查询学生表中,成绩大于90分的
				select * from student where  score > 90;
			查询学生中,成绩为空的学生
				判断为空不能使用 =null ,应该使用 is null
				select * from student where  score is null;
			查询学生中,成绩不为空的学生
				判断不为空 不能使用 <>null,应该使用 is not null
				select * from student where  score is not  null;

between and

	在...之间

语法
select 列限定 from 表限定 where 列名 between 值1 and 值1;
示例

查询学生表中 成绩在90100之间 (包含98100)
select * from student where score >= 98 and score<=100;  
等价于
select * from student where score between 98 and 100;

in

在指定数据中
语法
select 列限定 from 表限定 where 列名 in(值1,值2…);
示例

给出一个数据集合(1,3,10,20),获取学生id在这个数据集合中的学生信息
			select * from student where id in (1,3,10,20);

模糊查询(like)

  • % 匹配任意字符

  • _ 匹配单个字符

语法
select 列限定 from 表限定 where 列名 ‘值’ ;
示例

			把name中,把姓张的查询出来
				select * from student where  name like '张%';
			把 name中,姓名有两个字的查询出来
				select * from student where  name like '__';

单表查询操作

Order by 排序

能够让我们查询的数据进行排序展示
语法
select 列限定 from 表限定 order by 列名 asc/desc;

  • Asc : 升序

  • Desc : 降序

  • 不写默认升序

示例

查询所有学生信息,以成绩降序
	select * from student order by score desc;
查询所有学生信息,按成绩降序,如果成绩相同,按照id升序
	select * from  student order by score desc , id asc;

Limit

限制条数,通常和order by一起使用,因为我们使用排序之后,再去获取前几条数据,比较有价值,比如成绩前三名
语法
select 列限定 from 表限定 limit 条数;
select 列限定 from 表限定 limit 开始值(不包含) ,条数;
示例

查询学生表,分数前三名的信息
				select * from  student order by score desc limit 3;
			查询学生表,分数第二名和第三名
				select * from  student order by score desc limit 1,2;
	group by分组

常用组函数

MYSQL中有一类特殊的函数,用于统计,或者分组统计,
分组关键字使用 group by

  • count(*) : 总条数

  • max(字段名) : 最大值

  • min(字段名) : 最小值

  • avg(字段名) : 平均值

  • sum(字段名) : 总和

示例数据

create table student (
    id int ,
    name varchar(20),
    teacher_id int,
    score decimal(18,2) ,
    primary key (id)
);
create table teacher(
    id int ,
    name varchar(20),
    primary key (id)
);
insert into teacher (id,name)values(1,'张老师');
insert into teacher (id,name)values(2,'王老师');
insert into student (id,name,teacher_id,score)values(1,'张三',1,90);
insert into student (id,name,teacher_id,score)values(2,'李四',2,88.9);
insert into student (id,name,teacher_id,score)values(3,'王五',1,45.7);
insert into student (id,name,teacher_id,score)values(4,'赵六',1,84);
insert into student (id,name,teacher_id,score)values(5,'小明',2,92.5);
insert into student (id,name,teacher_id,score)values(6,'小红',2,47);
		语法
			select  count(*),max(字段名),min(字段名)... from 表名 group by 字段名;
		示例
			查看学生表共有多少学生
				select  count(*)  from   student;
			查看学生表中分数大于90分的有多少学生
				select  count(*)  from   student where score > 90;
			查询每个老师分别带了多少学生(显示老师id即可)
				select teacher_id, count(*) as stu_count  from student group by teacher_id;
			查询每个老师带的学生中的最高分数
				select teacher_id, count(*) as stu_count,max(score) as stu_max_score from student group by teacher_id;
			查询每个老师所带学生的总成绩与平均分
				select teacher_id, sum(score) as sum,avg(score) as avg from student group by teacher_id;

having过滤

对分组的结果进行过滤
示例

每个讲师所带学生的平均分大于60select teacher_id, avg(score) as avg from student group by teacher_id having avg > 60;

猜你喜欢

转载自blog.csdn.net/lfz9696/article/details/107795722
今日推荐