HTML基礎(chǔ)知識(shí)(31)【視圖】、【視圖算法】、【備份】、【數(shù)據(jù)還原】、【SQL備份、還原數(shù)據(jù)】、【增量備份】

一、視圖

view:是一種有結(jié)構(gòu),但是沒結(jié)果的虛擬表
1、創(chuàng)建視圖

基本語法:create view 視圖名字 as select 語句;

創(chuàng)建單表視圖:基表只有一個(gè)

創(chuàng)建多表視圖:基表來源至少兩個(gè)

2、查看視圖

show tables [like] / desc 視圖名 / show create table 視圖名;

3、修改視圖

alter view 視圖名字 as 新的select語句;

4、刪除視圖

drop view 視圖名字;

二、新增數(shù)據(jù)

多表視圖不能新增數(shù)據(jù)

可以向單表視圖插入數(shù)據(jù),但是視圖中包含的字段必須有基表中所有不能為空、或沒有默認(rèn)值的字段

視圖是可以向基表插入數(shù)據(jù)的

三、刪除數(shù)據(jù)

多表視圖不能刪除數(shù)據(jù)

單表視圖可以刪除數(shù)據(jù)

四、更新數(shù)據(jù)

多表視圖不能更新數(shù)據(jù)

更新限制:with check option;

五、視圖算法

視圖算法:系統(tǒng)對(duì)視圖以及外部查詢視圖的select語句的一種解析方式

視圖算法分三種

1、undefined:未定義(默認(rèn)的)

2、temptable:臨時(shí)表算法

3、merge:合并算法

六、算法指定

算法指定:在創(chuàng)建視圖的時(shí)候create algorithm=指定算法 view 視圖名字 as select語句;

七、方式:

1、數(shù)據(jù)表備份

2、單表數(shù)據(jù)備份

3、SQL備份

4、增量備份

八、數(shù)據(jù)表備份

1、存儲(chǔ)引擎:innodb、myisam

(1)innodb:只有表結(jié)構(gòu),數(shù)據(jù)全部存儲(chǔ)到 ibdata1 文件中

(2)myisam:表、數(shù)據(jù)和索引全部單獨(dú)分開存儲(chǔ)

九、單表數(shù)據(jù)備份

1、備份:select */字段列表 into outfile 文件所在路徑 from 數(shù)據(jù)源;

2、高級(jí)備份:select */字段列表 into outfile 文件所在路徑 fields 字段處理 lines 行處理 from 數(shù)據(jù)源;

(1)fields:字段處理,enclosed by(默認(rèn)'')、terminated by(默認(rèn)'\t')、escaped by(默認(rèn)'\\')

(2)lines:行處理,starting by(默認(rèn)'')、terminated by(默認(rèn)'\r\n')

3、數(shù)據(jù)還原:

(1)load data infile 文件所在路徑

(2)into table 表名[(字段列表)]

(3)fields 字段處理

(4)lines 行處理;

十、SQL備份

1、備份:mysqldump.exe

mysqldump/mysqldump.exe -hPup 數(shù)據(jù)庫名字 [數(shù)據(jù)表名字1 [數(shù)據(jù)表名字2...]] > 外部文件路徑

2、整庫備份:

mysqldump/mysqldump.exe -hPup 數(shù)據(jù)庫名字 > 外部文件路徑

十一、SQL還原數(shù)據(jù)

1、方案一:使用mysql.exe客戶端還原

mysql.exe/mysql -hPup 數(shù)據(jù)庫名字 < 備份文件目錄

2、方案二:使用SQL指令還原

source 備份文件所在路徑

十二、增量備份

備份的是系統(tǒng)日志文件


代碼部分————————————————————————

-- 視圖:單表+多表

create view my_v1as select *from my_student;

create view my_v2as select *from my_class;

create view my_v3as select *from my_student

as sleft join my_classas con s.c_id=c.id;-- id重復(fù)

-- 多表視圖

create view my_v3as select s.*,c.c_name,c.roomfrom my_student

as sleft join my_classas con s.c_id=c.id;

-- 查看視圖創(chuàng)建語句

showcreate view my_v3\G

-- 視圖使用

select *from my_v1;

select *from my_v2;

select *from my_v3;

-- 修改視圖

alter view my_v1as select id,name,sex,age,height,c_idfrom my_student;

-- 刪除視圖

drop view 視圖名;

-- 多表視圖插入數(shù)據(jù)

insert into my_v3

values(null,'bc20190006','張三豐','男',150,180,1,'Python1907','B407');-- 錯(cuò)誤的

-- 將學(xué)生表的學(xué)號(hào)字段設(shè)置為不允許為空

alter table my_student modify numberchar(10)not null unique;

-- 單表視圖出入數(shù)據(jù),視圖不包含所有不允許為空的字段

insert into my_v1

values(null,'張三豐',150,'男',180,1);-- 失敗(學(xué)號(hào)不允許為空)

-- 單表視圖插入數(shù)據(jù)

insert into my_v2

values(2,'Python1811','B410');

-- 多表視圖刪除數(shù)據(jù)

delete from my_v3where id=1;-- 失?。ú豢筛模?/p>

-- 單表視圖刪除數(shù)據(jù)

delete from my_v2where id=4;

-- 多表視圖更新數(shù)據(jù)

update my_v3set c_id=3 where id=5;

-- 視圖:age字段限制更新

create view my_v4as select *from my_student

where age>30 with check option;-- 表示視圖的數(shù)據(jù)來源都是年齡大于30歲,是由where age >30決定的

-- with check option決定通過視圖更新的時(shí)候,不能講已經(jīng)得到的數(shù)據(jù)age>30的改成<30的

-- 將視圖可以查到的數(shù)據(jù)改成年齡小于30

update my_v4set age=28 where id =3;

-- 可以修改數(shù)據(jù):可以改,但是視圖查不到

update my_v4set age=32 where id=2;

-- 獲取所有班級(jí)中最高的一個(gè)學(xué)生

create view my_v5as select *from my_studentorder by heightdesc;

select *from my_v5group by c_id;

select *from my_studentgroup by c_idorder by heightdesc;

-- 指定算法為臨時(shí)表算法

create algorithm=temptableview my_v6as select *from my_studentorder by heightdesc;

select *from my_v6group by c_id;

——————————————————————————

-- 查看MySQL的版本

select @@version;

-- 創(chuàng)建myisam表

create table my_myisam(

idint

)charset utf8 engine=myisam;

-- 向my_myisam表插入幾條記錄

insert into my_myisamvalues(1),(2),(3);

-- 單表數(shù)據(jù)備份

select *into outfile

'D:/1907/web/student.txt'

from my_student;

select *into outfile

'D:/1907/web/student.txt'

from my_class;

-- 指定備份處理方式

select *into outfile

'D:/1907/web/student.txt'

-- 字段處理

fields

enclosedby '''' -- 數(shù)據(jù)使用雙引號(hào)包括

terminatedby '|'-- 使用豎線分隔字段數(shù)據(jù)

-- 行處理

lines

startingby 'START:'

from my_class;

-- 刪除表

delete from my_class;

-- 還原數(shù)據(jù)

load data? infile

'D:o1907/web/student.txt'

into table my_class

-- 字段處理

fields

enclosedby '''' -- 數(shù)據(jù)使用雙引號(hào)包括

terminatedby '|'-- 使用豎線分隔字段數(shù)據(jù)

-- 行處理

lines

startingby 'START:'

-- SQL備份

mysqldump -uroot -p123456

mydatabase my_student > D:/1907/web/student.sql

-- 整庫備份

mysqldump -uroot -p123456

mydatabase > D:/1907/web/mydatabase.sql

-- 還原數(shù)據(jù):mysql客戶端還原

mysql -uroot -p123456 mydatabase < D:/1907/web/student.sql

-- SQL指令還原SQL備份

source D:/1907/web/student.sql;

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容