MySQL is a relational database, a product of sun company, which now belongs to Oracle, and is one of the mainstream database management systems. The current mainstream databases are: SQL Server, Oracle, DB2.
concept:
- Data: A symbolic record that describes something is called data.
- Database (DataBase, DB): A database is a long-term storage of a large amount of data in a computer that is organized and shared.
Database Management System (DBMS): System software for managing databases.
A database management system is a software through which multiple databases can be managed; a database corresponds to a file, and a general developer creates a database for an application; multiple tables are generally created in a database, and data is stored in the tables. Developers can open MySQL through the console and use the database in the code of Java language or other languages. The relationship is shown in the figure:
Why use a database to store data, is it bad to use a file?
Store with file:- file security issues
- File does not utilize query and management of data
- Files are not good for storing massive data
- The file is inconvenient to control in the program
Basic use of mysql
MySQL commands all need to end with a semicolon .
1. Startup of mysql
service mysqld start
2. The shutdown of mysql
service mysqld stop
3. Connect to mysql
mysql [-h 主机] -u 用户 -p ;
For example:
4. Basic statement to create a library
create database 数据库名;
5. Use Libraries
use 库名;
6. Data is stored in a table, and a row of the table is called a record. The basic syntax for creating a
table :
create table 表名(
字段1 列类型,
name varchar(20)
);
Insert data into the table : insert into 表名 values(values1, values2);
View the data in the table: select *from 表名;
7.SQL classification
- DDL data definition language, used to maintain the structure of stored data, representing instructions: create, drop, alter
- DML data manipulation language, used to operate on data, representing instructions: insert, delete, update
- A DQL is separated from DML, and data query languages such as select
- DCL data control language, responsible for authority management and transactions, representing instructions: grant, revoke, commit
8. We can use MySQL in the Windows command line window by configuring environment variables: right-click this computer, click Properties, in Advanced System Settings, click Environment Variables, add environment variables to Path, and install MySQL Enter the path, and finally click OK. As shown below:
Through the above study, we have a preliminary understanding of the database, know how the data is stored, we will explain in detail below.
Operation of the database
1. Create a database
grammar:create database [if not exists] db_name [CHARACTER SET [, COLLATE] ...]
For example: create a database that uses utf8 character set and the validation rule is utf8_bin:
Instructions:
1. It is generally recommended to capitalize keywords to distinguish them from database names, but there is no mandatory requirement.
2. [] is optional
3. CHARACTER SET: Specifies the character set used by the database.
4. COLLATE: Specify the comparison method of the database character set (the default is utf8_general_ci)
5. You can view the default character set and validation rules of the system through the command:
6. Create an existing database, if not add if not exists, an error will be reported ; if added, only a warning will appear, and no error will be reported. This is very important in programming.
2. Character set and verification rules
Character set : control what language is used, such as utf8 can support Chinese.
View the character set commands supported by the database: show charset;
the role of the validation rules:
- Case Sensitive
Validation rules use utf8_general_ci (case insensitive)
Validation rules use utf8_bin (case sensitive)
- Affects sorting
Validation rules use utf8_general_ci (in alphabetical order)
Validation rules use utf8_bin (in ASCII order)
3. Manipulate the database
- View database
show databases;
- show database creation statement
show create database 数据库名;
- View the current MySQL database connection status show processlist; you can find that if a user does not log in normally, the database is likely to be invaded.
- Backup and restore database :
Basic syntax:mysqldump -u root -p 数据库名 > 存放路径
(input at the OS command line)
For example: backup the database class to the desktop;
to restore the database , first create an empty database, preferably with the same name as before.
Basic syntax:source 存放路径;
(in the mysql console)
For example: restore the backup class;
backup precautions:
- What if the backup is not the entire database but a table?
Syntax:mysqldump -u root -p 数据库名 表名1 表名2... > 路径
restore: source the path to the backed up file;
- What if you want to backup multiple databases?
Backup:mysqldump -u root -p -B 数据库名1 数据库名2... > 路径
restore: source storage path;
by adding the -B option, there is no need to create an empty library first when restoring, and it can also be used when backing up a database.
- What if the backup is not the entire database but a table?
4. Modify the library
- Change the character set:
alter database class charset=gbk;
- Change the validation rules:
alter database class collate utf8_bin;
5. Delete the library
Syntax: drop [if exists] database 数据库名;
When this statement is executed, the corresponding database folder will be deleted, and the cascade will be deleted, that is, all data tables in the database will be deleted .