mysql 数据库 基础整理

1、 mysql 基本使用命令

  1. 启动mysql服务器

    1. windows中启动服务器
      net start mysql;
      关闭:net stop mysql;

    2. Linux启动服务器,默认开启的
      service mysql start;
      关闭:service mysql stop;

  2. 进入mysql
    命令:mysql -u 用户名 -p
    然后输入密码即可进入, 如果看到 mysql> 则成功进入

  3. **window与Linux的数据库命令一致

  4. 查看数据库的版本
    select version();

  5. select now();

  6. 退出
    exit 或者 quit

2 库的操作

  1. 展示所有的数据库
    show databases;
    展示所有的已经存在的数据库
    mysql默认会有管理自己的库,表,用户,配置的数据库

  2. 创建一个数据库
    格式:create database 数据库名 charset=“utf8”;
    例:create database school charset=“utf8”;

  3. 删除一个数据库
    格式:drop database 数据库名;
    例:drop database school;

  4. 使用某个仓库
    格式:use 数据库名;

  5. 查看当前使用的数据库
    格式:select database();

3 表的操作

  1. 查询当前库中的所有表
    格式:show tables;

  2. 创建一个表
    格式:create table 表名(字段名 字段描述,字段名n 字段描述,…)
    例:create table student(id int primary key auto_increment,name varchar(20) not null,age int default 17,address varchar(20),sex bit default 1);
    auto_increment:自增

  3. 查看表的结构
    格式1:desc 表名;
    格式2:show create table student;

  4. 删除一个表
    格式:drop table 表名;
    例:drop table student;

  5. 修改表

    1. 修改表名
      格式:rename table 旧表名 to 新表名;
      例:rename table student to students;

    2. 修改表结构
      格式:alter table 表名 add|drop|change;

      1. 添加一个新的字段
        格式:alter table 表名 add 字段名 字段描述;
        例:alter table students add phonenum varchar(20);
      2. 删除一个字段
        格式:alter table 表名 drop 字段名;
        例:alter table students drop phonenum;
      3. 修改一个字段
        格式:alter table 表名 change 要修改的字段名 新的字段名 新的字段描述;
        例:alter table students change phonenum phone varchar(20);
    3. 开发过程中尽量不要修改已经有数据的字段,设计表的时候尽量涉及一些预留字段

4 数据的操作

  1. 增加数据

    1. 插入一条数据
      格式: insert into 表名 values(对应的值1,对应的值2,对应的值n)
      例:insert into students values(0,“三胖”,19,“济南”,1,12345);
      注意:插入的值要与表对应字段一一对应如果是自动增长的数据类型,将该值设置为 0即会自动的增长
    2. 缺少值插入
      格式: insert into 表名(字段1,字段2,字段n,…) values(字段1对应的值,字段2对应的值,字段n对应的值,…);
      例:insert into students(name, age, sex) values (“四胖”,13,0);
    3. 插入多条数据
      格式: insert into 表名 values(对应的值1,对应的值2,对应的值n),(对应的值1,对应的值2,对应的值n) ,(对应的值1,对应的值2,对应的值n) …;
      例:insert into students values(0,“五胖”,10,“青岛”,1,12345),(0,“六胖”,15,“槐荫”,0,12345);
  2. 删除数据
    格式: delete from 表名 where 条件
    例:delete from students where id=2;
    delete from students where name=“四胖”;

  3. 修改数据
    格式: update 表名 set 字段名 = 值1,字段名2 = 值2 where 条件
    例:update students set name = “五五胖”,address=“中国” where id=3;

  4. 查询数据的表中的所有数据
    查询所有数据 格式: select * from 表名;
    例:select * from student;

5 查询语句

  1. 格式1: select 字段名1,字段名n,… from 表名 where 查询条件
    格式2: select 字段名1 as 别名,字段名n,… from 表名 where 查询条件

    1. select表示查询
    2. select后面是查询结果要显示的字段名
      例:select name, age from students;
    3. *表示显示所有字段的数据
    4. from 后面是表名,表示从那个表中查询
    5. where 条件 表示以某个条件进行筛选
    6. 如果没有where条件,表示查询所有
    7. 字段名1 as 别名 可以给显示的字段名取个别名,方便查看
      需求:展示所有学生的姓名和年龄
      select name as “姓名”, age as “年龄” from students;
      查询所有: select * from 表名
  2. 查询条件 表达式

    1. 比较运算符
      .> 大于
      < 小于
      .>= 大于等于
      <= 小于等于
      = 等于
      != 不等于
      1. 需求: 查询班上大于15岁的学生 ?
        select * from students where age>15;
      2. 查询班上小于等于15岁的学生 ?
        select * from students where age<=15;
      3. 查询班上不等于15岁的学生 ?
        select * from students where age!=15;
    2. 逻辑运算符
      且 and
      或 or
      非 not
      1. 需求: 查询班上大于10岁且小于20岁的学生 ?
        select * from students where age>10 and age<20;
      2. 需求: 查询班上除了中国的学生
        select * from students where not address=“中国”;
    3. 模糊运算符
      格式: … where 字段名 like ‘字符串’
      任意字符: % 任意多个任意字符
      _ 一个任意字符
      1. 需求: 查询姓五的同学?
        select * from students where name like"五%";
      2. 查询姓五的同学,且只有2个字?
        select * from students where name like"五_";
      3. 查询名字中包含 五 字的同学
        select * from students where name like"%五%";
    4. 范围运算符
      1. 格式:格式1: … where 字段名 in (值1,值2,值3)
        是否等于 () 中的某一个值
      2. 格式:… where 字段名 between 值1 and 值2
        需求: 查询出住在 槐荫 或者 青岛 或者… 的同学 ?
        select * from students where address in (“槐荫”,“青岛”);
    5. 空值判断
      1. 格式1: … where 字段名 is null;
        需求:将address为空的所有数据筛选出来?
        select * from students where address is null;
      2. 格式2: … where 字段名 is not null;
        需求:将address不为空的所有数据筛选出来?
        select * from students where address is not null;
    6. 运算符的优先级
      1. 以上运算符可以综合使用
      2. 加上()
  3. 聚合函数

    1. count(*) 统计查询结果的数量
    2. max(字段名) 统计某个字段的最大值
    3. min(字段名) 统计某个字段的最小值
    4. avg(字段名)
    5. sum(字段名) 统计某个字段的总和
      格式: select 聚合函数 from 表名 where 条件
      需求: 查询该表总共有多少学生
      select count(*) from students;
      需求: 获取学生的最大年龄?
      select max(age) from students;
      需求: 统计年龄的平均值 ?
      select avg(age) from students;
  4. 分组 group by
    格式: select … from 表名 where 条件 group by 字段名
    **统计某个字段有多少种值
    需求: 查看有多少种地址 ?
    select address from students group by address;
    需求:查询每个地址有多少人?
    select address,count() from students group by address;
    需求: 再查询出槐荫有多少人 ?
    select address,count(
    ) from students group by address having address=“槐荫”;

    having 条件 表示在某个结果集上继续筛选

    注意: where 与having 后面都是跟一个条件表示查询,
    但是where是先筛选, having是在where的结果后再筛选

  5. 排序 order by
    格式: select … from 表名 where 条件 order by 字段名 排序规则;(默认升序)
    需求:查询所有学生,并按年龄排序
    select * from student order by age desc;
    降序:desc
    升序:asc

6 表的设计(表的关系)

  1. 一对一关系
    场景: 学生 1—档案表 1

  2. 一对多关系
    场景: 班级 1 ----学生 多

  3. 多对多关系
    场景: 学生 多 — 课程 多
    注意:需要第3张表来维护关系

7 一对一案例
**在一对多的案例上的外健上加上唯一性的约束即可 unique

8 一对多关系

  1. 设计表

    1. 班级表
      create table classes(classid int primary key auto_increment,classneme varchar(20));
    2. 学生表
      create table stu(students int primary key auto_increment,stuname varchar(20) not null,classid int,foreign key(classid) references classes(classid));
      外键格式:foreign key(外键字段名) references 表名(关联字段名)
      classid是外健,需要关联班级表的classid主键
  2. 插入数据

    1. 班级表
      insert into classes values(0,“py01”),(0,“py02”),(0,“py03”),(0,“java01”),(0,“java02”),(0,“h501”),(0,“ui01”),(0,“h502”),(0,“ui02”);
    2. 学生表
      insert into stu values(0,“张三”,4);
      insert into stu values(0,“李四”,1);
      insert into stu values(0,“王五”,1);
      insert into stu values(0,“赵六”,2);
      insert into stu values(0,“田七”,1);
      注:如果关联的外健的值在关联的表中不存在,则无法插入
      insert into stu values(0,“胖八”,11);
  3. 查询数据

    1. 查看所有的学生姓名及其所在的班级:
      例:select stu.stuname,classes.classneme from stu,classes where stu.classid=classes.classid;
      注:表名.字段名 可以指定到对应表中的字段,再多表联合的时候使用
    2. 展示py01班的所有学生
      select stu.stuname,classes.classneme from stu,classes where stu.classid=classes.classid and classes.classid=1;
    3. 展示所有学生的所有信息
      select stu.,classes. from stu,classes where stu.classid=classes.classid ;
  4. 连接关系查询

    1. 内连接 inner join
    2. 左外连接 left join 或 left out join
    3. 右外连接 right join 或 right out join
      select stu.stuname,classes.classname from classes left join stu on stu.classid=classes.classid and classes.classid=1;
    4. 格式:select … from 表1 inner/left/right join 表2 on 条件;

9 多对多

  1. 设计表

    1. 学生表
      create table student3(studentid int primary key auto_increment,stuname varchar(20) not null);
    2. 课程表
      create table courses(courses int primary key auto_increment,coursesname varchar(20));
    3. 学生-课程 表即选修课表
      create table elective(studentid int,
      coursesid int,
      primary key(studentid,coursesid),
      foreign key(studentid) references student3(studentid),
      foreign key(coursesid) references courses(courses));
  2. 插入数据
    插入学生
    insert into student3 values(0,“张三”);
    insert into student3 values(0,“李四”);
    insert into student3 values(0,“王五”);
    insert into student3 values(0,“赵六”);
    insert into student3 values(0,“田七”);
    插入课程
    insert into courses values(0,“python”);
    insert into courses values(0,“java”);
    insert into courses values(0,“h5”);
    insert into courses values(0,“ui”);
    插入选修课数据
    insert into elective values(1,1);
    insert into elective values(1,2);
    insert into elective values(1,4);
    insert into elective values(3,1);
    insert into elective values(3,4);
    注意:如果关联了外健,而外健的值在关联的表中不存在,则无法成功插入

  3. 查询数据
    select student3.,courses. from student3,courses,elective where student3.studentid=elective.studentid and courses.courses=elective.coursesid;

猜你喜欢

转载自blog.csdn.net/luanluan8888/article/details/84110628
今日推荐