Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HOW TO DELETE TWO LINKED ROW FROM MYSQL DATABASE USING DELPHI ???

Status
Not open for further replies.

havsys

Programmer
Jul 23, 2012
5
RS
Hello,

Can somebody show my how to delete two linked row using delphi.

I Use delphi 2007 and MySql.

In MySql i have database with two Table. Table Contacts(ID, FirstName, LastName, Phone, PICID) and table Picture(ID, IMG)
MySql Tables:
Code:
CREATE TABLE `Picture`.`Picture` (
  `ID` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  `IMG` LONGBLOB,
  PRIMARY KEY (`ID`)
)
ENGINE = InnoDB;

Code:
CREATE TABLE `contacts` (
  `ID` int(10) unsigned NOT NULL auto_increment,
  `FirstName` varchar(45) NOT NULL,
  `LastName` varchar(45) NOT NULL,
  `Phone` varchar(45) default NULL,
  `PICID` int(10) unsigned default NULL,
  PRIMARY KEY  (`ID`),
  KEY `FK_contacts_1` (`PICID`),
  CONSTRAINT `FK_contacts_1` FOREIGN KEY (`PICID`) REFERENCES `picture` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

In my Delphi Application I have Delete Button. When I find some Contact on my DBGrid and press DELETE Button I can delete only contact from table Contact, I aslo want to delete contact picture.

I want to delete row from table CONTACTS and row from Table PICTURE. Table Picture Is linked to table Contact With foreign key.
 
Hi,

I have no experience with MySQL, but I think you could activate the action Cascade in the foreign key to automatically delete the Picture when delete a Contact.

If you really want to delete by the application, then you can use code in BeforeDelete event of Contacts DataSet to delete the picture.

Hope this helps.

[URL unfurl="true"]http://www.imoveisemexposicao.com.br/imoveis-venda-são_paulo-residencial-apartamento[/url]
 
Hello imex,

I have small problem with deleting row from two table. Can you help me with this.

I put code in BeforeDelete event of Contacts DataSet to delete the picture how you tell me.
I can delete Row but I always delete the last insert row.

if I want to delete contact with ID=7(from table Contacts) I will delete Contact Picture with ID=15 (from table Picture).

ADODataSet for table Contact is my "TContacts"
ADODataSet for Table Picture is my "TPicture"
Data modul is my "dm"

For delete contacts I use this code: dm.TContacts.Delete; - works fine
For BeforeDelete event of Contacts DataSet(TContacts) to delete the picture I use this code: dm.TPicture.Delete - But I delete only last inserted row.
Table Contacts and table Picture are conected with FOREIGN KEY. table CONTACTS(PICID)-->(ID) table PICTURE.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top