当前位置:网站首页>100 important knowledge points that SQL must master: updating and deleting data
100 important knowledge points that SQL must master: updating and deleting data
2022-06-30 11:06:00 【Guge academic】
This lesson introduces how to use UPDATE and DELETE Statement to further manipulate table data .
16.1 Update data
to update ( modify ) Table data , have access to UPDATE sentence . There are two uses UPDATE
The way :
Update specific rows in the table ;
Update all rows in the table .
Here are the introduction .
Be careful : Don't omit WHERE Clause
In the use of UPDATE Be careful when you do . Because I didn't pay attention , Will update all in the table
That's ok . Before using this statement , Please read this section completely .
Tips : UPDATE And security
On the client side / Server's DBMS in , Use UPDATE Statements may require special security
jurisdiction . In your use UPDATE front , You should ensure that you have sufficient security permissions .
Use UPDATE The statement is very easy , It's even too easy to say . Basic UPDATE sentence
It's made up of three parts , Namely :
Table to update ;
Column names and their new values ;
Determine which rows of filter conditions to update .
Take a simple example . Customer 1000000005 Now I have an email address , So his
Records need to be updated , The statement is as follows :
Input ▼
UPDATE Customers
SET cust_email = '[email protected]'
WHERE cust_id = 1000000005;
UPDATE Statements always start with the name of the table to be updated . In this case , The name of the table to be updated is
Customers . SET The command is used to assign a new value to the updated column . ad locum , SET Clause set
Set up cust_email Column as the specified value :
SET cust_email = '[email protected]'
UPDATE Statement to WHERE End of clause , It tells DBMS Which line to update . No, WHERE
Clause ,DBMS Will be updated with this email address Customers All the rows in the table ,
This is not what we want .
The syntax for updating multiple columns is slightly different :
Input ▼
UPDATE Customers
SET cust_contact = 'Sam Roberts',
cust_email = '[email protected]'
WHERE cust_id = 1000000006;
When updating multiple columns , Just use one SET command , Every “ Column = value ” Use... Between pairs
Comma separated ( Do not use comma after last column ). In this example , Update customer 1000000006
Of cust_contact and cust_email Column .
Tips : stay UPDATE The subquery is used in the statement
UPDATE You can use subqueries in statements , Make it possible to use SELECT The data retrieved by the statement
Update column data . More about subqueries and their use , Please refer to the first 11 course .
Tips : FROM keyword
yes , we have SQL Implementation support in UPDATE Use in statement FROM Clause , Use the number of a table
Update the rows of another table . If you want to know your DBMS Whether this feature is supported or not , Please refer to
Read its documentation .
To delete the value of a column , You can set it to NULL ( If the table definition allows NULL value ). as follows
Conduct :
Input ▼
UPDATE Customers
SET cust_email = NULL
WHERE cust_id = 1000000005;
among NULL Used to remove cust_email The value in the column . This is very different from saving an empty string
( Empty string with '' Express , It's a value ), and NULL No value .
16.2 Delete data
Delete... From a table ( Get rid of ) data , Use DELETE sentence . There are two uses DELETE
The way :
Delete specific rows from the table ;
Delete all rows from the table .
Here are the introduction .
Be careful : Don't omit WHERE Clause
In the use of DELETE Be careful when you do . Because I didn't pay attention , Will delete the table by mistake
All right . Before using this statement , Please read this section completely .
Tips : DELETE And security
On the client side / Server's DBMS in , Use DELETE Statements may require special security
jurisdiction . In your use DELETE front , You should ensure that you have sufficient security permissions .
As I said before , UPDATE Very easy to use , and DELETE Easier to use .
The following statement starts from Customers Delete a row from the table :
Input ▼
DELETE FROM Customers
WHERE cust_id = 1000000006;
This sentence is easy to understand . DELETE FROM Request to specify the name of the table from which to delete data ,
WHERE Clause to filter the rows to be deleted . In this case , Only delete customers 1000000006 .
If omitted WHERE Clause , It will delete every customer in the table .
Tips : Friendly foreign keys
The first 12 This class introduces the connection , Simply joining two tables requires only the common fields in the two tables .
Can also let DBMS Strictly enforce relationships by using foreign keys ( These definitions are in Appendix A
in ). When there are foreign keys ,DBMS Use them to enforce referential integrity . For example, to
Products Insert a new product into the table ,DBMS Unknown suppliers are not allowed id
Insert it , because vend_id Columns are connected as foreign keys to Vendors Tabular . that ,
This is related to DELETE What does it matter ? One of the benefits of using foreign keys to ensure referential integrity is ,
DBMS It is usually possible to prevent the deletion of rows needed by a relationship . for example , From you to
Products Delete a product from the table , And this product is used in OrderItems Of has been booked
Single medium , that DELETE Statement will throw an error and abort . This is another way to always define foreign keys
One reason .
Tips : FROM keyword
In some SQL In the implementation , With the DELETE Keywords after FROM It's optional . however
Even if you don't need , It's also best to provide this keyword . Doing so will ensure that SQL Code in DBMS
Portable between .
DELETE No column names or wildcards are required . DELETE Delete entire rows instead of columns . To delete
Specified column , Please use UPDATE sentence .
explain : Delete the contents of the table instead of the table
DELETE Statement to delete a row from a table , Even delete all rows in the table . however , DELETE
Don't delete the table itself .
Tips : Faster delete
If you want to delete all rows from the table , Do not use DELETE . You can use TRUNCATE TABLE
sentence , It does the same thing , And faster ( Because changes in data are not recorded ).
16.3 Guidelines for updating and deleting
The first two sections use UPDATE and DELETE Every sentence has WHERE Clause , The reason for doing so
Very full . If you omit WHERE Clause , be UPDATE or DELETE Will be applied to the table
All the lines in . let me put it another way , If you execute UPDATE Without WHERE Clause , In the table
Each row will be updated with the new value . Similarly , If you execute DELETE Statement without WHERE
Clause , All data in the table will be deleted .
Here are many SQL Programmers use UPDATE or DELETE Important principles to follow when .
Unless you really intend to update and delete each row , Otherwise, never use without WHERE Clause
Of UPDATE or DELETE sentence .
Make sure that every table has a primary key ( If you forget this content , Please refer to the first 12 course ), As far as possible
image WHERE Use it like clause ( Each primary key can be specified 、 Multiple values or ranges of values ).
stay UPDATE or DELETE Statements use WHERE Before clause , You should use it first SELECT Into the
Take a test , Make sure it filters the correct records , In case of writing WHERE Incorrect clause .
Use databases that enforce referential integrity ( About this , Please refer to the first 12 course ),
such DBMS Rows whose data is associated with other tables will not be allowed to be deleted .
yes , we have DBMS Allow database administrators to impose constraints , Prevent execution without WHERE Clause
Of UPDATE or DELETE sentence . If the DBMS Support this feature , Should be
It's time to use it .
if SQL There is no cancellation (undo) Button , Should be used with great care UPDATE and DELETE ,
Otherwise you will find yourself updating or deleting the wrong data .
边栏推荐
- 小程序中读取腾讯文档的表格数据
- SQL必需掌握的100个重要知识点:组合查询
- Cp2112 teaching example of using USB to IIC communication
- 林克庆到番禺区调研“发展要安全”工作 以“时时放心不下”责任感抓好安全发展各项工作
- LVGL 8.2 Drop down in four directions
- China will force a unified charging interface. If Apple does not bow its head, iPhone will be kicked out of the Chinese market
- Pycharm项目使用pyinstalle打包过程中问题及解决方案
- LeetCode Algorithm 86. 分隔链表
- [rust daily] several new libraries were released on January 23, 2021
- 再测云原生数据库性能:PolarDB依旧最强,TDSQL-C、GaussDB变化不大
猜你喜欢

Matplotlib notes: contour & Contour

【STL源码剖析】容器(待补充)

第一届中国数字藏品大会即将召开

Anhui "requirements for design depth of Hefei fabricated building construction drawing review" was printed and distributed; Hebei Hengshui city adjusts the pre-sale license standard for prefabricated

China will force a unified charging interface. If Apple does not bow its head, iPhone will be kicked out of the Chinese market

电商两位大佬花边新闻刷屏,代表电商回归正常,将有利于实体经济

Deep dive kotlin synergy (16): Channel

中国将强制统一充电接口,苹果如不低头,iPhone将被踢出中国市场

Mysql database foundation: constraint and identification columns
![When does the database need to use the index [Hangzhou multi surveyors] [Hangzhou multi surveyors _ Wang Sir]](/img/2a/f07a7006e0259d78d046b30c761764.jpg)
When does the database need to use the index [Hangzhou multi surveyors] [Hangzhou multi surveyors _ Wang Sir]
随机推荐
从开源项目探讨“FPGA挖矿”的本质
19:00 p.m. tonight, knowledge empowerment phase 2 live broadcast - control panel interface design of openharmony smart home project
Time complexity and space complexity
LVGL 8.2 Image styling and offset
深潜Kotlin协程(十六):Channel
Problems and solutions in pyinstall packaging for pychart project
【无标题】
LVGL 8.2 Simple Image button
[STL source code analysis] iterator
LVGL 8.2 menu from a drop-down list
软件测试工程师面试基础题(应届生和测试小菜必备)最基础的面试题
From introduction to mastery of MySQL 50 lectures (32) -scylladb production environment cluster building
Dickinson's soul chooses its companion
CP2112使用USB转IIC通信教学示例
在 sCrypt 中实现高效的椭圆曲线点加法和乘法
List introduction
Review of mathematical knowledge: curve integral of the second type
File sharing server
智能DNA分子纳米机器人模型来了
数据库什么时候需要使用索引【杭州多测师】【杭州多测师_王sir】