Results 1 to 8 of 8
  1. #1

    Simple mysql query question

    Hello, what is the mysql query to insert something new into a table without needing the ID? What I have been doing is exporting data from another table and editing some fields and execute the query. However when you export something it stores the ID of the query so if you run it again it errors saying the ID already exists unless you manually change it. How can I execute a query and leave out the ID so it automatically numbers it? Here is the example query:

    Code:
    INSERT INTO `table_blah` VALUES (1,'blah');
    1 is the ID of the query, and if I try to remove the 1, it errors. I would like to insert data without the need of the ID so it will automatically add the number for the entry. How can I do that? Thanks

  2. #2
    Join Date
    Nov 2004
    Location
    HK
    Posts
    309
    try in this way if the table is set to auto_increment:

    INSERT INTO table_blah(col_name) VALUES ('blah');
    Rails in DA - Ruby on Rails plugin for Directdmin | DA-Tomcat - Tomcat Manager plugin for Directdmin
    DA-PgSQL - PostgreSQL plugin for Directdmin | IP Deny Manager - IP Deny Manager plugin for Directdmin
    DeeperAdmin - Manager your DirectAdmin server "deeper"
    Order now at http://www.daplugin.com

  3. #3
    Quote Originally Posted by lexington
    Hello, what is the mysql query to insert something new into a table without needing the ID? What I have been doing is exporting data from another table and editing some fields and execute the query. However when you export something it stores the ID of the query so if you run it again it errors saying the ID already exists unless you manually change it. How can I execute a query and leave out the ID so it automatically numbers it? Here is the example query:

    Code:
    INSERT INTO `table_blah` VALUES (1,'blah');
    1 is the ID of the query, and if I try to remove the 1, it errors. I would like to insert data without the need of the ID so it will automatically add the number for the entry. How can I do that? Thanks
    make sure that the ID field is set as key and auto_increment is selected.
    THEN do
    Code:
    INSERT INTO `table_blah` VALUES ('','blah');
    thats how i do it.

  4. #4
    Join Date
    Mar 2006
    Posts
    965
    If the field is set to NULL, you can also change:

    PHP Code:
    INSERT INTO `table_blahVALUES ('','blah'); 
    into:

    PHP Code:
    INSERT INTO `table_blahVALUES ('NULL''blah'); 

  5. #5
    Quote Originally Posted by horizon
    If the field is set to NULL, you can also change:

    PHP Code:
    INSERT INTO `table_blahVALUES ('','blah'); 
    into:

    PHP Code:
    INSERT INTO `table_blahVALUES ('NULL''blah'); 

    either way should work perfectly fine.. its all about how you want to do it

  6. #6
    Join Date
    Mar 2006
    Posts
    965
    My mistake. There are no quotes between NULL.

    PHP Code:
    INSERT INTO `table_blahVALUES (NULL'blah'); 
    in case you're using your field as NULL with auto_increment.

  7. #7
    Join Date
    Dec 2003
    Posts
    143
    You need to modify the database structure to set the id as key and to auto increment, if you need help doing this, feel free to pm me.

  8. #8
    also, the way I learned was using an auto_increment and then just giving the id column a 0 value as in:

    PHP Code:
    INSERT INTO `table_blahVALUES (0'blah'); 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •