zoukankan      html  css  js  c++  java
  • 5.5---MySQL创建相互关联的表练习

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

    #用户表
    create table user(
    id int not null unique auto_increment,
    username varchar(20) not null,
    password varchar(50) not null,
    primary key(username,password)
    );
    
    #用户组表
    create table usergroup(
    id int primary key auto_increment,
    groupname varchar(20) not null unique
    );
    
    #主机表
    create table host(
    id int primary key auto_increment,
    ip char(15) not null unique default '127.0.0.1'
    );
    
    #业务线表
    create table business(
    id int primary key auto_increment,
    business varchar(20) not null unique
    );
    
    #建关系: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)
    );
    
    #建关系: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)
    );
    
    #建关系: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)
    );
    

    练习2:学校数据库的表建立

    # 班级表
    cid	caption
    # 学生表
    sid sname gender class_id
    # 老师表
    tid	tname
    # 课程表
    cid	cname	teacher_id
    # 成绩表
    sid	student_id course_id number
    
    # 创建学校数据库
    create database school;
    # 进入学校库
    use school;
    # 创建班级表
    create table class(
    	cid int,
        caption varchar(64)
    )
    # 创建学生表
    # 学生表与班级表为一对多的关系,且学生表为多的那一边
    create table student(
    	sid int primary key auto_increment,
        sname varchar(32),
    	gender enum("male","female","others") default"male",
        class_id int not null,
        foreign key(class_id) references class(cid)
        on update cascade
        on delete cascade
    );
    # 创建老师表
    create table teacher(
    	tid int primary key,	
    	tname varchar(64)
    );
    # 创建课程表
    # 老师与课程一对多,一个老师可以教多门课
    create table course(
    	cid int primary key,
        cname varchar(32),
        teacher_id int not null,
        foreign key(teacher_id) references teacher(tid)
        on update cascade
        on delete cascade
    );
    # 成绩表
    # student_id course_id 应为联合唯一
    create table score(
    	sid int primary key,
        student_id int not null,
        course_id int not null,
        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,
        unique(student_id,course_id)
    );
    
    
  • 相关阅读:
    Java String截取判断文件类型
    HttpServletRequest request 转 Json
    httprunner3.x详细教程七(三种方式实现参数化数据驱动)
    httprunner3.x详细教程六(httprunner的setup和teardown及hook)
    httprunner3.x详细教程五(debugtalk.py介绍)
    httprunner3.x详细教程四(.env文件介绍)
    httprunner3.x详细教程三(httprunner中testcase的构造)
    httprunner3.x详细教程二(har文件录制及har文件转换)
    httprunner3.x详细教程一(框架结构介绍及搭建)
    mybatis.xml和mapper.xml文件的基本配置
  • 原文地址:https://www.cnblogs.com/zhubincheng/p/12833669.html
Copyright © 2011-2022 走看看