/    Sign up×
Community /Pin to ProfileBookmark

MySQL and 0 for Auto_increment?!MySQL and 0 for Auto_increment?!

Suppose the following table:

CREATE TABLE categories (
id int auto_increment not null,
parent_id int not null,
name varchar(25) not null,
description varchar(255) not null,
PRIMARY KEY (id),
INDEX parent_id (parent_id),
INDEX name (name)
);

I want The first record “id” field to take a value = 0. However, MySQL do something else, it
assign the value = 1 to it.
INSERT INTO categories (id, parent_id, name,description)
VALUES (0, 0, ‘Top’, ‘This is the top level category.’);

Ok, I update the value of “id” to be set as 0 as follows

UPDATE categories SET id = 0 WHERE id = 1;

Wow, That’s so nice.

[b]The problem starts with the second record as follows[/b]

INSERT INTO categories (name, description)
VALUES (‘Fruits’, ‘Fresh and tasty fruits.’);

MySQL seems to have very strong memory ! It can not forget that the the first record “id” field
has the value 1, on the other hand it forgets it becomes 0!. The second record “id” field value
takes the value 2, inspite of I respect it to be 1.

[b] The question is: How can I solve this problem in MySQL?[/b]
Notice: The solution I respect should not need any use of MySQL function.

I have MySQL Ver 4.0.12 on windows ME.
This situation is from a tutorial named as Building an E-Commerce Site Part 1: Building a
Product Catalog
By Ying Zhang
On Developer Shed website.

Please Help Quickly

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NevermoreJul 22.2003 — Why do you need to start at 0?
Copy linkTweet thisAlerts:
@said_foxauthorJul 22.2003 — Because the Artice Author, had built the application data base on this hyposis
Copy linkTweet thisAlerts:
@Khalid_AliJul 22.2003 — just ommit the id from the sql query that creates the first record and it will start from 0.....so if you did this

INSERT INTO categories (parent_id, name,description)

VALUES (0, 0, 'Top', 'This is the top level category.');

it should start from z if this is the first record...but you should be aware of the fact that when you use auto increment,and if you delete a record it will skip that value which is deleted and the next value will be which it should have been as if the record was not deleted..I hope it made sense.
×

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.18,
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,
)...