Table of Contents
DCL
Grant/Revoke Permissions
Aggregation functions
COUNT
SUM
Home Topics php mysql MySQL basic use (2) DCL statements and aggregate functions

MySQL basic use (2) DCL statements and aggregate functions

Sep 05, 2020 pm 05:14 PM
mysql aggregate function

MySQL basic use (2) DCL statements and aggregate functions

【Related learning recommendations: mysql tutorial(Video)】

DCL

DCL is relatively simple and is mainly used to grant or revoke permissions to access the database, as well as commit and rollback database transactions.

Grant/Revoke Permissions

Take granting permissions as an example. After we create a new database, we want to grant specific users access and operation permissions for the database (generally in the production environment, for the sake of security, no (will operate the database through the root user), for this we first need to create a specific user, such as test. Control-level SQL statements such as DCL are generally executed on the command line. We enter the MySQL Docker container and connect to the database. , create a new test user through the CREATE USER statement, and set the password to test:

MySQL basic use (2) DCL statements and aggregate functions

After the creation is completed, you can see this user in the mysql.user data table:

MySQL basic use (2) DCL statements and aggregate functions

##Host field is % means test users can connect to the MySQL server from any host.

Or view it through the command line (SQL query statement is used here):

MySQL basic use (2) DCL statements and aggregate functions

Then we can run the

GRANT statement granttest The user has all operating permissions on the test database:

MySQL basic use (2) DCL statements and aggregate functions

After authorization, you need to run

flush privileges; Refresh the permissions so that you can see this user in the permission list of the test database:

MySQL basic use (2) DCL statements and aggregate functions权限

If we exit the current login state,

test When the user logs in, he can only see the test database because it has no operating permissions for other databases:

MySQL basic use (2) DCL statements and aggregate functions

To revoke permissions , you need to do it as root, delete this user in the permission list, or complete it through the

REVOKE statement on the command line:

1

2

revoke all privideges on test.* from 'test'@'%';

flush privileges;

Copy after login
All permissions are operated here, and specific permissions can also be specified. :

1

2

3

4

5

6

7

8

9

10

11

// 授予权限

grant select on test.* to 'user1'@'localhost';  /*给予查询权限*/

grant insert on test.* to 'user1'@'localhost'/*添加插入权限*/

grant delete on test.* to 'user1'@'localhost'/*添加删除权限*/

grant update on test.* to 'user1'@'localhost'/*添加权限*/

 

// 收回权限

revoke select on test.* from 'jack'@'localhost';

revoke insert on test.* from 'jack'@'localhost';

revoke delete on test.* from 'jack'@'localhost';

revoke update on test.* from 'jack'@'localhost';

Copy after login
Transaction submission/rollback

Database transaction (Database Transaction) refers to a series of operations performed as a single logical unit of work (operations related to addition, deletion, modification and query of the database, including a or multiple SQL statements), either completely executed or not executed at all.

For a single SQL statement, the database system automatically executes it as a transaction. This transaction is called

implicit transaction.

To manually execute multiple SQL statements as a transaction, you can use

BEGIN to open a transaction and use COMMIT to submit a transaction. This transaction is called Explicit transactions, if an error or exception occurs during transaction execution, the transaction can be rolled back through the ROLLBACK statement.

We briefly demonstrate the operation of database transactions on the command line:

MySQL basic use (2) DCL statements and aggregate functions

We start the transaction through the

BEGIN statement, but during execution After multiple statements, the transaction was not submitted through COMMIT. I tested the execution of these SQL statements and entered the "Browse" panel to view it. I found that no new records were inserted:

MySQL basic use (2) DCL statements and aggregate functions

If you add

ROLLBACK to roll back the transaction after the above SQL sequence, the effect will be the same:

1

2

3

4

5

6

BEGIN;

 

INSERT INTO post (`title`, `content, `created_at`) VALUES ('这是一篇测试文章2''测试内容哈哈哈''2020-05-26 13:00:00');

INSERT INTO post (`title`, `content, `created_at`) VALUES ('这是一篇测试文章3''测试内容哈哈哈''2020-05-26 13:30:00');

 

ROLLBACK;

Copy after login
And if you add the

COMMIT statement at the end , you can submit the modification smoothly:

MySQL basic use (2) DCL statements and aggregate functions

MySQL basic use (2) DCL statements and aggregate functions

This is a brief introduction to common SQL statements and visual demonstrations in phpMyAdmin. More Many details need to be explored by yourself in conjunction with online SQL tutorials. This is not the focus of this series of tutorials, so I will not go into details here.

Aggregation functions

In addition to common SQL queries and operation statements, SQL also has some built-in aggregate functions to facilitate simple and convenient statistics of results during data query. Here we introduce several common functions: count, sum, avg, max and min.

COUNT

count The function can be used to count the total number of query results. This function is usually used when performing paging queries. In order to facilitate the direct viewing of the results, we demonstrate in the command line:

MySQL basic use (2) DCL statements and aggregate functions

In order to improve readability when querying fields, you can specify it through as Field alias, here the post table has a total of three records, so the query result is 3.

SUM

sum can be used to sum a certain field in the statistical query results, so it can only be used for numeric type fields, here we are post A new field views is added to the table, which is used to store the number of views of the corresponding article record. In the post table structure, select to add a field after the content field and click "Execute":

MySQL basic use (2) DCL statements and aggregate functions

Add the field The name is set to views, and its type is set to UNSIGNED INT, which represents a non-negative integer. At the same time, the default value is set to 0. The corresponding SQL statement can be passed Preview function view:

MySQL basic use (2) DCL statements and aggregate functions

#Click "Save" to create this field, and you can see it in the table structure:

MySQL basic use (2) DCL statements and aggregate functions

Since views has a default value, the views value of all records currently is 0:

MySQL basic use (2) DCL statements and aggregate functions默认值

can be passed " Edit" function to set it to the corresponding simulation value:

MySQL basic use (2) DCL statements and aggregate functions

Next, we can sum the results through the sum function:

MySQL basic use (2) DCL statements and aggregate functions

##AVG

avg can be used to count the average value of a field in the query results, and sum The same applies to numeric type fields. For example, we can use it to count the average number of views of all articles:

MySQL basic use (2) DCL statements and aggregate functions

If it is a number that cannot be divisible, the average will be Accurate to four decimal places.

MAX

max can be used to get the maximum value of a numeric field in the query results. For example, to get the article information with the highest number of views, you can do this:

MySQL basic use (2) DCL statements and aggregate functions

MySQL command line defaults to Chinese garbled characters. We can set the encoding type to

utf8mb4 through set names utf8mb4;, so that it works normally Chinese and Emoji emoticons are displayed.

In addition, the concept of

subquery is also used here, which is to use the result of one query as the condition of another query. Here we pass the maximum number of views to the parent as the result of the subquery Query is used as the query condition to obtain the corresponding article information.

MIN

Relative to

max, the min function is used to obtain the minimum value of a numeric type field in the query results, such as to obtain the browse The article information with the lowest number can be done like this:

MySQL basic use (2) DCL statements and aggregate functions

Summary

Okay, we will briefly introduce the basic queries, operations and statistics of the MySQL database. Here, I believe you already have a basic understanding of MySQL and its operations. In the next tutorial, we will introduce to you how to connect to the MySQL database in PHP and perform add, delete, modify and query operations. Regarding some more complex operations, such as paging, grouping, connection query, association relationship, index setting and application, we will discuss it in subsequent tutorials. Demonstrate with specific examples.

This article comes from https://xueyuanjun.com/post/21656

For more related articles, please pay attention to

php mysql Column!

The above is the detailed content of MySQL basic use (2) DCL statements and aggregate functions. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

How to learn oracle database How to learn oracle database Apr 11, 2025 pm 02:54 PM

There are no shortcuts to learning Oracle databases. You need to understand database concepts, master SQL skills, and continuously improve through practice. First of all, we need to understand the storage and management mechanism of the database, master the basic concepts such as tables, rows, and columns, and constraints such as primary keys and foreign keys. Then, through practice, install the Oracle database, start practicing with simple SELECT statements, and gradually master various SQL statements and syntax. After that, you can learn advanced features such as PL/SQL, optimize SQL statements, and design an efficient database architecture to improve database efficiency and security.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

See all articles