Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old August 7th, 2008, 05:27 PM
samikhan83's Avatar
Member
 
Join Date: Sep 2007
Posts: 32
Default Foreign key problem

hi...
i am having a problem assigning a foreign to a table....

i have two tables product table which is having two primary key and quantity table which have quant_poduct_upc_code

1. product_id which is of integer type and auto increment

2. product_upc_code which is of varchar type with size 40

what i want to do is reference product_upc_code of product with the quantity product_upc_code

both the fields are of same type and size i.e. varchar(40) but mysql is throwing an error saying error 1005 which is index not found...

is this anything to do with the two primary keys in one table.......

can anybody help me its URGENT.....

Thanx in Adavance..
Reply
  #2  
Old August 7th, 2008, 07:20 PM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Age: 22
Posts: 2,790
Default

Hi.

Might be just me, but I didn't really understand your explanation of you table structure.
Could you post the CREATE statements you are using?
Reply
  #3  
Old August 7th, 2008, 08:25 PM
samikhan83's Avatar
Member
 
Join Date: Sep 2007
Posts: 32
Default

hi...

this is the product table

Expand|Select|Wrap|Line Numbers
  1.  
  2. DROP TABLE IF EXISTS `product`;
  3. CREATE TABLE `product` (
  4.   `p_id` int(10) unsigned NOT NULL auto_increment,
  5.   `p_manufacturer` varchar(60) NOT NULL,
  6.   `p_name` varchar(60) NOT NULL,
  7.   `p_upc_code` varchar(40) NOT NULL,
  8.   `p_model_number` varchar(40) NOT NULL,
  9.   `p_description` varchar(150) NOT NULL,
  10.   `p_price_a` int(10) unsigned NOT NULL,
  11.   `p_price_b` int(10) unsigned NOT NULL,
  12.   `p_price_c` int(10) unsigned NOT NULL,
  13.   `p_serial_number` varchar(5) NOT NULL,
  14.   `p_status` varchar(20) NOT NULL default 'ACTIVATED',
  15.   `p_type_id` int(10) unsigned NOT NULL,
  16.   `p_group_id` int(10) unsigned NOT NULL,
  17.   `p_category_id` int(10) unsigned NOT NULL,
  18.   PRIMARY KEY  (`p_id`,`p_upc_code`),
  19.   KEY `FK_product_1` (`p_type_id`),
  20.   KEY `FK_product_2` (`p_group_id`),
  21.   KEY `FK_product_3` (`p_category_id`),
  22.   CONSTRAINT `FK_product_1` FOREIGN KEY (`p_type_id`) REFERENCES `product_type` (`p_t_id`),
  23.   CONSTRAINT `FK_product_2` FOREIGN KEY (`p_group_id`) REFERENCES `product_group` (`p_g_id`),
  24.   CONSTRAINT `FK_product_3` FOREIGN KEY (`p_category_id`) REFERENCES `category_group` (`cat_grp_id`)
  25. ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
  26.  
  27.  
this is second table

where i can't make relation with the UPC_CODE field of product with the UPC_CODE field of this table


Expand|Select|Wrap|Line Numbers
  1.  
  2. DROP TABLE IF EXISTS `quantity`;
  3. CREATE TABLE `quantity` (
  4.   `quant_id` int(10) unsigned NOT NULL auto_increment,
  5.   `quant_upc_code` varchar(40) NOT NULL,
  6.   `quant` int(10) unsigned NOT NULL,
  7.   `quant_date` datetime NOT NULL,
  8.   `quant_timestamp` int(10) unsigned NOT NULL,
  9.   PRIMARY KEY  (`quant_id`)
  10. ) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=latin1;
  11.  
  12.  
  13.  

Last edited by Atli; August 8th, 2008 at 03:46 PM. Reason: Fixed the [code] tags.
Reply
  #4  
Old August 8th, 2008, 07:29 AM
Needs Regular Fix
 
Join Date: Mar 2008
Posts: 305
Default

Do you really want to have the two-column primary key in your first table?

Firstly, your first column is integer auto_increment and will be unique, so this alone is sufficient to serve as a primary key, and also as a unique foreign key to other tables.

Secondly, your second column in your primary key is of type varchar(40). This makes for a far less efficient index in general (since it is of type varchar which is text, is longer and is of variable length).

I would change things so that the primary key in the first table is only the first column. If you need an index on the varchar field to speed up queries, you could also add that.
Reply
  #5  
Old August 8th, 2008, 03:51 PM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Age: 22
Posts: 2,790
Default

I would have to agree with that.

There is no point having a joined Primary Key unless you want to be able to create multiple rows with the same p_upc_code.

I would make the p_id the Primary Key and the p_upc_code Unique, and then reference the p_id in the second table, rather than the upc_code, just to save space.
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles