1. 程式人生 > 其它 >15.SQL必知必會第16課 更新刪除資料(update & delete & 更新刪除許可權)

15.SQL必知必會第16課 更新刪除資料(update & delete & 更新刪除許可權)

技術標籤:sql必知必會資料庫sqlmysql

一、知識

update

更新語法如下:

update 表名 set   列名 = 欲更新的資料1,
				 列名 =欲更新的資料2,
				 列名=欲更新的資料3,
where  +限制條件

舉一例書上的例子

update tyqsl2.customers 
set cust_email = '[email protected]',
	cust_contact= 'martin'
where cust_id = 1000000005

在這裡插入圖片描述

限制條件很重要,如果沒加限制條件,該語句會修改對應列的所有行。

mysql更新刪除許可權

workbench環境:

我們在更新刪除時候,有時候會報 erro code 1046的錯
在這裡插入圖片描述
這是提醒我們安全許可權不夠,需要設定:

set sql_safe_updates=0 //關安全模式以更新刪除資料
set sql_safe_updates=1 //開啟安全模式,不能更新刪除資料

delete(行刪除)

delete只能以行為單位刪除。欲刪除最後一行

在這裡插入圖片描述

delete from tyqsl2.customers
where cust_name = 'toolman' //刪除工具人為名的行

在這裡插入圖片描述

刪除某行某列單一資料

實質是通過update把該值置為null
在這裡插入圖片描述

update  tyqsl2.customers
 set cust_email =
null where cust_name = 'toolman'

在這裡插入圖片描述

二、課後習題

1.題目要求把州轉為大寫,但是資料都是大寫,所以,我做的操作是,先把資料update成小寫,再update成大寫。

pdate tyqsl2.customers set cust_state 
= lower(cust_state);
update tyqsl2.vendors set vend_state 
= lower(vend_state);

在這裡插入圖片描述

pdate tyqsl2.customers set cust_state 
= upper(cust_state);
update tyqsl2.vendors set
vend_state = upper(vend_state);

在這裡插入圖片描述
2.

delete from tyqsl2.customers 
where cust_id = 121041201