PDA

View Full Version : How to DELETE a row from 2 tables?



tnonline
30-09-2003, 07:31
Dear guys,

How to DELETE a row from 2 tables?

For one table, I can use SQL statement like this:

sql = "DELETE FROM Table1 WHERE Field1 = 'abc'"

Now I have 2 tables:
Table 1: includes 1 Primary Key and a Foreign Key (used to link to Table 2), and some other fields;
Table 2: includes a Primary Key (link to Table 1) and some other fields.

I want to DELETE a row from Table 1 based on Primary Key, and the coresponding row in Table 2 based on Foreign Key.

Any body know how to write a SQL statement for this? Please help me.

Thank a lot.

ITCS
02-10-2003, 07:54
Ban chiu kho hoc them hoac viet cau hoi bang tieng Viet thi se nhan duoc cau tra loi som hon!
(Mac du ai cung co the doc tieng anh, nhung van thay ngai khi nhin thay bai cua ban).

kiddy
18-04-2008, 13:32
I'm sorry I don't know

temp2
18-04-2008, 13:51
SELECT from one table then delete both?

tom2
18-04-2008, 13:54
Dear guys,

How to DELETE a row from 2 tables?

For one table, I can use SQL statement like this:

sql = "DELETE FROM Table1 WHERE Field1 = 'abc'"

Now I have 2 tables:
Table 1: includes 1 Primary Key and a Foreign Key (used to link to Table 2), and some other fields;
Table 2: includes a Primary Key (link to Table 1) and some other fields.

I want to DELETE a row from Table 1 based on Primary Key, and the coresponding row in Table 2 based on Foreign Key.

Any body know how to write a SQL statement for this? Please help me.

Thank a lot.
Delete from table1 ...
Delete from table2 ...

vtnphong
18-04-2008, 14:09
hình như phải delete from table2 trước, nếu ko nó bị đứt link

minhpn
18-04-2008, 16:49
dùng khoá có thuộc tính là DELETE CASCADE là OK: Khi xoá 1 bản ghi ở bảng chính (có primary key) thì bảng có Foreign Key sẽ tự động bị xoá theo.

alone_night
14-06-2008, 10:00
Ban chiu kho hoc them hoac viet cau hoi bang tieng Viet thi se nhan duoc cau tra loi som hon!
(Mac du ai cung co the doc tieng anh, nhung van thay ngai khi nhin thay bai cua ban).

Đúng lắm!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!