MySqlTalk.com  

Go Back   MySqlTalk.com > MySQL > Database Design
User Name
Password
FAQ Members List Calendar Search Today's Posts Mark Forums Read


Reply
 
Thread Tools Search this Thread
Old 12-13-2004, 09:27 PM   #1
flycaster
Junior Member
 
Join Date: Dec 2004
Posts: 3 flycaster is on a distinguished road
Multi-part auto_increment key

I am trying to create a table with a multi-part key of which one part is auto_increment - eg for an order entry system. I need the counter to rest when the other part of the key changes. No matter what I try, MySql (5.0) keeps incrementing the counter regardless of the other field. I have tried with MyIsam and InnoDB.

example:
DROP TABLE IF EXISTS order_detail;
CREATE TABLE order_detail (item smallint NOT NULL AUTO_INCREMENT,
invoice smallint NOT NULL,
description varchar(128),
PRIMARY KEY (item,invoice)) TYPE=InnoDB;


INSERT INTO order_detail VALUES ("","1","screwdriver"),
("","1","wrench"),
("","2","screws");

I should see this
item | invoice | description
1 1 screwdriver
2 1 wrench
1 2 screws

instead I get this
item | invoice | description
1 1 screwdriver
2 1 wrench
3 2 screws

Anybody have any ideas
Thanks
G
flycaster is offline   Fork this post Reply With Quote
Old 12-16-2004, 10:03 AM   #2
Vmusic
Junior Member
 
Join Date: Dec 2004
Posts: 11 Vmusic is on a distinguished road
Re: Multi-part auto_increment key -PLEASE DON'T

Hi,
A 'concatenated' key is a BAD BAD BAD design idea.

Here's how invoices and items work.

An invoice is for one or many items.
An item (spec or type) can be on one or many invoices.

If you create an invoice table as a parent, and invoice_item table as its child you won't have this problem.

The key to the invoice table can be an auto increment.
The key to the item table is can be two columns. The first column is the parent invoice, the second item is simply the line item or invoice item number.

So the first item on invoice #234 has the columns #234 and 1
the second item on invoice #234 has the columns #234 and 2, etc.
You would generate the invoice item number (or line item) from your front end or.... you can select the max item number from the invoice item table where the invoice number = 'x'

You can/should also create a table of 'items'. This is what I referred to as type or spec. This is a simple listing of the 'items' you sell or buy.


Concatenated Keys are a bad, bad thing. It stops you from adapting to change. You write code based on that concatenation and then you're hosed.

Good design Saves Time!!
Vmusic
Vmusic is offline   Fork this post Reply With Quote
Old 12-16-2004, 10:53 AM   #3
flycaster
Junior Member
 
Join Date: Dec 2004
Posts: 3 flycaster is on a distinguished road
Re: Multi-part auto_increment key

Thanks for the suggestion. I do have two tables and was trying to do what you suggested automatically, rather than manually determine the next line number. I will give that a try. Thanks again.

Flycaster
flycaster is offline   Fork this post Reply With Quote
Old 12-16-2004, 11:26 AM   #4
Vmusic
Junior Member
 
Join Date: Dec 2004
Posts: 11 Vmusic is on a distinguished road
Re: Multi-part auto_increment key -

Flycaster,
You really have two choices:
1) Create a key that is two columns, column being the invoice key and column two being a line item number

2) Create a unique id for each row in the invoice item table regardless of the inovice number. Your invoice item table would be something like

invoice_item_id, display_order, invoice_id, quanity, unit_price, total_price, desc

In this case only the first column, invoice_item_id would be the key.


The problem here is that you would most likely want to keep track of which item was first, second, third, etc anyway for display purposes and to make it easy for the customer or user to identify their order or invoice items.

So if you're going through all of the that, why not choose the first choice above.?




To obtain a simple number for each unique invoice I would suggest
a) SELECT MAX idea to get the next item
b) CODE - Assuming you're using PHP


You can somehow 'reset' autoincrement. But the invoice item, or item number is NOT really unique by itself.

Glad to hear you're not concatenating two separate data elements into one column.

Vmsuic,
Good Design, Saves Time
Vmusic is offline   Fork this post Reply With Quote
Old 12-16-2004, 12:55 PM   #5
flycaster
Junior Member
 
Join Date: Dec 2004
Posts: 3 flycaster is on a distinguished road
Re: Multi-part auto_increment key

It seems like I have a few options worth persuing.
Thanks again for the advice.
Flycaster
flycaster is offline   Fork this post Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multi OS & vmware eNzr Installation 0 01-17-2005 04:53 AM
How do you populate one field (if empty) with part of another? Azkaban SQL syntax 0 12-18-2004 04:57 PM
How do you do a select for the rightmost part of a word? AmberLinkin SQL syntax 1 09-08-2004 05:09 AM



All times are GMT -4. The time now is 08:37 PM.



Powered by: vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Limited.
Google
  Web http://www.mysqltalk.com
DISCLAIMERS:
1. We have no commercial interest in this site.
Banner Ads and Subscriptions will only be used to help pay for hosting and maintenance costs.
2. MySQLTalk.com is NOT affiliated with MySQL AB in any way.
3. MySQLTalk.com is NOT endorsed by MySQL AB in any way.
4. Please do not post any content that is harmful to MySQL or MySQL AB, meaning no misleading or obsolete information will be tolerated.
Well-founded constructive criticism meant to help the community is permitted.
5. This website is founded with the goal of improving the MySQL community.
We not only tolerate newbies, we encourage them.
Please do not ask newbies to "read the manual".