mysql基本知识(1)

Mysql数据类型

  • 数值

    • 整形

      int tinyint

    • 浮点型

      float double

      表示的精度不同

      float 6位有效,double 16

  • decimal 浮点型,用来更加精确的保存小数

  • 字符串

    • char: 255个字符, 固定大小
    • varchar: 65535 字节,不固定(可变)
    • text: 64Kb
  • 枚举

    • enum() 1-2个字节存储数据
    • enum("值1","值2",.....)
  • 时间

    • date 年月日
    • datetime 年月日 时分秒
    • time 时分秒

数据库的操作

  -- 退出数据库
    exit
    quit 
    ctrl + d
  
  -- 查看当前使用的数据库
    select database();
  
  -- 查看当前使用的数据库
    select database();
    
  -- 指定编码的数据库创建
    create database python_db1 charset=utf8;

   -- 查看创建数据库的语句
   -- show create database ....
    show create database python_db;
    
   -- 删除数据库
   -- drop database 数据库名;
    drop database python_db;

SQL命令-表结构的创建

 -- 创建 classes 表(id、name)
    create table classes(
        id int unsigned primary key auto_increment,
        name varchar(10) not null,
        num tinyint

        );
   -- 查看表结构
    -- desc 数据表的名字;
    desc classes;
   
   -- 查看表的创建语句
    -- show create table 表名字;
    show create table students;

完整性约束条件primary key、auto_increment

  • primary key

    值不重复且值具有唯一性。主键不能为空

  • auto_increment

    (1)通过设置主键进行自增长,默认从1开始,每次+1

    (2)一个表中只能有1个自增长字段,而且自增长的字段一定配合主键使用,也就是说“被标识为自增长的字段,一定是主键,但是主键不一定是自增长的”。

    (3)自增长只对整数类、整数列有效,对字符串无意义。

insert into classes values(1,'王定',8); #向classes表中自己给定自增长列的值为1

insert into classes values('王定',8); #直接插入classes的值,id自动填充值为2

insert into classes values(null,'王定',8);     #用null也可以,id自动填充3

insert into classes values(default,'王定',8);  #用default也可以,id自动填充4

SQL命令-表结构的修改

 -- 修改表-添加字段 生日 datatime
    -- alter table 表名 add 列名 类型;
    alter table students add birthday datetime;

    -- 修改表-修改字段:不重命名版
    -- alter table 表名 modify 列名 类型及约束;
    alter table students modify birth date not null;

    -- 修改表-修改字段:重命名版
    -- alter table 表名 change 原名 新名 类型及约束;
    alter table students change birthday birth datetime;

    -- 修改表-删除字段
    -- alter table 表名 drop 列名;
    alter table students drop birth;

    -- 删除表
    -- drop table 表名;
    drop table students;

SQL命令-表数据的增删改查

-- 全列插入
-- insert [into] 表名 values(...)
-- 主键字段 可以用 0  null   default 来占位
 -- 向classes表中插入 一个班级
 insert into classes values(1, 'python20', 70);
 
 -- 部分插入
 -- insert into 表名(列1,...) values(值1,...)
  insert into students(id, name) values(null, '司马狗剩');

-- 多行插入
        insert into students values(null, '欧阳铁娃', 18, 1.78, '妖', 1),(null, '诸葛铁锤', 18, 1.78, '妖', 1);

  -- 修改
  -- update 表名 set 列1=值1,列2=值2... where 条件;
   -- 全部修改
   update students set age = 38;
   
    -- 按条件修改
    update students set age = 88 where name = '司马狗剩';
    -- 查询基本使用
    -- 查询所有列
    -- select * from 表名;
     select * from students;
     
     ---定条件查询
    select * from students where name='司马狗剩';

     -- 查询指定列
     -- select 列1,列2,... from 表名;
        select id,name from students;

      -- 可以使用as为列或表指定别名
      -- select 字段[as 别名] , 字段[as 别名] from 数据表;
      select id as '编号', name as '姓名' from students;

      -- 删除
      -- 物理删除
      -- delete from 表名 where 条件
      delete from students where id = 2;

        -- 逻辑删除
        -- 用一个字段来表示 这条信息是否已经不能再使用了
        -- 给students表添加一个 is_delete 字段 bit 类型  默认为0
        alter table students add is_delete bit default 0;
        -- bit 类型,智能保存 0 或者 1
        -- is_delete = 1  逻辑删除  
        update students set is_delete = 1 where id = 3;
	
	-- 数据库备份与恢复(了解)
		-- mysqldump –uroot –p 数据库名 > python.sql;
		-- mysql -uroot –p 新数据库名 < python.sql;

where

  • 比较运算

    > 、< 、>=、<=、=、!=或<>

select * from where age <> 18 ;   # 所有年龄不等于18岁的学生
  • 逻辑运算

    and、or、not

  • 模糊查询(like)

    % 表示任意0个或多个

    _ 任意一个字符

    -- 查询姓名中 有 "小" 所有的名字
    select * from students where name like '%小%';
    
    -- 查询有2个字的名字
    select * from students where name like '__';
    
    -- 查询至少有2个字的名字
    select * from students where name like '__%';
    
  • 范围查询

    in 用于非连续的范围的查询,相等于 多个值的 or (或) 关系

    between 起始 and 终值 连续,包含起始值和终值,是一种 and(与) 关系

    -- in (1, 3, 8)表示在一个非连续的范围内
    -- 查询 年龄为18、34的姓名
    select name from students where age in (18,34)
    
    -- between ... and ...表示在一个连续的范围内
    -- 查询 年龄在18到34之间的的信息
    select * from students where age between 18 and 34;
    
  • 空值判断

    is null 表示判断值为空

    is not null 判断值非空, 错误写法:not is null

  • order排序

    order by 排序字段1 排序规则, 字段2 规则2,.....
    -- asc从小到大排列,即升序(默认排序规则)
    -- desc从大到小排序,即降序

    -- 查询年龄在18到34岁之间的女性,身高从高到矮排序
    select * from students where age between 18 and 34 and gender='女' order by height desc;
    

聚合函数

聚合函数:聚合函数会对当前所在表当做一个组进行统计

常见的聚合函数:

count(*) 统计数量

max(字段) 找出该字段的最大值

min(字段) 找出该字段的最小值

sum(字段) 对该字段求和

avg(字段) 对该字段求平均值

四舍五入的函数: round(数值,保留的小数位数)

-- 计算班级学生的总数
select count(*) from students;
select count(*) '总人数' from students;

-- 查询女性的最高 身高
select max(height) from students where gender='女';

-- 计算男性的平均身高 保留2位小数
select round(avg(height),2)  from students where gender = '男';

去重(distinct)

select distinct cate_name from goods;

group分组

group by + 聚合函数,分组统计/计算

group by + group_concat() 分组 + 内容链接为一个字符串

group by + with rollup 分组 + 小计

  • having和where的区别:

    1. 作用的对象不同。where子句作用于表和视图,having 子句作用于组。

    2. having可以用聚合函数 ,where不能。因为where 在分组和聚集计算之前选取输入行(因此,它控制哪些行进入聚集计算), 而 having在分组和聚集之后选取分组的行。因此,WHERE 子句不能包含聚集函数 。

    如:having sum(qty)>1000 (正确) where sum(qty)>1000 (错误)

	-- group by
	-- 按照性别分组,查询所有的性别
	select gender from students group by gender;
	
	-- 注意下面这种查询会失败
	select name,gender from students group by gender;
	
	-- 计算每个年龄中的人数
	select age, count(*)  from students group by age;
	
	-- 查询同种性别中的姓名
	select gender, group_concat(name) from students group by gender;
	
	-- 查询平均年龄超过30岁的性别=
    select gender, avg(age) from students group by gender having avg(age) > 30;
    
    -- with rollup 汇总的作用(了解)
	select gender, count(*) from students group by gender with rollup;

limit限制记录

  • limit 数据有很多,只取指定数量的数据
    • 表中的数据,位置默认从 0 开始
  • limit 使用格式: limit 起始位置, 连续取的数目;
  • limit 一定要写到 SQL 语句的最后面
    注:
    在ms sql server中或access中,若要查询前10条记录,使用top 10即可
    但在mysql中不支持这个写法,它用limit 10。

LIMIT可以实现top N查询,也可以实现M至N(某一段)的记录查询

-- 检索记录行 6-15
select * from table limit 5,10;

注:标准的SQL书写格式

select 字段1,字段2,...
from  表名
[where 条件]
[group by 字段名]
[order by 字段名 排序规则]
[having 条件]
[limit 起始位置,数量]

连接

  • 内连接:查询的结果为两个表匹配到的数据,默认是笛卡尔积
    • 关键字 inner join
    • select 字段 from 表1 inner join 表2 where/on 表1.字段 = 表2.字段
  • 外连接
    • 左外连接 主表 left join 从表
      • 左外连接另外一个表,在从表中没有找到匹配,右侧补 NULL
    • 右外连接 从表 right join 主表
      • 右外连接一个表,在从表中没有找到匹配,左侧补 NULL
-- 查询 有能够对应班级的学生以及班级信息
	select * from students inner join classes where students.cls_id = classes.id;

-- 给数据表起名字
	select s.name, c.name  from students s inner join classes c where s.cls_id = c.id;

-- 内连接的另外一种写法
     select s.name, c.name  from students s, classes c where s.cls_id = c.id;
 
 -- 查询没有对应班级信息的学生
	select students.*  from students left join classes on students.cls_id = classes.id where classes.id is null;

posted @ 2020-03-30 17:44  Hhhighway  阅读(230)  评论(0编辑  收藏  举报