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 -
CREATE TABLE `shoppingcart`.`sub_categories` (
-
-
`sub_category_id` INT NOT NULL ,
-
-
`parent_category_id` INT NOT NULL ,
-
-
PRIMARY KEY (`sub_category_id`) ,
-
-
INDEX `sub_categories_categories` (`sub_category_id` ASC, `parent_category_id` ASC) ,
-
-
CONSTRAINT `sub_categories_categories`
-
-
FOREIGN KEY (`sub_category_id` , `parent_category_id` )
-
-
REFERENCES `shoppingcart`.`categories` (`id` , `id` )
-
-
ON DELETE NO ACTION
-
-
ON UPDATE CASCADE)
-
-
ENGINE = InnoDB
-
-
-
So , what's the wrong ? am using MySQL 5.0.7
5 5465 Luuk 1,047
Expert 1GB
You need and index on categoried like this: - ALTER TABLE `test`.`categories` ADD INDEX (`id`, `id`);
But that is not allowed, because it produces this error: - ERROR 1060 (42S21): Duplicate column name 'id'
@Luuk
So ? actually i hear this before on Official MySQL Forums but without any benefit !
Luuk 1,047
Expert 1GB
I think you need to do it this way: - CREATE TABLE `test`.`sub_categories` (
-
`sub_category_id` INT NOT NULL ,
-
`parent_category_id` INT NOT NULL ,
-
PRIMARY KEY (`sub_category_id`,`parent_category_id`) ,
-
INDEX `sub_categories_categories` (`sub_category_id` ASC, `parent_category_id` ASC) ,
-
CONSTRAINT `sub_categories_categories`
-
FOREIGN KEY (`sub_category_id` )
-
REFERENCES `test`.`categories` (`id` )
-
ON DELETE NO ACTION
-
ON UPDATE CASCADE,
-
CONSTRAINT `parent_categories_categories`
-
FOREIGN KEY (`parent_category_id` )
-
REFERENCES `test`.`categories` (`id` )
-
ON DELETE NO ACTION
-
ON UPDATE CASCADE)
-
ENGINE = InnoDB
-
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 ;)
Luuk 1,047
Expert 1GB
I should have copied this: (especially when i noted the type on previous response.....) - CREATE TABLE `sub_categories` (
-
`sub_category_id` int(11) NOT NULL,
-
`parent_category_id` int(11) NOT NULL,
-
PRIMARY KEY (`sub_category_id`,`parent_category_id`),
-
KEY `sub_categories_categories` (`sub_category_id`,`parent_category_id`),
-
KEY `parent_categories_categories` (`parent_category_id`),
-
CONSTRAINT `parent_categories_categories`
-
FOREIGN KEY (`parent_category_id`)
-
REFERENCES `categories` (`id`)
-
ON DELETE NO ACTION
-
ON UPDATE CASCADE,
-
CONSTRAINT `sub_categories_categories`
-
FOREIGN KEY (`sub_category_id`)
-
REFERENCES `categories` (`id`)
-
ON DELETE NO ACTION
-
ON UPDATE CASCADE
-
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Sign in to post your reply or Sign up for a free account.
Similar topics
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
|
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...
|
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...
|
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...
|
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...
|
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
|
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..
|
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...
|
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.....
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
| |