MySQL/SQL prevent deleting records that are use in another table -
the structure of 2 tables follows:
use exercise; create table companies ( id int, name varchar(100), city varchar(100), c_size varchar(100), primary key (id) ); insert companies(id, name, city, company_size) values (222, 'karma llc','city2', 'big'), (223, 'manny ind.','city1', 'medium'), (224, 'random plc','city1', 'medium'), (225, 'hijack llc','city1', 'medium'), (226, 'travels llc','city1', 'small'), (227, 'mirana ind.','city2', 'small'), (228, 'polla ind.','city3', 'small'), (229, 'americano llc','city3', 'small'), (230, 'macaroni llc','city4', 'small'); create table invoices ( id int auto_increment, `date` date, company int, amount decimal(7,2), primary key (id) ); insert invoices(`date`,company,amount) values ('2014-01-02', 222, 19500.00), ('2014-01-02', 222, 29205), ('2014-01-07', 222, 152.50), ('2014-01-07', 223, 6590), ('2014-01-07', 223, 4999), ('2014-01-09', 224, 720.60), ('2014-01-09', 225, 1100), ('2014-02-05', 226, 885), ('2014-02-05', 222, 9678.7);
as can see, company paid invoices, not. want know way prevent deleting companies, paid invoice. example, company americano llc (229) can deleted, karma llc not.
the answer may description how, not necessery query code.
i'm not sure if can done setting foreign key or trigger maybe else?.
sorry if spammed stackoverflow duplicate, i'm bit tired (2 a.m. here), can't sleep because of it. delete tomorrow if needed.
sorry poor english :(
foregin key deny deletion of row, used in other table. change company
foregin key of companies.id
Comments
Post a Comment