物理备份的优缺点: 物理备份的还原速度⾮常快。但是物理备份的最⼩粒度只能做到表。
逻辑备份的优缺点:
逻辑备份有⾮常强的兼容性,⽽物理备份则对版本要求⾮常⾼ 逻辑备份也对保持数据的安全性有保证。
逻辑备份要对RDBMS产⽣额外的压⼒,且逻辑备份的结果可能要⽐源⽂件更⼤,逻辑备份还可能会丢失浮点数的精度信息。
把创建索引的列的内容进行排序并生成倒序表,然后在倒序表内容后拼接上数据地址链,在查询时先拿出倒序表内容再拿到数据链地址,最后拿到具体数据。
优点:索引的优点是可以提⾼检索数据的速度。
缺点:创建和维护索引需要耗费时间,耗费时间的数量随着数据量的增加⽽增加,且索引需要占用物理空间。
视图通过以定制的⽅式显⽰来⾃⼀个或多个表的数据 视图是⼀种数据库对象,⽤⼾可以像查询普通表⼀样查询视图 视图内其实没有存储任何数据 ,它只是对表的⼀个查询 视图的定义保存在数据字典内,创建视图所基于对表称为“基表”。
视图是一个虚拟表,其内容由查询定义。数据库中只存放了视图的定义,而并没有存放视图中的数据,这些数据存放在原来的表中。
使操作简单化
增加数据的安全性
视图只能看到查询语句定义的相关内容,而其他的表结构及内容是看不到的。
提供了灵活⼀致级别安全性。
隐藏了数据的复杂性
简化了⽤⼾的SQL指令
通过重命名列,从另⼀个⻆度提供数据
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);
mysqldump -B -uroot -proot booksDB > /root/mysql/all_tables.sql
mysqldump -uroot -proot booksDB books > /root/mysql/table_books.sql
mysqldump -uroot -p --databases booksDB test >/root/mysql/books_test_DB.sql
Enter password:
[root@node1 ~]# mysql -u root -p booksDB 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> 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
-> );
mysql> create table category( cat_id int primary key auto_increment, cate_name varchar(20), parent_id int default 0 );
mysql> alter table goods engine=myisam;
mysql> alter table category engine=myisam;
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 | |
+-------------+-------------+------+-----+---------+----------------+
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
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
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
#Student
mysql> create table Student(
-> Sno int primary key auto_increment,
-> Sname varchar(20) not null,
-> Ssex char(1),
-> Sage int,
-> Sdept varchar(20)
-> );
#Course
mysql> create table Course(
-> Cno int primary key auto_increment,
-> Cname varchar(50)
-> );
#SC
mysql> create table SC( Sno int , Cno int primary key auto_increment ,Score float(6,2));
#Student
mysql> insert into Student values(
-> 1,'bob','m',20,'人工智能');
mysql> insert into Student values( 2,'harry','m',22,'电气信息');
mysql> insert into Student values( 3,'natasha','f',23,'电气信息');
mysql> insert into Student values( 4,'sarah','m',21,'人文');
mysql> insert into Student values( 5,'manalo','f',21,'艺术');
+-----+---------+------+------+--------------+
| Sno | Sname | Ssex | Sage | Sdept |
+-----+---------+------+------+--------------+
| 1 | bob | m | 20 | 人工智能 |
| 2 | harry | m | 22 | 电气信息 |
| 3 | natasha | f | 23 | 电气信息 |
| 4 | sarah | m | 21 | 人文 |
| 5 | manalo | f | 21 | 艺术 |
+-----+---------+------+------+--------------+
#Course
mysql> insert into Course values( 10,'JAVA');
mysql> insert into Course values( 11,'英语');
mysql> insert into Course values( 12,'近代史');
mysql> insert into Course values( 13,'C++');
mysql> insert into Course values( 14,'毛概');
+-----+-----------+
| Cno | Cname |
+-----+-----------+
| 10 | JAVA |
| 11 | 英语 |
| 12 | 近代史 |
| 13 | C++ |
| 14 | 毛概 |
+-----+-----------+
#SC
mysql> insert into SC values(1,10,78.5);
mysql> insert into SC values(2,11,82.5);
mysql> insert into SC values(3,14,90.0);
mysql> insert into SC values(4,13,98.5);
mysql> insert into SC values(5,12,75.5);
+-----+------+-------+
| Sno | Cno | Score |
+-----+------+-------+
| 1 | 10 | 78.50 |
| 2 | 11 | 82.50 |
| 3 | 14 | 90.00 |
| 4 | 13 | 98.50 |
| 5 | 12 | 75.50 |
+-----+------+-------+
create view stu_co_info as select s.Sname,s.Ssex,co.Cname,c.Score from Student s , SC c ,Course co where s.Sno=c.Sno and c.Cno=co.Cno;
#
mysql> select * from stu_co_info;
+---------+------+-----------+-------+
| Sname | Ssex | Cname | Score |
+---------+------+-----------+-------+
| bob | m | JAVA | 78.50 |
| harry | m | 英语 | 82.50 |
| manalo | f | 近代史 | 75.50 |
| sarah | m | C++ | 98.50 |
| natasha | f | 毛概 | 90.00 |
+---------+------+-----------+-------+
mysql> drop view stu_co_info;
mysql> select * from stu_co_info;
ERROR 1146 (42S02): Table 'test.stu_co_info' doesn't exist