Database Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesDatabase Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old July 22nd, 2003, 02:05 AM
said_fox said_fox is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Egypt
Posts: 5 said_fox User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

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



Reply With Quote
  #2  
Old July 22nd, 2003, 12:38 PM
Matt Matt is offline
Contributing User
Codewalkers Specialist (4000 - 4499 posts)
 
Join Date: Apr 2007
Location: Florida
Posts: 4,158 Matt User rank is Private First Class (20 - 50 Reputation Level)Matt User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 h 12 m 16 sec
Reputation Power: 7
RE: MySQL and 0 for Auto_increment?!

Auto incrementing columns are to be used to have a unique identifier. Another problem you are going to run into is that if you ever delete the last item in the table, mysql will NOT reassign that value. It will skip over it.

This is by design. To do what you want, I would suggest not using auto incrementing values, and just manually assign the id, starting with 0. Then when you insert a new row, find the max value and insert one higher.

Reply With Quote
  #3  
Old July 22nd, 2003, 01:48 PM
zombie zombie is offline
Codewalkers Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2007
Location: serbia
Posts: 1,876 zombie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 4
RE: MySQL and 0 for Auto_increment?!

yes. mysql autoincrement field cant have a value of 0.

if you write a query like

INSERT INTO table SET id=0, name='blah'

that is the same as writting

INSERT INTO table SET name='blah'

(without the id field), so then mysql sets id field to the next autoincrement value...


there is no way around that..

Reply With Quote
  #4  
Old July 22nd, 2003, 03:46 PM
Ashkhan Ashkhan is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 372 Ashkhan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 24 m 57 sec
Reputation Power: 3
RE: MySQL and 0 for Auto_increment?!

I'm sorry for that mistake. I believed my table has auto_increment id column with id=0. Which was true but it was not auto_increment.

Reply With Quote
Reply

Viewing: Codewalkers ForumsOther TechnologiesDatabase Help > MySQL and 0 for Auto_increment?!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 9 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek