Unix Technical Forum

INSERT and AUTO_INCREMENT

This is a discussion on INSERT and AUTO_INCREMENT within the MySQL forums, part of the Database Server Software category; --> If I have a table with a number of columns in, the first of which is an auto-increment field ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 09:51 AM
Ben
 
Posts: n/a
Default INSERT and AUTO_INCREMENT

If I have a table with a number of columns in, the first of which is an
auto-increment field for indexing, is there a way of inserting data
without having to specify all the column names that aren't the auto
increment field?

INSERT INTO tables VALUES(....)

Or alternatively could I insert a value into the autoincrement field
above without messing up the auto-increment?

Cheers,

Ben

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 09:51 AM
Paul Lautman
 
Posts: n/a
Default Re: INSERT and AUTO_INCREMENT

Ben wrote:
> If I have a table with a number of columns in, the first of which is
> an auto-increment field for indexing, is there a way of inserting data
> without having to specify all the column names that aren't the auto
> increment field?
>
> INSERT INTO tables VALUES(....)
>
> Or alternatively could I insert a value into the autoincrement field
> above without messing up the auto-increment?
>
> Cheers,
>
> Ben


use NULL as a placeholder for the auto-increment field.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 09:51 AM
Jerry Stuckle
 
Posts: n/a
Default Re: INSERT and AUTO_INCREMENT

Ben wrote:
> If I have a table with a number of columns in, the first of which is an
> auto-increment field for indexing, is there a way of inserting data
> without having to specify all the column names that aren't the auto
> increment field?
>
> INSERT INTO tables VALUES(....)
>
> Or alternatively could I insert a value into the autoincrement field
> above without messing up the auto-increment?
>
> Cheers,
>
> Ben
>


Ben,

As Paul indicated, you can use NULL for the value.

But IMHO it's always better to specify the column names on any such
request. That way you don't have as much code to change should you need
to go back and add a (not-null) column later.

But yes, I agree, it's more typing and a PITA. But I've gotten "bit" by
this before and had to find a LOT of code to change!

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 09:51 AM
aeg
 
Posts: n/a
Default Re: INSERT and AUTO_INCREMENT

You can specify the column names like so:
INSERT INTO ('col1','col2','col3') VALUES ('1','2','3');
etc

Hope that helps

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 09:51 AM
Peter H. Coffin
 
Posts: n/a
Default Re: INSERT and AUTO_INCREMENT

On Fri, 29 Dec 2006 17:37:55 -0500, Jerry Stuckle wrote:
> But IMHO it's always better to specify the column names on any such
> request. That way you don't have as much code to change should you need
> to go back and add a (not-null) column later.
>
> But yes, I agree, it's more typing and a PITA. But I've gotten "bit" by
> this before and had to find a LOT of code to change!


As an example of how it can bite one, imagine forgetting to update one
sweeping but rarely used query as part of inserting a new column into
the table any place other than the last one in the table.

--
4. Shooting is not too good for my enemies.
--Peter Anspach's list of things to do as an Evil Overlord
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-28-2008, 09:51 AM
Paul Lautman
 
Posts: n/a
Default Re: INSERT and AUTO_INCREMENT

aeg wrote:
> You can specify the column names like so:
> INSERT INTO ('col1','col2','col3') VALUES ('1','2','3');
> etc
>
> Hope that helps


But isn't this exactly what the OP said he didn't want to do: " is there a
way of inserting data
WITHOUT having to specify all the column names that aren't the auto
increment field?"


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-28-2008, 09:51 AM
Jerry Stuckle
 
Posts: n/a
Default Re: INSERT and AUTO_INCREMENT

Peter H. Coffin wrote:
> On Fri, 29 Dec 2006 17:37:55 -0500, Jerry Stuckle wrote:
>
>>But IMHO it's always better to specify the column names on any such
>>request. That way you don't have as much code to change should you need
>>to go back and add a (not-null) column later.
>>
>>But yes, I agree, it's more typing and a PITA. But I've gotten "bit" by
>>this before and had to find a LOT of code to change!

>
>
> As an example of how it can bite one, imagine forgetting to update one
> sweeping but rarely used query as part of inserting a new column into
> the table any place other than the last one in the table.
>


Yep, and even it it's the last one it will bite you. You'll get a
mismatch on the number of columns vs. the number of data items.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-28-2008, 09:51 AM
Peter H. Coffin
 
Posts: n/a
Default Re: INSERT and AUTO_INCREMENT

On Sat, 30 Dec 2006 19:51:05 -0000, Paul Lautman wrote:
> aeg wrote:
>> You can specify the column names like so:
>> INSERT INTO ('col1','col2','col3') VALUES ('1','2','3');
>> etc
>>
>> Hope that helps

>
> But isn't this exactly what the OP said he didn't want to do: " is there a
> way of inserting data
> WITHOUT having to specify all the column names that aren't the auto
> increment field?"


In which case the answer is simple "No. The right way is to specify the
column names."

Normally that only burdensome the first time; after that, you've got the
query saved someplace, and you merely update it occasionally.

--
"... I've seen Sun monitors on fire off the side of the multimedia lab.
I've seen NTU lights glitter in the dark near the Mail Gate.
All these things will be lost in time, like the root partition last week.
Time to die...". -- Peter Gutmann in the scary.devil.monastery
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:12 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com