MySQL: addition, deletion, modification and query of data



foreword

This blogger will use CSDN to record the experience and knowledge he has personally gained and learned on the way to study software development. Interested friends can pay attention to the blogger!
Perhaps a person can go fast alone, but a group of people can go farther!

1. Insert data

1. Practical problems

insert image description here

Solution: Use the INSERT statement to insert data into the table.

2. Method 1: Add by VALUES

Only one piece of data can be inserted into the table at a time using this syntax.

Case 1: Insert data in default order for all fields of the table

INSERT INTO 表名
VALUES (value1,value2,....);

Values ​​need to be specified for each field of the table in the list of values, and the order of the values ​​must be the same as the order in which the fields are defined in the data table.

Example:

INSERT INTO departments
VALUES (70, 'Pub', 100, 1700);
INSERT INTO	departments
VALUES		(100, 'Finance', NULL, NULL);

Case 2: Insert data for the specified field of the table

INSERT INTO 表名(column1 [, column2,, columnn]) 
VALUES (value1 [,value2,, valuen]);

To insert data into a specified field of a table is to insert values ​​into only some fields in the INSERT statement, while the values ​​of other fields are the default values ​​when the table is defined.

List the column names freely in the INSERT clause, but once listed, the value1,...valuen to be inserted in VALUES needs to correspond to the column1,...column columns one-to-one. If the types are different, the insert will fail and MySQL will generate an error.

Example:

INSERT INTO departments(department_id, department_name)
VALUES (80, 'IT');

Situation 3: Insert multiple records at the same time
The INSERT statement can insert multiple records into the data table at the same time, specify multiple value lists when inserting, and separate each value list with a comma. The basic syntax format is as follows:

INSERT INTO table_name 
VALUES 
(value1 [,value2,, valuen]),
(value1 [,value2,, valuen]),
……
(value1 [,value2,, valuen]);

or

INSERT INTO table_name(column1 [, column2,, columnn]) 
VALUES 
(value1 [,value2,, valuen]),
(value1 [,value2,, valuen]),
……
(value1 [,value2,, valuen]);

Example:

mysql> INSERT INTO emp(emp_id,emp_name)
    -> VALUES (1001,'shkstart'),
    -> (1002,'atguigu'),
    -> (1003,'Tom');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

When using INSERT to insert multiple records at the same time, MySQL will return some additional information that is not available when performing single-row insertion. The meaning of these information is as follows:

  1. Records: Indicates the number of records inserted.
  2. Duplicates: Indicates records that were ignored during insertion, possibly because these records contained duplicate primary key values.
  3. Warnings: Indicates problematic data values, such as data type conversions.

An INSERT statement that inserts multiple rows at the same time is equivalent to multiple INSERT statements that insert a single row, but multi-row INSERT statements are more efficient during processing. Because MySQL executes a single INSERT statement to insert multiple rows of data faster than using multiple INSERT statements, it is best to use a single INSERT statement to insert multiple records.

Summary:
4. VALUES can also be written as VALUE, but VALUES is the standard way of writing.
5. Character and date data should be enclosed in single quotes.

3. Method 2: Insert query results into the table

INSERT can also insert the results of the SELECT statement query into the table. At this time, it is not necessary to input the value of each record one by one. It only needs to use a combined statement consisting of an INSERT statement and a SELECT statement to quickly select from one or more records. Insert multiple rows into a table.

The basic syntax format is as follows:

INSERT INTO 目标表名
(tar_column1 [, tar_column2,, tar_columnn])
SELECT
(src_column1 [, src_column2,, src_columnn])
FROM 源表名
[WHERE condition]

• Include subqueries in INSERT statements.
• It is not necessary to write the VALUES clause.
• The list of values ​​in the subquery should correspond to the column names in the INSERT clause.
Example:

INSERT INTO emp2 
SELECT * 
FROM employees
WHERE department_id = 90;
INSERT INTO sales_reps(id, name, salary, commission_pct)
SELECT employee_id, last_name, salary, commission_pct
FROM   employees
WHERE  job_id LIKE '%REP%';

2. Delete data

insert image description here

• Delete data from a table using the DELETE statement
insert image description here

DELETE FROM table_name [WHERE <condition>];

table_name specifies the table to be deleted; "[WHERE]" is an optional parameter and specifies the deletion condition. If there is no WHERE clause, the DELETE statement will delete all records in the table.
• Use the WHERE clause to delete specified records.

DELETE FROM departments
WHERE  department_name = 'Finance';

• If the WHERE clause is omitted, all data in the table will be deleted

DELETE FROM  copy_emp;

• Data integrity error in deletion

DELETE FROM departments
WHERE       department_id = 60;

insert image description here

说明:You cannot delete a row that contains a primary key that is used as a foreign key in another table.

3. Update data

insert image description here

• Update data using the UPDATE statement. The syntax is as follows:

UPDATE table_name
SET column1=value1, column2=value2,, column=valuen
[WHERE condition]

• Multiple pieces of data can be updated at one time.
• If you need to roll back data, you need to make sure to set it before DML: SET AUTOCOMMIT = FALSE;
• Use the WHERE clause to specify the data that needs to be updated.

UPDATE employees
SET    department_id = 70
WHERE  employee_id = 113;

• If the WHERE clause is omitted, all data in the table will be updated.

UPDATE 	copy_emp
SET    	department_id = 110;

• Data integrity errors in updates

UPDATE employees
SET    department_id = 55
WHERE  department_id = 110;

insert image description here

Explanation: Department 55 does not exist

4. Query data

MySQL database uses SQL SELECT statement to query data.

Syntax:
The following is the general SELECT syntax for querying data in the MySQL database:

SELECT column_name,column_name
FROM table_name
[WHERE Clause]
[LIMIT N][ OFFSET M]
  1. In the query statement, you can use one or more tables, use commas (,) to separate the tables, and use the WHERE statement to set the query conditions.
  2. The SELECT command can read one or more records.
  3. You can use an asterisk (*) to replace other fields, and the SELECT statement will return all field data in the table
  4. You can use the WHERE clause to include any conditions.
  5. You can use the LIMIT attribute to set the number of records returned.
  6. You can use OFFSET to specify the data offset to start querying in the SELECT statement. By default the offset is 0.

Five, MySQL8 new features: calculated column

What is a calculated column? Simply put, the value of a certain column is calculated through other columns. For example, the value of column a is 1, the value of column b is 2, column c does not need to be manually inserted, and the result of defining a+b is the value of c, then c is a calculated column, which is calculated from other columns.

In MySQL 8.0, both CREATE TABLE and ALTER TABLE support adding computed columns. The following uses CREATE TABLE as an example to explain.

Example: Define data table tb1, and then define field id, field a, field b, and field c, where field c is a calculated column for calculating the value of a+b.
First create the test table tb1, the statement is as follows:

CREATE TABLE tb1(
id INT,
a INT,
b INT,
c INT GENERATED ALWAYS AS (a + b) VIRTUAL
);

Insert demo data, the statement is as follows:

INSERT INTO tb1(a,b) VALUES (100,200);

Query the data in the data table tb1, the results are as follows:

mysql> SELECT * FROM tb1;
+------+------+------+------+
| id   | a    | b    | c    |
+------+------+------+------+
| NULL |  100 |  200 |  300 |
+------+------+------+------+
1 row in set (0.00 sec)

To update the data in the data, the statement is as follows:

mysql> UPDATE tb1 SET a = 500;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

Guess you like

Origin blog.csdn.net/weixin_52533007/article/details/131587152