1 (edited by v_pozidis 2017-02-19 16:24:23)

Topic: delete record with realtionship between table

Hi, i like to delete a record from a tablegrid but I got the message in the picture. The table has a relationship with a second table. When I delete the records from the second table then I can delete the record from the first table. How can I avoid it so when I need to delete the record it will do the job ?

Post's attachments

Attachment icon Capture.PNG 8.44 kb, 205 downloads since 2017-02-19 

Re: delete record with realtionship between table

Hello.


You can not delete record, if this record have relationship from another table.


For example, you cannot delete an employee record from the Employees table if there are orders assigned to that employee in the Orders table. You can, however, choose to delete a primary record and all related records in one operation by selecting the Cascade Delete options for foreign key.

http://myvisualdatabase.com/help_en/database/relationship.png

Dmitry.

Re: delete record with realtionship between table

ok and one more question, before this message comes out how can I put instead this message (picture ) a  ShowMessage('Deleting this record you can loose other informations')  ?

Re: delete record with realtionship between table

If you enable Cascade delete, you will not to get a message, like in the first your post.


procedure Form1_bDelete_OnClick (Sender: string; var Cancel: boolean);
begin
    if MessageDlg('Deleting this record you can loose other informations', mtConfirmation, mbYes+mbNo, 0) = mrNo then
    begin
        Cancel := True;
    end;
end;
Dmitry.

Re: delete record with realtionship between table

Yes thank you again.

6 (edited by v_pozidis 2017-02-22 07:15:08)

Re: delete record with realtionship between table

I have test it it works,
but how can I just have a messagebox that it will say a message that this record is related in another table. What I want to do is in the following example : I have 2 tables (Stoixeia and Ypothesi  with relationship)  if  the  Stoixeia.id  e includes in the other table Ypothesi (Ypothesi.id_Stoixeia)  it sould give me a message
I try using the followin script but nothing. (I know that my SQL knowledge is very bad)


if  SQLExecute('Select id from Stoixeia Where id =  Ypothesi_id.Stoixeia') then
begin
showmessage('aaaaaaaaaaaa');
end;

Please your help.

Re: delete record with realtionship between table

v_pozidis wrote:

I have test it it works,
but how can I just have a messagebox that it will say a message that this record is related in another table. What I want to do is in the following example : I have 2 tables (Stoixeia and Ypothesi  with relationship)  if  the  Stoixeia.id  e includes in the other table Ypothesi (Ypothesi.id_Stoixeia)  it sould give me a message
I try using the followin script but nothing. (I know that my SQL knowledge is very bad)


if  SQLExecute('Select id from Stoixeia Where id =  Ypothesi_id.Stoixeia') then
begin
showmessage('aaaaaaaaaaaa');
end;

Please your help.


if  SQLExecute('Select Count(id) from Stoixeia Where Stoixeia.id = Ypothesi.id_Stoixeia') > 0 then
begin
showmessage('aaaaaaaaaaaa');
end;

Re: delete record with realtionship between table

bemorhona-qt wrote:
v_pozidis wrote:

I have test it it works,
but how can I just have a messagebox that it will say a message that this record is related in another table. What I want to do is in the following example : I have 2 tables (Stoixeia and Ypothesi  with relationship)  if  the  Stoixeia.id  e includes in the other table Ypothesi (Ypothesi.id_Stoixeia)  it sould give me a message
I try using the followin script but nothing. (I know that my SQL knowledge is very bad)


if  SQLExecute('Select id from Stoixeia Where id =  Ypothesi_id.Stoixeia') then
begin
showmessage('aaaaaaaaaaaa');
end;

Please your help.


if  SQLExecute('Select Count(id) from Stoixeia Where Stoixeia.id = Ypothesi.id_Stoixeia') > 0 then
begin
showmessage('aaaaaaaaaaaa');
end;

no dosen't work

Re: delete record with realtionship between table

attach your project

10 (edited by v_pozidis 2017-02-22 08:59:09)

Re: delete record with realtionship between table

ok here is my example. from the first screen press the first button and from there you will go to another screen. try to press the bib button .When pressin. it it should show a message that if the record you have choose with that lastname it is used in another table. Try the lastname Billy.

Post's attachments

Attachment icon Test.zip 328.9 kb, 460 downloads since 2017-02-22 

Re: delete record with realtionship between table

v_pozidis wrote:

ok here is my example. from the first screen press the first button and from there you will go to another screen. try to press the bib button .When pressin. it it should show a message that if the record you have choose with that lastname it is used in another table. Try the lastname Billy.

Check it!

Post's attachments

Attachment icon Test_fixed.zip 8.62 kb, 485 downloads since 2017-02-22 

Re: delete record with realtionship between table

bemorhona-qt wrote:
v_pozidis wrote:

ok here is my example. from the first screen press the first button and from there you will go to another screen. try to press the bib button .When pressin. it it should show a message that if the record you have choose with that lastname it is used in another table. Try the lastname Billy.

Check it!


Yes!!!!!!!!! Thank you very much for your help.