日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長提供免費收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:52010
  • 待審:67
  • 小程序:12
  • 文章:1106242
  • 會員:784

介紹

在本實驗中,我們將學(xué)習(xí)和練習(xí)索引、視圖、備份和恢復(fù)。這些概念對于數(shù)據(jù)庫管理員來說非常重要。

學(xué)習(xí)目標(biāo)

創(chuàng)建索引
創(chuàng)建視圖
備份與恢復(fù)

準(zhǔn)備

開始之前,我們需要準(zhǔn)備好環(huán)境。

啟動mysql服務(wù)并以root身份登錄。

cd ~/project
sudo service mysql start
mysql -u root

登錄后復(fù)制

加載文件中的數(shù)據(jù)。需要在mysql控制臺輸入命令來構(gòu)建數(shù)據(jù)庫:

source ~/project/init-database.txt

登錄后復(fù)制登錄后復(fù)制

指數(shù)

索引是與表相關(guān)的結(jié)構(gòu)。它的作用相當(dāng)于一本書的目錄。您可以根據(jù)目錄中的頁碼快速找到內(nèi)容

當(dāng)你要查詢一張記錄數(shù)量較多的表,并且該表沒有索引時,那么會拉出所有記錄一一匹配搜索條件,并返回符合條件的記錄。非常耗時,導(dǎo)致大量的磁盤i/o操作。

如果表中存在索引,那么我們可以通過索引值快速找到表中的數(shù)據(jù),從而大大加快查詢過程。

有兩種方法可以為特定列設(shè)置索引:

alter table table name add index index name (column name);

create index index name on table name (column name);

登錄后復(fù)制

讓我們使用這兩條語句來構(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ù)制

我們使用索引來加速查詢過程。當(dāng)沒有足夠的數(shù)據(jù)時,我們將無法感受到它的神奇力量。這里我們使用命令show index from table name來查看我們剛剛創(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語句查詢時,where條件會自動判斷是否存在索引。

看法

視圖是從一個或多個表派生的虛擬表。它就像一個窗口,通過它人們可以查看系統(tǒng)提供的特殊數(shù)據(jù),從而不必查看數(shù)據(jù)庫中的全部數(shù)據(jù)。他們可以專注于他們感興趣的事情。

如何解釋“view是一個虛擬表”?

數(shù)據(jù)庫中只存儲view的定義,而其數(shù)據(jù)存儲在原表中;
當(dāng)我們使用view查詢數(shù)據(jù)時,數(shù)據(jù)庫會相應(yīng)地從原表中提取數(shù)據(jù)。
由于view中的數(shù)據(jù)取決于原始表中存儲的內(nèi)容,因此一旦表中的數(shù)據(jù)發(fā)生變化,我們在view中看到的內(nèi)容也會發(fā)生變化。
將 view 視為表格。

創(chuàng)建view時使用的語句格式:

create view view name (column a, column b, column c) as select column 1, column 2, column 3 from table name;

登錄后復(fù)制

從語句中我們可以看到后半部分是一條select語句,這意味著view也可以建立在多個表上。我們需要做的就是在 select 語句中使用子查詢或 join 。

現(xiàn)在讓我們創(chuàng)建一個名為v_emp的簡單視圖,其中包含三列v_namev_agev_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ù)庫管理中極其重要。

導(dǎo)出文件僅保存數(shù)據(jù)庫中的數(shù)據(jù),而備份將整個數(shù)據(jù)庫結(jié)構(gòu)(包括數(shù)據(jù)、約束、索引、視圖等)保存到新文件。

mysqldump是mysql中用于備份的實用程序。它生成一個 sql 腳本文件,其中包含從頭開始重新創(chuàng)建數(shù)據(jù)庫的所有基本命令,例如 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ù)制

嘗試備份整個數(shù)據(jù)庫mysql_labex。將文件命名為 bak.sql。首先按ctrl+z退出mysql控制臺,然后打開終端輸入命令:

cd ~/project/
mysqldump -u root mysql_labex > bak.sql;

登錄后復(fù)制

使用命令“l(fā)s”,我們會看到備份文件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ù)

在本實驗的前面,我們練習(xí)了使用備份文件來恢復(fù)數(shù)據(jù)庫。我們使用了類似這樣的命令:

source ~/project/init-database.txt

登錄后復(fù)制登錄后復(fù)制

此語句從 import-database.txt 文件恢復(fù) mysql_labex 數(shù)據(jù)庫。

還有另一種方法來恢復(fù)數(shù)據(jù)庫,但在此之前,我們需要先創(chuàng)建一個名為 test 的空數(shù)據(jù)庫:

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測試數(shù)據(jù)庫:

mysql -u root test 



我們可以通過輸入命令查看測試數(shù)據(jù)庫中的表來確認(rèn)恢復(fù)是否成功:<p>
<br></p><pre class="brush:php;toolbar:false">mysql -u root
use test
show tables

登錄后復(fù)制

MariaDB [(none)]&gt; 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]&gt; SHOW TABLES;
+----------------+
| Tables_in_test |
+----------------+
| department     |
| employee       |
| project        |
| table_1        |
+----------------+
4 rows in set (0.000 sec)

登錄后復(fù)制

我們可以看到4張表已經(jīng)恢復(fù)到測試數(shù)據(jù)庫了

概括

恭喜!您已經(jīng)完成了有關(guān) mysql 中其他基本操作的實驗。您已經(jīng)學(xué)習(xí)了如何創(chuàng)建索引、視圖以及如何備份和恢復(fù)數(shù)據(jù)庫。


? 現(xiàn)在練習(xí):其他基本操作


想了解更多嗎?

?學(xué)習(xí)最新的mysql技能樹

? 閱讀更多 mysql 教程

? 加入我們的 discord 或發(fā)推文@wearelabex

分享到:
標(biāo)簽:備份 恢復(fù) 操作 索引 視圖
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 52010

    網(wǎng)站

  • 12

    小程序

  • 1106242

    文章

  • 784

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨大挑戰(zhàn)2018-06-03

數(shù)獨一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運動步數(shù)有氧達人2018-06-03

記錄運動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定