Index, view, import and export, backup, restore

1. Introduction to the experiment

1.1 Experiment content

In this section of the experiment, we will learn and practice other basic operations of the database: index, view, import and export, backup and restore, etc.

These concepts are very important for database administrators, please understand and complete all experiments carefully.

1.2 Experimental knowledge points

  • index
  • view
  • import and export
  • Backup and restore

1.3 Experimental environment

The experimental environment used in the course is Ubuntu Linux 14.04 64-bit version. The program will be used in the experiment:

  • Mysql 5.5.50
  • Xfce Terminal

2. Development preparation

Note: If you entered this section directly from the previous section to study, please delete the database created in the previous section first mysql_shiyan, and delete the statement as DROP DATABASE mysql_shiyan;.

Before officially starting the experimental content, you need to download the relevant code.

The downloaded code will build a  mysql_shiyan database named (there are three tables: department, employee, project) and insert data into it.

The specific operation is as follows, first enter the command to enter the  /home/shiyanlou/Desktop directory:

cd /home/shiyanlou/Desktop

  

Then enter the command to download the code:

git clone https://github.com/shiyanlou/SQL6

  

After the download is complete, enter the command to start the MySQL service and log in with the root user:

#Open MySQL service
sudo service mysql start        

#Log in as root user
mysql -u root     

  

In the downloaded SQL6 directory, there are two files  MySQL-06.sql and  in.txt, where the first file is used to create a database and insert data into it, and the second file is used for subsequent experimental steps.

(The SQL6 directory is on the desktop, and you can use gedit to view and edit the files in it.)

Enter the command to run the first file, set up the database and insert the data:

source /home/shiyanlou/Desktop/SQL6/MySQL-06.sql

  

3. Experimental steps

3.1 Index

An index is a structure related to a table, and its function is equivalent to the directory of a book, which can quickly find the desired content according to the page number in the directory.

When there are a large number of records in the table, if you want to query the table without an index, it is a full-table search: take out all the records one by one, compare them with the query conditions one by one, and then return the records that meet the conditions. Doing so can consume a lot of database system time and cause a lot of disk I/O operations.

If an index has been established in the table, and an index value that meets the query conditions is found in the index, the data in the table can be quickly found through the index value, which can greatly speed up the query.

To build an index on a column in a table, there are the following two statement formats:

ALTER TABLE table name ADD INDEX index name (column name);

CREATE INDEX index name ON table name (column name);

  

We use these two statements to build indexes separately:

ALTER TABLE employee ADD INDEX idx_id (id); #Create an index named idx_id on the id column of the employee table

CREATE INDEX idx_name ON employee (name); #Create an index named idx_name on the name column of the employee table

  

The effect of the index is to speed up the query. When there is not enough data in the table, you cannot feel its effect. Here we use the command SHOW INDEX FROM table name; to view the newly created index:

01

When using the SELECT statement to query, the conditions in the WHERE statement will automatically determine whether there is an available index.

3.2 View

A view is a table derived from one or more tables and is a virtual table. It is like a window, through which you can see the data specially provided by the system, so that users can not see the data in the entire database, but only care about the data that is useful to them.

Note that understanding views are virtual tables:

  • The database only stores the definition of the view, but does not store the data in the view, which is stored in the original table;
  • When using a view to query data, the database system will fetch the corresponding data from the original table;
  • The data in the view depends on the data in the original table. Once the data in the table changes, the data displayed in the view will also change;
  • When using a view, you can think of it as a table.

The format of the statement to create a view is:

CREATE VIEW view name (column a, column b, column c) AS SELECT column 1, column 2, column 3 FROM table name;

  

It can be seen that the statement to create the view, the second half of the statement is a SELECT query statement, so the view can also be established on multiple tables, just use sub-query or join query in the SELECT statement, which has been carried out in previous experiments.

Now we create a simple view named v_emp with three columns v_name, v_age, v_phone:

02

3.3 Import

The import operation can save the data in a file into a table. The import statement format is:

LOAD DATA INFILE 'file path and file name' INTO TABLE table name;

  

Now there is a file named in.txt in the SQL6 directory, we try to import the data in this file into the employee table of the database mysql_shiyan.

First hold down Ctrl+Z to exit MySQL, the following steps need to be  Xfce 终端executed.

Open the Xfce terminal and enter the command to copy the SQL6 folder to the  /tmp directory:

cp -a /home/shiyanlou/Desktop/SQL6 /tmp/

  

Then use the command to  gedit /tmp/SQL6/in.txt view  in.txt the contents of the file:

03

Then use the following command to log in to the database as the root user, and then connect to the mysql_shiyan database:

# Enter the command in the Xfce terminal
mysql -u root

# Enter the command in the MySQL console
use mysql_shiyan

  

Check out the data in the employee table before importing the data:

04

Now execute the import statement, the data in the file is successfully imported into the employee table:

05

3.4 Export

Exporting and importing are the opposite processes, which are to save the data in a certain table of the database to a file. The basic format of the export statement is:

SELECT column 1, column 2 INTO OUTFILE 'file path and file name' FROM table name;

  

Note: There cannot already be a file with the same name under the "file path" in the statement.

Now we export the data of the entire employee table to the /tmp directory, and the export file is named out.txt The specific statement is:

SELECT * INTO OUTFILE '/tmp/out.txt' FROM employee;

  

Use gedit to view  /tmp/out.txt the contents of the exported file:

06

3.5 Backup

The data in the database may be very important. For security reasons, you should pay attention to using the backup function in the use of the database.

The difference between backup and export: the exported file only saves the data in the database; while backup is to save the structure of the database, including data, constraints, indexes, views, etc., as a file.

mysqldump is MySQL's utility for backing up databases. It mainly produces an SQL script file containing the commands CREATE TABLE INSERT etc. necessary to recreate the database from scratch.

Statement using mysqldump backup:

mysqldump -u root database name > backup file name; #backup the entire database

mysqldump -u root database name table name > backup file name; #backup the entire table

  

We try to back up the entire database  mysql_shiyan, name the backup file  bak.sqlCtrl+Z exit the MySQL console first, then open the Xfce terminal, enter the command in the terminal:

mysqldump -u root mysql_shiyan > bak.sql;

  

Use the command "ls" to see that the backup file has been generated  bak.sql:

07

You can use gedit to view the contents of the backup file, and you can see that not only the data is saved, but also other information about the database that was backed up.

3.6 Recovery

Restoring the database with a backup file, in fact, we have used it for a long time. At the beginning of this experiment, we used such a command:

source /tmp/SQL6/MySQL-06.sql

  

This is a restore statement that restores the database saved in the MySQL-06.sql file mysql_shiyan .

There is another way to restore the database, but before that we first create an empty database test using the command:

mysql -u root #Because you have exited MySQL in the previous step, you need to log in again now

CREATE DATABASE test; #Create a new database named test

  

Ctrl+Z again to exit MySQL, then enter the statement to restore, restore the bak.sql just backed up to the test database:

mysql -u root test < bak.sql

  

We enter the command to view the table of the test database to verify whether the recovery is successful:

mysql -u root #Because you have exited MySQL in the previous step, you need to log in again now

use test #connect to database test

SHOW TABLES; #View the table of the test database

  

You can see 4 tables and 1 view of the original database, which have now been restored to the test database:

08

Check the recovery of the employee table again:

09

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326497863&siteId=291194637