Home >Database >SQL >How to delete data using delete statement

How to delete data using delete statement

藏色散人
藏色散人Original
2020-05-26 10:49:0716536browse

How to delete data using delete statement

How to use delete statement to delete data?

Create a temporary table to demonstrate the use of delete in sqlserver syntax

Recommended: "SQL Tutorial"

IF OBJECT_ID('tempdb..#tblDelete') IS NOT NULL DROP TABLE #tblDelete; 
CREATE TABLE #tblDelete(
    Code varchar(50),        
    Total int    
);

How to delete data using delete statement

Insert a few test rows into the temporary table #tblDelete to demonstrate how to delete data

insert into #tblDelete(Code, Total) values('Code1', 30);
insert into #tblDelete(Code, Total) values('Code2', 40);
insert into #tblDelete(Code, Total) values('Code3', 50);
insert into #tblDelete(Code, Total) values('Code4', 6);

How to delete data using delete statement

Query the temporary table#tblDelete Test data in

select * from #tblDelete;

How to delete data using delete statement

Delete the record with Code field = Code3 in temporary table #tblDelete, use the following delete statement

delete #tblDelete where Code = 'Code3'

How to delete data using delete statement

Query the temporary table again#tblDelete results, you can see that the record of Code3 is gone

select * from #tblDelete;

How to delete data using delete statement

Delete the temporary table#tblDelete in the Code field =Code2 To record, use the delete statement below.

Note that there is a from keyword after the delete statement below. This keyword can be omitted, but it is recommended not to omit it

delete from #tblDelete where Code = 'Code2'

How to delete data using delete statement

Query temporarily again As a result of table #tblDelete, you can see that the record of Code2 is gone

select * from #tblDelete;

How to delete data using delete statement

Finally, if you want to quickly delete all the data in the table, there are two ways below. The second way is faster

delete from #tblDelete;
truncate table #tblDelete;

How to delete data using delete statement

The above is the detailed content of How to delete data using delete statement. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn