Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    MYSQL




Auto-increment And Update/insert


I have the following problem with a MySQL update/insert.

my problem lies in the fact that I have to insert a new row between 2 existing rows. the table is sorted by ID and has ID as auto-increment so I cannot insert a new row between 2 rows as inserting will assign the insert a new ID number...

I figured now I have to do an update and then loop till the end of the table and then do an insert at the end..




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
How Do I Insert An Auto Increment ID Into Other Tables
I have been trying to insert An auto_increment ID of one table into other tables and I am getting wierd results. I am trying to insert the CLIENT table primary key into the other three tables and it is inserted correctly into the CONTACTS table but the JOBSHEET and KNOWLEDGE tables both insert 1 into the clientID field. Code:

Increment Of Auto Increment Is Not Consistent
I have a table with one the the column set as auto increment. MY storage engine is innodb. The problem is the my increment of the auto increment is not consistent at all it was growing from 1 till 6 then suddenly it went to 802 ? Can some one guide how to make sure its increment is one by one ? Thanks.

Increment Column Without Auto Increment
What's the fastest way I can use an INSERT statement to insert a new record including an 'ID' which is one greater than the current highest ID in the table.

Is this possible without first doing a SELECT query? I'll be using php too btw, so if there's a way to do this in php, then I'm open to such suggestions also.

Auto Increment
In my database, Ive used auto increment for IDs:-
create table member (ID INT(4) NOT NULL AUTO_INCREMENT,
etc;
When you insert values do you leave this empty, eg:-
insert into member values (' ','etc');
Could you please help and tell me how auto increment works?


Auto Increment
I can make tables all nice and fine until I try to use auto-increment!

Here is a copy of my table code:

Auto Increment Always Add A Value Different?
Is it possible to make the Auto Increment always add a value different then 1 to the new register?
For example, I have my auto Increment setup for 71 and I want the second new register to be 75 and not 72. The table would be:

ID Name
71 Alex
75 John
79 Peter

etc.

Auto Increment On The Fly...
Have a query:
SELECT sum(Sales)AS TotSales, SalesManager
FROM Data
WHERE Region = 'East'
GROUP BY SalesManager
ORDER BY TotSales DESC

Is there a way of creating an autoincrement field

SELECT sum(Sales)AS TotSales, SalesManager, something like
...autoincrement( xxx) AS Rank
FROM Data
WHERE Region = 'East'
GROUP BY SalesManager
ORDER BY TotSales DESC

100 John 1
75 Mary 2
50 Ted 3

etc....

Auto Increment
I have a table that has a number of columns, but the only column that uniquely identifies a row is the datetime. So that needs to be a primary key because I can't have any duplicates in the datetime column.

However, one of the columns also needs to be an id to be referenced as a foreign key. So naturally I think to make this an auto-increment integer, but it appears any auto-increment column needs to be a primary key as well.

Having both be primary keys obviously won't give me datetime uniqueness, so now I don't know what to do. The only thing I can think of is to make the datetime the column to be referenced as a foreign key, but all sorts of alarms are going off having a datetime as a foreign key.

Auto-increment?
I've got:

entry_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,

and... I wanted to use it to number entries but the problem is that if i delete an entry lets say No4 from the table the next entry won't be No4 but No5 but it's not what I need because i need the number to be consequant 1,2,3,4,5,.... and not 1,2,3,5,...

How To Get The NEXT Auto Increment Value
How do I get the next insert value for an autoincrement column ? I know SHOW TABLE STATUS will work but how can I narrow down the result set ?? How can I just get what I want and not all the other information it returns. I have a program where I need to know the value before I insert a row.

Auto Increment In Sql
I have just started using MySQL and I was trying to set up a table and set the type of one of the fields to auto increment. I would like it so that when I add a new record to the particular table, the ID field takes the next value in the sequence.
Could anyone please tell me how this would be done?

Auto Increment
In my database, Ive used auto increment for IDs:-
create table member (ID INT(4) NOT NULL AUTO_INCREMENT,
etc;
When you insert values do you leave this empty, eg:-
insert into member values (' ','etc');
Could you please help and tell me how auto increment works?

Auto Increment
Has anyone ever heard of problems with an auto-increment field giving duplicate numbers to 2 threads that might hit the DB at exactly the same time?

The reason why I ask is because during our transition from MySQL to Oracle, the Oracle guys here said that we had to create a SEQUENCE object in Oracle to ensure that this didn't happen.

Now that we're back to MySQL, I just want to make sure this isn't an issue with an AUTO_INCREMENT field.

Auto Increment
I am trying to return a set of stats on player points (goals + assists). So the player with the most points shows up at the top of the results as I have ORDER BY TotalPoints DESC;

I'd like to have a column on the left side of the table that starts at 1 and increments by 1 for each subsequent row. That column would be call something like Standing or Points Position. Basically the top points player would have a 1 beside their row, the second place person would have a 2 and so on. Not sure how to get those numbers in there though. Can you add an auto increment field to a SELECT statement?

Points Position, Player, Total Points
1, Brad, 10
2, John, 8
3, Billy, 7

Get Auto-Increment
I have a database with a primary key which is also the auto_increment field, this works fine, but I need to know the auto_increment value that MySQL holds internally, the LAST_INSERT_ID() function is no good for my case since I need this value to be inserted and not after, also tried the "SELECT MAX(fld_HAM_ID) FROM tbl_HAM;" this gives the last assigned value, I thought great I just need to add 1 to this and it's done, but no, if some records are deleted at the end of the RS then it would give me value 13 I add 1 = 14 but the auto_increment is going to be 16, this is what I need to know, the increment value that MySQL holds internallly.

Auto ID Increment
I have a table with an Id number that auto increments
and I use the GUI SQLyog. I accidentally jumped ahead my Id number for one entry and when I changed it to the correct one, it now automatically starts with that higher number (and then one higher) for every new entry I make.

Auto Increment
I need to create a DB in MySQL with an id field that autoincrements. However, the first record is to start at 09100, then next is 09101, 09102, 09103 etc etc.

Is there any way to make the field begin at 09100 instead of 0?

Auto Increment
Q1) Since we all know that if we create an auto increment column, it will start from the number '1' but is there a way we can force it to start with the number '300' and it should increment to 301 on the next new record inserted then 302 and so on?
Q2) Is there a way to make the auto increment field prefixed by 000? For example instead of making it run from the number '1' it should auto increment in this method '001' then 002 and so on?

Auto-Increment
I am aware of how to create an auto-increment field but I was wondering if it is possible to create a custom auto increment id using both Alpha and Numeric (ex. A001423)?
If so, can you have the Alpha character roll-over sequentially (A999999 to B000000)?

Auto Increment
I have a quick question, at least I think I do.
I know that you can only use Auto_Increment on one column and it must be a primary key.
I have a second column where I want to increase the value of the new record by 1 based on the value of the previous record.

Let's say my table currently has five records. The table has two columns ID and Book_ID. ID is primary and set to auto_increment. In record 5 the Book_ID has a value of 10. When I insert a new record, the ID column will be incremented to 6 and I want the Book_ID to be increased to 11. Taking the value of 10 from record 5 and adding 1 to record 6.

Auto Increment
I have a field
Quote: customer_id int(11) unsigned auto_increment primary key
Auto increment was working properly until 9. Then for the next record the value is 10531.
It is a MyISAM table.
No changes have been made to the table structure. What could be the reason for this behaviour?

Auto Increment
I am working with a table which relates image id numbers to the file name. The image_id is set to auto increment which is good but the images are often deleted along with their record in the table. This leave a lot of unused image_id's.
My question is will MySQL ever go back and use image_id's that have all ready been used but have been deleted or do I have to make the data type large enough to account for all the image_id's that might ever occor.
I ask this because there will never be more the a medium int worth of images uploaded at one time but over a year or so more then a medium int may end up being uploaded and then deleted and I am worring about running out of auto increment room.

Auto Increment
I am using an auto incrementing field.
INT Type,

If I delete ALL records using SQL delete statement, how can I just reset the auto incrementing field to start back over at 1?

Auto-increment
I have a simple table that represents users of a system. There is a userID which i set to auto increment when creating the table. It works perfectly for creating new users, however if i remove user entrys the next time a user is created it registers the user as though the deleted user still exists.
Say for instance there is 4 users with USER IDs being 1,2,3,4.
Then i delete user 4.
Then at somepoint create another user i would like them to come in at userID 4. However it is currently adding them at userID 5.
Is there a way around this??


Using Auto Increment
for tables with huge numbers of rows like logs is it possible and what happens if the value increments to a point that it exceeds the max limit say an UNSIGNED INT 2^32 = 4 billion, especially if you delete rows the next counting number still increments from the number of the last row you have deleted, meaning even if you have a small data, as time goes by, as deleting and inserting rows, the auto increment id field will eventually hit the max?

Auto Increment Value
I want to have auto increment value start from 2000000 (an arbitrary number) instead of default 1 for first row.I want to do it before inserting any data into the respective table.can i do something while creating the table ?.Anybody has any idea how to do this

Auto Increment Value
If a primary key is defined as auto increment and a row is deleted and the server restarts, is it possible that Innodb assigns the deleted value to a newly inserted entry?

Set Auto Increment
Is there a way to tell MySQL to start at 'number' instead of starting from 0? I need my rows to start at 7156 and auto increment from there.

Get The Present Auto Increment Value
I know how to get the last present id useing a simple SQL satament

SELECT id FROM table ORDER BY id desc LIMIT 0, 1
but what i want is the next auto increment value that will be added, so i need to know a way of getting the present or next value.
anyone got any pointers

Auto Increment Hassle
Say I have a table with an auto incrementing primary key with row values
1
2
3
4

If I delete record 4, then insert a new record, the rows have values
1
2
3
5

I dont like this! I need it to insert 4 again! Surely there is an easy way to sort this!

Pulling Auto Increment Value
I am using this query to put data into my tables. The null value is an auto_incremented column.

$query = 'insert into round_info values (null,'.$course_id.',1,'.$gross_score.');

Is there a code that allows me to pull the value that it automatically increments without doing another query?

Different Values From Auto Increment
I run a query, looking for a value in a non-increment column. I then do an insert and the auto_increment field gets inserted with the next value + 1 from the key field in the query result. OK so far. When I query again for a different value in the non-increment field and then do an insert, the auto_increment field gets inserted with the next value + 1 from this result set, not necessarily the highest value in the dataset.
I am checking the values with:

$query = "SELECT LAST_INSERT_ID()";
$result = mysql_query($query);

Why would an auto_increment field give me a lower, than a higher, than a lower value. I have many years exp with databases, but new to MySQL.

Retrieving Last Auto-increment
I've got a project where I'm adding records to a database, and the primary key is autogenerated (pretty standard). What I want to do is retrieve the id of the last record added to the database, so I can use it in another part of the script.

I'm currently using a select statement, but there has to be a way of doing it without another query.

Catch The Auto-increment ID
How can I catch an auto-increment ID in a table with a SQL querie?

For example:

...ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=20 ;

and I wan´t select the 20. How can I do this??

Mysql Auto Increment
I have a member table that stores information of my user such as password,username,address,etc. Currently i have about 250k registered user(So thats 250k rows). The table has been running smoothly and querying to this table has been pretty fast.

The table doesn't have an auto increment field before and i just assign the primary key to the username. Now, i need to add another auto increment field to it. But after i've done that, the database becomes significantly slower. Querying is super slow and sometimes when i try to browse the data from phpmyadmin or navicat, the database just hangs. As a result i have to restart mysql.

This problem only occurs after i add the auto increment field. I am very sure. Because after i remove it, everything goes back to normal...

Auto-increment Limit?
I have a database which holds the name and location of reports stored on a system.  Each one has an auto incrementing primary key.  Throughout the day, a crontab entry "freshens" the list by basically adding new report entries to the database.  At night, I flush the database by deleting all of the entries and rebuilding the table.  This ensures database integrity so that I don't have to worry about a report having been renamed, moved, or deleted.

There are now a bunch of reports in the database and being that it is flushed daily, the primary keys are getting high.  Is there any problem if the primary key increments too high (is there an upper limit where I will get an error)?  Does it roll over? 

Auto Increment Limitation
I made a table in a DB with an auto_increment row (id), and gave it the type 'INT(4)', after the maximum (127) was reached, I couldn't put anymore data in this table. Of course, I can give the id-row a bigger type for example BIGINT, so the max. is a 19 digit number instead of a 3-digit one, but like this, there's always a limit, no?

Is there any solution for that or do I just have to restart the database ever before reaching the maximum number?

Auto Increment Problem
I've set up the recordID as an auto-increment, I understood that when a record was deleted the next insert would be added to the end of the table, it would seem that record 2 (of 10) has been deleted and the next record inserted has used that same ID (2), I would have expected it to be 11, consequently its thrown out another section of my site.

Is there something else I need to do to make sure new records always take the next number?

Auto-increment Starting Value
If I have a column in a table like this:
member_id int unsigned NOT NULL auto_increment

Auto-increment Question
I'm a little fuzzy on the workings of auto-increment. It is not clear to me, what the difference is between auto-increment as a table option and auto-increment as a field option. I can't seem to get either to work (syntax errors).

Does a field have to be an index to get auto-imcremented (as opposed to a primary key)? What field gets incremented if the table option is set? What is the correct syntax for turning it on? I am trying to set it for an existing table:

alter table mytable add primary key (mykeyfield) auto_imcrement;

Auto-Increment Issue
In the table you can see below I have an auto-increment value I use as my primary key, currently it increments starting at 1,2,3 and so on is there anyway that I can make it go FG01,FG02,FG03 on so on. I mean can the auto-increment value be set to a value of your choice

CREATE TABLE `grind_record` (
  `grind_record_id` mediumint(8) unsigned NOT NULL auto_increment,
  `user_id` mediumint(8) unsigned NOT NULL default '0',
  `secondary_exam_id` tinyint(2) unsigned NOT NULL default '0',
  `subject_id` tinyint(2) unsigned NOT NULL default '0',
  `secondary_level_id` tinyint(2) unsigned NOT NULL default '0',
  PRIMARY KEY  (`grind_record_id`),
  KEY `user_id` (`user_id`,`secondary_exam_id`,`subject_id`,`secondary_level_id`)
) TYPE=InnoDB COMMENT='Grind Records' AUTO_INCREMENT=61 ;

Getting Last Auto Increment ID (perl DBI)
i am inserting a new line into a table with an auto incremented unique ID. after that i want to add a row to another table that will cross reference with using the ID from the first.

what is a reliable method of performing this? selecting the highest ID is open to possible race errors so i'm not keen to do it that way. can it be done in one line?

Next Id On Auto Increment Field
is there a way to know where is the next id without use mysql_insert_id() and any records stored?

Auto Increment By Multiple
I have a table with an ID field that auto increments for each new insertion into the table < ID int not null auto_increment >.

Is it possible to change the number by which the field auto increments from 1 to some other number?

Now, I have other tables that make use of auto_increment also, this change must not affect them.

Auto Increment Field
I am using server version: 4.1.11-nt on windows 2000.

I want to create a table with ID as auto increment and initialise it
with a starting value of 1000.

I am trying to do following

DROP TABLE IF EXISTS `demo`;
CREATE TABLE `demo` (
id bigint not null auto_increment=1000,
`Author_Name` tinytext,
`Authors_Email` varchar(255) default '',
`Author_Password` varchar(8) default '',
`Author_Zipcode` int(6) default NULL,
`Author_DOB` date default NULL,
PRIMARY KEY (id)
) DEFAULT CHARSET=latin1;

The error i am getting is shown below

ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that
corresponds to your MySQL server version for the right syntax to use
near '=1000
,
`Author_Name` tinytext,
`Authors_Email` varchar(255) default '',
`A' at line 2
[color=blue]
>From MySQL manual i learned that innoDB engine cannt have a default[/color]
value.

Please suggest me how to create a table so that it gets initialised by
1000 value (First record starts from 1000).

Reset An Auto Increment
I have some tables that I'm constantly deleting all the data from and then re updating with new data. I have an auto increment field that I would like to reset to 0 each time I perform this delete and re-addition of data.

Is there a SQL command to do such a thing? Or do I have to drop and re create the table?

Auto Increment Reset
Hello, I can't seem to make this work:

 mysql_query("ALTER TABLE member AUTO_INCREMENT = 1")
or die(mysql_error());
echo "Auto Increment=1";

Am I doing something wrong?

Removing Auto-increment
I would like to change one of the fields (primary key field) in my table to not auto-increment. The table is still empty. Can I do this without having to drop the table and recreate it again?

Resetting Auto-increment
Is there any way to leave a table in place, delete all the rows and reset the autoincrement pointer? Or do I need to drop and recreate the table structure to make that happen?


Copyright © 2005-08 www.BigResource.com, All rights reserved