MySql 基础操作

## 数值型分为俩种:

1. ### 整形

   1. 整数常用的五种类型
      1. (Tinyint) 非常小的整形 1字节:-128~127;0~255
      2. (Smallint)较小的整形    2字节:-32768~32767
      3. (Mediumint)中等大小整形  3字节: 0~2 24方
      4. (int) 标准整形 4字节:0~2 32方
      5. (bigint) 大整形  8字节:0~2 64方

2. 浮点型

   1. float 4字节
   2. double  8字节
   3. decinal H+2字节

3. ### 字符串格式有哪些

   1. 字符型分为4类
      1. char(n) 255 最大长度  常用于长度不变的(如:姓名列)索引快,但浪费空间
      2. varchar (n) 255(可变长度)  节省空间但索引慢
      3. text   2 16方-1   文本数据(文章)
      4. blog   二进制图片

4. ### 日期型

   1. date(日期)   YYY-MM-DD
   2. TIME(时间) hh:mm:ss
   3. DATATIME(日期和时间) YYY-MM-DD hh:mm:ss
   4. TIMESTAMP(时间戳)           YYYMMDDhhmmss
   5. YEAR(年)   YYYY
MySQL 数据类型

 MySQL 常用增删改查

  • 创建数据库

    • show databases; # 查看所有的数据库;

    • create database xiaomo charset utf8; #创建数据库

    • use xiaomo # 选择使用xiaomo数据库

    • show create database xiaomo; # 查看创建数据库语法

    创建表结构

    • show tables; # 查看数据库(xiaomo)中所有的表

    • ##创建一张xiaoxiaomo的表
      create table xiaoxiaomo(
      id int auto_increment,
      name char(32) not null,
      age int not null,
      register_data date not null,
         primary key (id));
      )

      # 解释意思
      mysql> create table xiaoxiaomo(   # 在xiaomo 数据库中创建xiaoxiaomo的表
        -> id int auto_increment,     # 在表中创建第一个字段 id
        -> name char(32) not null,  # 在表中创建第二个字段 name
        -> age int not null,
        -> register_data date not null, # 日期
        ->     primary key (id));     # 将表的主键设置为 id ## 一张表只有一个主键
      Query OK, 0 rows affected (0.01 sec)  # 创建成功
    • desc xiaoxiaomo; ## 查看表的信息

    • drop table xiaoxiaomo; ## 删除这张表

插入数据

  • # 指定字段插入数据
    insert into xiaoxiaomo(name,age,register_data) values("zhangsan","100",'2020-07-15')
    # 所有字段插入
    insert into [表名] values(2,"lisi",200,'2020-07-15')

    insert into xiaoxiaomo values(1,"lisi","11","2020-07-15");
    insert into xiaoxiaomo values(2,"wangwu","12","2020-07-15");
    insert into xiaoxiaomo values(3,"tom","13","2020-07-16");
    insert into xiaoxiaomo values(4,"nike","14","2020-07-16");
    insert into xiaoxiaomo values(5,"xiaoliu","19","2020-03-16");
    insert into xiaoxiaomo values(6,"xiaohua","55","2020-03-16");
    insert into xiaoxiaomo values(7,"zhangsan","88","2020-09-16");
    insert into xiaoxiaomo values(8,"zhangsan","88","2020-09-16");

查询数据库

  # * 就是代表所有的数据
  select * from xiaoxiaomo;
  # 只查询 name 那一列
  select name from xiaoxiaomo;
  # limit 截取 查看前2条数据
  select * from xiaoxiaomo limit 2;
  # 截取 查询 5 - 3 数据 # 从第三行开始 查取5条数据
  select * from xiaoxiaomo limit 5 offset 3;
  # where ; like; order by 使用;
  # where 表民 后面是where where 后面试查询条件
  # 查询 id > 3 并且(and) age>10 的所有的用户;
  select * from xiaoxiaomo where id > 3 and age >10;

  # 查询所有在2020-07的数据
  # 写法格式 select * from 表名 where 字段名 like '模糊查询数据';
  select * from xiaoxiaomo where register_data like '2020-07%';

  # 通过id降序 # order by 排序的意思 # desc 将数据从大到小排序
  # 写法格式 select * from 表名 order by (要排序的字段民) desc;
  select * from xiaoxiaomo order by id desc;
​  
  

# 通过id正序 # 从小到大
改成 asc就可以了
  select * from xiaoxiaomo order by id asc;


  # 查找名字为 "%si" 结尾的所有条目,并且按照id降序
  select * from xiaoxiaomo where name like '%si' order by id desc;
  # GROUP BY :指定以什么分组(比如可以统计出游多少同名的数据)
  # 查看xiaoxiaomo表中名字出现的次数 通过group by 分组 count(*) 就是计数的意思
  # 写法格式: select 字段名,count(*) from 表名 group by 字段名;
  select name,count(*) from xiaoxiaomo group by name;

  ## coalesce # 添加一个字段 对应 with rollup 如果不添加 则为null,with rollup 分组的基础上统计数据
  select coalesce(name,'Total age'),sum(age) from xiaoxiaomo group by name with rollup;

MySql 修改

    # update 修改 
   # 将 id = 3 的这条数据 name 修改为 xiaohong age 修改为22
   # 写法格式: update 表名 set [想要修改的字段名='修改的值'] where 条件;
   update xiaoxiaomo set name='xiaohong',age=22 where id=3;
   
   
   # 修改 id>5 的所有信息
   update xiaoxiaomo set name='nice',age=18 where id>5;

 

 



MySql 删除

  # 删除 xiaoxiaomo表中所有name为lisi的
  # 写法格式 delete from 表名 where 条件
  delete from xiaoxiaomo where name='lisi';

 

 


 

添加字段

  # 添加一个字段
  # 给xiaoxiaomo这张表添加一个sex字段只能使用 enum 只能使用 B G 来代替 选项
  # 写法格式: alter table 表名 add [你要添加的字段名称] enum('','','') # 更具需求自定义
  alter table xiaoxiaomo add sex enum('B','G');

  # 插入数据 sex 字段使用 B(boy) 或 G 来代替
  insert into xiaoxiaomo(name,age,register_data,sex) values('xiha','520','2020-07-16','B');

修改字段属性 字段名称 修改表名

  # 修改字段属性 # 修改的数据类型
  # 写法格式: alter table [表名] modify column varchar(50) default null;
  alter table xiaoxiaomo modify column name varchar(50) default null;
  # 修改字段名称
  alter table xiaoxiaomo change name new_name char(32);

  # 删除一个字段
  # 写法格式: alter table [表名] drop [要删除的字段名称];
  alter table xiaoxiaomo drop sex;
  # 修改表名
  alter table xiaoxiaomo rename to xxm;

 

 

posted @ 2020-07-16 10:21  Handsome、Snake  阅读(173)  评论(0编辑  收藏  举报