Sqlserver 增刪改查 刪

2022-03-14 23:28:59 字數 3587 閱讀 7251

--我們就以院系,班級,學生來舉例。

create table [dbo].yuanxi

( id

int identity(1,1) not null,--學校id 自增量

yuanxiname varchar(

50) null, --院系名字

)create table [dbo].class

( id

int identity(1,1) not null,--班級id 自增量

yuanxiid

intnull,--院系id

classname varchar(

50) null --班級名字

)create table [dbo].student

( id

int identity(1,1) not null,--學生id 自增量

classid

intnull,--班級id

studentname varchar(

50) null,--學生姓名

)--這個表是表,我臨時加的,後面可能用這個演示

create table [dbo].pathimg

(

id int identity(1,1) not null,--學校id 自增量

tablename varchar(

50) null, --這個是表明,比如我要在資訊工程學院**,那就要在改表中把xuanxi表的表名子加上,還有資訊工程學院的id加上

tablenameid

intnull,--這個就是某乙個表中的某乙個id

path varchar(

50) null, --存放班級**路徑的

)

其實刪和插入操作差不多。

我們就簡單的說常用且簡單的刪除:

1,刪除表中所有資料:

delete from yuanxi where

1=1

2,根據條件刪除表中某些資料

delete from yuanxi where yuanxiname=傳入的引數
接下來,就是涉及多表資料刪除的問題了。注意:在實際開發中在執行刪除操作的時候,一定要遵循乙個原則,不要留尾巴,也就是在刪除某一條資料的時候,一定要把和這條資料所有有牽扯並且不影響其他資料的資料全部刪除,如果不這樣做,其他表中會留有殘餘,也就是資料垃圾,不利於後續專案的維護。所以,在執行刪除操作的時候,往往你要刪除一條資料,大多都會牽扯的幾個表的資料刪除。

這裡我也要說一下,刪除的時候也要考慮級別,   就以上面四個表為例:如果我要刪除yuanxi表中的資訊工程學院,那麼我就把和資訊工程學院有關的所有資料都要刪除,班級表中的資訊工程學院的班級,student表中班級下的所有學生,還有通用**中以上三個表中所有資料路徑都刪除。但是如果我要刪除class表中多**,這時,我就不能刪除yuanxi表中資訊工程學院了,如果我要刪除資訊工程學院那麼,class表中電腦科學與技術就會變得無用了。這樣就會出現資料錯誤,影響平台的正常顯示了。這時你只能刪除,學生表中在資訊工程學院下的所有學生,和通用表中的關於班級路徑和學生路徑。

下面我就演示一下刪除學院,也是最多表的刪除資料的操作了,當然是針對上面四個表來說的:

@yuanxiid:傳入的yuanxi表中的id引數,下面寫儲存過程要用,所以上面直接寫成這個形式了。

--//第一步:

delete from yuanxi where id=@yuanxiid--先刪除院系

delete fom pathimg where tablename='yuanxi' and tablenameid=@yuanxiid --刪除通用表中路徑

--//第二步:注意:這裡如果你直接刪除class表中的資料,那student表中的資料就沒法刪除了,因為與student表中關聯的class中的id已經被刪除,所以這時候應該這樣做

//先刪除與班級關聯的sudent表中學生和pathimg路徑

這裡要逆序刪除,不然會出現找不到關聯表id的情況了。

先在pathimg表中刪除關於學生的路徑id

delete from pathimg where tablename=』student' and id in(select id from student where id in(select id from class where yuanxiid=@yuanxiid))

-- /(2)/刪除student表中學生id

delete from student where id in(select id from class where yuanxiid=@yuanxiid)

-- //(3)刪除class表中班級id

delete from class where yuanxiid=@yuanxiid

--//(4)刪除pathimg表中班級路徑id 

delete from pathimg where tablename='class' and tablenameid in (select id from class where yuanxiid=@yuanxiid)

--注意:(1)和(2)必須要在(3)和(4)的前面執行,(1)必須要在(2)前面執行。這樣才能把資料刪除乾淨。

use

[testdata]go

set ansi_nulls on

goset quoted_identifier on

goalter

procedure

[dbo

].[deleteyuanxi

]@yuanxiid

int--

表示yuanxi表中的id列的引數變數

asdelete

from yuanxi where id=

@yuanxiid

--先刪除院系

delete from pathimg where tablename=

'yuanxi

'and tablenameid=

@yuanxiid

--刪除通用表中路徑

delete

from pathimg where tablename=』student'

and id in(select id from student where id in(select id from class where yuanxiid=@yuanxiid))--刪除pathimg表中學生路徑

delete from student where id in(select id from class where yuanxiid=@yuanxiid)--刪除student表中學生id

delete from class where yuanxiid=@yuanxiid--刪除class表中班級id

delete from pathimg where tablename=

'class'

and tablenameid in (select id from class where yuanxiid=@yuanxiid)--刪除pathimg表中班級路徑

現在看來,刪除操作並不像我們想的那麼簡單了。

SQL Server 增刪改查基礎

主鍵為自增時可不填寫 插入單條資料 insert into product values 008 原子筆 辦公用品 100,null,2019 11 11 insert into product select 008 原子筆 辦公用品 100,null,2019 11 11 插入多條資料 inser...

Sql server 實現增刪改查

vs2008 c sql server csharp view plain copy using system.data.sqlclient csharp view plain copy sqlconnection conn csharp view plain copy 連線資料庫 private ...

Sqlserver 增刪改查 改

我們就以院系,班級,學生來舉例。create table dbo yuanxi id int identity 1,1 not null,學校id 自增量 yuanxiname varchar 50 null,院系名字 create table dbo class id int identity 1...