zoukankan      html  css  js  c++  java
  • 数据库-表关系练习

    完成下例sql语句的编写

    用户表

    create table user(
    id int not null unique auto_increment,
    username varchar(20) not null,
    password varchar(50) not null,
    primary key(username,password)
    );

    insert into user(username,password) values
    ('root','123'),
    ('egon','456'),
    ('alex','alex3714')
    ;

    用户组表

    create table usergroup(
    id int primary key auto_increment,
    groupname varchar(20) not null unique
    );

    insert into usergroup(groupname) values
    ('IT'),
    ('Sale'),
    ('Finance'),
    ('boss')
    ;

    主机表

    create table host(
    id int primary key auto_increment,
    ip char(15) not null unique default '127.0.0.1'
    );

    insert into host(ip) values
    ('172.16.45.2'),
    ('172.16.31.10'),
    ('172.16.45.3'),
    ('172.16.31.11'),
    ('172.10.45.3'),
    ('172.10.45.4'),
    ('172.10.45.5'),
    ('192.168.1.20'),
    ('192.168.1.21'),
    ('192.168.1.22'),
    ('192.168.2.23'),
    ('192.168.2.223'),
    ('192.168.2.24'),
    ('192.168.3.22'),
    ('192.168.3.23'),
    ('192.168.3.24')
    ;

    业务线表

    create table business(
    id int primary key auto_increment,
    business varchar(20) not null unique
    );
    insert into business(business) values
    ('轻松贷'),
    ('随便花'),
    ('大富翁'),
    ('穷一生')
    ;

    建关系: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)
    );

    insert into user2usergroup(user_id,group_id) values
    (1,1),
    (1,2),
    (1,3),
    (1,4),
    (2,3),
    (2,4),
    (3,4)
    ;

    建关系: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)
    );

    insert into host2business(host_id,business_id) values
    (1,1),
    (1,2),
    (1,3),
    (2,2),
    (2,3),
    (3,4)
    ;

    建关系: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)
    );

    insert into user2host(user_id,host_id) values
    (1,1),
    (1,2),
    (1,3),
    (1,4),
    (1,5),
    (1,6),
    (1,7),
    (1,8),
    (1,9),
    (1,10),
    (1,11),
    (1,12),
    (1,13),
    (1,14),
    (1,15),
    (1,16),
    (2,2),
    (2,3),
    (2,4),
    (2,5),
    (3,10),
    (3,11),
    (3,12);

    编写sql语句 设计下图中的表,并说明相互之间的关系

  • 相关阅读:
    Java对【JSON数据的解析】--Gson解析法
    Java对【JSON数据的解析】--官方解析法
    Java之JSON数据
    网络编程应用:基于UDP协议【实现聊天程序】--练习
    {网络编程}和{多线程}应用:基于UDP协议【实现多发送方发送数据到同一个接收者】--练习
    PHP获取页面执行时间的方法(推荐)
    Linux下查看CPU型号,内存大小,硬盘空间的命令(详解)
    Elasticsearch 全字段搜索_all,query_string查询,不进行分词
    mysql 查询某字段值全是数字
    linux服务器中Apache隐藏index.php失败
  • 原文地址:https://www.cnblogs.com/yangyuanhu/p/11178844.html
Copyright © 2011-2022 走看看