【MySQL】Download and install and introduce SQL

1. Database related concepts

In the past, when we worked on systems, we used file storage for persistent data storage. Storing to a file can achieve the effect that the data will not be lost when the system is shut down. Of course, file storage also has its disadvantages.

Suppose the following data is stored in the file:

name age sex address
Zhang San 23 Male Xisanqi, Beijing
Li Si 24 Female Beijing West Second Banner
Wang Wu 25 Male Xi'an Software New City

Now we want to modify the gender data of Li Si's data to male. The IO technology we are learning now can read all the data into the memory, then modify it and save it to the file. There is a big problem with this method of operation. Now there are only three pieces of data. If 1T of data is stored in the file, it will be found that the memory cannot store it at all.

It is now necessary to use technologies that can not only store data persistently, but also avoid the above problems in our system. Database is such a technology.

1.1 Database

  • == A warehouse for storing and managing data, where data is stored in an organized manner. ==

  • The English name of the database is DataBase, referred to as DB.

The database is to store data on the hard disk, which can achieve the effect of persistent storage. Then how to solve the above problems? Use a database management system.

1.2 Database Management System

  • ==Large software for managing databases==

  • English: DataBase Management System, referred to as DBMS

After installing the database management system on the computer, you can create a database through the database management system to store data, and you can also use the system to perform data addition, deletion, modification, and query related operations on the data in the database. The MySQL database we usually talk about is actually the MySQL database management system.

Through the above description, everyone should already know the relationship between 数据库管理系统and 数据库. So what are the common database management systems?

1.3 Common database management systems

Next, a brief introduction to the database management systems listed above:

  • Oracle: a large database for a fee, a product of Oracle Corporation

  • ==MySQL==: Open source and free small and medium-sized database. Later, Sun acquired MySQL, and Sun was acquired by Oracle.

  • SQL Server: A medium-sized database charged by Microsoft Corporation. C#, .net and other languages ​​are often used

  • PostgreSQL: open source free small and medium database

  • DB2: IBM's large-scale fee-based database product

  • SQLite: Embedded micro-database. Such as: as an Android built-in database

  • MariaDB: open source free small and medium database

What we learned in the course is the MySQL database management system, and PostgreSQL is also used in some companies. At this time, everyone will definitely think about what to do if we use the PostgreSQL database management system that we have not learned in the company in the future? You don’t have to worry about this, as shown in the following figure:

We can operate the database through the database management system, add, delete, modify and query the data in the database, but how to let users deal with the database management system? It can be realized through a programming language (SQL).

1.4 SQL

  • English: Structured Query Language, referred to as SQL, Structured Query Language

  • A programming language for manipulating relational databases

  • Define a unified standard for operating all relational databases. You can use SQL to operate all relational database management systems. If you use other database management systems in your future work, you can also use SQL to operate.

2,MySQL

2.1 MySQL installation

Installation environment: Win10 64-bit software version: MySQL 5.7.24 decompressed version

2.1.1 Download

MySQL :: Download MySQL Community Server (Archived Versions)

Click on the above link to see the following interface:

Select the version corresponding to your own system digitsDownload and click on the right , and you will enter another page at this time, and also find the position shown in the figure below near the bottom of the page:

img

Regardless of the login and registration buttons above, just click No thanks, just start my download.to download.

2.1.2 Installation (Decompression)

After the download is complete, what we get is a compressed package. After decompressing it, we can get the software body of MySQL 5.7.24 (just a folder), and we can put it in the location you want to install.


2.2 MySQL uninstall

If you want to uninstall MySQL, it's easy too.

Right-click the start menu, select 命令提示符(管理员), open the black box.

  1. Type net stop mysqland enter.

net stop mysql

  1. Type again mysqld -remove mysqland press Enter.

mysqld -remove mysql

  1. Finally, delete the MySQL directory and related environment variables.

At this point, MySQL uninstallation is complete!

2.3 MySQL configuration

2.3.1 Add environment variables

There are many options in the environment variable, here we only use Paththis parameter. Why add environment variables at the beginning of initialization? PathEnter the name of an executable program in the black box (that is, CMD), and Windows will first search in the path indicated in the environment variable. If it is found, it will be executed directly. If it is not found, it will be searched in the current working directory. If not found, report an error. The purpose of adding environment variables is to be able to directly call related programs in MySQL in any black box without always modifying the working directory, which greatly simplifies the operation.

Right click 此电脑属性, click高级系统设置

click环境变量

系统变量Create a new MYSQL_HOME in

系统变量Find and double click inPath

click新建

Finally click OK.

How to verify whether the addition is successful?

Right-click the start menu (that is, the lower left corner of the screen), select 命令提示符(管理员), open the black box, type mysql, and press Enter. If it prompts Can't connect to MySQL server on 'localhost', it proves that the addition is successful; if it prompts, mysql不是内部或外部命令,也不是可运行的程序或批处理文件it means that the addition fails, please check the steps again and try again.

2.3.2 Create a new configuration file

Create a new text file with the following content:

[mysql]
default-character-set=utf8
[mysqld]
character-set-server=utf8
default-storage-engine=INNODB
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

Save the above text file as, select in the save type 所有文件 (*.*), the file name my.ini, and the storage path is MySQL 根目录(for example, mine is D:\software\mysql-5.7.24-winx64, modify according to your own MySQL directory location).

The above code means that the default encoding set of the configuration database is utf-8 and the default storage engine is INNODB.

2.3.3 Initialize MySQL

Type in the black box just now mysqld --initialize-insecure, press Enter, and wait for a while. If there is no error message (as shown in the figure below), it proves that there is no problem with the initialization of the data directory. At this time, check that the data directory has been generated under the MySQL directory.

mysqld --initialize-insecure

tips: If the following error occurs

It is caused by insufficient permissions, go to C:\Windows\System32run cmd.exe as an administrator

2.3.4 Register MySQL service

Type in the black box mysqld -installand press Enter.

mysqld -install

The MySQL service is now installed on your computer.

MySQL server

2.3.5 Start the MySQL service

Type in the black box net start mysqland press Enter.

net start mysql // start mysql service
    
net stop mysql // stop mysql service

2.3.6 Modify the default account password

Type in the black box mysqladmin -u root password 1234, here 1234refers to the password of the default administrator (that is, the root account), you can modify it to your liking.

mysqladmin -u root password 1234

So far, the MySQL 5.7 decompressed version has been installed!

2.4 MySQL login and logout

2.4.1 Login

Right-click the start menu, select 命令提示符, open the black box. Enter in the black box, mysql -uroot -p1234and press Enter, the following figure appears and the lower left corner is mysql>, then the login is successful.

mysql -uroot -p1234

Here you can start your MySQL journey!

Login parameters:

mysql -u username -p password -h the ip address of the mysql server to be connected (default 127.0.0.1) -P port number (default 3306)

2.4.2 Exit

exit mysql:

exit
quit

2.5 MySQL data model

Relational Database:

A relational database is a database based on a relational model. Simply put, a relational database is a database composed of multiple two-dimensional tables that can be connected to each other.

As shown in the figure below, 订单信息表and 客户信息表are both two-dimensional tables with rows and columns. We call this a relational database.

  • DQL (Data Query Language) Data Query Language, used to query the records (data) of tables in the database

    The simple understanding of DQL is to perform query operations on data. Query the data we want from the database table.

  • DCL (Data Control Language) Data Control Language, used to define database access rights and security levels, and create users

    The simple understanding of DML is to control the authority of the database. For example, I let a certain database table only allow a certain user to operate and so on.

Note: In the future, we will most often operate DMLand DQL, because the most common operation in our development is data.

3. Overview of SQL

After understanding the data model, we will learn SQL statements next, and use SQL statements to add, delete, modify and query databases, tables, and data.

3.1 Introduction to SQL

  • English: Structured Query Language, referred to as SQL

  • Structured Query Language, a programming language for manipulating relational databases

  • Define a unified standard for operating all relational databases

  • For the same requirement, each database operation method may have some differences, which we call "dialects"

3.2 General syntax

  • SQL statements can be written in one or more lines and end with a semicolon.

    As above, ending with a semicolon is a complete SQL statement.

  • The SQL statements of the MySQL database are not case-sensitive, and it is recommended to use capital letters for keywords.

    The same sql statement is written as shown in the figure below, and the results can also be run.

  • note

    • Single-line comment: -- comment content or # comment content (MySQL specific)

    • Note: When using -- to add a single-line comment, a space must be added after --, and # is not required.

    • Multi-line comment: /* comment */

3.3 SQL classification

  • DDL (Data Definition Language): Data definition language, used to define database objects: database, table, column, etc.

    A simple understanding of DDL is used to operate databases, tables, etc.

  • DML (Data Manipulation Language) data manipulation language, used to add, delete, and modify data in tables in the database

    Simple understanding of DML to add, delete and modify data in the table

  • DQL (Data Query Language) Data Query Language, used to query the records (data) of tables in the database

    The simple understanding of DQL is to perform query operations on data. Query the data we want from the database table.

  • DCL (Data Control Language) Data Control Language, used to define database access rights and security levels, and create users

    The simple understanding of DML is to control the authority of the database. For example, I let a certain database table only allow a certain user to operate and so on.

Note: In the future, we will most often operate DMLand DQL, because the most common operation in our development is data.

Guess you like

Origin blog.csdn.net/weixin_45481821/article/details/132056667