zoukankan      html  css  js  c++  java
  • MySQL中的相关表操作

    简单表操作

    1.表操作之修改表

    1.修改表名
    alter table 表名 
                          rename 新表名
    
    2.增加字段
    alter table 表名 
                          add 新字段名  数据类型[相关约束性条件...]add 新字段名  数据类型[相关约束性条件...];
    
    alter table 表名
                          add 新字段名 数据类型[相关约束性条件...] first;
    
    alter table 表名
                          add 新字段名 数据类型[相关约束性条件...] after 字段名;
    
    3.删除字段
    alter table 表名
                          drop 字段名;
    
    4.修改字段
    alter table 表名
                        modify 字段名  数据类型[完整性约束条件...];
    alter table 表名
                        change 旧字段名 新字段名 数据类型[完整性约束条件...];
    alter table 表名
                        change  旧字段名 新字段名 新数据类型[完整性约束条件...];

    2.复制表

    # 复制表结构+记录 (key不会复制: 主键、外键和索引)
    create table new_service select * from service;
    
    # 只复制表结构
    select * from service where 1=2;        //条件为假,查不到任何记录
    
    create table new1_service select * from service where 1=2;  
    
    create table t4 like employees;

    表之间的对应关系

    1.一对多

    # 场景:员工和部门
       从员工的角度:多个员工可以属于同一个部门。
        从部门角度:多个部门不可以有同一个员工。
    这种关系就是一对多关系。
    
    
    # 创建部门表
    create table dep(
        id int primary key auto_increment,
        dep_name varchar(20),
        dep_desc varchar(50)
        );
    
    # 创建员工表
    create table emp(
        id int primary key auto_increment,
        name varchar(20),
        gender varchar(5),
        dep_id int,
        foreign key(dep_id) references dep(id)
        on update cascade
        on delete cascade
        );
    
    # 插入部门数据
    insert into dep(dep_name,dep_desc) values
    ('sfd','sfadfasfsafd');
    
    #  插入员工数据
    insert into emp(name,gender, dep_id) values
    ('sfsf','fsf',1);

    2.多对多关系

    # 图书表与作者表之间的关系
    """
    仍然站在两张表的角度:
    1.站在图书表:一本书可不可以有多个作者,可以!那就是书多对一作者
    2.站在作者表:一个作者可不可以写多本书,可以!那就是作者多对一书
    双方都能一条数据对应对方多条记录,这种关系就是多对多!
    """
    # 先来想如何创建表?图书表需要有一个外键关联作者,作者也需要有一个外键字段关联图书。问题来了,先创建谁都不合适!如何解决?
    # 建立第三张表,该表中有一个字段fk左表的id,还有一个字段是fk右表的id
    create table author(
        id int primary key auto_increment,
        name char(16)
    );
    
    create table book(
        id int primary key auto_increment,
        bname char(16),
        price int
    );
    
    insert into author(name) values
    ('eg'),
    ('a'),
    ('wx')
    ;
    insert into book(bname,price) values
    ('python从入门到入土',200),
    ('葵花宝典切割到精通',800),
    ('九阴真经',500),
    ('九阳神功',100)
    ;
    
    create table author2book(
        id int primary key auto_increment,
        author_id int,
        book_id int,
        foreign key(author_id) references author(id)
        on update cascade
        on delete cascade,
        foreign key(book_id) references book(id)
        on update cascade
        on delete cascade
    );
    
    insert into author2book(author_id,book_id) values
    (1,3),
    (1,4),
    (2,2),
    (2,4),
    (3,1),
    (3,2),
    (3,3),
    (3,4);

    3.一对一关系

    # 客户和学生表   客户——————学生
    # 左表的一条记录唯一对应右表的一条记录,反之也一样
    
    create table customer(
        id int primary key auto_increment,
        name char(20) not null,
        qq char(10) not null,
        phone char(16) not null
    );
    
    create table student(
        id int primary key auto_increment,
        class_name char(20) not null,
        customer_id int unique, #该字段一定要是唯一的
        foreign key(customer_id) references customer(id) #外键的字段一定要保证unique
        on delete cascade
        on update cascade
    );
    # 三种外键关系都是用foreign key,区别在于如何使用以及其他条件限制即可做出三种关系
  • 相关阅读:
    BDD
    linux 删除中文名称乱码的文件
    python代码调用linux命令
    linux 查看内存
    java学习day17--API-注解
    java学习day17--API-单例设计模式
    java学习day17--API-同步锁
    java学习day16--API-多线程创建两种方式
    java学习day16--API-多线程-->进程和线程
    java学习day15--API-Map-->HashMap
  • 原文地址:https://www.cnblogs.com/wanglei957/p/10862651.html
Copyright © 2011-2022 走看看