数据库2

位, 字节, 字符

  • 位(bit):是计算机 内部数据 储存的最小单位,11001100是一个八位二进制数。

  • 字节(byte):是计算机中 数据处理 的基本单位,习惯上用大写 B 来表示,1B(byte,字节)= 8bit(位)

  • 字符:是指计算机中使用的字母、数字、字和符号

    • UTF-8编码:1个英文字符 = 1个字节

      ​ 英文标点 = 1个字节

      ​ 1个中文(含繁体) = 3个字节

      ​ 中文标点 = 3个字节

增加表

  • create table 表名(

    字段名 列类型 [可选的参数],

    ...

    字段名 列类型 [可选的参数]

    ) charset = utf8;

    • 列类型:

      • 整型:

        • tinyint, smallint, int(一般使用), mediumint, bigint

        • unsigned表示不能取负数,只适用于整型

      • 浮点型:

        • 精确的小数类型: decimal(m, n)---m表示数字的总个数(负号不算), n是小数点后的数字的个数

        • 不精确的小数类型: float

          create table tankdsb (
              id int,
              salary decimal(6, 4),
              num float
          ) charset = utf8;
          
          insert into tankdsb (salary, num) values (12.3386, 1.0319);
          
          '''
          +------+---------+--------+
          | id   | salary  | num    |
          +------+---------+--------+
          | NULL | 12.3386 | 1.0319 |
          +------+---------+--------+
          '''
          
        • 字符串类型: char和varchar参数都是指能存储的最大字符数。例如: CHAR(30)最多可容纳30个字符。

          • char(n): 无论插入的字符串的长度为多少, 永远占规定的长度

          • varchar(n): 根据插入的字符串的长度来计算所占的字节数, 通常有一个字节是用来保存字符串的大小的

          如果不能确定插入的数据的大小, 一般使用 varchar(255)

        • 时间日期类型

          • year

          • date

          • time

          • datetime---推荐使用

            create table VarDate (
                t time,
                d date,
                dt datetime
            );
            
            insert into VarDate values (now(), now(), now());
            
             select * from VarDate;
            +----------+------------+---------------------+
            | t        | d          | dt                  |
            +----------+------------+---------------------+
            | 17:17:33 | 2019-10-29 | 2019-10-29 17:17:33 |
            +----------+------------+---------------------+
            
          • timestamp

        • 枚举类型: 列出所有的选项---gender enum('male','female')

    • 列约束参数:

      • auto_increment: 自增1

      • primary key: 主键索引, 加快查询速度, 列的值不能重复

      • not null: 表示该字段不能为空

      • default: 为该字段设置默

        推荐使用

        create table nick (
        
        id int auto_increment primary key,
        
        name char(5) not null default "xxx",
        
        gender char(5)
        
        );
        

增加表的数据行

  • insert into 表名 (列1, 列2) values (值1, "值a"), (值2, "值b");
  • insert into 表名1 (字段名1) select (字段名2) from 表名2;

修改表中的数据

  • update 表名 set 列名1 = 新值1, 列名2 = 新值2 where 条件;

查询表中数据

  • select 列1,列2 from 表名 where 条件; # *代表查询所有的列
  • select * from nick where id between 10 and 12; # between ... and ... 表示的范围是闭区间
  • select distinct 字段名 from 表名; # 去重查询
  • select age*10 as age from nick # 通过四则运算查询(一般不用)
  • select * from nick where id in (1, 2, 100);
  • select * from nick where name like 'x%'; # 模糊查询, 以x开头(一般不用)
  • select * from nick where name like '%x'; # 模糊查询, 以x结尾(一般不用)
  • select * from nick where name like '%x%'; # 模糊查询, 包含x(一般不用)

修改表名

  • `alter table 旧表名 rename 新表名;

删除表

  • drop table 表名; # 线上禁用

删除表中数据

  • delete from 表名 where 条件;

    delete from nick where id >= 4 or name = "xxx";

    delete from nick; # 删除表中所有数据

  • truncate 表名; # 全选删除

区别:

  1. delete之后, 插入数据从上一次主键自增加1开始, truncate则是从1开始
  2. delete是一行一行删除, truncate是全选删除, truncate删除的速度高于delete

查询库中所有表

  • show tables

查询表结构

  • show create table 表名

复制表结构---不复制数据

  • create table 新表明 like 已存在的表名;

字段操作

  • 添加字段

    alter table nick
    add height int,
    add weight int;
    
    '''
    select * from nick;
    +----+------+--------+--------+--------+
    | id | name | gender | height | weight |
    +----+------+--------+--------+--------+
    |  1 | xxx  | male   |   NULL |   NULL |
    |  2 | lqc  | male   |   NULL |   NULL |
    |  3 | qqq  | male   |   NULL |   NULL |
    | 10 | cccc | male   |   NULL |   NULL |
    | 11 | rrrc | male   |   NULL |   NULL |
    | 12 | lqc  | male   |   NULL |   NULL |
    +----+------+--------+--------+--------+
    '''
    
  • 查看表中所有字段: desc 表名

  • 添加字段至首列

    alter table 表名
    add 字段名 列类型 [可选参数] first;
    
  • 添加字段至某个字段后面

    alter table 表名
    add 字段名 列类型 [可选参数] after 字段名;
    
  • 删除字段

    alter table 表名
    drop 字段名;
    
  • 修改字段的格式

    alter table 表名
    modify 字段名 数据类型 [可选参数];
    
  • 修改字段名

    alter table 表名
    change 旧字段名 新字段名 新数据类型 [可选参数]
    
原文地址:https://www.cnblogs.com/-406454833/p/11761645.html