MySQL basic learning - basic database operations

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:

  1. Data: A symbolic record that describes something is called data.
  2. Database (DataBase, DB): A database is a long-term storage of a large amount of data in a computer that is organized and shared.
  3. 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:
    write picture description here
    Why use a database to store data, is it bad to use a file?
    Store with file:

    1. file security issues
    2. File does not utilize query and management of data
    3. Files are not good for storing massive data
    4. 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:
write picture description here
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

  1. DDL data definition language, used to maintain the structure of stored data, representing instructions: create, drop, alter
  2. DML data manipulation language, used to operate on data, representing instructions: insert, delete, update
  3. A DQL is separated from DML, and data query languages ​​such as select
  4. 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:
write picture description here

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:
write picture description here
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:
write picture description here
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.
write picture description here

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:

  1. Case Sensitive
    Validation rules use utf8_general_ci (case insensitive)
    write picture description here
    Validation rules use utf8_bin (case sensitive)
    write picture description here
  2. Affects sorting
    Validation rules use utf8_general_ci (in alphabetical order)
    write picture description here
    Validation rules use utf8_bin (in ASCII order)
    write picture description here

3. Manipulate the database

  1. View databaseshow databases;
  2. show database creation statementshow create database 数据库名;
  3. 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.
  4. 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;
    write picture description here
    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;
    write picture description here
    backup precautions:
    1. What if the backup is not the entire database but a table?
      Syntax: mysqldump -u root -p 数据库名 表名1 表名2... > 路径
      write picture description here
      restore: source the path to the backed up file;
      write picture description here
    2. What if you want to backup multiple databases?
      Backup: mysqldump -u root -p -B 数据库名1 数据库名2... > 路径
      write picture description here
      restore: source storage path;
      write picture description here
      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.

4. Modify the library

  1. Change the character set:alter database class charset=gbk;
    write picture description here
  2. Change the validation rules:alter database class collate utf8_bin;
    write picture description here

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 .

Guess you like

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