Alessio.Bussolari
Alessio.Bussolari

Reputation: 63

SQL Syntax error foreign key

I've a problem, i try to connect 2 db table, and if I make the query for make a DB structure the answer is Syntax Error.

I paste the code here, somebody can help me ?

DROP TABLE IF EXISTS album;
CREATE TABLE `album` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(30) DEFAULT NULL,
  `description` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=armscii8;

DROP TABLE IF EXISTS picture;
CREATE TABLE `picture` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `id_album` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`id_album`) REFERENCES album(id)
) ENGINE=InnoDB DEFAULT CHARSET=armscii8;

sorry for my bad english.

Upvotes: 0

Views: 159

Answers (2)

ASNAOUI Ayoub
ASNAOUI Ayoub

Reputation: 472

Just add Unsigned in Foreign key line as follows :

DROP TABLE IF EXISTS album;
CREATE TABLE `album` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(30) DEFAULT NULL,
  `description` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=armscii8;

DROP TABLE IF EXISTS picture;
CREATE TABLE `picture` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `id_album` int(11) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`id_album`) REFERENCES album(id)
) ENGINE=InnoDB DEFAULT CHARSET=armscii8;

Upvotes: 0

Jens
Jens

Reputation: 69440

Think your id_album must have bean unsigned, Same as id in album

Upvotes: 1

Related Questions