Click to See Complete Forum and Search --> : MySQL and 0 for Auto_increment?!MySQL and 0 for Auto_increment?!


said_fox
07-21-2003, 09:56 PM
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.

The problem starts with the second record as follows

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.

The question is: How can I solve this problem in MySQL?
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

Nevermore
07-22-2003, 10:10 AM
Why do you need to start at 0?

said_fox
07-22-2003, 11:31 AM
Because the Artice Author, had built the application data base on this hyposis

Khalid Ali
07-22-2003, 03:27 PM
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.