/    Sign up×
Community /Pin to ProfileBookmark

AUTO_INCREMENT MYsql?!

Hello,
I have a table in a database which handles products’ data. This table have many columns, two of them are the problem. The first one is [i]

product_id[/i] column and the second is [i]barcode[/i] column. On the application side, the user has the choice to make the barcode for the

product equals to the product_id value or not. The [i]product_id[/i] column is the primary key and it is [b]Auto Incremented[/b]. The [i]

barcode[/i] column is integer.
The problem is:
When I want to make the value [i]barcode[/i] column equals to the value of [i]product_id[/i] as follows,

[code]
INSERT INTO products (product_id, barcode) VALUES (“”,product_id);
[/code]

The value assigned to the [i]barcode[/i] = 0. This may due to that the primary key is an index and it is processed at later step of the query

after the [i]barcode[/i].
However, I used [b]mysql_insert_id()[/b] to get the last value of the auto_increment field, then by UPDATE clause I assign the value I want

to the barcode, [i]i.e barcode value = product_id value[/i]
The real problem is:
In a real world web application, I think, [b]mysql_insert_id()[/b] may return a wrong value or 0, this due to many users and clients

probably using the application at the same time, i.e the same table ([i]products[/i]). The manual of MySQL tell us the follwoing fact:-

[QUOTE]

The most recently generated ID is maintained in the server on a per-connection basis. It will not be changed by [b]another client[/b]. It will not

even be changed if you update another AUTO_INCREMENT column with a non-magic value (that is, a value that is not NULL and not 0).

[/QUOTE]

The question is : Is everyone (every web page’s browser to the application) using the web application regarded as single client? Or the

application itself (Website or PHP parser) regrded as the client?
If the answer to the second question is yes, what would I can to do to complete my task.
Best Regards.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@chazzyDec 27.2005 — mysql_insert_id() should never return a false result, since it's bound to the current connection, UNLESS, you close the connection too early.

However, maybe you can rethink your model a bit? Maybe you have a different flag for barcode and use CASE logic in your select statements to get the product_id if the barcode is a certain flag, otherwise, return the value of barcode.
Copy linkTweet thisAlerts:
@Reli4ntDec 28.2005 — mysql_insert_id won't be a problem here but I'm curious to know, if the user has some control over the barcode, then how do you ensure that the barcode remains unique?
Copy linkTweet thisAlerts:
@said_foxauthorDec 29.2005 — Hi,

It is easy to ensure that the barcode field will be unique. The database itself will refuse inserting a repeated value for the barcode field, it is set to be unique. So if the user decided to make it custom, he must insert or invent a code that it has never inserted befor. In the following Quote I will supply you with the structure of the produts table, note the bold words:-

CREATE TABLE item (

item_id bigint(13) unsigned zerofill NOT NULL auto_increment,

[B]item_code[/B] bigint(13) unsigned zerofill default NULL,

item_title varchar(55) NOT NULL default '',

item_description text NOT NULL,

item_price float NOT NULL default '0',

item_qty int(5) NOT NULL default '0',

item_unit varchar(16) NOT NULL default '',

PRIMARY KEY (item_id),

UNIQUE KEY item_title (item_title),

[B]UNIQUE KEY item_code (item_code)[/B]

) TYPE=MyISAM
[/Quote]
Copy linkTweet thisAlerts:
@chazzyDec 29.2005 — you should never rely on just yoru database to force something to be correct, it should be handled twice - the database and the application. I think you should think about what I mentioned as far as not recording bar codes. Basically, by having two options either you can enter your own barcode or use the given ID as the bar code, you're breaking a seemingly fine logical stance. By setting it up this way, you can't really change what the bar code value is, or further more you can't change it from it's ID to a new code. You're making bar code sort of like another primary key.
Copy linkTweet thisAlerts:
@said_foxauthorJan 02.2006 — Ok,

You should notice that the final result of the data set in the regarded table will not contain any empty barcode fields. I just made user's life easier, if he decide to make the item's barcode as the auto-generated ID and also if he decided to invent his own. In addition I made the application to deal with items separetely with the ID and the application regarded the barcode value as any data related with the item like price or title.

I understod from your reply that you encorage to cancel the Id or the barcode and make one of them is the primary key. I prefer to make the application user more free with less efforts from his side and what I did, as I think, is the best to achieve this goal.
×

Success!

Help @said_fox spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...