• day46---数据库练习


    练习:账号信息表,用户组,主机表,主机组

    1. 新建用户表
    # 新建用户表
    create table user(
        id int not null unique auto_increment,
        username varchar(20) not null,
        password varchar(50) not null,
        primary key(username,password)
    );
    

    1. 用户组表
    # 用户组表
    create table usergroup(
        id int primary key auto_increment,
        groupname varchar(20) not null unique
    );
    

    1. 主机表
    # 主机表
    create table host(
        id int primary key auto_increment,
        ip char(15) not null unique default '127.0.0.1'
    );
    

    1. 业务线表
    create table business(
        id int primary key auto_increment,
        business varchar(20) not null unique
    );
    

    1. 建关系:user和usergroup
    create table user2usergroup(
        id int not null unique auto_increment,
        user_id int not null,
        group_id int not null,
        primary key(user_id,group_id),
        foreign key(user_id) references user(id),
        foreign key(group_id) references usergroup(id)
    );
    

    1. 建关系:host和business
    create table host2business(
        id int not null unique auto_increment,
        host_id int not null,
        business_id int not null,
        primary key(host_id,business_id),
        foreign key(host_id) references host(id),
        foreign key(business_id) references business(id)
    );
    

    1. 建关系:user和host
    create table user2host(
        id int not null unique auto_increment,
        user_id int not null,
        host_id int not null,
        primary key(user_id,host_id),
        foreign key(user_id) references user(id),
        foreign key(host_id) references host(id)
    );
    

    练习

    # 班级表
    create table class(
        cid int primary key auto_increment,
        caption varchar(20) not null unique
    );
    
    # 学生表
    create table student(
        sid int not null unique auto_increment,
        sname varchar(20) not null,
        gender enum('female','male') default 'male',
        class_id int not null,
        primary key(sid,sname),
        foreign key(class_id) references class(cid)
        on update cascade
        on delete cascade
    );
    
    # 老师表
    create table teacher(
        tid int not null unique auto_increment,
        tname varchar(20) not null,
        primary key(tid,tname)
    );
    
    # 课程表
    create table course(
        cid int not null unique auto_increment,
        cname varchar(20) not null unique,
        teacher_id int not null,
        primary key(cid,cname),
        foreign key(teacher_id) references teacher(tid)
        on update cascade
        on delete cascade
    );
    
    # 成绩表
    create table score(
        sid int primary key auto_increment,
        student_id int not null,
        course_id int not null unique,
        number int not null,
        foreign key(student_id) references student(sid)
        on update cascade
        on delete cascade,
        foreign key(course_id) references course(cid)
        on update cascade
        on delete cascade
    );
    
  • 相关阅读:
    前端学习的几个网站
    程序员怎么写出一份漂亮的简历
    程序员斗图专用表情包
    2018年国内就业薪资高的7大编程语言排行
    微信小程序初步运营方案
    「干货」从菜鸟到大神,前端学习书籍推荐
    数据分析概述和理论基础
    十大厂商为什么要联合推出“快应用”对标小程序?
    数据分析的过程
    H5混合开发二维码扫描以及调用本地摄像头
  • 原文地址:https://www.cnblogs.com/surpass123/p/12831971.html
Copyright © 2020-2023  润新知