• python程 day38 数据库——表和数据的操作 数据类型 set和enum 约束 外键 表与表之间的关系


    一、今日内容

    1、存储引擎

    • 存储数据的方法

    • Innodb

      • 数据和索引存储在一起 2个文件

        • 数据索引表结构

      • 数据持久化

      • 支持事务 :为了保证数据的完成性,

      • 支持行级锁

        • 修改的行少

        • 保证数据安全

      • 支持表级锁

        • 批量修改多行的时候使用

      • 支持外键

        • 约束两张表中的关联字段不能随意的添加(删除),

    2、创建表

    create TABLE student( cid int primary key auto_increment, #唯一,递增 name char(18) not null, gender enum('male','female') DEFAULT 'male', #设置默认值 class_id int, FOREIGN key(class_id) REFERENCES class(id) on update CASCADE )

    FOREIGN key(class_id) REFERENCES class(id) 建立约束

    on update CASCADE 同步更新

     

    存储引擎

    # Innodb mysql5.6之后的默认存储引擎
      # 2个文件,4个支持(支持事务,行级锁,表级锁,外键)
    # Myisam mysql5.5之前的默认存储引擎
      # 3个文件 支持表级锁
    # Memory
      # 1个文件 数据断电消失

    数据类型

    # 数字 : bool int float(7,2)
    # 日期 : date time datetime year
    # 字符串 :
      # char   定长 效率高浪费空间 255
      # varchar 变长 效率低节省空间 65535
    # enum 和 set :
      # 单选和多选

    约束

    # unsigned 无符号的
    # not null 非空
    # default 设置默认值
    # unique   唯一,不能重复
      # unique(字段1,字段2,字段3) 联合唯一
    # auto_increment 自增
      # int 必须至少unique字段,自带not null
    # primary key 主键
      # not null + unique
      # 一张表只能有一个主键
    # foreign key 外键
      # a表中有一个字段关联b表中的一个unique
      # a表中的是外键

    建表

    # create table 表名(
    #   字段名1 类型(长度) 约束,
    #   字段名1 类型(选项) 约束,
    # );

    修改表结构

    # alter table 表名 rename 新名字;
    # alter table 表名 add 字段名 类型(长度) 约束 after 某字段;
    # alter table 表名 drop 字段名;
    # alter table 表名 modify 字段名 类型(长度) 约束 first;
    # alter table 表名 change 旧字名 新名字 类型(长度) 约束;

    表之间的关系

    # 一对一
    # 一对多
    # 多对多

    删除表

    # drop table 表名;

     

     

     

     

     

     

     

     

     

     

     

     

    创建教室表:

    create table class( cid int primary key auto_increment, caption char(20) not null )

    创建学生表:

    create table student( sid int primary key , sname char(20) not null, gender enum('男','女','未知') DEFAULT '男', class_id int, foreign key(class_id) references class(cid) )

  • 相关阅读:
    Different AG groups have the exactly same group_id value if the group names are same and the ‘CLUSTER_TYPE = EXTERNAL/NONE’
    An example of polybase for Oracle
    use azure data studio to create external table for oracle
    Missing MSI and MSP files
    You may fail to backup log or restore log after TDE certification/key rotation.
    Password is required when adding a database to AG group if the database has a master key
    Use KTPASS instead of adden to configure mssql.keytab
    ardunio+舵机
    android webview 全屏100%显示图片
    glide 长方形图片显示圆角问题
  • 原文地址:https://www.cnblogs.com/iaoyuyuyuhuanghuang/p/14436508.html
Copyright © 2020-2023  润新知