SQL研習錄(06) 刪除資料

2021-10-01 19:48:25 字數 1512 閱讀 4709

我們通常通過使用sql中的delete語句來刪除表中的現有資料

首先我們準備一些測試資料,**如下:

create

table student(

s_id varchar(50

)primary

key,

s_name varchar(50

),s_gender varchar(50

))insert

into student(s_id,s_name,s_gender)

values

('s101'

,'lucy'

,'female');

insert

into student(s_id,s_name,s_gender)

values

('s102'

,'jack'

,'male');

insert

into student(s_id,s_name,s_gender)

values

('s103'

,'tom'

,'male');

insert

into student(s_id,s_name,s_gender)

values

('s104'

,'bruce'

,'male');

insert

into student(s_id,s_name,s_gender)

values

('s105'

,'jayce'

,'male'

);

delete

from table_name

[where condition]

;

注:

1、table_name是指要執行刪除操作的表

2、where子句為可選引數,用於指定刪除的條件

我們可以通過where子句限制刪除條件來在表中刪除部分資料,示例如下:

例如在測試資料中刪除s_id為『s105』的學生資料,**如下:

delete

from student where s_id=

's105'

;

與更新資料相似,若沒有where子句限制的話就會刪除全部記錄,示例如下:

如,刪除學生表中的全部記錄,**如下:

delete

from student;

我們還有一種方式就是通過truncate刪除所有的資料,它的語法如下:

truncate

table table_name;

例如,刪除學生表中的所有資料,**如下:

truncate

table student;

truncate和delete的區別:

sql 刪除資料

drop table student back 這樣就刪除表了,這樣的刪除不僅會刪除表中的資料,還包括表結構 字段 檢視 索引 觸發器和依賴的約束等等。此方法慎用!truncate table student back 這樣只是刪除表中的所有資料,會保留表結構 字段 約束 索引等等,但是不能加 wh...

SQL研習錄(26) 子查詢

子查詢 sub query 或者說內查詢 inner query 也可以稱作巢狀查詢 nested query 是一種巢狀在其他 sql 查詢的 where 子句中的查詢 子查詢用於為主查詢返回其所需資料,或者對檢索資料進行進一步的限制 子查詢可以在 select insert update 和 d...

用SQL刪除資料

使用 delete 命令可以 刪除資料,使用 truncate 命令可以刪除整表資料但保留結構。4.7.1 刪除記錄 在 命令編輯區 輸入 delete from scott.test where empno 7500 and empno 8000 然後單擊 執行 按鈕,出現如圖4.47所示的結果。...