当前位置:Gxlcms > 数据库问题 > Mysql语句

Mysql语句

时间:2021-07-01 10:21:17 帮助过:5人阅读

  • 存储引擎是用于根据不同的机制处理不同的数据。

  • 查看mysql中所用引擎:

    • show engines;

    • create table t1(id int)engine=innodb; #innob:默认使用存储引擎 :支持事务,行锁,外键
      
      create table t2(id int)engine=myisam; #myisam:5.5以前老版本使用的存储引擎
      
      create table t3(id int)engine=blackhole; #黑洞,存入值消失
      
      create table t4(id int)engine=memory; #通电数据存在,断电丢失
  • 创建表完整的语句

    • #约束条件:可有可无

    • #宽度:限制某些数据类型的存储大小

    • create table 表名(

      字段名1 字段类型(宽度) 约束条件,

      字段名2 字段类型(宽度) 约束条件

      )

    • #初始约束条件  

      • 初始约束条件: not null
                create table teacher(
                    id int not null,  # 约束插入记录时id不能为空
                    name varchar(16),
                    age int
                );

        注意:
        1.创建表的字段名不能重复;
        create table test(
        id int,
        id int
        );

        2.最后一个字段不能在末尾加 , 号
        create table test(
        id int,
        age int,
        );

        3.字段名必须要有字段类型与宽度
        create table test(
        id int,
        name char
        );
  •      字段类型

            1) 确定表结构
        2) 字段与字段类型
            
    - 整型:
                - tinyint: 默认范围 -128, 127
                    create table t5(
                        id tinyint,
                        name varchar(16)
                    );
                    5.6.40
                    insert into t5 values(-128, ‘tank‘), (127, ‘jason‘);
                    insert into t5 values(-129, ‘tank‘);
                    insert into t5 values(128, ‘jason‘);
                    insert into t5 values(12, ‘sean‘);
    
                - int: 默认范围是(-2147483648, 2147483647)
    
                    应用场景: id号、年龄...
    
                    create table t6(
                        id int
                    );
    
                    # int 默认宽度11---> 默认展示宽度
                    insert into t6 values(-2147483649);
                    insert into t6 values(2147483648);
                    insert into t6 values(100);
    
                    create table t7(
                        id int(3)
                    );
    
                    # 若插入超过设定宽度,则正常显示
                    insert into t7 values(123456);
    
                    # 若插入不足够4位,则以空格补全
                    insert into t7 values(1);
    - 浮点型:

    应用场景: 工资、身高、体重...
            - float
                - double
                - decimal
    
                # 范围255是最大长度(包括.小数), 30代表是小数的位数
                create table t8(x float(255, 30));
                create table t9(x double(255, 30));
                create table t10(x decimal(65, 30));
    
                # 插入数据
                # 三种浮点型: 区别在于精确度不一样
                insert into t8 values(1.111111111111111111111111111111);
                insert into t9 values(1.1111111111111111111111111111);
                insert into t10 values(1.1111111111111111111111111111);
    - 字符类型
    - char(16): 定长字符

    char: 手机号、身份证号、银行卡号等...
              - 插入16个字符:
    
                    create table t11(
                        id int,
                        name char(4)  # 4
                    );
    
                    insert into t11 values(1, tank);
    
                    # utf8 中文3个bytes   gbk 中文2个bytes
                    insert into t11 values(2, 大鸡哥大鸡哥);
    
                    优点:
                        存取速度快
                    缺点:
                        浪费空间。
                        insert into t11 values(1, t);  # t+三个空格
    
                    egon + sean + tank
    
                - varchar(16): 不定长字符
                    - 存几个字符,就是几个字符的大小,每个字符前都要+1bytes
                    - 插入16个字符 ---> 1bytes+
    
                    优点:
                        节省空间。
    
                    create table t12(id int, name varchar(4));
                    insert into t12 values(1, egon);  # 1bytes + egon
                    insert into t12 values(2, tank);  # 1bytes + tank
                    insert into t12 values(3, sean);  # 1bytes + sean
    
                    insert into t12 values(4, t);  # 1bytes + t
                    1bytes + egon  、 1bytes + tank、 1bytes + sean
      - 日期类型
                - date: 2019-12-11
                - datetime: 2019-12-11 11:11:11
                - time: 11:11:11
                - year: 2019
                - timestamp: 时间戳
    
                create table student(
                    id int,
                    name varchar(4),
                    birth date,
                    register datetime,
                    work_time year,
                    t_time time,
                    update_time timestamp
                );
    
                insert into student values(1, 张全蛋, 2019-11-11, 2019-11-11 11:11:11,2019, 11:11:11, null);
                insert into student values(2, HCY, 1000-11-11, 1980-11-11 11:11:11,2019, 11:11:11, null);
    
                update student set name=HCY2号 where id=2;
    
                python 插入时间数据时,转成str类型。
    - 枚举与集合
      - enum: 可以 多选一
                    create table t13(
                        id int,
                        name varchar(4),
                        gender enum(male, female, others)
                    );
                    # insert into 表名(字段名) values(字段名对应的值);
                    insert into t13(id, name, gender) values(1, tank, male);
    
                    # 严格模式下,选择枚举以外的值会报错 set sql_mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION";
                    insert into t13(id, name, gender) values(2, gd, 人Y);
    
                - set: 可 多选一 或 多选多
                    create table t14(
                        id int,
                        name varchar(4),
                        gender enum(male, female, others),
                        hobbies set(read, sing, 生蚝, HSNM, 架子鼓)
                    );
    
                    # 多选一
                    insert into t14 values(1, 大鸡J, others, HSNM);
                    # 多选多
                    insert into t14 values(2, tank, male, read,架子鼓,sing,生蚝);
    
                    # 多选多的顺序可不一
                    insert into t14 values(2, tank, male, read,架子鼓,sing,生蚝);
    
    
    3.约束条件
     - not null + uniquecreate table user1(
                    id int not null,
                    name varchar(4)
                );
    
                insert into user1(id, name) values(null, tank);
                insert into user1(id, name) values(1, tank);
    
            - unique 将某个字段设置为唯一的值
    
                # not null + unique
                create table user2(
                    id int not null unique,
                    name varchar(4)
                );
    
                insert into user2(id, name) values(1, tank), (2, sean);
    
            - primary key + auto_increment: 主键+自增
    
                - primary key -----》 not null + unique
                    - pk就是表中的索引: 可以通过索引快速查找某些数据。
                        - 提高查询效率
    
                    # 将id设置为主键,非空切唯一
                    create table user3(
                        id int primary key,
                        name varchar(4)
                    );
    
                    insert into user3(id, name) values(1, tank);
                    insert into user3(id, name) values(2, tank);
    
                - auto_increment:
                    # 将id设置为自增
                    create table user4(
                        id int primary key auto_increment,
                        name varchar(4)
                    );
    
                    # 自增默认从0开始
                    insert into user4(name) values(tank);
                    insert into user4(name) values(sean);
                    insert into user4(name) values(egon);
                    insert into user4(name) values(大鸡哥);
    
                    # 若想自增从指定值开始,可插入第一条数据时先指定id的值;
                    insert into user4(id, name) values(10, tank);
                    insert into user4(name) values(sean);  # 11
                    insert into user4(name) values(egon);  # 12
                    insert into user4(name) values(大鸡哥);  # 13
    - unsigned
                - 无符号
                    create table user5(
                        id int unsigned
                    );
                    # 报错
                    insert into user5 values(-100);
                    insert into user5 values(0);
                    insert into user5 values(100);
    
                - 有符号
                    create table user6(
                        id int
                    );
    
                    insert into user6 values(-100);
    
            - zerofill
                使用0填充空格
                create table user7(
                        id int zerofill
                );
    
                insert into user7 values(100);
    
    
            - 删除记录
                create table user8(
                    id int primary key auto_increment,
                    name varchar(4)
                );
    
                insert into user8(name) values(tank);
                insert into user8(name) values(大大大), (egon);
    
                - delete:
                    # 清空user8表中的所有记录
                    delete from user8;
    
                - truncate:
                    # 清空user8表中的所以记录,并且id重置为0
                    truncate table user8;

     

     
     
     
     

    Mysql语句

    标签:update   大小   主键   数据类型   查看   UNC   删除   engine   设置   

    人气教程排行