当前位置:网站首页>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
边栏推荐
- The reasons why there are so many programming languages in programming internal skills
- 【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】
- [how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]
- Conversion function and explicit
- Disruptor -- a high concurrency and high performance queue framework for processing tens of millions of levels
- [机缘参悟-37]:人感官系统的结构决定了人类是以自我为中心
- Golang — template
- Error handling when adding files to SVN:.... \conf\svnserve conf:12: Option expected
- The shadow of the object at the edge of the untiy world flickers, and the shadow of the object near the far point is normal
- 顺序表(C语言实现)
猜你喜欢
Flutter dynamic | fair 2.5.0 new version features
[bw16 application] instructions for firmware burning of Anxin Ke bw16 module and development board update
Screenshot of the operation steps of upload labs level 4-level 9
全面发展数字经济主航道 和数集团积极推动UTONMOS数藏市场
如何使用lxml判断网站公告是否更新
刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?
Golang - command line tool Cobra
Use and design of Muduo buffer class
Kivy教程之 如何自动载入kv文件
常见的几种最优化方法Matlab原理和深度分析
随机推荐
网上开户哪家证券公司佣金最低,我要开户,网上客户经理开户安全吗
[quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion
Replace the GPU card number when pytorch loads the historical model, map_ Location settings
挡不住了,国产芯片再度突进,部分环节已进到4nm
pytorch 载入历史模型时更换gpu卡号,map_location设置
双向链表(我们只需要关注插入和删除函数)
Dynamic programming 01 knapsack and complete knapsack
The reasons why there are so many programming languages in programming internal skills
Golang — 命令行工具cobra
Ocean CMS vulnerability - search php
Common network state detection and analysis tools
GoLand 2021.1: rename the go project
Sequence table (implemented in C language)
Stack application (balancer)
Go: send the get request and parse the return JSON (go1.16.4)
windos 创建cordova 提示 因为在此系统上禁止运行脚本
When updating mysql, the condition is a query
如何使用lxml判断网站公告是否更新
MyCms 自媒体商城 v3.4.1 发布,使用手册更新
[sort] bucket sort