What happens is that I am trying to create a table in phpMyadmin where I must place a foreign key that is the primary key of muncipio but I get this error #1072 - The key column 'idMunicipio' does not exist in the table, and it turns out that the table is called municipality like this:
and it really is a primary key
I try to enter the sentence like this:
CREATE TABLE clientes(
idClientes INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(idClientes),
ccCliente INT,
Nombres VARCHAR(50),
Apellidos varchar (50),
Email varchar(50),
Telefono varchar(50),
fechaRegistro date,
Usuario varchar(50),
Password varchar(50),
FOREIGN KEY(idMunicipio)REFERENCES municipio(idMunicipio))
but I get the error, I had even made tables with a foreign key and it worked for me, I did it with the example that I had and it didn't give me the system either?
I think you need to define the field
idMunicipio
in your table you only make the reference to the tablemunicipio
Example:
The detail of the error is that:
Your foreign key
idMunicipio
must be explicitly declared on the entityclientes
so that when you try to establish the link between it andMunicipios
it can be constructed.You should proceed like this:
The Municipalities entity must have a primary key ( you already have this when you declared it
idMunicipio
)The entity
clientes
must have a column that will serve as a foreign key that we can callmunicipio_id
and that must be of the same data type and length as the primary call mentioned in the previous point ( this is not done yet ), so right after where you declare the columnpassword
you must put the followingLater you create the constraint to join both tables declaring the foreign key ( this is already done )
Optionally:
I recommend you declare the constraint with a name so that later it is easier for you to identify which are the linked entities in it, it could look like this:
Finally being like this:
Reference