Super classic 20,000 words, MySQL database quick start.

"Author's Homepage": Shibie Sanshi wyx
"Author's Profile": CSDN top100, Alibaba Cloud blog expert, Huawei cloud expert, high-quality creator in the field of network security
"Column Introduction": This article has been entered in the column "MySQL Database Quick Start"

1. Environmental preparation

1. MySQL download and installation

MySQL 8.0 Community Edition Download and Installation Tutorial

2. MySQL start

When using MySQL, the MySQL service must be started.

cmd starts the mysql service:

net start mysql80

The following two situations indicate that the mysql service is successfully started.
insert image description here
Note: mysql80 is the default mysql service name. If you have modified the service name, you need to change it to the modified service name.

cmd closes the mysql service:

net stop mysql80

This interface appears, indicating that the mysql service was successfully shut down:
insert image description here

3. MySQL client connection

Three ways for MySQL to connect to the client

4. MySQL Basic Syntax

  1. SQL statements end with a semicolon.
  2. SQL statements are not case sensitive
  3. The content of the comment is not executed
    a. Single-line comment: -- comment content or # comment content
    b. Multi-line comment: / comment content /

For a detailed grammar tutorial, you can refer to my other article: What are the exploitable loopholes in MySQL grammar?

Second, the database operation

1. Inquiry

Query all databases:

show databases;

Query the currently used database:

select database();

2. Create

create database 数据库名;

3. Use

use 数据库名;

4. Delete

drop database 数据库名;

5. Cases

Let's practice with a case.

First, check which databases are in MySQL: there are 4 databases
insert image description here
Create a user database: successfully created
insert image description here
Check again which databases are in MySQL: 5, one more user
insert image description here

Check the currently used database: Empty, the database is not currently used
insert image description here
Use the user database: Use successfully
insert image description here
Check the currently used database again: It has become user
insert image description here
Delete the user database: Delete successfully
insert image description here
Check again which databases are in MySQL: 4, the user has been deleted
insert image description here

3. Table operation

1. Inquiry

Query all tables in the current database

show tables;

2. Create

create table 表名(
	字段1 字段1的数据类型,
    字段2 字段2的数据类型,
    ……
    字段3 字段3的数据类型,
)

3. Modification

Modify table name

alter table 表名 rename 新表名;

add field

alter table 表名 add 字段名 数据类型(长度);

Modify data type

alter table 表名 modify 字段名 新数据类型(长度)

Modify field name and field type

alter table 表名 change 旧字段名 新字段名 数据类型(长度);

delete field

alter table 表名 drop 字段名

4. Delete

drop table 表名;

5. Data Types

A brief introduction to the two most commonly used data types:

  1. int: numeric type, commonly used to store numbers
  2. varchar: character type, commonly used to save strings

6. Cases

Let's practice with a case.

Prerequisite: Create a user database and use the user database.
First, look at the tables in the current database: empty, not
insert image description here
a single table. Create a student table with three fields:

  1. id: int type, storing the student number
  2. name: varchar type, stores the student's name
  3. age: int type, storing age

Note: int(10) The number in parentheses is the maximum length of the data in bytes.

After the creation is successful, check which tables are in the database again: there is one more student table
insert image description here
Delete the student table, check which table is in the database again: empty, the student table has been deleted
insert image description here

4. Data manipulation

1. Add

Add data to specified fields

insert into 表名 (字段名1,……) values(1,……),(1,……),……

Add data to all fields

insert into 表名 values (1,……),(值1,……),……

2. Modification

update 表名 set 字段1 =1,……[where 条件]

3. Delete

delete from 表名 [where 条件]

4. Inquiry

select * from 表名

5. Cases

In the user database, create a user table. The table creation statement is as follows:

use user;
create table student(
	id int(10),
	name varchar(50),
	age int(10)
);

After adding two pieces of data to the specified field (id, name, age), query the data in the user table: the data is added successfully
insert image description here
After adding a piece of data to all fields, query the data in the user table: the data is added successfully
insert image description here
Modify the age of zhangsan to 28: , after deleting the data of lisi, query the data in the user table:
insert image description here

5. Query operation

1. Conditional query

select 字段列表 from 表名 where 条件列表;

2. Sort query

select 字段列表 from 表名 order by 排序字段列表;

3. Paging query

select 字段列表 from 表名 limit 第几条开始,显示几条;

Query conditions can be used in conjunction with each other. The complete query syntax is as follows:

select 字段列表
from 表名
where 条件列表
group by 分组字段列表
having 分组后条件列表
order by 排序字段列表
limit 分页参数

4. Cases

Next, let's practice the query operation through a case:

Query the information of users who meet the conditions ( age > 18 ) in the student table:
insert image description here
On the basis of the above query, sort by name (by default by id): The sorting changes
insert image description here
On the basis of the above query, the information is displayed in pages, starting from item 1 The data starts, and 1 is displayed:
insert image description here
Note: The data is counted from 0.

Related articles are recommended, click the link below to view the article:
MySQL order by keyword detailed explanation
order by sorting to determine the principle of the number of fields

6. Common functions

1. Information collection category

function effect
user() User currently using the database
version() Database version
database() currently used database
@@datadir database location
@@version_compile_os OS version

2. Injection related

Click the function name on the left to view the detailed usage of the function

function effect
group_concat(field1, field2) Concatenate multiple rows of query results into one row
concat(str1,str2) concatenate multiple strings
substr(str,start,length) intercept string
if(condition,T,F) Conditional judgment
length(str) return string length
Hex(str) convert string to hexadecimal
sleep(time) Delay the specified time (in seconds)

Thank you for your likes, collections and comments, I am three days, I wish you happiness!

Guess you like

Origin blog.csdn.net/wangyuxiang946/article/details/123094920