概念:

  1、mysql是一个关系型数据库管理系统,关系数据库将数据保存在不同的表中,而不是一个大仓库,这样就增加了速度并提高了灵活性,使用SQL语句操作数据。

使用数据库:

一、在终端使用数据库

  1、登录数据库 

    命令 mysql -uroot -proot(用户名root,密码root)。

  2、查询数据库服务器中的所有数据库 

    命令 mysql> show databases;。

  3、如何选中某一个数据库进行操作

    mysql> use test(选中test数据库)

  4、SQL语句查询数据库的数据

    select * from admin;(查询admin表中的所有数据)

    select * from admin where id=1;(通过ID去查询一条数据)

    insret into user values('张三','123456' ,'男','18');(插入一条数据)

    update  user set name='李四' where sex='女';(修改数据)

    delete from pet where name='张三';(删除数据)

  5、如何退出数据库命令

    命令 exit;

  6、如何在数据库服务器中创建我们的数据库?

    命令 create database test;

  7、如何查看某个表中的所有数据

    命令 show tablse;

  8、如何创建一张表

    CREATE TABLE pet(

      name VARCHAR(20),

      owner VARCHAR(20),

      sex CHAR(1),

      birth DATE,

      death DATE

    );

  9、查看创建的表

    show pet;

  10、查看创建好的数据表结构

    describe  pet;

 

mysql常用数据类型有哪些

  1、MySQL支持多种类型,大致可以分为三类:数值、日期/时间和字符串(字符)类型。

  https://www.runoob.com/mysql/mysql-data-types.html(详细解析)

 

mysql 建表约束

  1、主键约束

    他能够唯一确定一张表中的一条记录,但该字段数据不能重复且不为空  primary key

    create table user(

      id int primary key,

      name varchar(20)

    );

    ----联合主键的使用(联合主键中只要联合起来的主键不同即可,其中有个别字段相同没事)

    cerate table user2(

      id int,

      name varchar(20),

      password varchar(20),

      primary key(id, name)

    );

    ----自增约束(自增约束搭配主键约束使用)

    cerate table user3(

      id int ,primary key auto_increment,

      name varchar(20)

    );

    ----创建表示忘记添加主键时

    cerate table user4( 

      id int 

      name varchar(20)

    );

    然后添加主键add primary key(id); 

    alter table user4 add primary key(id);

 

    删除主键  drop primary key

    alter table user4 drop primary key ;

 

    修改主键   modify id int primary key;

    alter table user4 modify id int primary key;

 

  2、唯一约束 uniqun , modify

    约束修饰的字段不可以重复

    create table user5(

      id int,

      name varvher(20)

    );

    添加唯一约束

    alter table user5 and uniqun(name)意思是user5表中name字段不可以重复 ,否则报错

    alter table user5 modify name varcher(20) unique;  modify的方式添加

    或者创建表时创建

    create table user5(

      id int,

      name varcher(20) unique

    );

    或者

    create table user5(

      id int,

      name varcher(20),

      unique(name)

    );

    unique()里面的字段可以添加多个字段,但表示的意思是多个字段组成的唯一约束不重复即可

 

    删除唯一约束 drop index name;

    alter table user5 drop index name;

 

  3、默认约束  default 10

    就是当我们没有传字段是,就会使用默认值,如果有就使用传入的字段

    create table user6(

      id int,

      name vercher(20),

      age int default 10

    );

 

  4、非空约束 not null

    修饰的字段不能为空

    create table user6(

      id int,

      name varcher(20)  not null(表示name字段不能为空)

    );

 

  5、外键约束

    涉及两个表一个是父表,一个是子表

    班级父表

    create table classes(

      id int primary key,

      name vercher(20),

    );

    学生子表

    ceratle table students(

      id int primary key,

      name vercher,

      class_id int

      foreign key(class_id) references.classes.classes(id) 设置外键

    );

    1)、主表中没有的数据在附表中是不可以使用的

    2)、主表中的记录被附表引用,是不可以被删除的

 

数据表的三大设计范式

  第一范式、

    数据表中的所有字段都是不可分割的原子值,比如说地址address中的国家省市区街道可以分割为国家,省,市,街道等,不可以写在一起作为一个字段

  第二范式、

    满足第一范式的前提下,除主键外的每一列都必须完全依赖于主键

  第三范式、

    必须满足第二范式,除主键列的其他列之间不能有传递依赖关系

 

mysql的SQL语句联系

  学生表

   Student 学号  姓名 性别 出生年月日 所在班级

   create table Student(

    sno varchar(20) primary key,

    sname varchar(20) not null,

    ssex varchar(20) not null,

    sbirthday datetime,

    class vercher(20)

  );

  教师表

   Teacher 教师编号 教师名字 教师性别 出生日期 职称 所在部门

     create table Teacher(

    tno varchar(20) primary key,

    tname varchar(20) not null,

    tsex varchar(20) not null,

    tbirthday datetime,

    prof varchar(20) not null,

    depart varchar(20) not null

    );

  课程表

      Course 课程号 课程名称 教师编号

    create table Courise(

      cno varchar(20) primary key,

      cname varchar(20) not null,

      tno varchar(20) not null,

      foreign key(tno) references teacher(tno)

    );

  成绩表

      Score 学号 课程号 成绩

    create table score(

      sno varchar(20) not null,

      cno varchar(20) not null,

      degree decimal,

      foreign key(sno) references student(sno),

      foreihn key(cno) references course(cno)

      primary key(sno,cno);

    );

  添加学生信息

  insert into student values('101','张三','男','2020-08-01','9501');

  添加教师信息

  insert into tercher values('201','姜子牙','男','1999-02-01,'副教授','计算机系');

  添加课程信息

  insert into course values('3-101','计算机导论','201');

  添加成绩信息

  insert into score values('101','3-101','99');

-----查询student表中的所有字段

  select * from student;

-----查询student表中所有记录 sname  ssex  class列

  select sname,ssex,class ,from student;

-----查询教师所有单位即不重复的depart列    (查询的列前加distinct  用于排重)

  select distinct depart from teacher;

-----查询score表中成绩在60--80之间的所有记录(表示区间)

  select * from score where degree between 60 and 80;

  或者 select * from score where degree>60 and degree<80;(这种方式不建议使用,容易造成数据库性能问题)

-----查询score表中成绩为85,86,或88 的记录(意思是表或者关系前提是同一个字段)

  select *from score where degree in(85,86,88);

-----查询student表中‘9501’班或性别为'女'的同学记录(该查询也为或者关系但是不同的字段)

  select * from student where class='9501' or ssex='女';

-----以class降序查询student表的所有记录

  select * from student order by class desc;(降序)

  select * from student order by class ;(默认 升序 或将desc换为asc)

-----以con升序,degree降序查询score表的所有记录

  select * from score order by con asc,degree desc;

-----查询"9501"班的学生人数。(统计count())

  select count(*) from student where class='9501'; 

----查询score表中最高分的学生学号和课程号

  select sno, cno from score where degree=(select max(degree) from score);

---通过排序查询score表中最高分的学生学号和课程号

  limit 0,1的意思是从降序排列中的第0个位置开始取出一条数据

  select sno,cno from score where degree order by desc limit 0,1;

-----查询没门课的平均成绩

  group by(分组,查询的时候先分组在查询)

  select avg(degree) from score group by con;

-----查询score表中至少两名学生选修的并以3开头的课程的平均成绩。

  group by 分组

  having

  count(cno)统计

  like '3%'模糊查询

  select  avg(degree) from score group by cno having count(cno)>=2 and con like '3%';

-----查询份数大于70,小于90的sno列

  select sno from score

  where between 70 and 90;

  或者 select sno from score

  where degree>70 and degree<90;

-----查询所有学生的sname,cno,degree 列(多表查询)

  select sname ,cno,degree from student,score

  where student.sno=score.son;

-----查询所有学生的sno,cname,degree 列

  select sno,cname,degree from Courise,score

  where score.cno=Courise.cno;

----查询所有学生的sname,cname,degree列(三表查询)

  select sname cname,degree

  from  student,course,score 

  where student.sno=score.sno

  and course.con=score.con;

-----查询9501 班学生每门课的平均分(子查询加分组求平均数)

  select cno, avg(degree )

  from score where sno

  in(select sno from student where class='9501') 

  group by cno;

-----查询选修“3-105”课程的成绩高于“109”号同学“3-105”成绩的所有同学的记录

  select * from student

  where cno='3-105'

  and degree>(select degree from score where sno='109' and cno='3-105');

----查询成绩高于109 课程号为‘3-105’的成绩的所有记录

  select * from student where cno='3-105' and degree>(select degree from score where cno='109');

----查询和学号为108.101的同学同年出生的学生sno,sname,sbirthday列(year函数和带in关键字的子查询)

  select sno,sname,sbirthday from

  where year(sbirthday)

  in (select year(sbirthday) from student where sno in('108'.'101'));

 

MYSQL的事务

  事务其实是一个最小的不可分割的工作单元。事务能够保证一个业务的完整性。

  比如银行转账 A向B中转账1000元,需要同时保证A账户少1000元,B账户同时多出1000元。事务解决的问题保证事务的完整性,要么同时成功,要么同时失败。

  mysql如何控制事务

    mysql事务默认是开启事务的并自动提交不可回滚的(即SQL语句执行完毕即使调用了rollback 事务回滚,也不能撤销执行的结果)。

    通过select @@autocommit;语句查询  默认值为1

    设置mysql默认的自动提交为false。(set autocommit=0;这就是关闭了自动提交)

    关闭自动提交后执行插入语句,然后执行commit;进行手动提交。如果没有commit提交,此时的表是一个临时表可以通过rollback进行事务的回滚,将执行结果删除。如果提交了commit 则不能通过rollback进行回滚。

    通过begin 或start transaction来开启一个事务

    通过commit 来提交事务

    通过rollback来关闭事务

事务的四大特征(ACID)

    原子性:事务是最小的单位,不可分割

    一致性:事务要求同一个事务的SQL语句 必须同时成功或同时失败

    隔离性:事务1与事务2具有隔离性

    持久性:事务一旦执行完毕,就不能改变。

事务隔离的不同级别

    读未提交(Read uncommitted):

      有两个事务,事务A对数据进行操作,在没有提交之前,事务B可以看到事务A操作的结果,这样会造成脏读,

    读提交(read committed):

      事务 A 多次读取同一数据,事务 B 在事务A多次读取的过程中,对数据作了更新并提交,导致事务A多次读取同一数据时,结果 不一致。

    可重复读(repeatable read):

      系统管理员A将数据库中所有学生的成绩从具体分数改为ABCDE等级,但是系统管理员B就在这个时候插入了一条具体分数的记录,当系统管理

      员A改结束后发现还有一条记录没有改过来,就好像发生了幻觉一样,这就叫幻读。

    串行化(Serializable):

      事务A 在操作数据还没有提交时 事务B也操作这个数据 这时事务B就处于等待状态 直到A事务提交后才执行。

    事物的隔离级别越高 其性能越差。

查看数据库的隔离级别

    mysql8.0

      系统级别

      select @@global.transaction_isolation;

      会话级别

      select @@transaction_isolation;

    mysql5.x    

      系统级别

      select @@global.tx_isolation;

      会话级别

      select @@tx_isolation;

设置数据库的隔离级别

    set globa transaction isolation level read uncommint

    

 

posted on 2020-08-02 00:56  ¥简竹¥  阅读(590)  评论(0)    收藏  举报