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

#用户表

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. 小案例:struts1.3利用nested标签使用POJO

    其中的关键就是这个POJO是你自己去new一个,struts是不会帮你创建的!参考http://luohua.iteye.com/blog/39976 表单页 <%@ page language ...

  2. target="_blank" 导致的钓鱼攻击

    挺久的漏洞,之前没仔细看现在看了下 直接构建实验环境: test1.html: <!DOCTYPE html> <html> <head> <meta cha ...

  3. 1.2.3 Task and Back Stack - 任务和回退堆

    一个应用通常包含多个Activities.每个activity的设计应该围绕着某种指定类型的action,如果这样做了,用户就可以执行该action,也可以用它来开启另外的activity.例如,邮件 ...

  4. HDU 1075 What Are You Talking About (Trie)

    What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K ...

  5. WebService 页面重定向错误

    “/”应用程序中的服务器错误. 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败. xxx.xxx.xxx.xxx:xx 说明: 执行当前 Web 请求期间,出现未经处理的异常.请 ...

  6. poj3041 Asteroids 匈牙利算法 最小点集覆盖问题=二分图最大匹配

    /** 题目:poj3041 Asteroids 链接:http://poj.org/problem?id=3041 题意:给定n*n的矩阵,'X'表示障碍物,'.'表示空格;你有一把枪,每一发子弹可 ...

  7. pl/sql 实例精解 06

    1. 简单循环 1: LOOP 2: statement1; 3: statement2; 4: EXIT WHEN condition; 5: END LOOP; 6: statement3; 也可 ...

  8. jQuery Colorbox使用教程

    jQuery Colorbox是一款弹出层,内容播放插件,效果极佳,最关键的是大小只有10KB,当然我主要是用来弹出图片啦,(之前介绍过jquery Fancybox插件,个人很喜欢).jQuery ...

  9. Python中3元运算符的实现

    这是今天在温习lambda表达式的时候想到的问题,众所周知C系列语言中的 三元运算符(?:)是一个非常好用的语句, 关于C中的三元运算符表达式1?表达式2:表达式3 那么在python应该如何实现呢, ...

  10. eventlet设计模式

    1. 客户端模式(Client Pattern) 一个权威的客户端模式就是网络爬虫,下面例子列出一些站点URL,并尝试检索他们的网页内容以做后续操作 import eventlet from even ...