栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Day 08 数据库多表查询

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Day 08 数据库多表查询

 

//插入中文会报错
//修改表编码
    alter table teacher convert to character set utf8;    
 

    /通知服务器客户端使用的编码是gbk
    set character_set_client=gbk;

    //通知服务器客户端查看结果集使用的编码是 gbk
    set character_set_results=gbk;
    
        数据库命令框如果有中文就乱码
     charset gbk;

数据库命令

///建库
    CREATE DATAbase 数据库名;
    
    //删除创建的数据库 
    drop database mydb1;

    //选择数据库 
    use mydb1

    //产看数据库创建细节
    show create database mydb1;

    //创建一个使用GBK的字符集的数据库
    create database mydb2 character set gbk;

数据库表的命令

    //创建表 
    create table student(id int,name varchar(20),sex varchar(20),age int,salary float(6,2),birthday date)

    //删除表
    drop table student; 

    //查看所有表 
    show tables 

    //查看表的创建细节 
    show create table student; 

    //展示表结构
    desc student 

    // 数据库中表 添加
    // 在原有的学生基础上添加address列
    alter table student add address varchar(20) 

    //数据库中表 删除
    //在原有的学生基础上删除address列 
    alter table student drop address


建表的注意事项

- AUTO_INCREMENT定义列为自增的属性,一般用于主键,数值会自动加1。

- PRIMARY KEY关键字用于定义列为主键。标识数据库记录唯一性,键值不能为空,主键也是一个特殊索引  

- UNIQ0UE KEY的用途:防止数据插入的时候重复

- ENGINE:设置存储引擎
 
- CHARSET: 设置编码


数据库的插入语言

    //插入数据 
    insert into student values(1,’zhangsan’,’nan’,19,389.10,’1999-10-10’); 
    
   数据库的删除语言

    //删除单条数据
    delete from student where id=1;

    //删除所有数据,不删除结构,会放到日志中,事务提交后才生效 
    delete from student; 

    //摧毁表,删除表中所有数据,不删除结构,立即生效 
    truncate table student;

    注意:delete from student; 与 truncate table student; 都能删除该表中所有数据,区别:前者

          删除后自增主键还在,后者主键会从1开始。


数据库的修改数据

    //设置所有人的年龄加10岁 
    update student set age=age+10 

    //修改zhangsan 为张三 
    update student set name=’张三’ where name=’zhangsan’ 

    //修改王五的salery和出生日期 
    update student set salery=100.01,birthday=’1999-10-10’ where id=3;


数据库的查询语言 DQL

    //删除student 
    drop table student 

    //创建数据库表-学生成绩表 
    create table student(id int primary key auto_increment,name varchar(20) unique 
    not null,chinese float,english float,math float); 

    //添加几条数据 
    insert into student values(1,’张三’,90,80,80); 
    insert into student values(2,’李四’,90,87,60); 
    insert into student values(3,’王五’,70,60,69); 
    insert into student values(4,’赵六’,99,90,87); 

    //查询所有学生信息 
    select * from student; 

    //查询id为1的学生信息 
    select * from student where id=1; 

    //查询id为1的学生姓名 
    select name from student where id=1; 

    //查询数学成绩大于80的同学成绩 
    select * from student where math>80 

    //查询所有学生成绩,并输出效果为 姓名 语文 英语 数学 效果,见下图: 
    select name as 姓名,chinese as 语文,english as 英语,math as 数学 from student 

    //查询所有成绩及数学分+10分 
    select *,(math+10)from student 

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

    //查询总分大于230分的同学 
    select * from student where (math+english+chinese)>230 

    //查询数学成绩在80-90之间的同学 
    select * from student where math between 80 and 90 

    //查询数学语文英语都大于80的同学成绩 
    select * from student where math>80 and english>80 and chinese >80; 

    //查询数学成绩在 80 60 90内的同学,即数学成绩有60、80、90的。 
    select * from student where math in(80,60,90); 

    //模糊查询 
    //查询所有姓名中包含张的同学 
    select * from student where name like ‘%张%’

数据库的分页查询

    limit是mysql的分页查询语法: select * from table limit m,n
    其中m是指记录从m+1开始,,N代表取n条记录。

    //取出第3条至第6条,4条记录 
    select * from student limit 2,4
 
    //查询出数学成绩由高到低前两名 
    select * from student order by math desc limit 0,2;

    ase升序  desc降序


数据库的分组查询
         
          分组 group by 

    //创建一个订单表 
    create table employee(id int,name varchar(20),sex varchar(20),age int); 
    insert into employee values(1,'sunsan','男',18);
    insert into employee values(2,'lisi','男',18);
    insert into employee values(3,'wangwu','女',19);
    insert into employee values(4,'zhaoliu','男',15); 

    //分组查询 
    select * from employee group by sex;

    //分组查询加条件 
    select * from employee group by sex having age>18;

    注意:  (1) having 条件表达式:用来分组查询后指定一些条件来输出查询结果

        (2) having作用和where一样,但having只能用于group by

数据库的报表查询
    count 个数
    sum 总数
    avg 平均数
    max 最大值
    min 最小值

    //统计班级里边有多少学生
    select count(*)from student; 

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

    //统计班级里边各科总成绩 
    select sum(math),sum(english),sum(chinese) from student 
    
    //统计所有科目的总成绩 
    select sum(math+english+chinese) from student; 

    //统计一下语文平均成绩
    select sum(chinese)/count(*) from student; 
    select avg(chinese) from student;

    //统计一下班级语文最高分和最低分
    select max(chinese) from student;
    select min(chinese) from student; 

    //报表查询订单根据名称合并后,总价格>10000的商品 
    select * from orders group by product having sum(price) >7000

数据库命令多表
 
多表联查
  
  一对一

   一张表的一条记录一定只能与另外一张表的一条记录进行对应,反之亦然

    有时候,为了业务,或者避免一张表中数据量过大,过复杂,在开发中      会进行一对一方式来设计表。

  一对多
    一个实体的某数据与另一个实体的多个数据有关联需要设计表的外键
 
    班级表中的名称对应学生表中的班级名称

   2. 创建数据库表

    - constraint 约束

        - foreign key就是表与表之间的某种约定的关系,由于这种关系
    的存在,能够让表与表之间的数据,更加的完整,关连性更强。

        - foreign key语句的式例:FOREIGN KEY(Sno)  references         Student(Sno)

        **注意:**表的外键必须是另一张表的主键
    //创建班级表
    create table class(id int primary key auto_increment,name varchar(20));
    //创建学生表
    create table student(id int primary key auto_increment,name varchar(20),sex varchar(20),class_id int,constraint foreign key(class_id) references class(id)); 
    //插入班级数据 
    insert into class values(1,'ceshiban'); 
    insert into class values(2,'kaifa'); 
    //插入学生数据 
    insert into student values(1,'zhangsan','nan',1); 
    insert into student values(2,'lisi','nan',2); 
    insert into student values(3,'jingjing','nan',2); 
    //联查 
    select * from student where class_id=(select id from class where id=2);

        删除主键信息时,当该主键字段值在外键表中存在时,该记录是不能

    删除的。---要把外键表的相关信息删除之后,才能删除。
    
        解决:创建一个中间表,专门用来维护多表之间的对应关系,通常是能够唯一标识出数据的字段

    create table teacher(id int primary key,name varchar(100)); 
    create table student (id int primary key,name varchar(100)); 
    create table teacher_student(teacher_id int,student_id int,constraint foreign key    
        (teacher_id) references teacher(id),constraint foreign key(student_id) references student(id)); 

    insert into teacher values(1,'梁老师'); 
    insert into teacher values(2,'李老师');

    insert into student values(1,”张三”); 
    insert into student values(2,”李四”); 

    insert into teacher_student values(1,1); 
    insert into teacher_student values(1,2); 
    insert into teacher_student values(2,1); 
    insert into teacher_student values(2,2);

    //查询李老师所教的学生 
    select id from teacher where name=’李老师’ 
    select student_id from teacher_student where teacher_id=id 

    select * from student where id in(select student_id from teacher_student where teacher_id 
    =(select id from teacher where name='李老师'));

    //查询张三的所有老师 
    select * from teacher where id in(select teacher_id from teacher_student where student_id
     =(select id from student where name='张三'));

     连表查询
     分类:内连接,外连接,交叉连接   
     
      初始定义表链接
      create table customer(id int primary key auto_increment,
      name varchar(20),city varchar(20)); 
      
      create table orders(id int primary key auto_increment,good_name varchar(20),
      price float(8,2),customer_id int); 

      insert into customer (name,city) values('李老师','东北'); 

      insert into customer (name,city) values('崔老师','山西'); 

      insert into customer (name,city) values('张老师','内蒙'); 

      insert into customer (name,city) values('闫老师','天津'); 

      insert into orders(good_name,price,customer_id) values('电脑',59,1); 

      insert into orders(good_name,price,customer_id) values('笔记本',88,2); 

      insert into orders(good_name,price,customer_id) values('吹风机',99,1); 

      insert into orders(good_name,price,customer_id) values('香水',300,3);

      insert into orders(good_name,price,customer_id) values('牛奶',100,6);

    交叉查询
       交叉查询,笛卡尔积查询,将所有的数据查询出来,产生临时表,比较占内存 
        生成记录=表1数据个数*表2数据个数
    select * from customer,orders; 
    select * from customer cross join orders;
    
    内连接查询
       内连接,inner join on 查询两张表,设定条件,将两张表中对应的数据查询出来,性能高
    
    select * from customer c inner join orders o on c.id=o.customer_id; 
    select * from customer,orders where customer.id=orders.customer_id; 
    select * from customer c,orders o where c.id=o.customer_id;    
    
     左外连接
    左外连接 left join on 设定条件,将两张表对应的数据查询出来,同时将左表自己没有关联    的数据也查询出来
    注意:join前面是左,后面是右    
      
    select * from customer c left join orders o on c.id=o.customer_id;
    
    右外连接
        右外连接:right join on 设定条件,将两张表对应的数据查询出来,同时将右表自己没有关联的所有数据查询出来             select * from customer c right join orders o on c.id=o.customer_id;

    联合查询
    select * from customer left join orders on customer.id=orders.customer_id having price>20;

1.三个表关联查询,展示所有信息
2.查询6C班所有学生的成绩
3.查询6B班所有学生的语文成绩
4.查询6B班所有学生的语文平均成绩
5.查询刘备所在班级的所有同学的英语平均成绩
6.查询所有班级学生的语文平均成绩

--1(1) select * FROM class,student,score where class.id=student.c_id and student.id=score.s_id;  
   (2) select * FROM class inner join teachear on class.id=teachear.c_id inner join student on teachaer.id=student.t_id;
     
--2 1 select * FROM class,student,score where class.id=student.c_id and tudent.id=score.s_id and class.cname='6C'

--3select * FROM class,student,score where class.id=student.c_id and student.id=score.s_id;  
  and class.cname='6B' and score.chinses;

--4 select avg(score.chinses) FROM class,student,score where class.id=student.c_id and student.id=score.s_id and class.cname='6B';

-- 5.select avg(score.english) FROM class,student,score where class.id=student.c_id and student.id=score.s_id and student.sname='刘备';

--6 select avg(score.chinses)FROM class,student,score where class.id=student.c_id and student.id=score.s_id ;

update student set name=’张三’ where name=’zhangsan’ 


constraint foreign key(class_id) references class(id)); 
(物理外键)

create table score(id int primary key auto_increment,chinses varchar(20),english varchar(20),math varchar(20),s_id int,constraint foreign key(s_id) references student(id));
 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/618132.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号