Manru
Manru

Reputation: 43

Foreign key constraint is incorrectly formed when I create a table

I have two SQL query's, when I write the second query it drops a SQL error, like this "Foreign key constraint is incorrectly formed". And I don't know, the first table have ID with primary key.

CREATE TABLE clients(
    id_client INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(32),
    surname VARCHAR(32),
    dni VARCHAR(32),
    address VARCHAR(32),
    type CHAR
)

And the other:

CREATE TABLE clients_vehicles(
    id_client_vehicle INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    license_plate VARCHAR(32),
    num_chasis VARCHAR(32),
    color VARCHAR(32),
    brand VARCHAR(32),
    model VARCHAR(32),
    model_version VARCHAR(32),
    fuel_type CHAR,
    km INT,
    cv INT,
    type CHAR,
    id_client INT NOT NULL,
    FOREIGN KEY (id_client) REFERENCES clients(id_client)
)

Upvotes: 0

Views: 26

Answers (1)

Derviş Kayımbaşıoğlu
Derviş Kayımbaşıoğlu

Reputation: 30545

clients_vehicles.id_client is INT whereas clients.id_client is UNSIGNED INT. There is type mismatch.

change UNSIGNED INT to INT and it will work

Upvotes: 1

Related Questions