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

#用户表

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. javascript断点设置和调试

    JS调试必备的5个debug技巧http://www.jb51.net/article/47812.htm Chrome 中的 JavaScript 断点设置和调试技巧http://www.xuebu ...

  2. 文件io之——read/write

    read函数从打开的设备或文件中读取数据. #include <unistd.h>ssize_t read(int fd, void *buf, size_t count);返回值:成功返 ...

  3. android怎样写一个自己定义的dialog能够在Title的位置弹出来

    先上效果图: Title的Layout为: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/andr ...

  4. 转载:ffmpeg 音视频合成分割

    http://blog.csdn.net/jixiuffff/article/details/5709976 当然先安装了 gentoo 下一条命令搞定 emerge  ffmpeg 格式转换 (将f ...

  5. Unix系统编程()open,read,write和lseek的综合练习

    需求:程序的第一个命令行参数为将要打开的文件名称,余下的参数则指定了文件上执行的输入输出操作.每个表示操作的参数都以一个字母开头,紧跟以相关值(中间无空格分隔). soffet:从文件开始检索到off ...

  6. Angular js ie 7,8 兼容性

    Angularjs  官网有云: 1)在html 里面 ,有ng-app 的标签里需要定义个id ,id='ng-app'; 2)ie 7及以下版本需要json2.js或json3.js,主要用来解析 ...

  7. 通过某个进程号显示该进行打开的文件 lsof -p 1 11. 列出多个进程号对应的文件信息 lsof -p 123,456,789 5. 列出某个用户打开的文件信息 lsof -u username

    linux命令 — lsof 查看进程打开那些文件 或者 查看文件给那个进程使用 lsof命令是什么? 可以列出被进程所打开的文件的信息.被打开的文件可以是 1.普通的文件,2.目录  3.网络文件系 ...

  8. php-fpm 如果dm设置为 static,那么其实只有pm.max_children这个参数生效。系统会开启设置数量的php-fpm进程。

    php-fpm未优化网友反映的问题 1.最近将Wordpress迁移至阿里云.由于自己的服务器是云服务器,硬盘和内存都比较小,所以内存经常不够使,通过ps ax命令查看后,发现启动php-fpm进程数 ...

  9. js 播放声音文件

    from:http://hi.baidu.com/xykking/item/7f64a04364e43ce4bdf45127 我在做项目的时候,也遇到了这个问题,可以灵活的使用下面的这两种方法,我使用 ...

  10. C#中oracle数据库的连接方法

    C#中oracle数据库的连接方法 一.关于数据库的操作 1.数据库连接      有2种:      第一种:古老的方法(较为死板,不利于灵活操作),即用OracleConnection的类来连接 ...