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

#用户表

mysql> create table user(
id int not null unique auto_increment,
username varchar(50) not null,
password varchar(50) not null,
primary key(username,password));
Query OK, 0 rows affected (0.12 sec)

插入用户信息
mysql> insert into user(username,password) values('root',''),('alex',''),('mike','');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from user;
+----+----------+----------+
| id | username | password |
+----+----------+----------+
| 1 | root | 123 |
| 2 | alex | 1234 |
| 3 | mike | 1234 |
+----+----------+----------+
3 rows in set (0.09 sec)


#用户组表

mysql> create table usergroup(
id int primary key auto_increment,
groupname varchar(20) not null unique);
Query OK, 0 rows affected (0.21 sec) mysql> insert into usergroup(groupname) values
('IT'),
('sale'),
('Finance'),
('boss');
Query OK, 4 rows affected (0.10 sec)
Records: 4 Duplicates: 0 Warnings: 0 mysql> select * from usergroup;
+----+-----------+
| id | groupname |
+----+-----------+
| 4 | boss |
| 3 | Finance |
| 1 | IT |
| 2 | sale |
+----+-----------+
4 rows in set (0.00 sec)

#主机表

mysql> create table host(
id int primary key auto_increment,
ip char(16) not null unique default '127.0.0.1');
Query OK, 0 rows affected (0.13 sec

插入ip记录

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')
;

#业务线表
mysql> create table business(id int primary key auto_increment,business varchar(20) not null unique);
Query OK, 0 rows affected (0.20 sec) mysql> insert into business(business) values
-> ('轻松贷'),
-> ('随便花'),
-> ('大富翁'),
-> ('穷一生')
-> ;
Query OK, 4 rows affected (0.04 sec)
Records: 4 Duplicates: 0 Warnings: 0

多对多关系练习

#建关系:user与usergroup

创建一张user2usergroup表
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)
on delete cascade
on update cascade,
foreign key(group_id) references usergroup(id)
on delete cascade
on update cascade
);

插入记录


mysql> insert into user2usergroup(user_id,group_id) values(1,1),(1,2),(1,3),(1,4),(2,3),(2,4),(3,4);
Query OK, 7 rows affected (0.09 sec)
Records: 7 Duplicates: 0 Warnings: 0 mysql> select * from user2usergroup;
+----+---------+----------+
| id | user_id | group_id |
+----+---------+----------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
| 4 | 1 | 4 |
| 5 | 2 | 3 |
| 6 | 2 | 4 |
| 7 | 3 | 4 |
+----+---------+----------+
7 rows in set (0.00 sec)


#建关系: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)
on delete cascade
on update cascade,
foreign key(business_id) references business(id)
on delete cascade
on update cascade
);

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)
on delete cascade
on update cascade,
foreign key(host_id) references host(id)
on delete cascade
on update cascade
);
insert into user2host(user_id,host_id) values
(1,1),
(1,2),
(1,3),
(1,4),
(2,2),
(2,3),
(2,4),
(2,5),
(3,10),
(3,11),
(3,12)
;
ysql> select * from user2host;
+----+---------+---------+
| id | user_id | host_id |
+----+---------+---------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
| 4 | 1 | 4 |
| 5 | 2 | 2 |
| 6 | 2 | 3 |
| 7 | 2 | 4 |
| 8 | 2 | 5 |
| 9 | 3 | 10 |
| 10 | 3 | 11 |
| 11 | 3 | 12 |
+----+---------+---------+
11 rows in set (0.00 sec)
 
 

 


mysql 建立表之间关系 练习 1的更多相关文章

  1. mysql 建立表之间关系 练习 2

    创建数据库db6 create database db6 charset=utf8; user db6; # 创建班级表 mysql) not null unique); Query OK, rows ...

  2. mysql 建立表之间关系 一对一 练习1

    创建db5数据库 create database db5 charset=utf8; use db5; 例一:一个用户只有一个博客 用户表: id name 1 mike 2 alex 3 jack ...

  3. mysql 建立表之间关系 一对一 练习2

    创建db5数据库 create database db5 charset=utf8; use db5; 例二:一个管理员唯一对应一个用户 用户表: id user password 1 egon xx ...

  4. Django数据库的查看、删除,创建多张表并建立表之间关系

    配置以下两处,可以方便我们直接右键运行tests.py一个文件,实现对数据库操作语句的调试: settings里面的设置: #可以将Django对数据库的操作语法,能输出对应的的sql语句 LOGGI ...

  5. 【转】Oracle - 数据库的实例、表空间、用户、表之间关系

    [转]Oracle - 数据库的实例.表空间.用户.表之间关系 完整的Oracle数据库通常由两部分组成:Oracle数据库和数据库实例. 1) 数据库是一系列物理文件的集合(数据文件,控制文件,联机 ...

  6. Hibernate_day03--课程安排_表之间关系_一对多操作

    Hibernate_day03 上节内容 今天内容 表与表之间关系回顾(重点) Hibernate的一对多操作(重点) 一对多映射配置(重点) 一对多级联操作 一对多级联保存 一对多级联删除 一对多修 ...

  7. 阶段3 1.Mybatis_09.Mybatis的多表操作_1 mybatis表之间关系分析

    4.mybatis中的多表查询     表之间的关系有几种:         一对多         多对一         一对一         多对多     举例:         用户和订单 ...

  8. Oracle - 数据库的实例、表空间、用户、表之间关系

    完整的Oracle数据库通常由两部分组成:Oracle数据库和数据库实例. 1) 数据库是一系列物理文件的集合(数据文件,控制文件,联机日志,参数文件等): 2) Oracle数据库实例则是一组Ora ...

  9. [转]Oracle - 数据库的实例、表空间、用户、表之间关系

    本文转自:http://www.cnblogs.com/adforce/p/3312252.html 完整的Oracle数据库通常由两部分组成:Oracle数据库和数据库实例. 1) 数据库是一系列物 ...

随机推荐

  1. oracle分区的名称和值要一致

    名称是01,后面的值也必须是02,不能是前面的是1,后面的是02,被这个问题困扰了好久.

  2. Android开发 - 图形化生成的贝塞尔插值器

    基于三次方贝塞尔曲线的插值器 在动画开发过程中,经常需要使用到插值器来满足我们的动画设计需求.然而,官方提供的插值器并不能满足所有的需求,所以我们需要自定义插值器. 下面介绍的三次方贝塞尔曲线的插值器 ...

  3. vmware复制虚拟机出现Error:No suitable device found:no device found for connection 'System eth0'

    vmware复制虚拟机出现Error:No suitable device found:no device found for connection 'System eth0' 废话不多说,直接给出解 ...

  4. Grodno 2015 (Urozero May 2015 Day 5) D Triangles

    给出$P(<=10^9)$, 求有多少个有序三元组$(a, b, c),\ gcd(a, b, c) = 1,\ a + b + c <= P$且以它们构成的三角形中存在某个角是另外一个角 ...

  5. 【vijos】1768 顺序对的值(特殊的技巧)

    https://vijos.org/p/1768 之前不知道为什么,我yy了一个n^2的做法,但是没能写出来..sad 然后看了题解才发现这题好神.. 为什么一定要照着题意找两个点然后算呢?这就是问题 ...

  6. 使用Array的原型使对象具有length,和数组的内容

    var elems = { length: , add: function (elem) { Array.prototype.push.call(this, elem); }, gather: fun ...

  7. 根本上解决npm install 报错“ajv-keywords@3.4.0 requires a peer of ajv@^6.9.1 but none is installed. You must install peer dependencies yourself.“

    每次项目npm install 的时候都报这个错误, 然后网上找的方法就把这个 ajv重新安装下,感觉有点麻烦, 后来有次我把npm更新了一下(我的版本是: 6.1.0),更新到了最新版本,这个问题就 ...

  8. DataContract 和 DataMember

    数据契约(DataContract) 服务契约定义了远程访问对象和可供调用的方法,数据契约则是服务端和客户端之间要传送的自定义数据类型. 一旦声明一个类型为DataContract,那么该类型就可以被 ...

  9. 基于注解的Spring AOP拦截含有泛型的DAO

    出错场景 1.抽象类BaseDao public abstract class BaseDao<T> { public BaseDao() { entityClass = (Class&l ...

  10. docker 中安装 FastDFS 总结

    如题,参考各资料后,安装FastDFS总结.基于已有docker镜像 https://hub.docker.com/r/luhuiguo/fastdfs/ docker pull luhuiguo/f ...