[MySQL database] Basic command operations and statement summary

Table of contents

Preface

1. Database connection

2. Create database

2.1 Create database

2.2 Delete database

2.3 View database

2.4 Using database

3. Create data table

3.1 Create data table

3.2 Delete data table

3.3 Insert statement

3.4 View data sheet

4. Database operations

4.1 Display columns

4.2 Display index

4.3 Modify table name

4.4 Modify data type

4.5 Modify field name

4.6 Add fields

4.7 Delete fields

5. Query statement

5.1 Get all columns in the table

5.2 Get the data of column names

5.3 Using WHERE clause

5.4 Use multi-table query

5.5 Search using wildcard character (*)

6. Modify the statement

6.1 Update a single field

6.2 Update multiple fields

6.3 Using the WHERE clause

7. Delete statements

7.1 Delete all records in the table

7.2 Delete records that meet specified conditions

Summarize



Preface

In this blog, I will introduce the basic operations of MySQL database. MySQL is an open source relational database management system that is widely used for various types of application development and data storage. Whether you are a beginner or an experienced developer, understanding the basic operations of MySQL is crucial to managing and operating your database correctly and efficiently.

The focus of this blog is to introduce the following aspects of MySQL: creating databases, creating data tables, and database operations. Please see the text for details.


1. Database connection

The installation of the database is not covered here. After installing the database, use the cmd command line to connect.

Sample code:

mysql -h(主机地址) -u root(用户名) -p(指定密码)

Notice:

You can log in on your own computer without adding the -h parameter. If you don't want others to see your login password, you can just enter -p, and then enter the password in the Enter password that appears (the password will appear with an * sign). -p: If the login password is empty, it can be ignored.

My screenshot:

2. Create database

2.1 Create database

A new database can be created using the `CREATE DATABASE` (case-insensitive) statement.
Sample code:

CREATE DATABASE 新的数据库名称;

Note :
The sql statement must end with an English semicolon; the keyword "create database" for MySQL cannot be typed incorrectly or omitted; the database name cannot contain spaces, otherwise an error will be reported; the name of the database cannot begin with a number.

Running screenshot:

2.2 Delete database

Use the `drop database` statement to drop a previously created database.

Sample code:

drop database 数据库名;

2.3 View database

Use the `show databases; ` statement to list the data list of the mysql database management system. Please note that databases is the plural form here.

2.4 Using database

Use the `use` statement to select the database that needs to be operated. After using this command, all Mysql commands will only target this database.
The appearance of the changed field indicates success.
Sample code:
use 数据库名;

Running screenshot:

3. Create data table

3.1 Create data table

Use the `CREATE TABLE` statement to create a data table. Creating a mysql data table requires the following information: table name, table field name, and definition of each table field.
Sample code:
CREATE TABLE 表名 (属性名 数据类型 完整性约束条件,
属性名 数据类型 完整性约束条件,
属性名 数据类型 );

For the content of data types and constraints, please refer to my previous blog
[MySQL Database] Summary of Basic Theoretical Knowledge_Filotimo_'s Blog-CSDN Blog

My screenshot (created a students table):

3.2  Delete data table

Use the ` DROP TABLE` statement to delete a data table . Deleting a data table in MySQL is very easy, but you must be very careful when deleting a table, because all data will disappear after executing the delete command.
Sample code:
DROP TABLE 表名;

3.3  Insert statement

Use the `INSERT INTO` statement to insert data.

Sample code:

INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);

Here, table_nameis the name of the data table into which data is to be inserted. column1, column2, ...Is the name of the column into which data is to be inserted. value1, value2, ...is the data value to be inserted.

My screenshot ( 10 student records successfully inserted ):

3.4 View data sheet

(1) Use the ` select * from` statement to view the data table data.
Sample code:
select * from 表名;
My screenshot (view the students table generated by the just operation):
(2) Use the ` show tables; ` statement to view all tables in the specified database. Before using this command, you need to use the use command to select the database to be operated.

4. Database operations

4.1 Display columns

(1) Use `SHOW COLUMNS FROM` to display the column information of the specified data table, including column name, data type, whether it is allowed to be empty, whether it is the primary key, default value and other attribute information.

Sample code:

SHOW COLUMNS FROM 表名;

My screenshot:

In
the above example result, the `Field` column shows the column name, the `Type` column shows the data type, the `Null` column indicates whether NULL values ​​are allowed, the `Key` column indicates whether it is a primary key, and the `Default` column Default values ​​are shown, and the `Extra` column shows additional information.

(2) `SHOW FULL COLUMNS FROM` This command is similar to the previous command, but when displaying column information, it will also provide more detailed remarks (if any), such as column comments, character sets, and column permissions. wait.
   
Sample code:

SHOW FULL COLUMNS FROM 表名;

  My screenshot: Compared
   with

4.2 Display index

Use `SHOW INDEX FROM` to display detailed index information of the specified data table, including ordinary indexes and primary key indexes. It provides information about each index's name, index columns, sorting method, number of unique values, compression method, etc.

Sample code:

SHOW INDEX FROM 表名;

  My screenshot:

In the example result above, Table the column shows the table name, Non_unique the column indicates whether the index can be repeated, the column indicates Key_name the name of the index, Seq_in_index the column indicates the sort order of the columns in the index, Column_name the column indicates the column name, and Collation the column indicates how the column is stored in the index ( A means ascending, NULL means no sort), Cardinality column shows the estimated number of unique values ​​for the index, Sub_part column indicates the number of characters indexed if the column is only partially indexed, Packed column indicates how the keyword is compressed, Null column indicates Whether the column contains NULL values, Index_type the column displays the index method used, and Comment the column is used to add other remark information.

4.3 Modify table name

The table name can be modified using keywords ALTER TABLEin the statement RENAME.

Sample code:
Alter table 旧表名 rename 新表名;

4.4 Modify data type

To modify the data type of a field in a table, you can use the `MODIFY` keyword in the `ALTER TABLE` statement.

Sample code:

Alter table 表名 modify 属性名 数据类型;

Here, `table name` is the table name of the field to be modified, `property name` is the field name to be modified, and `new data type` is the new data type to which the field is to be modified.

4.5 Modify field name

To modify the name and data type of a field in a table, you can use the `CHANGE` keyword in the `ALTER TABLE` statement.

Sample code:

ALTER TABLE 表名 CHANGE 旧属性名 新属性名 新数据类型;

Here, `table name` is the table name of the field to be modified, `old attribute name` is the old name of the field to be modified, `new attribute name` is the new name of the field to be modified, `new data type` is The new data type to modify the field to.

4.6  Add fields

To add a new field to a table, use the `ADD` keyword in the `ALTER TABLE` statement.

Sample code:

ALTER TABLE 表名 ADD 属性名 数据类型 [完整性约束条件];

Here, `table name` is the name of the table to which the field is to be added, ` property name` is the name of the new field, ` data type` is the data type of the new field, ` integrity constraints` is optional and is used to define Field constraints.

4.7  Delete fields

To delete a field from a table, you can use the `DROP` keyword in the `ALTER TABLE` statement.

Sample code:

Alter table 表名 drop 属性名 ;

Here, `table name` is the name of the table where the field is to be deleted, and `property name` is the name of the field to be deleted.

5. Query statement

For database query operations, the `SELECT` statement can be used to retrieve data. Here are some common query statement examples:

5.1 Get all columns in the table

Get all columns in the table:

SELECT * FROM 表名;

This will return all columns and rows in the table, where `table_name` is the name of the table being queried.

5.2 Get the data of column names

Get the data of the specified column name:

SELECT 列名1, 列名2 FROM 表名;

This will return data for the specified column names in the table, where `columnname1` and `columnname2` are the names of the columns you want to query, and `tablename` is the name of the table you want to query.

5.3 Using WHERE clause

Use the WHERE clause to set query conditions:

SELECT 列名 FROM 表名 WHERE 条件;

This will return a data set that satisfies the conditions based on the specified conditions, where `columnname` is the name of the column to be queried, `tablename` is the name of the table to be queried, and `conditions' are the query conditions to be set.

Description: The where clause is similar to the if condition in programming language, and reads the specified data based on the field value in the mysql table. Where string comparisons are case-insensitive. You can use the where binary keyword to make string comparisons in where clauses case-sensitive.

5.4 Use multi-table query

Use multi-table query:

SELECT 列名 FROM 表名1, 表名2 WHERE 条件;

This will retrieve data from multiple tables and perform a join based on specified criteria. You can specify join conditions by separating multiple table names with commas and then using the `WHERE` statement.

5.5 Search using wildcards

Use the wildcard character (*) to retrieve all field data:

SELECT * FROM 表名;

This will return all field data for the table without specifying specific column names.

6. Modify the statement

In MySQL, you can use the `UPDATE` command to modify or update data. Here are some examples of `UPDATE` commands:

6.1 Update a single field

UPDATE 表名 SET 字段名 = 新值 WHERE 条件;

This will update the value of the specified field in rows that meet the specified criteria. Among them, `table name` is the name of the table where the data is to be updated, `field name` is the name of the field to be updated, `new value` is the new value of the field, and `condition` is the condition of the row to be updated.

6.2 Update multiple fields

Update the values ​​of multiple fields at the same time:

UPDATE 表名 SET 字段名1 = 新值1, 字段名2 = 新值2 WHERE 条件;

This will simultaneously update the values ​​of multiple fields in rows that meet the specified criteria. You can add more fields and corresponding new values ​​as needed.

6.3 Using the WHERE clause

Use the `WHERE` clause to specify updated rows:

UPDATE 表名 SET 字段名 = 新值 WHERE 条件;

You can use the `WHERE` clause to limit the range of rows for update operations. Only rows that meet the specified criteria will be updated.

7. Delete statements

In MySQL, you can use DELETEstatements to delete records in the data table. Here are some examples:

7.1 Delete all records in the table

DELETE FROM 表名;

This will delete all records in the specified table, which is equivalent to clearing the table.

7.2 Delete records that meet specified conditions

DELETE FROM 表名 WHERE 条件;

This will delete records that meet the specified criteria. You can use the `WHERE` clause to limit the range of records to be deleted.


Summarize

Through the study of this blog, you will master the basic operations of MySQL database, so that you can correctly create and delete databases, create and delete tables, insert and query data, and perform basic data update and delete operations. These basic operations are the basis for your further in-depth study and application of MySQL database, and are also the cornerstone of other advanced operations. I hope this blog can help you quickly get started with the basic operations of the MySQL database, allowing you to better manage and operate data.

Follow, like, collect, I hope friends can connect three times with one click!

Guess you like

Origin blog.csdn.net/m0_71369515/article/details/133336437