Home >Database >Mysql Tutorial >sql 删除语句

sql 删除语句

WBOY
WBOYOriginal
2016-06-07 17:47:502631browse

sql 删除语句一般简单的删除数据记录用delete就行了,但是如何要删除复杂的外键就不是一条delete删除来实例的,我们本文章先讲一下delete删除,然后再告诉你利用触发器删除多条记录多个表。删除数据库中的数据

sql 删除语句
一般简单的删除数据记录用delete就行了,但是如何要删除复杂的外键就不是一条delete删除来实例的,我们本文章先讲一下delete删除,然后再告诉你利用触发器删除多条记录多个表。
删除中的数据
delete from 语句用于从数据库表中删除记录。


语法
delete from table_name
where column_name = some_value

*/
$con = _connect("localhost","peter","abc123");
if (!$con)
  {
  die('could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

mysql_query("delete from person where lastname='griffin'");

mysql_close($con);

/*

删除其中一张表中的"id=001"的记录后,
其他表中关于"id=001"的记录也自动删除

 

创建测试表,插入测试数据
create table t1([id] int)
create table t2([id] int)
create table t3([id] int)
create table t4([id] int)
insert t1
select 100
insert t2
select 100
insert t3
select 100
insert t4
select 100
 

--创建删除数据的触发器,在t1表上
go
 create   trigger del_data
on t1
for delete
as

declare @a int
select @a=[id] from deleted
 
delete t2 where [id]=@a
delete t3 where [id]=@a
delete t4 where [id]=@a

go
--删除t1表中的记录,那么其他表的记录都将删除
delete t1 where [id]=100
--看看各个表中的记录还存在否?
select * from t1
select * from t2
select * from t3
select * from t4
*/
?>

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
Previous article:创建表的sql语句Next article:sql如何创建临时表