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 ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| 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 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. |
| |||
| 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 ================== |
| |||
| 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 |
| |||
| 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?" |
| |||
| 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 ================== |
| ||||
| 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 |