MySQL第二天介绍

SQL
    SQL介绍
        SQL它的全称叫 Structured Query Language,结构化的查询语言。之所以出现这个东西,是为了统一/屏蔽不同数据库厂商生产数据库产品之间的差异
    简单的DDL
        比如更改表名
    alter table  表名   rename   新表名
        更改字段名
     alter table 表名  change   列名  新列名   数据类型
更改表的列名 和数据类型  当然数据类型可以不改,但是必须写
      alter  table  t_teacher  change  teacher  name   varchar(20);
        添加字段
    alter  table   表名  add  列名类型
    如 alter  table  t_teacher  add  birthday  datetime  ;默认添加到尾部
     alter table t_teacher  add  birthday   datetime  after  teacher   name;把列添加到指定列的后面
     alter  table t_teacher  add 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  表名;
    主键设置
        第一种:创建表语句时,添加主键约束
        第二种:创建表完成之后,通过alter添加主键约束

代码示例

第一种:创建表语句时,添加主键约束

	create table  person(

		id int, 

		name varchar(100),

		income  decimal(18,2),

		primary  key  (id)

)

上面代码设置了一个主键

如果只有一列主键,也可以直接写在字段后面

create table  person2(

		id  int  primary  key,

		name  varchar(100),

)
第二种:创建表完成之后,通过alter 添加主键约束

create table person3(

		id  int, 

		name  varchar(100),

		income  decimal(18,2)

);

alter  table  person3  add  primary  key(id);

    主键自增
        第一种:建表是添加自增
        第二种:创建表之后添加自增
        设置自增的起始值

代码示例

第一种创建完表时
create table person4(

	id int auto_increment ,

	name varchar(200),

	 primary key(id)

);

测试语句 : 

insert into person4(name)values('测试');

并未输入id的值,但是可以自动填充
第二种创建完表之后

语法 : alter table 表名modify 主键列名 类型 auto_increment;

create table person5(

	id int ,

	name varchar(200),

	 primary key(id)

);

alter table person5 modify id int auto_increment;

测试语句 : 

insert into person5 (name)values('测试');

并未输入id的值,但是可以自动填充

    关联完整性(外键)
        第一种:创建表时添加外键约束
        第二种 : 创建完表之后,添加外键约束

第一种 : 创建表时添加外键约束

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)

);

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

测试语句

添加一个讲师

insert into  teacher (id,name) values(1,'张老师');

添加一个学生小明,学生通过teacher_id可以指向张老师

insert into  student (id,name,teacher_id) values(1,'小明',1);

添加一个学生小红,teacher_id没有设置值

insert into  student (id,name) values(2,'小红');

添加一个小黑,teacher_id指向一个不存在的讲师,报错

insert into  student (id,name,teacher_id) values(3,'小黑',2);

第二种 : 创建完表之后,添加外键约束

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 表名 add foreign key (外键列列名) references 指向的表名 (主键列列名);

alter table student1 add foreign key (teacher_id) references teacher1 (id);

    唯一约束  unique
        第一种 : 创建表时,添加unique约束
        第二种 : 创建表之后,添加unique约束
    非空约束  not  null   与    默认值   default
        第一种:创建表时,添加约束
        第二种:创建表之后,添加约束
    基础DQL
        DQL : Data Query Language,数据查询语言,主要用于查询表。
        它通常用来从一张表或者多张表(视图或者子查询等)中按指定的条件筛选出某此记录。涉及到的命令有select。
        语法 : 
            select 列限定 from 表限定 where 行限定;
        最简单粗暴的一个DQL : 
            select * from teacher;
            会查询到teacher表中所有的数据
        如果想查看所有行的name
            select name from teacher;
        如果想查看id为1的讲师姓名
            select name from teacher where id = 1;
    条件判断
        and
            且,和,的意思,一般用于 必须符合两个添加的判断,等同于java中的 &&
            语法 : 
                select 列限定 from 表限定 where A表达式 and B表达式;
            如 : 查询学生表中,name是张三且成绩大于90分
            select * from student where name='张三' and score > 90;
            只会查询出符合两个条件的学生
        or
            或的意思,一般用于 符合一个添加判断的情况下,等同于java中的 ||
            语法 : 
                select 列限定 from 表限定 where A表达式 or B表达式;
            如 : 查询学生表中,name是张三 或 成绩大于90分
            select * from student where name='张三' or score > 90;
            只要符合两个条件中的任何一个条件,就可以
            注意 : 如果 一个语句中,同时出现了and和or的话,and优先级高
        关系表达式
            > , >= , <  , <= ,<>,=
            > : 大于
            < : 小于
            >= : 大于等于
            <= : 小于等于
            = : 相等
            <> : 不等于
            注意 : = 和 <> 额外留意,和java中有所不同,java中判断相等用 == , 这里只用 = , java中判断不相等用 != , 这里使用 <> 
        between   and
            在...之间
            语法 : 
                select 列限定 from 表限定 where 列名 between 值1 and 值1;
            如 : 查询学生表中 成绩在98到100之间 (包含98和100)
            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
            我们经常会用到搜索功能,比如百度,搜索功能实现,就是使用like模糊查询技术点
            其中 % 匹配任意个数的任意字符
             _ 匹配单个任意字符
            语法 : 
                select 列限定 from 表限定 where 列名 like  '值' ;
            如 : 把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;
        单表查询(组函数)
            常用组函数有 : 
                count(*) : 总条数
                max(字段名) : 最大值
                min(字段名) : 最小值
                avg(字段名) : 平均值
                sum(字段名) : 总和
        Group  by 
            如 : 查询每个老师分别带了多少学生(显示老师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
            刚才我们使用group by 和 组函数,可以对数据进行分组查询,并且也可以查询到平均值等数据
            但是有时候我们也需要做一些判断,比如求出平均值了,我只想要平均值 大于60分的平均分数,这时候用where就不行了
            select teacher_id, avg(score) as avg from student  where avg > 60  group by teacher_id;
            这个时候就需要使用having进行过滤
            select teacher_id, avg(score) as avg from student group by teacher_id having avg > 60;