• python学习之老男孩python全栈第九期_数据库day002知识点总结 —— MySQL数据库day2


    一. 复习

    1. MySQL:
        - 服务端
        - 客户端
    2. 通信交流
        - 授权
        - SQL语句
            - 数据库
                创建数据库:
                    create database db1 default charset utf8;
                删除数据库:
                    drop database db1;
            - 数据表
                创建数据表:
                    create table tb1(
                        id int not null auto_increment primary key,
                        name char(10),
                        depatment_id int,
                        constraint fk_l foreign key ('department_id') references 表名('tid')
                    )engine = innodb default charset = utf8;

            - 数据行
                    增:insert into tb1(name,age) values('alex','18');

                    删:delete from tb1;
                        delete from tb1 where id > 10
                        truncate table tb1;

                    改:update tb1 set name = 'root' where > 10

                    查:select * from tb;         # * 的效率低
                        select id,name from tb;

                        
    二. 外键的补充

    1. 主键:
        - 一张表 只能有 一个主键
        - 一个主键 不一定 是一列

    主键可以这样写:
    create table t1(

        nid int(11) not null auto_increment,
        pid int(11) not null,
        num int(11) null,
        primery key (nid,pid)                   # 主键有两列,不常用

    )engine = innodb default charset = utf8;


    # 主键设置成两列,外键就可以关联两列
    create table t2(

        id int auto_increment primary key,
        name varchar(32),
        id1 int,
        id2 int,
        constraint fk_t1_t2 foreign key ('id1','id2') references t1('nid','pid')

    )engine = innodb default charset = utf8;


    create table t1(

        nid int(11) not null auto_increment,
        pid int(11) not null,
        num int(11) null,
        primery key (nid,pid)                   # 主键有两列,不常用

    )engine = innodb default charset = utf8;


    三. 自增列之起始值

    show create table t1  # 查看怎么创建的(代码)

    show create table t1 G  # 查看怎么创建的(代码)



    create table t1(

        nid int(11) not null auto_increment,
        pid int(11) not null,
        num int(11) null,
        primery key (nid,pid)                   # 主键有两列,不常用

    )engine = innodb auto_increment = 4 default charset = utf8;            # 设置auto_increment默认值

    alter table t1 auto_increment = 1                # 修改自增的初始值


    四. 自增列之步长
        
    1. MySQL:自增步长
        基于会话级别:
            登录一次可以设置一个步长,不能像sqlServer一样(可以在每个表里面设置步长)
        
            show session variables like 'auto_inc%';        # 查看步长(auto_increment_increment)
            set session auto_increment_increment = 2;        # 设置会话步长
        基于全局级别:(尽量不用)
            show global variables like 'auto_inc%';            # 查看全局变量
            set global auto_increment_increment = 2;
            更改之后,打开一个会话就会默认使用更改之后的步长

    2. sqlServer:自增步长
        基于表级别:
            create table t1(

                nid int(11) not null auto_increment,
                pid int(11) not null,
                num int(11) null,
                primery key (nid,pid)
                
            )engine = innodb auto_increment = 4 步长 = 2 default charset = utf8;
            
            
            create table t2(

                nid int(11) not null auto_increment,
                pid int(11) not null,
                num int(11) null,
                primery key (nid,pid)
                
            )engine = innodb auto_increment = 4 步长 = 2 default charset = utf8;

    五. 唯一索引(约束不能重复(可以有一个值为空),加速查找)

        create table t1(
            id int ...,
            num int,
            xx int,
            # unique uq1 (num)        # num 是唯一的,不允许重复
            unique uq1 (num,xx)        # num与xx 联合唯一,两个不能都一样
        )

    PS:
    主键 也不能重复,但不能为空


    六. 外键的变种

    a. 用户表和部门表:(一对多)
        用户:         部门id
            1 alex        1
            2 root        1
            3 egon        2
            4 laoyao    3
        部门:
            1 服务
            2 保安
            3 公关
    b. 用户表和博客表:(一对一)
        用户:        
            1 alex
            2 root
            3 egon
            4 laoyao
        博客表(最多4个):        
            id    博客地址    用户id (FK() + 唯一索引)
            1  /alex3714/    1
            2  /yuanchenqi/  4
        
        一个用户只能有一个博客园:FK() + 唯一索引 进行约束
    c. 多对多:
        示例1:
            用户表(百合网)
            相亲记录表
    示例2: 用户表 主机表 用户主机关系表


      

  • 相关阅读:
    进程相关知识点
    vue上传
    Storageclass 外挂NFS配置与应用
    centos强制关机后,网卡无法启动
    前端工程化2-webpack使用与学习
    android应用获取应用签名
    js获取tif格式图片的dpi
    查询生成二级树型结构最高效的方式
    03解决隔离的方案
    02SingleSpa实战
  • 原文地址:https://www.cnblogs.com/lpgit/p/9434319.html
Copyright © 2020-2023  润新知