SQL的更新和刪除

2022-05-02 14:36:11 字數 1488 閱讀 5320

1、sql更新資料

示例:

update

table_name

set some_column =

'some_value

',other_column =

'other_value

'where unique_column =

'unique_value

'

上述示例示更新指定行,若想更新所有的行,那麼去掉where子句即可。為了資料的安全性,最好是新增where子句。

--

update與select結合使用

update

table_name

set (column1, column2, column3) = (select col1, col2 from

other_table_name

where some_col =

'some_value')

where some_column =

'other_value';

--update與from結合使用(要看dbms是否支援)

update table_name as

t1set col1 = t2.column1, col2 =

t2.column2

from other_table_name as

t2where t1.some_col =

t2.some_col;

--update與join結合使用

update t1 inner

join t2 on t1.col1 =

t2.col1

set t1.some_col =

'some_col_value',

t2.other_col ='

other_col_value

'where t1.condition_col =

'condition_value';

update t1 left

join t2 on t1.col1 =

t2.col1

set t1.some_col =

'some_col_value',

t2.other_col ='

other_col_value

'where t1.condition_col =

'condition_value

';

2、sql刪除資料

示例:

delete

from

table_name

where some_col =

'some_value

';

delete語句不需要指出列名或者萬用字元,若沒有指定where關鍵字,那麼是刪除所有的行。其實,若想刪除表中所有行,不要使用delete,可以使用truncate table語句,它完成相同的工作,並且速度要快,因為它不記錄資料的變動。

3、sql更新和刪除操作原則

sql更新和刪除

update 語句 update 語句用於修改表中的資料。語法 update 表名稱 set 列名稱 新值 where 列名稱 某值 person lastname firstname address city gates bill xuanwumen 10 beijing wilson champ...

SQL級聯更新和級聯刪除

alter table 表名 add constraint 約束名 foreign key 欄位名 references 主表名 欄位名 on delete cascade 語法 foreign key column n references referenced table name ref co...

SQL學習(2) 插入,更新和刪除

一 資料插入insert 1.簡單插入 insert into aa values a b null 有幾個值就必須寫幾個值 insert into aa id,name,code values a b null 可以插入特定列的值,使用這個語法要求,這些被忽略的特定列是可以為null,或者有預設值...