A complete list of commonly used commands for MySQL database (full version)

1. MySQL commands

  • MySQL commands are commands used to interact and operate with the MySQL database. These commands can be used for a variety of operations, including connecting to a database, selecting a database, creating tables, inserting data, querying data, deleting data, etc.

2. MySQL basic commands

  • Default port number: 3306
  • Check the server version: select version(); or cmd command mysql -verison
  • Log in to the database: mysql -uroot -p
  • Exit the database: exit/quit
  • View the databases under the current system: show databases;
  • Create database: create database name;
  • Use database: use database name;
  • View tables: show tables;
  • Create table: create table table name (field name + space + data type);
  • View table structure: desc table name;
  • Add value: insert into table name (column name) values ​​(value);
  • View all data in the table: select * from table name;
  • Query the structure when creating a table: show create table table name;
  • Delete the value in the field: delete from table name where condition;
  • Delete a field in the table: delete from table name drop column field name; or alter table table name drop column field name
  • Delete table: drop table table name;
  • Delete library: drop database library name;
  • Primary key constraints: primary key
  • Unique constraint: unique
  • Non-null constraint: not null
  • Default constraints: default
  • Foreign key constraints: foreign key (foreign key) references main table (primary key)
  • View tables from other databases: show tables from table name;

3. Introduction to MySQL commands

  • MySQL commands are commands used to interact and operate with the MySQL database.

The following are some commonly used MySQL commands:

  • mysql: The command to connect to the MySQL database, you need to specify the user name and password.
  • use: Use a database.
  • show databases: Show all databases.
  • create database: Create a new database.
  • drop database: delete the database.
  • create table: Create a new table.
  • alter table: modify the table structure.
  • drop table: delete the table.
  • insert into: Insert new records into the table.
  • Delete from: Delete records that meet the conditions in the table.
  • update: Update matching records in the table.
  • select: Query records in the table.
  • where: Conditionally filter the queried records.
  • order by: Sort the queried records.
  • group by: Group the queried records.
  • having: Filter the grouped records.
  • set names: Set character set encoding.

These commands are only part of the MySQL commands. There are many more commands that can be used to manage and operate MySQL databases.

4. MySQL common commands

4.1 MySQL preparation

4.1.1 Starting and stopping the MySQL service

net start mysql // 启动mysql服务
net stop mysql // 停止mysql服务

4.1.2 Modify MySQL account password

  • To change the MySQL root user password, log in to MySQL first.
mysql -u root -p123456

Note: mysql -uroot -p你的MySQL密码

  • Change the password of root user:
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';

4.1.3 MySQL login and logout

  • MySQL login
  • Win+R and enter cmd to open the command line window, enter mysql -uroot -p123456 and press Enter. If the following picture appears and mysql> is displayed in the lower left corner, the login is successful.
mysql -uroot -p123456

Note: mysql -uroot -p你的MySQL密码

Insert image description here

  • MySQL logout
exit
quit

pick one of two

4.1.4 Check MySQL version

SELECT VERSION();

4.2 DDL (data definition)

  • In MySQL, DDL is the abbreviation of Data Definition Language (Data Definition Language), which is used to define and manage the structure of the database.

4.2.1 Query the database

  • Query all databases
SHOW DATABASES;

4.2.2 Create database

  • Create database:
CREATE DATABASE 数据库名称;
  • Create database (judge, create if it does not exist)
CREATE DATABASE IF NOT EXISTS 数据库名称;

4.2.3 Using database

  • Use database
USE 数据库名称;
  • View the currently used database
SELECT DATABASE();

4.2.4 Delete database

  • Delete database
DROP DATABASE 数据库名称;
  • Delete the database (judgment, delete if it exists)
DROP DATABASE IF EXISTS 数据库名称;

4.2.5 Query table

  • Query the names of all tables in the current database
SHOW TABLES;
  • Query table structure
DESC 表名称;

4.2.6 Create table

  • Create table
CREATE TABLE 表名 (
字段名1 数据类型1,
字段名2 数据类型2,
字段名n 数据类型n
);
create table tb_user (
id int,
username varchar(20),
password varchar(32)
);

Note: No comma is allowed at the end of the last line

4.2.7 Modify table

  • Modify table name
ALTER TABLE 表名 RENAME TO 新的表名;
-- 将表名student修改为stu
alter table student rename to stu;
  • add a column
ALTER TABLE 表名 ADD 列名 数据类型;
-- 给stu表添加一列address,该字段类型是varchar(50)
alter table stu add address varchar(50);
  • Modify data type
ALTER TABLE 表名 MODIFY 列名 新数据类型;
-- 将stu表中的address字段的类型改为 char(50)
alter table stu modify address char(50);
  • Modify column names and data types
ALTER TABLE 表名 CHANGE 列名 新列名 新数据类型;
-- 将stu表中的address字段名改为 addr,类型改为varchar(50)
alter table stu change address addr varchar(50);
  • Delete column
ALTER TABLE 表名 DROP 列名;
-- 将stu表中的addr字段 删除
alter table stu drop addr;

4.2.8 Delete table

  • Delete table
DROP TABLE 表名;
  • Determine whether the table exists when deleting it
DROP TABLE IF EXISTS 表名;

4.2.9 View data table structure

desc [表名];

4.2.10 View table creation statements

SHOW CREATE TABLE [表名]

4.2.11 Add, delete and modify fields to grow automatically

(1) Add self-increasing field

ALTER TABLE table_name ADD id INT NOT NULL AUTO_INCREMENT PRIMARY KEY;

Note: table_nameThe name of the table representing the auto-increasing field you want to add idrepresents the name of the auto-increasing field you want to add.

(2) Modify the self-increasing field

ALTER TABLE table_name CHANGE column_name new_column_name INT NOT NULL AUTO_INCREMENT PRIMARY KEY;
  • table_nameRepresents the name of the table containing the auto-increment field, column_namerepresents the original auto-increment field name, and new_column_namerepresents the new auto-increment field name. Please note that change the data type to INT, otherwise you cannot make the column an ​​auto-increasing primary key. Once completed, you will need to restart the form for the changes to take effect.

(3) Delete the self-increasing field

ALTER TABLE table_name MODIFY column_name datatype;

Note: table_nameRepresents the table name of the auto-increasing field to be deleted, column_namethe name of the auto-increasing field to be deleted, and datatypethe data type to be set.

4.2.12 Add, delete and modify columns of data tables

(1) Add columns to the data table

ALTER TABLE <表名> ADD COLUMN <列名> <数据类型>;

ALTER TABLE student ADD COLUMN age INT;

The above command will studentadd a type column agenamed to the table INT.

(2) Delete columns of the data table

ALTER TABLE <表名> DROP COLUMN <列名>;

ALTER TABLE student DROP COLUMN age;

The above command will studentdelete agethe column named from the table.

(3) Modify the columns of the data table

ALTER TABLE <表名> MODIFY COLUMN <列名> <数据类型>;

ALTER TABLE student MODIFY COLUMN age VARCHAR(10);

The above command will change the data type of the columns studentin the table to .ageVARCHAR(10)

4.2.13 Add, delete and view indexes

(1) Add index:

  • To add an index to a column in a table, you can use the following command:
ALTER TABLE table_name ADD INDEX index_name (column_name);

Among them, table_name is the name of the table, index_name is the name of the index, and
column_name is the name of the column to which the index is added.

  • For example, if you want to add an index named idx_email to the email column of the table named users, you can use the following command:
ALTER TABLE users ADD INDEX idx_email (email);

(2) Delete index:

  • To delete an index from a table, you can use the following command:
ALTER TABLE table_name DROP INDEX index_name;

Among them, table_name is the name of the table, and index_name is the name of the index to be deleted.

  • For example, if you want to delete the idx_email index of the table named users, you can use the following command:
ALTER TABLE users DROP INDEX idx_email;

(3) View index:

  • To view index information in a table, you can use the following command:
SHOW INDEX FROM table_name;

Among them, table_name is the name of the table. This command returns a result set containing index information.

  • For example, if you want to view the index information of the table named users, you can use the following command:
SHOW INDEX FROM users;

4.2.14 Create temporary table

  • To create a temporary table, you can use the following syntax:
CREATE TEMPORARY TABLE temp_table_name (  
    column1 datatype,  
    column2 datatype,  
    ...  
	);

where temp_table_name is the name of the temporary table you want to create. You specify the table's columns and data types just like you would when creating a regular table.

  • Here is a specific example:
CREATE TEMPORARY TABLE temp_users (  
    id INT PRIMARY KEY,  
    name VARCHAR(50),  
    email VARCHAR(100)  
);

The above command will create a temporary table named temp_users, which contains the id, name and email columns. The id column is the primary key.

  • Note: Temporary tables are only visible during the current session and are automatically deleted at the end of the session. Therefore, it is a convenient way to store temporary data during a session.

4.2.15 Create memory table

  • To create a memory table, you can use the following syntax:
CREATE TABLE mem_table_name (  
    column1 datatype,  
    column2 datatype,  
    ...  
) ENGINE=MEMORY;

where mem_table_name is the name of the memory table you want to create. You specify the table's columns and data types just like you would when creating a regular table. By setting the ENGINE option to MEMORY, the table will be created as an in-memory table.

Here is a specific example:

CREATE TABLE mem_users (  
    id INT PRIMARY KEY,  
    name VARCHAR(50),  
    email VARCHAR(100)  
) ENGINE=MEMORY;

The above command will create an in-memory table named mem_users, which contains the id, name and email columns. The id column is the primary key.

  • Note: Memory tables are stored in memory, so data modifications take effect immediately and are visible to all users. However, when the MySQL server is shut down, the data in the memory table will be lost. Therefore, it is suitable for scenarios such as temporary storage of data or caching.

4.2.16 View the storage location of database data tables

To see where the data tables are stored in a MySQL database, you can perform the following steps:

  • To connect to the MySQL server, you can use the following command:
mysql -u username -p

Where username is your MySQL username. You will be prompted for your password.

Select the database whose storage location you want to view. Use the following command to select the database:

USE database_name;

where database_name is the name of the database where you want to view the storage location.

  • Execute the following command to view the storage location of the data table:
SHOW TABLE STATUS;

This command returns a result set containing information about all data tables in the database. Among them, you can pay attention to the File column, which will display the storage location of the data table.

  • If you only want to view the storage location of a specific data table, you can use the SHOW TABLE
    STATUS and LIKE statements together. For example, to see where the table named table_name is stored, you can execute the following command:
SHOW TABLE STATUS LIKE 'table_name';

This will return details for a specific data table, including storage location.

  • Note: These commands are valid in MySQL version 5.5 and higher.

4.2.17 Clear table contents

  • To clear the contents of a MySQL table, you can use the following command:
TRUNCATE TABLE table_name;

Among them, table_name is the name of the table whose contents are to be cleared.

This command will delete all data in the table but retain the structure of the table. In other words, it deletes all rows in the table but retains the table's primary keys, indexes, and other properties.

  • Note: Once this operation is performed, the contents of the table cannot be restored. Therefore, before using this command, make sure you have backed up your important data.

4.3 DML (Data Operation)

  • DML is a data manipulation language. It is a set of commands and statements used to manage and process databases, and are used to insert, update, delete, query, and modify data in the database.

  • The DML operations in MySQL commands mainly include Insert, Delete and Update.

4.3.1 Data increase

  • Add operation:
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);

For example, to add a record to the table named users, you can execute the following command:

INSERT INTO users (id, name, email) VALUES (1, 'John Doe', '[email protected]');

4.3.2 Data deletion

  • Delete operation:
DELETE FROM table_name WHERE condition;

For example, to delete the record with id 1 in the users table, you can execute the following command:

DELETE FROM users WHERE id = 1;

4.3.3 Data modification

  • Modification operation:
UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;

For example, to modify the email of the record named 'John Doe' in the users table, you can execute the following command:

UPDATE users SET email = '[email protected]' WHERE name = 'John Doe';

4.4 DQL (data query)

  • The main command of DQL (Data Query Language) operation in MySQL is SELECT, which is used to retrieve data from database tables.

4.4.1 Retrieve all data

SELECT * FROM table_name;

This command will return all records in the table.

4.4.2 Specify the columns to retrieve

SELECT column1, column2 FROM table_name;

This command will return the specified columns, such as column1 and column2.

4.4.3 Use the WHERE clause to specify conditions

SELECT * FROM table_name WHERE condition;

This command will return all records that meet the specified criteria. For example, SELECT * FROM users WHERE age > 18 will return all user records whose age is greater than 18.

4.4.4 Using aggregate functions to calculate statistics

SELECT COUNT(*) FROM table_name;

This command will return the number of records in the table. Other aggregate functions can also be used, such as SUM, AVG, MAX, and MIN, etc.

4.4.5 Use GROUP BY to group data

SELECT column1, COUNT(*) FROM table_name GROUP BY column1;

This command will group by column1 and count the number of records in each group.

4.4.6 Use ORDER BY to sort data

SELECT * FROM table_name ORDER BY column1;

This command will sort all records in ascending order of column1. You can also use the DESC keyword to sort in descending order. #### 4.4.7 Use LIMIT to limit the number of records returned

SELECT * FROM table_name LIMIT 10;

This command will return the first 10 records in the table. You can also use the OFFSET keyword to specify which row to start returning records from.

4.5 DCL (data control)

  • The main commands for DCL (Data Control Language) operations in MySQL are used to manage users and permissions.

4.5.1 GRANT command: grant access permissions

GRANT 权限列表 ON 对象类型 对象名称 TO 用户名@用户地址 IDENTIFIED BY用户口令;

For example, grant user test full access to all databases:

GRANT ALL PRIVILEGES ON *.* TO 'test'@'localhost' IDENTIFIED BY 'password';

4.5.2 REVOKE command: revoke access rights

REVOKE 权限列表 ON 对象类型 对象名称 FROM 用户名@用户地址;

For example, to revoke user test's access to all databases:

REVOKE ALL PRIVILEGES ON *.* FROM 'test'@'localhost';

4.5.3 SET PASSWORD command: modify user password

SET PASSWORD FOR 用户名@用户地址 = SET PASSWORD BY PASSWORD ('新口令');

For example, change the password of user test to a new password:

SET PASSWORD FOR 'test'@'localhost'=SET PASSWORD BY PASSWORD ('newpassword');

4.5.4 FLUSH command: refresh permissions

FLUSH PRIVILEGES;

For example, refreshing permissions causes the app to change immediately:

FLUSH PRIVILEGES;

おすすめ

転載: blog.csdn.net/weixin_61370021/article/details/130797638