Summary of basic knowledge and operations of MySQL database

Summary of basic knowledge and operations of MySQL database

This article uses examples to explain the basic knowledge and operations of MySQL database. Share with you for your reference, the details are as follows:

database

1. Modify the data table

Add a column:

ALTERTABLE tab_name ADD col_name column_defi[FIRST|AFTER col_name];

You can specify the position of the new column, either at the very beginning (FIRST) or after a column (AFTER...), otherwise the new column is placed at the end by default.

Add multiple columns:

ALTERTABLE tab_name ADD (col_name1 column_defi1, col_name2 column_defi2 ...);

Delete a column:

ALTERTABLE tab_name DROP col_name;

To delete multiple columns or add a new column after deleting them, just add a comma after drop col_name and then write drop/add.

Add constraints:

ALTERTABLE tab_name ADD PRIMARY KEY (index_column);

The primary key can be replaced by other constraints such as UNIQUE and FOREIGN KEY, and the default constraint is SET DEFAULT.

To delete a constraint:

ALTERTABLE tab_name DROP {INDEX|KEY} index_name

To delete a foreign key:

ALTER TABLE tab_name DROP FOREIGN KEY fkey_name

The foreign key constraint name fkey_name can be obtained by viewing the constraint item through show create table tab_name;, for example

CONSTRAINT `users_ibfk_1` FOREIGN KEY (`pid`) REFERENCES

Modify the column definition:

ALTERTABLE tab_name MODIFY col_name col_defi;

Modify the column name:

ALTERTABLE tab_name CHANGE old_colname new_name col_defi;

Modify the data table name:

RENAMETABLE old_name TO new_name;

2. Data Table Operation

Insert a record:

INSERT tab_name[(col_name)] VALUES (val1,val2...)

The column name is optional. If you do not set the column name, the default is to insert each column. The second insertion method: INSERT tab_name SET col_name=val...The third method: insert tab_name select…, insert the query results into the data table.

Update log:

UPDATE tab_name SET col_name=val [WHERE condition];

If the where condition is not filled in, all records will be updated by default.

Deleting records:

DELETE FROM tab_name [WHERE condition];

Delete the records that meet the conditions. If you do not fill in where, all records will be deleted.

Query records:

SELECT col_name1,col_name2... /*Select the column name to be queried*/

FROM tab_name/*Select the data table to be queried*/

WHERE condition/*Query condition*/

GROUP BY col_name [ASC|DESC]/*Query result grouping*/

HAVING condition/*Filter the query group*/

ORDER BY col_name[asc|desc]/*Sort the query results by row*/

LIMIT [n1,]n2/*Returns n2 results starting from n1. If n1 is not filled in, it will return from the beginning by default*/

3. Subquery

The parent query and the subquery are connected by comparison symbols. When the subquery returns multiple results, the results can be modified by ANY or ALL.

Multi-table update: Update this table with reference to another table.

Inner join: INNERJOIN ON join_condition , returns the items that meet the conditions in the left and right tables.

Left outer join: LEFTJOIN ON join_condition , returns all items in the left table that meet the condition in the right table.

Left outer join: RIGHT JOIN ON join_condition , returns all items in the right table that meet the condition in the left table.

4. Database Functions

CONCAT('a','b'...)

Concatenate multiple strings a and b

CONCAT_WS('s','a','b'…)

Use s as the connector to connect multiple strings

FORMAT(f,n)

Display the number f with n decimal places

LOWER()/UPPER()

Convert content to lower/upper case

LEFT/RIGHT('s',n)

Get the first n characters on the left/right of a string

LENGTH()

Get the length of a string

LTRIM/RTRIM/TRIM()

Remove spaces on the left/right/both sides of a string

REPLACE('s','a','b')

Replace the character a in string s with character b

SUBSTRING('s',n,e)

Extract e characters from the string starting at the nth position

IS NULL

Empty

n IN(a,b,c…)

Determine whether n is among the listed values

n BETWEEN a AND b

Determine whether n is between a and b

Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL query skills", "MySQL common functions summary", "MySQL log operation skills", "MySQL transaction operation skills summary", "MySQL stored procedure skills" and "MySQL database lock related skills summary"

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • PHP basics: connecting to MySQL database and querying data
  • Basic tutorial on connecting and operating MySQL database with PHP
  • MySQL Basic Database Creation
  • PHP5 basic code for operating MySQL database
  • MySQL 8.0.15 installation graphic tutorial and database basics
  • MySQL database basic commands (collection)
  • Very comprehensive Mysql database, database table, data basic operation notes (including code)
  • MySQL database basic notes
  • Python Topic 9 Basic Knowledge of MySQL Database Programming
  • Summary of Common Commands for Getting Started with MySQL Database Basics
  • MySQL Database Basics: A Summary of Basic Commands
  • Summary of basic knowledge points of MySql database

<<:  Nginx defines domain name access method

>>:  Vue implementation example using Google Recaptcha verification

Recommend

A brief talk about MySQL pivot tables

I have a product parts table like this: part part...

jQuery implements all selection and reverse selection operation case

This article shares the specific code of jQuery t...

Detailed explanation of the functions and usage of MySQL common storage engines

This article uses examples to illustrate the func...

How to query json in the database in mysql5.6 and below

When saving data in MySQL, sometimes some messy a...

The concept of MySQL tablespace fragmentation and solutions to related problems

Table of contents background What is tablespace f...

Detailed explanation of eight methods to achieve CSS page bottom fixed

When we are writing a page, we often encounter a ...

Some indicators of excellent web front-end design

The accessibility of web pages seems to be somethi...

Solution to the error reported by Mysql systemctl start mysqld

Error message: Job for mysqld.service failed beca...

MYSQL stored procedures, that is, a summary of common logical knowledge points

Mysql stored procedure 1. Create stored procedure...

Detailed description of the function of meta name="" content="

1. Grammar: <meta name="name" content...

Example code for implementing auto-increment sequence in mysql

1. Create a sequence table CREATE TABLE `sequence...

Detailed introduction to MySQL database index

Table of contents Mind Map Simple understanding E...

vue-router hook function implements routing guard

Table of contents Overview Global hook function R...