1
CREATE TABLE admin_user (
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY
);

CREATE TABLE mappa (
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
id_utente INT(11) NOT NULL,
data_creazione timestamp NOT NULL default CURRENT_TIMESTAMP,
foreign key(id_utente) references admin_user(id) on delete cascade on update no action
);

The second table gives me this error:
Can't create table db.mappa (errno: 150 "Foreign key constraint is incorrectly formed")

What is wrong in the key syntax mysql reference?

Michael Armes
  • 1,056
  • 2
  • 17
  • 31

2 Answers2

0

You have an INT(11) UNSIGNED type in the admin_user table and INT(11) in the referrer. This doesn't work. Like MySQL documentation says:

The size and sign of integer types must be the same.

Make the id_utente column also UNSIGNED.

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
0

Your admin_user table id you assigned UNSIGNED value & Your reference table id_utente foreign key should be assign UNSIGNED.

Try this:

CREATE TABLE mappa (
   id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
   id_utente INT(11) UNSIGNED  NOT NULL,
   data_creazione timestamp NOT NULL default CURRENT_TIMESTAMP,
   FOREIGN KEY (id_utente) 
   REFERENCES admin_user(id) 
   ON UPDATE NO ACTION
   ON DELETE CASCADE
);