473,394 Members | 1,770 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,394 software developers and data experts.

Foreign key problem

samikhan83
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..
Aug 7 '08 #1
4 1467
Atli
5,058 Expert 4TB
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?
Aug 7 '08 #2
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.  
Aug 7 '08 #3
coolsti
310 100+
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.
Aug 8 '08 #4
Atli
5,058 Expert 4TB
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.
Aug 8 '08 #5

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

Similar topics

0
by: Jeremiah Jacks | last post by:
I just upgraded to MySQL 4.0.14-standard for RedHat Linux and am using = the pre-compiled binaries. I have a database with INNODB tables. When I insert a row into one of the child tables, I get...
1
by: Andrew DeFaria | last post by:
I created the following .sql file to demonstrate a problem I'm having. According to the manual: If |ON DELETE CASCADE| is specified, and a row in the parent table is deleted, then InnoDB...
2
by: Gunnar Vøyenli | last post by:
Hi! For the sake of simplicity, I have three tables, Employee, Department and Work Employee >---- Department \ / \ / ^ ^ Work
10
by: Bodza Bodza | last post by:
I'm having an argument with an incumbent self-taught programmer that it is OK to use null foreign keys in database design. My take is the whole point of a foreign key is that it's not supposed...
1
by: Jason Madison | last post by:
We sometimes get very large databases that we want to cut down to use for testing. The information is all related to a central accounts table. The way I thought of doing this is to grab all...
0
by: Scott Ribe | last post by:
I've got a problem which I think may be a bug in Postgres, but I wonder if I'm missing something. Two tables, A & B have foreign key relations to each other. A 3rd table C, inherits from A. A...
10
by: Shawn Chisholm | last post by:
Hi, I am trying to deal with a deadlock situation caused by foreign key references on insert and I was wondering if anyone knows what order the foreign keys are locked (or evaluated) in for a...
2
by: Ian Davies | last post by:
I have created a database with about 17 tables. I have been creating foreign keys some of which have worked but when creating others I get the message below ************************* 1005...
9
by: sonal | last post by:
Hi all, I hv started with python just recently... and have been assigned to make an utility which would be used for data validations... In short we take up various comma separated data files for...
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...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.