数据库第八次作业

一、备份与恢复作业:
创库,建表:

mysql> create database booksDB;
mysql> use booksDB;

mysql> create table books(
    -> bk_id INT NOT NULL PRIMARY KEY,
    -> bk_title VARCHAR(50) NOT NULL,
    -> copyright YEAR NOT NULL);
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE authors(
    -> auth_id INT NOT NULL PRIMARY KEY,
    -> auth_name VARCHAR(20),
    -> auth_gender CHAR(1));
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE authorbook(
    -> auth_id INT NOT NULL,
    -> bk_id INT NOT NULL);
Query OK, 0 rows affected (0.01 sec)

插入数据:

mysql> INSERT INTO books VALUES 
    -> (11078, 'Learning MySQL', 2010),
    -> (11033, 'Study Html', 2011),
    -> (11035, 'How to use php', 2003),
    -> (11072, 'Teach youself javascript', 2005),
    -> (11028, 'Learing C++', 2005),
    -> (11069, 'MySQL professional', 2009),
    -> (11026, 'Guide to MySQL 5.5', 2008),
    -> (11041, 'Inside VC++', 2011);
Query OK, 8 rows affected (0.00 sec)
Records: 8  Duplicates: 0  Warnings: 0

mysql> INSERT INTO authors VALUES
    -> (1001, 'WriterX' ,'f'),
    -> (1002, 'WriterA' ,'f'),
    -> (1003, 'WriterB' ,'m'),
    -> (1004, 'WriterC' ,'f'),
    -> (1011, 'WriterD' ,'f'),
    -> (1012, 'WriterE' ,'m'),
    -> (1013, 'WriterF' ,'m'),
    -> (1014, 'WriterG' ,'f'),
    -> (1015, 'WriterH' ,'f');
Query OK, 9 rows affected (0.00 sec)
Records: 9  Duplicates: 0  Warnings: 0

mysql>  INSERT INTO authorbook VALUES
    -> (1001, 11033), (1002, 11035), (1003, 11072), (1004, 11028),(1011, 11078), (1012, 11026), (1012, 11041), (1014, 11069);
Query OK, 8 rows affected (0.00 sec)
Records: 8  Duplicates: 0  Warnings: 0

1、使用mysqldump命令备份数据库中的所有表

mysqldump  -B -uroot -proot booksDB > /root/mysql/all_tables.sql

2、备份booksDB数据库中的books表

mysqldump  -uroot -proot booksDB books > /root/mysql/table_books.sql

3、使用mysqldump备份booksDB和test数据库(test数据库自行准备)

mysqldump -uroot -p --databases booksDB test >/root/mysql/books_test_DB.sql
Enter password:******

4、使用mysql命令还原第二题导出的book表

[root@node1 ~]# mysql -u root -p booksDB</root/mysql/table_books.sql
Enter password: 
mysql> select * from books;
+-------+--------------------------+-----------+
| bk_id | bk_title                 | copyright |
+-------+--------------------------+-----------+
| 11026 | Guide to MySQL 5.5       |      2008 |
| 11028 | Learing C++              |      2005 |
| 11033 | Study Html               |      2011 |
| 11035 | How to use php           |      2003 |
| 11041 | Inside VC++              |      2011 |
| 11069 | MySQL professional       |      2009 |
| 11072 | Teach youself javascript |      2005 |
| 11078 | Learning MySQL           |      2010 |
+-------+--------------------------+-----------+

5、进入数据库使用source命令还原第二题导出的book表

mysql> drop table books;
mysql> source /root/mysql/table_books.sql
mysql> select * from books;
+-------+--------------------------+-----------+
| bk_id | bk_title                 | copyright |
+-------+--------------------------+-----------+
| 11026 | Guide to MySQL 5.5       |      2008 |
| 11028 | Learing C++              |      2005 |
| 11033 | Study Html               |      2011 |
| 11035 | How to use php           |      2003 |
| 11041 | Inside VC++              |      2011 |
| 11069 | MySQL professional       |      2009 |
| 11072 | Teach youself javascript |      2005 |
| 11078 | Learning MySQL           |      2010 |
+-------+--------------------------+-----------+

二、索引作业:
创库,建表: 

mysql> create table goods(
    -> goods_id int primary key auto_increment,
    -> goods_name varchar(20) not null,
    -> cat_id int not null default 0,
    -> brand_id int not null default 0,
    -> goods_sn char(12) not null,
    -> shop_price float(6,2) not null default 0.00,
    -> goods_desc text);
Query OK, 0 rows affected (0.01 sec)

 mysql> create table category( 
     -> cat_id int primary key auto_increment, 
     -> cate_name varchar(20), 
     -> parent_id int default 0 );
Query OK, 0 rows affected (0.01 sec)

1、删除 goods 表中的 goods_desc 字段及货号字段,并增加 click_count 字段

mysql> alter table goods drop column goods_desc;
mysql> alter table goods drop column goods_sn;
+-------------+-------------+------+-----+---------+----------------+
| Field       | Type        | Null | Key | Default | Extra          |
+-------------+-------------+------+-----+---------+----------------+
| goods_id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| goods_name  | varchar(20) | NO   | UNI | NULL    |                |
| cat_id      | int(11)     | NO   |     | 0       |                |
| brand_id    | int(11)     | NO   |     | 0       |                |
| shop_price  | float(6,2)  | NO   |     | 0.00    |                |
+-------------+-------------+------+-----+---------+----------------+


mysql> alter table goods add click_count int;
+-------------+-------------+------+-----+---------+----------------+
| Field       | Type        | Null | Key | Default | Extra          |
+-------------+-------------+------+-----+---------+----------------+
| goods_id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| goods_name  | varchar(20) | NO   |     | NULL    |                |
| cat_id      | int(11)     | NO   |     | 0       |                |
| brand_id    | int(11)     | NO   |     | 0       |                |
| goods_sn    | char(12)    | NO   |     | NULL    |                |
| shop_price  | float(6,2)  | NO   |     | 0.00    |                |
| click_count | int(11)     | YES  |     | NULL    |                |
+-------------+-------------+------+-----+---------+----------------+

2、在 goods_name 列上加唯一性索引(用alter table方式):

mysql> alter table goods add unique index goods_name_index (goods_name(20));
mysql> show create table goods\G;
*************************** 1. row ***************************
       Table: goods
Create Table: CREATE TABLE `goods` (
  `goods_id` int(11) NOT NULL AUTO_INCREMENT,
  `goods_name` varchar(20) NOT NULL,
  `cat_id` int(11) NOT NULL DEFAULT '0',
  `brand_id` int(11) NOT NULL DEFAULT '0',
  `shop_price` float(6,2) NOT NULL DEFAULT '0.00',
  `click_count` int(11) DEFAULT NULL,
  PRIMARY KEY (`goods_id`),
  UNIQUE KEY `goods_name_index` (`goods_name`),
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4

3、在 shop_price 列上加普通索引(用create index方式)

mysql> create index g_price_index on goods(shop_price);
mysql> show create table goods\G;
*************************** 1. row ***************************
       Table: goods
Create Table: CREATE TABLE `goods` (
  `goods_id` int(11) NOT NULL AUTO_INCREMENT,
  `goods_name` varchar(20) NOT NULL,
  `cat_id` int(11) NOT NULL DEFAULT '0',
  `brand_id` int(11) NOT NULL DEFAULT '0',
  `shop_price` float(6,2) NOT NULL DEFAULT '0.00',
  `click_count` int(11) DEFAULT NULL,
  PRIMARY KEY (`goods_id`),
  UNIQUE KEY `goods_name_index` (`goods_name`),
  KEY `g_price_index` (`shop_price`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4

4、在 click_count 上增加普通索引,然后再删除 (分别使用drop index和alter table删除)

mysql> alter table goods add index c_l_index (click_count);
mysql> show create table goods\G;
*************************** 1. row ***************************
       Table: goods
Create Table: CREATE TABLE `goods` (
  `goods_id` int(11) NOT NULL AUTO_INCREMENT,
  `goods_name` varchar(20) NOT NULL,
  `cat_id` int(11) NOT NULL DEFAULT '0',
  `brand_id` int(11) NOT NULL DEFAULT '0',
  `shop_price` float(6,2) NOT NULL DEFAULT '0.00',
  `click_count` int(11) DEFAULT NULL,
  PRIMARY KEY (`goods_id`),
  UNIQUE KEY `goods_name_index` (`goods_name`),
  KEY `g_price_index` (`shop_price`),
  KEY `c_l_index` (`click_count`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4

mysql> alter table goods drop index c_l_index;
mysql> drop index c_l_index on goods;

mysql> show create table goods\G;
*************************** 1. row ***************************
       Table: goods
Create Table: CREATE TABLE `goods` (
  `goods_id` int(11) NOT NULL AUTO_INCREMENT,
  `goods_name` varchar(20) NOT NULL,
  `cat_id` int(11) NOT NULL DEFAULT '0',
  `brand_id` int(11) NOT NULL DEFAULT '0',
  `shop_price` float(6,2) NOT NULL DEFAULT '0.00',
  `click_count` int(11) DEFAULT NULL,
  PRIMARY KEY (`goods_id`),
  UNIQUE KEY `goods_name_index` (`goods_name`),
  KEY `g_price_index` (`shop_price`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很高兴能够帮助您,以下是第八章的数据库理论作业题目和答案,供您参考。 1. 什么是多值依赖?请举例说明。 答:多值依赖是指在一个关系中,存在非主属性组成的部分函数依赖关系,即存在非主属性组成的属性集合,能够决定其他非主属性的值,而不受主属性的影响。例如,学生选课关系中,如果一个学生选了多门课程,那么这些课程之间就存在多值依赖关系,因为这些课程并不依赖于学生的主码,而是彼此之间互相依赖。 2. 什么是第三范式?请简要阐述其原理。 答:第三范式是指在一个关系中,每个非主属性都不依赖于其他非主属性,而是只依赖于主属性。其原理是通过消除传递依赖,使得关系模式中的属性之间仅存在简单的函数依赖关系。这样可以避免数据冗余和更新异常,提高数据库的性能和可靠性。 3. 什么是BCNF?与第三范式有何不同? 答:BCNF(Boyce-Codd范式)是一种更高级别的范式,它要求关系中的所有属性都依赖于候选码,而不是依赖于主码。与第三范式的不同在于,第三范式只消除了部分函数依赖和传递依赖,而BCNF还能够消除包含函数依赖和其他更高级别的依赖关系。 4. 什么是函数依赖?请简要阐述其原理。 答:函数依赖是指在一个关系中,一个或多个属性能够决定其他属性的值。例如,在学生选课关系中,学生姓名能够决定学生的学号和班级,因此学生姓名对学号和班级具有函数依赖关系。函数依赖原理是通过消除属性之间的函数依赖,使得一个关系模式能够达到最小化和规范化的状态,从而提高数据库的性能和可靠性。 5. 什么是主属性和非主属性? 答:主属性是指在一个关系中,能够唯一标识元组的属性集合,也称为候选码或主码。非主属性是指除了主属性以外的属性集合,它们不具有唯一性特征,但是能够通过主属性和其他非主属性的组合来确定元组的属性值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值