daniel__
daniel__

Reputation: 11845

foreign key problem - mysql

i have this mysql code, but when i delete an element of agrupamento, the child is not deleted in emprego, and it is supposed.

For example i have in agrupamento - id - 1 || area - science

and i have in profissao (job) - ref - 1 || empregoid - 1 || emprego - scientist

 CREATE TABLE agrupamento (  
        id smallint(5) unsigned NOT NULL auto_increment,  
        area varchar(30),  
        PRIMARY KEY (id) 
    )

CREATE TABLE emprego (  
    ref int(10) unsigned NOT NULL auto_increment,  
    empregoid smallint(5) unsigned NOT NULL,  
    emprego varchar(50),  
    PRIMARY KEY (ref)  
)

    ALTER TABLE emprego  
    ADD CONSTRAINT FK_emprego
    FOREIGN KEY (empregoid) REFERENCES agrupamento(id)  
    ON UPDATE CASCADE  
    ON DELETE CASCADE;  

what is the problem?

(if the area science doesn't exists, the scientist also not.)

thanks!

Upvotes: 1

Views: 169

Answers (1)

devasia2112
devasia2112

Reputation: 6044

What is the kind of table that are you using in your database, certify that the table is innoDB to use FK. In myIsam tables it will not work properly.

Upvotes: 2

Related Questions