0
I am unable to create a Foreign key between two tables, whose type is CHAR
:
ALTER TABLE hardware ADD CONSTRAINT fk_INC_LOCALIDADE FOREIGN KEY ( INC_LOCALIDADE ) REFERENCES localidade (INC);
The mistake is:
ALTER TABLE hardware ADD CONSTRAINT fk_INC_LOCALIDADE FOREIGN KEY ( INC_LOCALIDADE ) REFERENCES locality (INC); failed : Cannot add or update a Child Row: a Foreign key Constraint fails
I gave one show full columns
and the result is this:
LOCALITY TABLE:
Field Type Collation Null Key Default Privileges
INC char(11) utf8_general_ci NO PRI A select,insert,update,references
HARDWARE TABLE:
Field Type Collation Null Key Default Privileges
INC_LOCALIDADE char(11) utf8_general_ci NO A select,insert,update,references
The fields have to be CHAR
, when I change to INT
works, but the data will come in CHAR
.
What is the database?
– Reginaldo Rigo
The database is Mysql
– Anderson Fidelis
the data will come in CHAR? Have you how to convert to INT before recording? If yes, use int, your bank will thank for the fact that indexing is infinitely better.
– Angelo Simonato
@Angelo Simonato, Unfortunately no. Or char or varchar, because the data that will enter are unique, but are alphanumeric.
– Anderson Fidelis
This table already has records?
– Neto Costa
The table hardware already, the locality not yet.
– Anderson Fidelis