|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hi, i have a problem with creating a foreign key in table departments.
here's how i created the tables: create table customer( custid int not null auto_increment primary key, --------more fields------ salesman varchar(30) ) type=innodb; create table departments( departid int not null auto_increment, cust_id int not null, -----fields--------- department varchar(30), primary key (departid), foreign key (cust_id) references customer(custid) on delete restrict ) type=innodb; relation between 2 tables is 1 to many (and not many to many). And I don't know if table departments needs 2 primary keys(departid and cust_id) the error i get is "ERROR 1005: Can't create table '.\crm\departments.frm' (errno: 150)". can someone help me with this problem? to your help is greatly appreciated. thanks. |
|
#2
|
|||
|
|||
|
Ok, the 2nd table needs to have an index on the referencing column as well.This can be part of the primary key, or a seperate one.See example:
CREATE TABLE parent(id INT NOT NULL, PRIMARY KEY (id) ) TYPE=INNODB; CREATE TABLE child(id INT, parent_id INT, INDEX par_ind (parent_id), FOREIGN KEY (parent_id) REFERENCES parent(id) ON DELETE CASCADE ) TYPE=INNODB; So, add the INDEX customer_id(cust_id) before your FOREIGN KEY and it should work. Mind you: I didn't test this. Hope this helps, Angela |
![]() |
| Viewing: Dev Articles Community Forums > Databases > MySQL Development > errno:150 |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|