第八次作业(备份和索引)

一、备份与恢复作业:
创库,建表:
    CREATE DATABASE booksDB;
    use booksDB;

    CREATE TABLE books
    (
    bk_id  INT NOT NULL PRIMARY KEY,
    bk_title VARCHAR(50) NOT NULL,
    copyright YEAR NOT NULL
    );

    CREATE TABLE authors
    (
    auth_id     INT NOT NULL PRIMARY KEY,
    auth_name  VARCHAR(20),
    auth_gender CHAR(1)
    );

    CREATE TABLE authorbook
    (
    auth_id  INT NOT NULL,
    bk_id   INT NOT NULL
    );

插入数据:
    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);
    
    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');
    
    INSERT INTO authorbook
        VALUES (1001, 11033), (1002, 11035), (1003, 11072), (1004, 11028),
        (1011, 11078), (1012, 11026), (1012, 11041), (1014, 11069);


1、使用mysqldump命令备份数据库中的所有表
2、备份booksDB数据库中的books表
3、使用mysqldump备份booksDB和test数据库(test数据库自行准备)
4、使用mysql命令还原第二题导出的book表
5、进入数据库使用source命令还原第二题导出的book表

1、C:\Windows\System32>mysqldump -uroot -p20040830Nmx@ booksDB > book.sql
2、C:\Windows\System32>mysqldump -uroot -p20040830Nmx@ booksDB books> book1.sql
3、C:\Windows\System32>mysqldump -uroot -p20040830Nmx@ --databases booksDB test> bbt.sql
4、C:\Windows\System32>mysql -uroot -p20040830Nmx@ book<book.sql
5、mysql> source book1.sql
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 8 rows affected (0.00 sec)
Records: 8  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)
 

二、索引作业:
创库,建表:
    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 
    );
     
    mysql> create table category( 
    cat_id int primary key auto_increment, 
    cate_name varchar(20), 
    parent_id int default 0 );


1、删除 goods 表中的 goods_desc 字段及货号字段,并增加 click_count 字段
2、在 goods_name 列上加唯一性索引(用alter table方式):
2、在 shop_price 列上加普通索引(用create index方式)
4、在 click_count 上增加普通索引,然后再删除 (分别使用drop index和alter table删除)
 

1、mysql>alter table goods drop goods_desc;
mysql> alter table goods drop goods_id;
mysql> alter table goods add click_count int;
2、mysql> alter table goods add unique index index_name(goods_name);
3、mysql> create index index_price on goods(shop_price);
4、mysql> create index index_count on goods( click_count);
mysql> drop index index_count on goods;
mysql> alter table goods drop index index_count;
 

 

  • 12
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在MySQL 8中备份库表和索引有几种方法。如果你只需要备份表字段和数据(不包含主键和索引),你可以使用快速备份命令。如果你想备份表字段(包含主键和索引)和数据,你可以使用普通的备份命令。 快速备份表字段和数据(不包含主键和索引)可以使用以下命令: ``` mysqldump --no-create-info --skip-add-drop-table database_name table_name > backup.sql ``` 这个命令将会生成一个.sql文件,其中包含了指定表的字段和数据,但不包含主键和索引。 如果你想备份表字段(包含主键和索引)和数据,可以使用以下命令: ``` mysqldump database_name table_name > backup.sql ``` 这个命令将会生成一个.sql文件,其中包含了指定表的字段、主键、索引和数据。 在备份过程中,你还可以使用一些选项来控制备份的行为。例如,你可以使用`--single-transaction`选项来确保在备份期间不会锁定表。 关于索引,你可以使用`key`列来查看MySQL实际决定使用的键(索引)。如果没有选择索引,键的值将为NULL。如果你想强制MySQL使用或忽视`possible_keys`列中的索引,可以在查询中使用`FORCE INDEX`、`USE INDEX`或者`IGNORE INDEX`。 另外,需要注意的是,InnoDB引擎不支持FULLTEXT类型的全文索引。但是你可以使用Sphinx插件来支持全文索引,并且在效果上更好。 总结起来,你可以使用快速备份命令来备份表字段和数据(不包含主键和索引),或者使用普通备份命令来备份表字段(包含主键和索引)和数据。同时,你可以使用选项来控制备份的行为,并且在索引使用上可以使用一些关键字来指定MySQL的行为。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [MySQL表备份语句(包括索引等)](https://blog.csdn.net/a1275302036/article/details/120922910)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [mysql索引详解](https://blog.csdn.net/guorui_java/article/details/118558095)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值