Mysql数据库学习:备份、索引、视图

目录

备份:先创建如下数据库和表

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

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

3、使用mysqldump备份booksDB和test数据库

4、使用mysqldump备份服务器中的所有数据库

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

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

索引

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

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

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

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

视图


备份:先创建如下数据库和表

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
);
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);

CREATE TABLE authors
(
  auth_id     INT NOT NULL PRIMARY KEY,
  auth_name  VARCHAR(20),
  auth_gender CHAR(1)
);
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');

CREATE TABLE authorbook
(
  auth_id  INT NOT NULL,
  bk_id   INT NOT NULL,
  PRIMARY KEY (auth_id, bk_id),
  FOREIGN KEY (auth_id) REFERENCES authors (auth_id),
  FOREIGN KEY (bk_id) REFERENCES books (bk_id)
);

INSERT INTO authorbook
VALUES (1001, 11033), (1002, 11035), (1003, 11072), (1004, 11028),
(1011, 11078), (1012, 11026), (1012, 11041), (1014, 11069);

	

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

mysqldump --databases booksDB -uroot -p123 > /booksDB1.sql

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

mysqldump  booksDB books -uroot -p123 > /booksDB2.sql

3、使用mysqldump备份booksDB和test数据库

mysqldump  --databases booksDB --tables books -uroot -p123 > /booksDB3.sql

4、使用mysqldump备份服务器中的所有数据库

mysqldump  --all-databases -uroot -p123 > /booksDB3.sql

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

mysql -uroot -p123  booksDB <  /booksDB2.sql

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

source  /booksDB2.sql

索引

创建如下数据库

create database test1 charset utf8;
use test1
create table goods(
    -> goods_id int(11) primary key auto_increment,
    -> goods_name varchar(20) not null,
    -> cat_id int(11) not null default(0),
    -> brand_id int(11) not null default(0),
    -> goods_sn char(12) not null,
    -> shop_price float(6,2) not null default(0.00),
    -> goods_desc text)engine=myisam charset utf8;


create table category(
    -> cat_id int(11) not null primary key auto_increment,
    -> cate_name varchar(20) not null,
    -> parent_id int(11) not null default(0))engine=myisam charset utf8;

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

alter table goods drop goods_desc;
alter table goods drop goods_id;
alter table goods add click_count int(11);

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

alter table goods add unique index uni_index_goods_name(goods_name);

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

create index shop_price_index on goods(shop_price);


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

创建索引:
create index click_count_index on goods(click_count);

drop 删除:
drop index click_count_index on goods;

alter删除:
alter table goods drop index click_count_index;

视图

创建如下表:

create table student(
    -> sno int(11) primary key auto_increment,
    -> sname varchar(20),
    -> ssex enum('m','f'),
    -> sage int(11),
    -> sdept varchar(20));

create table course( cno int(11) primary key, cname varchar(20));
create view info as select * from student s,course c where s.sno=c.cno;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一路喝狗狗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值