Difficulty making Foreign key with two char fields in Mysql

Asked

Viewed 105 times

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?

  • The database is Mysql

  • 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, Unfortunately no. Or char or varchar, because the data that will enter are unique, but are alphanumeric.

  • This table already has records?

  • The table hardware already, the locality not yet.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.