4.4 - 数据库 - 表操作练习题

表操作练习题:练习:用户表,用户组表,主机表,业务线表      关联:用户与用户组  主机与业务线  用户与主机

# 用户表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‘);mysql> desc user;+----------+-------------+------+-----+---------+----------------+| Field    | Type        | Null | Key | Default | Extra          |+----------+-------------+------+-----+---------+----------------+| id       | int(11)     | NO   | UNI | NULL    | auto_increment || username | varchar(20) | NO   | PRI | NULL    |                || password | varchar(50) | NO   | PRI | NULL    |                |+----------+-------------+------+-----+---------+----------------+3 rows in set (0.00 sec)

mysql> select * from user;+----+----------+----------+| id | username | password |+----+----------+----------+|  1 | root     | 123      ||  2 | egon     | 456      ||  3 | alex     | alex3714 |+----+----------+----------+3 rows in set (0.00 sec)

# 用户组表create table usergroup(    id int primary key auto_increment,    groupname varchar(20) not null unique);insert into usergroup(groupname) values(‘IT‘),(‘Sale‘),(‘Finance‘),(‘Boss‘);mysql> desc usergroup;+-----------+-------------+------+-----+---------+----------------+| Field     | Type        | Null | Key | Default | Extra          |+-----------+-------------+------+-----+---------+----------------+| id        | int(11)     | NO   | PRI | NULL    | auto_increment || groupname | varchar(20) | NO   | UNI | NULL    |                |+-----------+-------------+------+-----+---------+----------------+2 rows in set (0.00 sec)

mysql> select * from usergroup;+----+-----------+| id | groupname |+----+-----------+|  4 | Boss      ||  3 | Finance   ||  1 | IT        ||  2 | Sale      |+----+-----------+4 rows in set (0.01 sec)

# 主机表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‘);mysql> desc host;+-------+----------+------+-----+-----------+----------------+| Field | Type     | Null | Key | Default   | Extra          |+-------+----------+------+-----+-----------+----------------+| id    | int(11)  | NO   | PRI | NULL      | auto_increment || ip    | char(15) | NO   | UNI | 127.0.0.1 |                |+-------+----------+------+-----+-----------+----------------+2 rows in set (0.00 sec)

mysql> select * from host;+----+---------------+| id | ip            |+----+---------------+|  5 | 172.10.45.3   ||  6 | 172.10.45.4   ||  7 | 172.10.45.5   ||  2 | 172.16.31.10  ||  4 | 172.16.31.11  ||  1 | 172.16.45.2   ||  3 | 172.16.45.3   ||  8 | 192.168.1.20  ||  9 | 192.168.1.21  || 10 | 192.168.1.22  || 12 | 192.168.2.223 || 11 | 192.168.2.23  || 13 | 192.168.2.24  || 14 | 192.168.3.22  || 15 | 192.168.3.23  || 16 | 192.168.3.24  |+----+---------------+16 rows in set (0.01 sec)

# 业务线表create table business(    id int primary key auto_increment,    business varchar(20) not null unique);insert into business(business) values(‘轻松贷‘),(‘随便花‘),(‘大富翁‘),(‘穷一生‘);mysql> desc business;+----------+-------------+------+-----+---------+----------------+| Field    | Type        | Null | Key | Default | Extra          |+----------+-------------+------+-----+---------+----------------+| id       | int(11)     | NO   | PRI | NULL    | auto_increment || business | varchar(20) | NO   | UNI | NULL    |                |+----------+-------------+------+-----+---------+----------------+2 rows in set (0.00 sec)

mysql> select * from business;+----+-----------+| id | business  |+----+-----------+|  3 | 大富翁    ||  4 | 穷一生    ||  1 | 轻松贷    ||  2 | 随便花    |+----+-----------+4 rows in set (0.00 sec)

# 建关系 user usergroupcreate 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);insert into user2usergroup(user_id,group_id) values(1,1),(1,2),(1,3),(1,4),(2,3),(2,4),(3,4);mysql> desc user2usergroup;+----------+---------+------+-----+---------+----------------+| Field    | Type    | Null | Key | Default | Extra          |+----------+---------+------+-----+---------+----------------+| id       | int(11) | NO   | UNI | NULL    | auto_increment || user_id  | int(11) | NO   | PRI | NULL    |                || group_id | int(11) | NO   | PRI | NULL    |                |+----------+---------+------+-----+---------+----------------+3 rows in set (0.00 sec)

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 businesscreate 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);mysql> desc host2business;+-------------+---------+------+-----+---------+----------------+| Field       | Type    | Null | Key | Default | Extra          |+-------------+---------+------+-----+---------+----------------+| id          | int(11) | NO   | UNI | NULL    | auto_increment || host_id     | int(11) | NO   | PRI | NULL    |                || business_id | int(11) | NO   | PRI | NULL    |                |+-------------+---------+------+-----+---------+----------------+3 rows in set (0.00 sec)

mysql> select * from host2business;+----+---------+-------------+| id | host_id | business_id |+----+---------+-------------+|  1 |       1 |           1 ||  2 |       1 |           2 ||  3 |       1 |           3 ||  4 |       2 |           2 ||  5 |       2 |           3 ||  6 |       3 |           4 |+----+---------+-------------+6 rows in set (0.00 sec)

# 建关系 user hostcreate 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),(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);mysql> desc user2host;+---------+---------+------+-----+---------+----------------+| Field   | Type    | Null | Key | Default | Extra          |+---------+---------+------+-----+---------+----------------+| id      | int(11) | NO   | UNI | NULL    | auto_increment || user_id | int(11) | NO   | PRI | NULL    |                || host_id | int(11) | NO   | PRI | NULL    |                |+---------+---------+------+-----+---------+----------------+3 rows in set (0.00 sec)

mysql> select * from user2host;+----+---------+---------+| id | user_id | host_id |+----+---------+---------+|  1 |       1 |       1 ||  2 |       1 |       2 ||  3 |       1 |       3 ||  4 |       1 |       4 ||  5 |       1 |       5 ||  6 |       1 |       6 ||  7 |       1 |       7 ||  8 |       1 |       8 ||  9 |       1 |       9 || 10 |       1 |      10 || 11 |       1 |      11 || 12 |       1 |      12 || 13 |       1 |      13 || 14 |       1 |      14 || 15 |       1 |      15 || 16 |       1 |      16 || 17 |       2 |       2 || 18 |       2 |       3 || 19 |       2 |       4 || 20 |       2 |       5 || 21 |       3 |      10 || 22 |       3 |      11 || 23 |       3 |      12 |+----+---------+---------+23 rows in set (0.00 sec)

原文地址:https://www.cnblogs.com/alice-bj/p/8830569.html

时间: 2024-10-24 22:14:49

4.4 - 数据库 - 表操作练习题的相关文章

mysql数据库表操作及授权

表操作:增删改查 把/etc/passwd文件的内容导入 passwd表里. mysql>load data infile"/etc/passwd" into table passwd fields terminated by ":"; 基于前面的passwd表,完成下列操作: 1:列出uid低于500且3个字母的用户 mysql> select name from passwd where uid<500 and name like "

Mysql1:数据库表操作,增删改查举例

数据库表的相关操作 添加数据库表 语法:  实例: 查看数据库表 语法: show tables; 实例: 查看数据库表结构 1)使用DESCRIBE/DESC  语法: 实例:  2)SHOW CREATE TABLE 语法:  实例: 使用此方法查看数据库表结构时,不仅可以查看表创建时候的详细语句,而且还可以查看存储引擎和字符编码.  修改表名 语法: to为可选参数,使用与否均不影响 实例: 将user_info表改为user_data 修改字段的数据类型 语法:  实例: 将id 的数据

2、数据库和数据库表操作

一.(dos下)数据库的创建.删除.查看和使用 1.1 普通方式登录后再使用数据库 1 Microsoft Windows [版本 6.1.7601] 2 版权所有 (c) 2009 Microsoft Corporation.保留所有权利. 3 4 C:\Users\Dell>cd c: 5 6 c:\>d: 7 8 D:\>cd D:\Program Files\MySQL\MySQL Server 5.0\bin 9 10 D:\Program Files\MySQL\MySQL

Mysql 数据库表操作

1 创建表: create table table_name ( filed 字段类型 [完整性约束], filed 字段类型 [完整性约束], filed 字段类型 [完整性约束], ): 2 查看表 desc table_name; 查看表结构 show tables: 是该数据库下所有的表名 show create table table_name : 查看数据库的表创建信息 3 修改表: alter table table_name add field type 完整性约束 alter

数据库表操作时出去死锁或卡主,最好的解决方法。。。。

就是利用可视化工具,先复制这张表,然后在删除卡死的那张表,最后将复制的表重命名即可. 出现卡死的原因可能是: 1.频繁的对某张表的字段进行操作,比如修改他的大小或数据类型啥的,可能就会导致出现锁表或卡死的状态. 2.若发现对某张表进行的某个字段进行操作时,卡死了,先去试试其他字段,或者其他表的字段看可不可以修改,若可以修改,怎就按照上面的办法,先复制,在删除,最后重命名. 还有一种方法: 利用可视化工具Navicat,按F6进行命令行,然后使用命令查看进程: >show full process

python 数据库表操作

语法: 1. 修改表名 ALTER TABLE 表名 RENAME 新表名; 2. 增加字段 ALTER TABLE 表名 ADD 字段名 数据类型 [完整性约束条件…], ADD 字段名 数据类型 [完整性约束条件…]; 3. 删除字段 ALTER TABLE 表名 DROP 字段名; 4. 修改字段 ALTER TABLE 表名 MODIFY 字段名 数据类型 [完整性约束条件…]; ALTER TABLE 表名 CHANGE 旧字段名 新字段名 旧数据类型 [完整性约束条件…]; ALTE

学习简单数据库表操作(Mysql)1

以orcl实例下,person表为例: 1.登录mysql: mysql安装路径下,bin目录,打开命令窗口,输入mysql -u用户名 -p密码; 2.显示所有实例,show databases; 3.进入orcl实例,use orcl; 4.显示person表字段信息,desc person; 5.修改字段描述,alter table person change pid PID int(11) not null; 6.修改表名,alter table person rename to peo

学习简单数据库表操作(Mysql)2

以orcl实例下,person表为例: 1.登录mysql: mysql安装路径下,bin目录,打开命令窗口,输入mysql -u用户名 -p密码; 2.显示所有实例,show databases; 3.进入orcl实例,use orcl; 4.显示person表字段信息,desc person; 5.修改字段描述,alter table person change pid PID int(11) not null; 6.修改表名,alter table person rename to peo

数据库表操作练习

/* 1.创建成绩表,字段包括:学生姓名,语文成绩,数学成绩,英语成绩 向表中插入多条数据: 查询: (1) 查询所有学生的数学成绩和总成绩 (2) 查询所有学生的语文和数学成绩和,按从高到低排序 (3) 查询班级总成绩最高的学生姓名 (4) 查询班里所有姓李学生的总成绩最高的姓名 */ -- 创建表 create table examres( id int primary key auto_increment, name VARCHAR(20), Chinese FLOAT(5,2), Mat