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)
    );
    
    
  • 相关阅读:
    TFIDF<细读>
    数据挖掘工程师的面试问题与答题思路【转】
    CTR常见规则摘录
    分类中数据不平衡问题的解决经验[转载]
    机器学习积累【2】
    机器学习-常见问题积累【1】
    数据的归一化和标准化
    数据挖掘之特征选择
    jupyter-notebook快捷键
    python基础学习记录一
  • 原文地址:https://www.cnblogs.com/zhubincheng/p/12833669.html
Copyright © 2011-2022 走看看