6
I’m a beginner in mysql and can’t add a fk to a table I created. I have two tables (person, object) and I want to create a 'fk_person' in object that receives the value of the 'id' attribute of person. Tables are already configured to use the Innodb engine. I have typed the following:
ALTER TABLE objeto ADD CONSTRAINT fk_pessoa FOREIGN KEY(id) REFERENCES pessoa (id);
So give me back this:
ERROR 1072 (42000): Key column 'id' doesn’t exist in table
I tried to follow the pattern of this video: https://www.youtube.com/watch?v=lxUe5pttK5U "Creating Tables in Mysql 5 with Foreign Keys"
and also tried to follow the example of devmedia http://www.devmedia.com.br/criando-uma-chave-estrangeira-no-mysql/20299
My object table is like this:
id_objeto int(4) PRIMARY KEY auto_increment
fk_pessoa int(4) NOT NULL
My person table looks like this:
id int(4) PRIMARY KEY auto_increment
Thanks in advance
Does the id column exist in the object table? Post the structure of this ai table.
– user28595
actually 'id' only exists in the person table I even changed the first parentheses with (fk_person) but it returns me another error saying: "Cannot add Foreign key Constraint"
– Lucas_Menchone
See the answer below.
– user28595