473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

error 150: Can't create table

Hi

Am trying to create a table called (sub_categories). This table have two columns :

sub_category_id: Primary (int)
parent_category id: not null (int)

Both tables reference the column (id) from categories table. When i try to creating the table it gives me the error :

ERROR 1005: Can't create table 'shoppingcart.sub_categories' (errno: 150)

Here is the SQL syntax
Expand|Select|Wrap|Line Numbers
  1. CREATE  TABLE `shoppingcart`.`sub_categories` (
  2.  
  3.   `sub_category_id` INT NOT NULL ,
  4.  
  5.   `parent_category_id` INT NOT NULL ,
  6.  
  7.   PRIMARY KEY (`sub_category_id`) ,
  8.  
  9.   INDEX `sub_categories_categories` (`sub_category_id` ASC, `parent_category_id` ASC) ,
  10.  
  11.   CONSTRAINT `sub_categories_categories`
  12.  
  13.     FOREIGN KEY (`sub_category_id` , `parent_category_id` )
  14.  
  15.     REFERENCES `shoppingcart`.`categories` (`id` , `id` )
  16.  
  17.     ON DELETE NO ACTION
  18.  
  19.     ON UPDATE CASCADE)
  20.  
  21. ENGINE = InnoDB
  22.  
  23.  
  24.  
So , what's the wrong ? am using MySQL 5.0.7
May 28 '12 #1
5 5465
Luuk
1,047 Expert 1GB
You need and index on categoried like this:
Expand|Select|Wrap|Line Numbers
  1.  ALTER TABLE `test`.`categories` ADD INDEX (`id`, `id`);
But that is not allowed, because it produces this error:
Expand|Select|Wrap|Line Numbers
  1. ERROR 1060 (42S21): Duplicate column name 'id'
May 28 '12 #2
@Luuk
So ? actually i hear this before on Official MySQL Forums but without any benefit !
May 28 '12 #3
Luuk
1,047 Expert 1GB
I think you need to do it this way:
Expand|Select|Wrap|Line Numbers
  1. CREATE  TABLE `test`.`sub_categories` ( 
  2.   `sub_category_id` INT NOT NULL , 
  3.   `parent_category_id` INT NOT NULL , 
  4.   PRIMARY KEY (`sub_category_id`,`parent_category_id`) , 
  5.   INDEX `sub_categories_categories` (`sub_category_id` ASC, `parent_category_id` ASC) , 
  6.   CONSTRAINT `sub_categories_categories` 
  7.     FOREIGN KEY (`sub_category_id` ) 
  8.     REFERENCES `test`.`categories` (`id` ) 
  9.     ON DELETE NO ACTION 
  10.     ON UPDATE CASCADE,
  11.   CONSTRAINT `parent_categories_categories` 
  12.     FOREIGN KEY (`parent_category_id` ) 
  13.     REFERENCES `test`.`categories` (`id` ) 
  14.     ON DELETE NO ACTION 
  15.     ON UPDATE CASCADE) 
  16. ENGINE = InnoDB 
  17.  
But, of course, i'm not sure because i dont have enough info on what you exacly want to do, and my experiance with foreign key is not that deep ;)
May 28 '12 #4
It doesn't work ! Unfo.
May 28 '12 #5
Luuk
1,047 Expert 1GB
I should have copied this: (especially when i noted the type on previous response.....)

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `sub_categories` (
  2.   `sub_category_id` int(11) NOT NULL,
  3.   `parent_category_id` int(11) NOT NULL,
  4.   PRIMARY KEY (`sub_category_id`,`parent_category_id`),
  5.   KEY `sub_categories_categories` (`sub_category_id`,`parent_category_id`),
  6.   KEY `parent_categories_categories` (`parent_category_id`),
  7.   CONSTRAINT `parent_categories_categories` 
  8.     FOREIGN KEY (`parent_category_id`) 
  9.     REFERENCES `categories` (`id`) 
  10.     ON DELETE NO ACTION 
  11.     ON UPDATE CASCADE,
  12.   CONSTRAINT `sub_categories_categories` 
  13.     FOREIGN KEY (`sub_category_id`) 
  14.     REFERENCES `categories` (`id`) 
  15.     ON DELETE NO ACTION 
  16.     ON UPDATE CASCADE
  17. ) ENGINE=InnoDB DEFAULT CHARSET=utf8
May 28 '12 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Morten Gulbrandsen | last post by:
USE company; DROP TABLE IF EXISTS EMPLOYEE; CREATE TABLE EMPLOYEE ( # PK SSN CHAR(9) NOT NULL, # FK SUPERSSN CHAR(9), DNO INT NOT NULL DEFAULT 1, CONSTRAINT EMPPK
0
by: CoOL! . | last post by:
Hello, I found the key to solve this problem in: http://darkstar.ist.utl.pt/mysql/doc/en/InnoDB_foreign_key_constraints.html You'll probably need an INDEX for that new foreign key you are...
0
by: NW | last post by:
Hi I had error message "Can't create table *.frm (errno:121)" when I was trying to create an innoDB table in MySQL Server 4.0.15. I know that the reason is the .frm files was deleted by some one...
0
by: lynn | last post by:
Hello everybody, I am having difficulties: I am working on Mac OSX 10.2.8 with mysql 4. I needed to change the data directory of MySQL from its original place (where the application have to be...
2
by: blumi | last post by:
I get this error when I try to create the following tables: ERROR 1005 (HY000): Can't create table '.\haps\transcript.frm' (errno: 150) create table teaching ( ProfId int, CrsCode...
0
by: bnthorat80 | last post by:
How can i create table in runtime in window application in visual studio 2005 in vb.net on click event of button. plz help
6
by: daveftl | last post by:
Good day to all...i always have this error after making an ALTER command ... #1005 - Can't create table '.\_ftl\#sql-918_f.frm' (errno: 150) whats the cause of this error...thanks in advance..
2
by: jarea | last post by:
I have read quite a bit about this error but I have yet to find the solution to my problem. I am trying to execute the following mysql statement: alter table line_items add...
2
by: RoshiniKamath | last post by:
Errror Code:1005 Can't create table '.\test\#sql-580_1.frm' (errno: 150)..... I got this errror when i tried to create a foregin key.......Please help me out on this error.....
3
by: SteveP26 | last post by:
Hi guys, I keep getting this error message (below) when I try to run the sql query for my database Heres the code, but the error message only applies to the LAST table (SalesCopy), I have no...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.