mysql初级应用和了解

1 why?
    1.1 java代码将数据保存到内存中
        优点: 速度快
        缺点: 无法持久保存, 无法将数据保存到硬盘上
    
    1.2 java代码使用IO流 将数据保存到硬盘上
        优点: 持久保存, 将数据保存到硬盘上
        缺点: 速度慢

    1.3 既要速度,也要持久化保存, 还要安全?  数据库

2 what?
    数据库 是保存数据的软件

    厂商: oracle, sql server, DB2(IBM),  mysql(主角) 等

    下载, 安装, 启动使用

3 HOW?
    3.1 内部存储结构

    3.2 操作 数据库
        3.2.1 增加
            1、创建一个名称为mydb1的数据库。
                create database mydb1;
            2、创建一个使用utf8字符集的mydb2数据库。
                create database mydb2 character set utf8;
                
        3.2.2 删除
            需求:删除前面创建的mydb1数据库
                drop database mydb1;

        3.2.3 修改
            需求: 将mydb2的编码集改成 gbk
                alter database mydb2 character set gbk;

        3.2.4 查询
            1、查询mysql数据库软件的所有 数据库
                show databases;
            2、查看mysql数据库的编码
                show create database mysql;

            3 查询正在使用的数据库
                select database();
            4 切换指定使用的数据库 mydb2
                use mydb2;
                select database();

        3.2.5 其他

    3.3 操作 数据表结构
        3.3.1 增加
            需求:创建雇员表,包含雇员的姓名,密码,性别, 生日信息。
            -- 雇员employ 简写 emp
            create table emp(
                username varchar(50), -- 
                password varchar(50),
                sex char(10),
                birthday date
            );

            show tables; -- 查询所有表
            desc emp; -- 查询表结构

            约束: 主键 , 唯一, 不为空
                创建雇员表2:工号 整形 主键 自增长,用户名 唯一且不为空,密码不为空, 性别 ,  生日。

                create table emp2(
                    id int primary key auto_increment,
                    username varchar(50) unique not null,
                    password varchar(50) not null,
                    sex char(10),
                    birthday date
                );

        3.3.2 删除
            需求:删除emp表
                drop table emp;

        3.3.3 修改
            需求1:在emp2表上增加salary列
                alter table emp2 add salary double;

            需求2:在emp2表上增加age列
                alter table emp2 add age int;

            需求1:修改birthday列不能为null
                alter table emp2 modify birthday date not null;
                
            需求2:修改username列的长度为60
                alter table emp2 modify username varchar(60) unique not null;

            需求:修改列名username为name
                alter table emp2 change username name varchar(60) unique not null;

            需求:删除age列
                alter table emp2 drop age;

            需求:将emp2表名修改为person表
                rename table emp2 to person;

            需求7: 将emp的编码修改成utf8
                alter table emp character set utf8;

        3.3.4 查询
            需求:查看雇员表结构
                desc emp; -- 查询表结构

                需求1:查看当前库的所有表。
                    show tables;

                需求2:查看person表结构。
                    desc person;

                需求3:查看person表的编码。
                    show create table person;

        3.3.5 其他

    3.4 操作 数据表的内容
        3.4.1 增加
            1、给表中的所有列插入数据
                insert into person(id, name, password, sex, birthday, salary)
                values(1, 'zhangsan', '123', 'nan', '1985-8-8', 15000);
                
                 select * from person; -- 查询表内数据
            2、简化给表中的所有列插入数据: 所有列,而且表结构列顺序必须一致
                insert into person
                values(null, 'lisi', '123', 'nv', '1990-9-9', 18000);
                
            3、给表中不为空的列插入数据
                insert into person(name, password, birthday)
                values('wangwu', '123', '2000-9-10');

        3.4.2 删除
            1、删除person表中name为lisi的用户记录
                delete from person
                where name='lisi';
            2、删除表中所有记录
                delete from person; -- 逐行删除

                truncate person; -- 删除表的一切,再根据sql语句创建新的表

        3.4.3 修改
            1、修改person表中的所有用户的password 为 abcdef
                update person set password='abcdef';

            2、修改姓名为zhangsan的这个用户的薪水 88888
                update person set salary=88888 where name='zhangsan';
            3、把id为 3 的用户 的 name和password 修改为 zhaoliu
                update person 
                    set name='zhaoliu', password='zhaoliu'
                    where id=3;

            4、把id为 3 的这个用户 用户名修改为 中文的 赵六;
                update person 
                    set name='赵六'
                    where id=3;


        3.4.4 查询
            需求:查询学生的所有信息。
                select * from student;

            需求:查询所有学生的姓名 和成绩
                select name, score from student;
            
            需求:查询表中年龄大于等于24岁的学生信息
                select * 
                from student
                where age >= 24;
            
            需求:查询年龄不是25岁的学生。
                select * 
                from student
                where age!=25;

                select * 
                from student
                where age<>25;
            
            需求:查询年龄>23,并且成绩>80的同学信息
                select *
                from student
                where age>23 and score>80
            
            需求:查询成绩在80~100(包含)之间的学生信息
                select *
                from student
                where score>=80 and score<=100;

                -- 简化方式: 列名 between 较小值 and 较大值;
                select *
                from student
                where score between 80 and 100;
            
            需求:查询年龄为18,23,25的同学信息
                select * 
                from student
                where age=18 or age=23 or age=25;


                select * 
                from student
                where age in (18, 23, 25);
            
            需求:查询所有含有 思 的学生信息
                select * 
                from student
                where name like '%思%';


            需求1:查询没有生日学员信息
                select * 
                from student
                where birthday is null;
            
            需求2:查询有年龄学员信息
                select * 
                from student
                where age is not null;
            
            需求:显示不重复的年龄
                select distinct age from student;
            
            1、对成绩排序后输出
                -- 成绩正序排列
                select * 
                from student
                order by score asc;

                -- 成绩倒序排列
                select * 
                from student
                order by score desc;

            2、对年龄排序按从高到低(降序)的顺序输出
                select * 
                from student
                order by age desc;

            3、对学生年龄按照降序排序,年龄相同按照成绩降序
                select * 
                from student
                order by age desc, score desc;
            
            需求1、给年龄和分数起别名
                select age as 年龄, score as 成绩
                from student;

            需求2、省略关键as 再次查询
                select age 年龄2, score 成绩2
                from student;

            聚合函数
                1、统计一个班级共有多少学生?
                    select count(*) from student;

                2、查询成绩大于80的学生信息:
                    select * from student
                    where score>80;

                3、统计成绩大于80的学生有多少个?
                    select count(*) from student
                    where score>80;

                1、统计一个班级成绩和    
                    select sum(score) from student;

                2、分别统计年龄和, 成绩和
                    select sum(age), sum(score) from student;

                3、统计年龄和成绩和值
                    select sum(age)+sum(score) from student; -- 491.96

                    select sum(age+score) from student; --  407.96000000000004

                    -- ifnull(列名, 默认值)  如果这列值为null,就使用默认值
                    select sum(ifnull(age, 0)+score) from student; --  491.96000000000004

                    -- truncate(值, 保留的小数位) 保留指定的小数位
                    select truncate(sum(ifnull(age, 0)+score), 2) from student;

                需求:求一个班级平均年龄
                    -- 不允许使用avg
                    select sum(age),count(*),sum(age)/count(*) from student; -- 19.0000

                    select sum(age),count(age),sum(age)/count(age) from student; --  23.7500

                    select avg(age) from student; -- 23.7500
                
                需求:求班级最高分和最低分
                    select max(score),min(score) from student;
                
                分组:
                需求1: 求每个学生的总成绩?
                    select name, sum(score) -- 注意: 现实的列只能是被分组的列和聚合函数
                    from student
                    group by name;

                需求2 : 求平均分大于80的学生? 
                    select name, avg(score)
                    from student
                    -- where avg(score)>80 -- 这是错误的
                    group by name;

                为什么是错误的? 因为违背了mysql的sql的执行顺序
                    from 1
                    where 2
                    group by 3
                    having 4
                    order by 5
                    select 6

                    聚合函数只能在分组之后使用, 只能出现在4,5,6中
                
                    
                    需求2 : 求平均分大于80的学生? 
                        select name, avg(score)
                        from student
                        group by name
                        having avg(score)>80;
                
                扩展需求3: 求每门课 男生和女生的平均分?
                    select course, sex, avg(score)
                    from student
                    group by course, sex;
                     
        3.4.5 其他


        备份
            1、重新开启一个新的dos窗口。
            2、将mydb2数据库导出到硬盘文件.sql。
                mysqldump -u root -p mydb2 > e:/dashuju10.sql

        恢复
            1、创建heima数据库。
                create database heima;
            2、重新开启一个新的dos窗口。
            3、将mydb2备份的数据表和表数据 恢复到heima中。
                mysql -u root -p heima < e:/dashuju10.sql 

猜你喜欢

转载自blog.csdn.net/qq_43406741/article/details/92587129