Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Foreign Key to existing table

I want to add a Foreign Key to a table called "katalog".

ALTER TABLE katalog  ADD CONSTRAINT `fk_katalog_sprache`  FOREIGN KEY (`Sprache`) REFERENCES `Sprache` (`ID`) ON DELETE SET NULL ON UPDATE SET NULL; 

When I try to do this, I get this error message:

Error Code: 1005. Can't create table 'mytable.#sql-7fb1_7d3a' (errno: 150) 

Error in INNODB Status:

120405 14:02:57 Error in foreign key constraint of table mytable.#sql-7fb1_7d3a:

FOREIGN KEY (`Sprache`) REFERENCES `Sprache` (`ID`) ON DELETE SET NULL ON UPDATE SET NULL: Cannot resolve table name close to: (`ID`) ON DELETE SET NULL ON UPDATE SET NULL 

When i use this query it works, but with wrong "on delete" action:

ALTER TABLE `katalog`  ADD FOREIGN KEY (`Sprache` ) REFERENCES `sprache` (`ID` ) 

Both tables are InnoDB and both fields are "INT(11) not null". I'm using MySQL 5.1.61. Trying to fire this ALTER Query with MySQL Workbench (newest) on a MacBook Pro.

Table Create Statements:

CREATE TABLE `katalog` ( `ID` int(11) unsigned NOT NULL AUTO_INCREMENT, `Name` varchar(50) COLLATE utf8_unicode_ci NOT NULL, `AnzahlSeiten` int(4) unsigned NOT NULL, `Sprache` int(11) NOT NULL, PRIMARY KEY (`ID`), UNIQUE KEY `katalogname_uq` (`Name`)  ) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=DYNAMIC$$  CREATE TABLE `sprache` ( `ID` int(11) NOT NULL AUTO_INCREMENT,  `Bezeichnung` varchar(45) NOT NULL,  PRIMARY KEY (`ID`),  UNIQUE KEY `Bezeichnung_UNIQUE` (`Bezeichnung`), KEY `ix_sprache_id` (`ID`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 
like image 629
frgtv10 Avatar asked Apr 05 '12 12:04

frgtv10


People also ask

How to add a foreign key to a column in SQL?

If you want to add a FOREIGN KEY to the column into the SQL table, you have to follow the below steps in the given sequence: Create the database in the system. Create two tables in the same database. View Table structure before foreign key addition. Add a foreign key to the table.

How to add a foreign key (grade_ID) to an existing table?

To add a foreign key (grade_id) to an existing table (users), follow the following steps: ALTER TABLE users ADD grade_id SMALLINT UNSIGNED NOT NULL DEFAULT 0; ALTER TABLE users ADD CONSTRAINT fk_grade_id FOREIGN KEY (grade_id) REFERENCES grades(id);

What is a foreign key in access?

A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in another table. The table containing the foreign key is called the child table, and the table containing the candidate key is called the referenced or parent table.

When adding foreign key constraints to a table using ALTER TABLE?

When you add a foreign key constraint to a table using ALTER TABLE, remember to create the required indexes first. Show activity on this post. Show activity on this post. Show activity on this post.


1 Answers

To add a foreign key (grade_id) to an existing table (users), follow the following steps:

ALTER TABLE users ADD grade_id SMALLINT UNSIGNED NOT NULL DEFAULT 0; ALTER TABLE users ADD CONSTRAINT fk_grade_id FOREIGN KEY (grade_id) REFERENCES grades(id); 
like image 54
iltaf khalid Avatar answered Oct 03 '22 02:10

iltaf khalid