Getting started with (My)SQL

The title (My)SQL is used here. The purpose is to introduce some extensions of MySQL on standard SQL while introducing standard SQL. It is hoped that after reading this section, readers can understand the basic syntax of standard SQL and some of the extended syntax of MySQL.

1. SQL classification

SQL statements can be mainly divided into the following three categories

DDL (Data Definition Languages) statement: data definition language, these statements define the definitions of different data segments, databases, tables, columns, indexes and other database objects. Commonly used statement keywords mainly include create, drop, alter, etc.

DML (Data Manipulation Language) statement: Data manipulation statement, used to add, delete, update and query database records, and check data integrity. Commonly used statement keywords mainly include insert, delete, udpate and select.

DCL (Data Control Language) statement: Data control statement, a statement used to control the direct permission and access level of different data segments. These statements define the access rights and security levels of the database, tables, fields, users. The main statement keywords include grant, revoke, etc.

Two, DDL statement

DDL is the abbreviation of Data Definition Language. Simply put, it is an operating language for creating, deleting, and modifying objects inside the database. The biggest difference between it and the DML language is that DML only operates on the internal data of the table, and does not involve the definition of the table, the modification of the structure, and does not involve other objects. DDL statements are more used by database administrators (DBAs), and are rarely used by general developers.

Here are some examples to introduce the usage of common DDL statements in MySQL

1. create database

After starting the MySQL service, enter the following command to connect to the MySQL server:

[mysql@db3 ~]$ mysql-uroot-p

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 7344941 to server version: 5.1.9-beta-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

In the above command line, mysql represents the client command, -u is followed by the connected database user, and -p indicates that a password needs to be entered.

If the database settings are normal and you enter the correct password, you will see the above welcome interface and a mysql> prompt.

The following sections are introduced in the welcome screen.

The end character of the command, end with ; or \g.

The connection ID of the client. This number records the number of connections to the MySQL service so far. Each new connection will automatically increase by 1. In this example, it is 7344941.

The version of the MySQL server. In this example, it is "5.1.9-beta-log", indicating that it is the beta version of 5.1.9. If it is the standard version, Standard will be used instead of Beta.

Use the "help;" or "\h" command to display the help content, and use the "\c" command to clear the command line buffer. Enter the SQL statement to be executed after the mysql> prompt, each SQL statement ends with a semicolon or \g, and press Enter to execute.

Because all data is stored in the database, the first command to learn is to create a database, the syntax is as follows:

CREATE DATABASE dbname

For example, to create database test1, execute the command as follows:

mysql>create database test1;

Query OK, 1 row affected (0.00 sec)

It can be found that after the creation command is executed, there is a line below that prompts "Query OK, 1 row affected (0.00 sec)". This prompt can be divided into 3 parts. "Query OK" means that the above command is executed successfully. Readers may be surprised. It is not to execute the query operation, why is it displayed that the query is successful? In fact, this is a feature of MySQL. All DDL and DML (excluding SELECT) operations will display "Query OK" after successful execution, which is understood to mean that the execution is successful; "1 row affected" means that the operation only affects the row in the database. A record of one line, "0.00 sec" records the execution time of the operation.

If the database already exists, the system will prompt:

mysql>create database test1; ERROR1007(HY000):Can't create database 'test1';database exists At this time, if you need to know which databases exist in the system, you can use the following command to view:

mysql> show databases;

+--------------------+

| Database |

+--------------------+ |

information_schema |

| cluster |

| mysql |

| test |

| test1 |

+--------------------+

5 rows in set (0.00 sec)

It can be found that in the above list, in addition to the test1 just created, there are four other databases, which are automatically created by the system when MySQL is installed, and their respective functions are as follows.

information_schema: It mainly stores some database object information in the system. For example, user table information, column information, permission information, character set information, partition information, etc.

cluster: Stores the cluster information of the system.

mysql: stores the user authority information of the system.

test: The test database automatically created by the system, which can be used by any user.


Zero-based Xiaobai Getting Started Tutorial


Close "BlankLB", you can get a lot of programming materials source code courseware without waste

Guess you like

Origin blog.csdn.net/lghtdw1314/article/details/123920466