介紹
在本實(shí)驗(yàn)中,我們將學(xué)習(xí)和練習(xí)索引、視圖、備份和恢復(fù)。這些概念對(duì)于數(shù)據(jù)庫(kù)管理員來(lái)說(shuō)非常重要。
學(xué)習(xí)目標(biāo)
創(chuàng)建索引
創(chuàng)建視圖
備份與恢復(fù)
準(zhǔn)備
開(kāi)始之前,我們需要準(zhǔn)備好環(huán)境。
啟動(dòng)mysql服務(wù)并以root身份登錄。
cd ~/project sudo service mysql start mysql -u root
登錄后復(fù)制
加載文件中的數(shù)據(jù)。需要在mysql控制臺(tái)輸入命令來(lái)構(gòu)建數(shù)據(jù)庫(kù):
source ~/project/init-database.txt
登錄后復(fù)制登錄后復(fù)制
指數(shù)
索引是與表相關(guān)的結(jié)構(gòu)。它的作用相當(dāng)于一本書(shū)的目錄。您可以根據(jù)目錄中的頁(yè)碼快速找到內(nèi)容
當(dāng)你要查詢(xún)一張記錄數(shù)量較多的表,并且該表沒(méi)有索引時(shí),那么會(huì)拉出所有記錄一一匹配搜索條件,并返回符合條件的記錄。非常耗時(shí),導(dǎo)致大量的磁盤(pán)i/o操作。
如果表中存在索引,那么我們可以通過(guò)索引值快速找到表中的數(shù)據(jù),從而大大加快查詢(xún)過(guò)程。
有兩種方法可以為特定列設(shè)置索引:
alter table table name add index index name (column name); create index index name on table name (column name);
登錄后復(fù)制
讓我們使用這兩條語(yǔ)句來(lái)構(gòu)建索引。
在employee表的id列建立idx_id索引:
alter table employee add index idx_id (id);
登錄后復(fù)制
在employee表的name列建立idx_name索引
create index idx_name on employee (name);
登錄后復(fù)制
我們使用索引來(lái)加速查詢(xún)過(guò)程。當(dāng)沒(méi)有足夠的數(shù)據(jù)時(shí),我們將無(wú)法感受到它的神奇力量。這里我們使用命令show index from table name來(lái)查看我們剛剛創(chuàng)建的索引
show index from employee;
登錄后復(fù)制
mariadb [mysql_labex]> alter table employee add index idx_id (id); query ok, 0 rows affected (0.005 sec) records: 0 duplicates: 0 warnings: 0 mariadb [mysql_labex]> show index from employee; +----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+ | table | non_unique | key_name | seq_in_index | column_name | collation | cardinality | sub_part | packed | null | index_type | comment | index_comment | ignored | +----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+ | employee | 0 | primary | 1 | id | a | 5 | null | null | | btree | | | no | | employee | 0 | phone | 1 | phone | a | 5 | null | null | | btree | | | no | | employee | 1 | emp_fk | 1 | in_dpt | a | 5 | null | null | | btree | | | no | | employee | 1 | idx_id | 1 | id | a | 5 | null | null | | btree | | | no | | employee | 1 | idx_name | 1 | name | a | 5 | null | null | yes | btree | | | no | +----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+ 5 rows in set (0.000 sec)
登錄后復(fù)制
當(dāng)我們使用select語(yǔ)句查詢(xún)時(shí),where條件會(huì)自動(dòng)判斷是否存在索引。
看法
視圖是從一個(gè)或多個(gè)表派生的虛擬表。它就像一個(gè)窗口,通過(guò)它人們可以查看系統(tǒng)提供的特殊數(shù)據(jù),從而不必查看數(shù)據(jù)庫(kù)中的全部數(shù)據(jù)。他們可以專(zhuān)注于他們感興趣的事情。
如何解釋“view是一個(gè)虛擬表”?
數(shù)據(jù)庫(kù)中只存儲(chǔ)view的定義,而其數(shù)據(jù)存儲(chǔ)在原表中;
當(dāng)我們使用view查詢(xún)數(shù)據(jù)時(shí),數(shù)據(jù)庫(kù)會(huì)相應(yīng)地從原表中提取數(shù)據(jù)。
由于view中的數(shù)據(jù)取決于原始表中存儲(chǔ)的內(nèi)容,因此一旦表中的數(shù)據(jù)發(fā)生變化,我們?cè)趘iew中看到的內(nèi)容也會(huì)發(fā)生變化。
將 view 視為表格。
創(chuàng)建view時(shí)使用的語(yǔ)句格式:
create view view name (column a, column b, column c) as select column 1, column 2, column 3 from table name;
登錄后復(fù)制
從語(yǔ)句中我們可以看到后半部分是一條select語(yǔ)句,這意味著view也可以建立在多個(gè)表上。我們需要做的就是在 select 語(yǔ)句中使用子查詢(xún)或 join 。
現(xiàn)在讓我們創(chuàng)建一個(gè)名為v_emp的簡(jiǎn)單視圖,其中包含三列v_name、v_age、v_phone:
create view v_emp (v_name,v_age,v_phone) as select name,age,phone from employee;
登錄后復(fù)制
然后輸入
select * from v_emp;
登錄后復(fù)制
mariadb [mysql_labex]> create view v_emp (v_name,v_age,v_phone) as select name,age,phone from employee; query ok, 0 rows affected (0.003 sec) mariadb [mysql_labex]> select * from v_emp; +--------+-------+---------+ | v_name | v_age | v_phone | +--------+-------+---------+ | tom | 26 | 119119 | | jack | 24 | 120120 | | jobs | null | 19283 | | tony | null | 102938 | | rose | 22 | 114114 | +--------+-------+---------+ 5 rows in set (0.000 sec)
登錄后復(fù)制
備份
出于安全考慮,備份在數(shù)據(jù)庫(kù)管理中極其重要。
導(dǎo)出文件僅保存數(shù)據(jù)庫(kù)中的數(shù)據(jù),而備份將整個(gè)數(shù)據(jù)庫(kù)結(jié)構(gòu)(包括數(shù)據(jù)、約束、索引、視圖等)保存到新文件。
mysqldump是mysql中用于備份的實(shí)用程序。它生成一個(gè) sql 腳本文件,其中包含從頭開(kāi)始重新創(chuàng)建數(shù)據(jù)庫(kù)的所有基本命令,例如 create、insert 等。
使用mysqldump備份的聲明:
mysqldump -u root database name > backup file name; #backup entire database mysqldump -u root database name table name > backup file name; #backup the entire table
登錄后復(fù)制
嘗試備份整個(gè)數(shù)據(jù)庫(kù)mysql_labex。將文件命名為 bak.sql。首先按ctrl+z退出mysql控制臺(tái),然后打開(kāi)終端輸入命令:
cd ~/project/ mysqldump -u root mysql_labex > bak.sql;
登錄后復(fù)制
使用命令“l(fā)s”,我們會(huì)看到備份文件bak.sql;
cat bak.sql
登錄后復(fù)制
-- mariadb dump 10.19 distrib 10.6.12-mariadb, for debian-linux-gnu (x86_64) -- -- host: localhost database: mysql_labex -- ------------------------------------------------------ -- server version 10.6.12-mariadb-0ubuntu0.22.04.1 /*!40101 set @old_character_set_client=@@character_set_client */; /*!40101 set @old_character_set_results=@@character_set_results */; /*!40101 set @old_collation_connection=@@collation_connection */; /*!40101 set names utf8 */; /*!40103 set @old_time_zone=@@time_zone */; /*!40103 set time_zone='+00:00' */; /*!40014 set @old_unique_checks=@@unique_checks, unique_checks=0 */; /*!40014 set @old_foreign_key_checks=@@foreign_key_checks, foreign_key_checks=0 */; /*!40101 set @old_sql_mode=@@sql_mode, sql_mode='no_auto_value_on_zero' */; /*!40111 set @old_sql_notes=@@sql_notes, sql_notes=0 */; ……
登錄后復(fù)制
恢復(fù)
在本實(shí)驗(yàn)的前面,我們練習(xí)了使用備份文件來(lái)恢復(fù)數(shù)據(jù)庫(kù)。我們使用了類(lèi)似這樣的命令:
source ~/project/init-database.txt
登錄后復(fù)制登錄后復(fù)制
此語(yǔ)句從 import-database.txt 文件恢復(fù) mysql_labex 數(shù)據(jù)庫(kù)。
還有另一種方法來(lái)恢復(fù)數(shù)據(jù)庫(kù),但在此之前,我們需要先創(chuàng)建一個(gè)名為 test 的空數(shù)據(jù)庫(kù):
mysql -u root create database test;
登錄后復(fù)制
mariadb [(none)]> create database test; query ok, 1 row affected (0.000 sec) mariadb [(none)]> show databases; +--------------------+ | database | +--------------------+ | information_schema | | mysql | | mysql_labex | | performance_schema | | sys | | test | +--------------------+ 6 rows in set (0.000 sec)
登錄后復(fù)制
ctrl+z退出mysql。恢復(fù)bak.sql以測(cè)試數(shù)據(jù)庫(kù):
mysql -u root test 我們可以通過(guò)輸入命令查看測(cè)試數(shù)據(jù)庫(kù)中的表來(lái)確認(rèn)恢復(fù)是否成功:<p> <br></p><pre class="brush:php;toolbar:false">mysql -u root use test show tables
登錄后復(fù)制
MariaDB [(none)]> USE test; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed MariaDB [test]> SHOW TABLES; +----------------+ | Tables_in_test | +----------------+ | department | | employee | | project | | table_1 | +----------------+ 4 rows in set (0.000 sec)
登錄后復(fù)制
我們可以看到4張表已經(jīng)恢復(fù)到測(cè)試數(shù)據(jù)庫(kù)了
概括
恭喜!您已經(jīng)完成了有關(guān) mysql 中其他基本操作的實(shí)驗(yàn)。您已經(jīng)學(xué)習(xí)了如何創(chuàng)建索引、視圖以及如何備份和恢復(fù)數(shù)據(jù)庫(kù)。
? 現(xiàn)在練習(xí):其他基本操作
想了解更多嗎?
?學(xué)習(xí)最新的mysql技能樹(shù)
? 閱讀更多 mysql 教程
? 加入我們的 discord 或發(fā)推文@wearelabex