当前位置:网站首页>MySQL data processing value addition, deletion and modification
MySQL data processing value addition, deletion and modification
2022-07-03 13:52:00 【Chen Yixin】
Catalog
1.1 The way 1:VALUES The way to add
1.1.1 situation 1: Inserts data in the default order for all fields of the table
1.1.2 situation 2: Insert data for the specified fields of the table
1.1.3 situation 3: Insert multiple records at the same time
1.2 The way 2: Insert the query results into the table
5.mysql8.0 New characteristics : Calculated column
2. Run the following script to create the table my_employees
3. Display table my_employees Structure
4. towards my_employees Insert the following data into the table
5. towards users Insert data into the table
6. take 3 Of employee No last_name It is amended as follows “drelxer”
7. All wages will be less than 900 The salary of the employee is modified to 1000
8. take userid by Bbiri Of user Table and my_employees All records in the table are deleted
9. Delete my_employees、users Table all data
10. Check the corrections made
11. Clear the table my_employees
0. preparation
Data files :mysql Inquire about sql file - Dataset document class resources -CSDN library
1. insert data
1.1 The way 1:VALUES The way to add
Using this syntax, you can only insert into a table at a time One data .1.1.1 situation 1: Inserts data in the default order for all fields of the table
Grammar format :
INSERT INTO Table nameVALUES (value1,value2,....);In the value list, you need to specify a value for each field of the table , And the order of values must be the same as that of fields in the data table .
1.1.2 situation 2: Insert data for the specified fields of the table
Grammar format :
INSERT INTO Table name (column1 [, column2, …, columnn])VALUES (value1 [,value2, …, valuen]);Insert data for the specified fields of the table , Is in the INSERT Statement inserts values into only some fields , The values of other fields are the default values when the table is defined .
1.1.3 situation 3: Insert multiple records at the same time
INSERT Statement can insert multiple records into the data table at the same time , Specify multiple value lists when inserting , Each value list is separated by commas .Grammar format :
INSERT INTO table_nameVALUES(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]);
explain :
1. Use INSERT When inserting multiple records at the same time ,MySQL It will return some additional information that is not available when performing single line insertion , The content of this information The meaning is as follows : ● Records: Indicates the number of records inserted . ● Duplicates: Indicates the records that were ignored during insertion , The reason may be that Some records contain duplicate primary key values . ● Warnings: Data values indicating a problem , For example, data type conversion occurs .
2. A that inserts multiple rows of records at the same time INSERT Statement is equivalent to multiple single line inserts INSERT sentence , But multi line INSERT sentence During processing More efficient . because MySQL Execute a single article INSERT Statement inserts more rows of data than using multiple INSERT sentence fast , Therefore, when inserting multiple records, it is best to choose to use a single record INSERT Insert... In the form of a statement .
1.1.4 Summary :
1. VALUES Or you could write it as VALUE , however VALUES It's standard writing .2. Character and date data should be enclosed in single quotes .1.2 The way 2: Insert the query results into the table
INSERT Can also be SELECT Insert the result of the statement query into the table , At this time, it is not necessary to input the values of each record one by one , just To use a INSERT Statement and a line SELECT Statements can be quickly inserted from one or more tables into a table Multiple lines .Grammar format :INSERT INTO Target table name(tar_column1 [, tar_column2, …, tar_columnn])SELECT(src_column1 [, src_column2, …, src_columnn])FROM Source table name[WHERE condition]explain :1. stay INSERT Add a subquery to the statement .2. There's no need to write VALUES Clause .3. The list of values in the subquery should be the same as INSERT The column name in Clause corresponds to .
Be careful :
1. The data type of the query field must correspond to the data type of the field added to the table one by one
2. The length of the field added to the table cannot be less than the query field , Otherwise, there is a risk of unsuccessful addition .
2. Update data
Use UPDATE Statement update data . The grammar is as follows :
UPDATE table_nameSET column1=value1, column2=value2, … , column=valuen[WHERE condition]It can be updated at one time multiple data .If you need to roll back data , It needs to be guaranteed in DML front , Set it up :SET AUTOCOMMIT = FALSE;
notes : Adding 、 When modifying or deleting data , There may be unsuccessful .( When possible, due to the influence of constraints )
3. Delete data
Use DELETE Statement to delete data from a table , The syntax is as follows :DELETE FROM table_name[WHERE <condition>];table_name Specify the table to delete ;“[WHERE ]” Is an optional parameter , Specify deletion criteria , without WHERE Clause , DELETE Statement will delete all records in the table .notes : Adding 、 When modifying or deleting data , There may be unsuccessful .( When possible, due to the influence of constraints )
4. Summary
1.DML The operation is by default , After execution, the data will be submitted automatically , If you want to finish the execution , Do not submit data automatically , You need to use SET autocommit = FALSE;
2. Adding 、 When modifying or deleting data , There may be unsuccessful .( When possible, due to the influence of constraints )3. Basic grammar :add to : The way 1:INSERT INTO Table nameVALUES (value1,value2,....);The way 2:INSERT INTO Target table name (tar_column1 [, tar_column2, …, tar_columnn])SELECT (src_column1 [, src_column2, …, src_columnn])FROM Source table name[WHERE condition]to update ( modify ):UPDATE table_nameSET column1=value1, column2=value2, … , column=valuen[WHERE condition]Delete :DELETE FROM table_name[WHERE <condition>];5.mysql8.0 New characteristics : Calculated column
What is a calculated column ? Simply put, the value of a column is calculated from other columns . for example ,a The column value is 1、b The column value is 2,c Column Manual insertion is not required , Definition a+b As the result of the c Value , that c Is the calculation column , It is calculated from other columns .stay MySQL 8.0 in ,CREATE TABLE and ALTER TABLE Both support adding calculation Columns . Let's say CREATE TABLE For example .
6. practice
1. Create database dbtest11
CREATE DATABASE IF NOT EXISTS dbtest11 CHARACTER SET 'utf8';2. Run the following script to create the table my_employees
USE dbtest11;CREATE TABLE my_employees(id INT(10),first_name VARCHAR(10),last_name VARCHAR(10),userid VARCHAR(10),salary DOUBLE(10,2));CREATE TABLE users(id INT,userid VARCHAR(10),department_id INT);3. Display table my_employees Structure
4. towards my_employees Insert the following data into the table
5. towards users Insert data into the table
1 Rpatel 102 Bdancs 103 Bbiri 204 Cnewman 305 Aropebur 40
6. take 3 Of employee No last_name It is amended as follows “drelxer”
7. All wages will be less than 900 The salary of the employee is modified to 1000
8. take userid by Bbiri Of user Table and my_employees All records in the table are deleted
9. Delete my_employees、users Table all data
10. Check the corrections made
11. Clear the table my_employees
边栏推荐
- Go language unit test 5: go language uses go sqlmock and Gorm to do database query mock
- Use docker to build sqli lab environment and upload labs environment, and the operation steps are provided with screenshots.
- Setting up remote links to MySQL on Linux
- Richview trvstyle liststyle list style (bullet number)
- Unity EmbeddedBrowser浏览器插件事件通讯
- Brief analysis of tensorboard visual processing cases
- Asp. Net core1.1 without project JSON, so as to generate cross platform packages
- Complete deep neural network CNN training with tensorflow to complete picture recognition case 2
- 掌握Cypress命令行选项,是真正掌握Cypress的基础
- Go language web development series 28: solve cross domain access of CORS with gin contrib / CORS
猜你喜欢

Kivy tutorial how to automatically load kV files

从零开始的基于百度大脑EasyData的多人协同数据标注
![[机缘参悟-37]:人感官系统的结构决定了人类是以自我为中心](/img/06/b71b505c7072d540955fda6da1dc1b.jpg)
[机缘参悟-37]:人感官系统的结构决定了人类是以自我为中心

Implementation of Muduo accept connection, disconnection and sending data

常见的几种最优化方法Matlab原理和深度分析

Internet of things completion -- (stm32f407 connects to cloud platform detection data)

Students who do not understand the code can also send their own token, which is easy to learn BSC

Go language web development series 29: Gin framework uses gin contrib / sessions library to manage sessions (based on cookies)

Go language unit test 3: go language uses gocovey library to do unit test
![[how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]](/img/95/09552d33d2a834af4d304129714775.png)
[how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]
随机推荐
又一个行业被中国芯片打破空白,难怪美国模拟芯片龙头降价抛售了
MySQL functions and related cases and exercises
Can newly graduated European college students get an offer from a major Internet company in the United States?
Brief analysis of tensorboard visual processing cases
JVM系列——概述,程序计数器day1-1
CVPR 2022 | 美团技术团队精选6篇优秀论文解读
Leetcode-1175. Prime Arrangements
Resource Cost Optimization Practice of R & D team
C language standard IO function sorting
CVPR 2022 | interpretation of 6 excellent papers selected by meituan technical team
[技术发展-24]:现有物联网通信技术特点
解决MySql 1045 Access denied for user ‘root‘@‘localhost‘ (using password: YES)
[redis] cache warm-up, cache avalanche and cache breakdown
Go language web development series 28: solve cross domain access of CORS with gin contrib / CORS
Go language unit test 5: go language uses go sqlmock and Gorm to do database query mock
The network card fails to start after the cold migration of the server hard disk
Go language unit test 3: go language uses gocovey library to do unit test
Static linked list (subscript of array instead of pointer)
Go 1.16.4: purpose of go mod tidy
Qt学习20 Qt 中的标准对话框(中)
























