Home >> FAQs/Tutorials >> MySQL Tutorials

MySQL Tutorial - Omitting Columns in INSERT Statements

By: FYIcenter.com

(Continued from previous topic...)

How To Omit Columns with Default Values in INSERT Statement?

If you don't want to specify values for columns that have default values, or you want to specify values to columns in an order different than how they are defined, you can provide a column list in the INSERT statement. If a column is omitted in the column, MySQL applies 3 rules:

  • If default value is defined for the column, that default value will be used.
  • If no default value is defined for the column and NULL is allowed, NULL will be used.
  • If no default value is defined for the column and NULL is not allowed, MySQL will provide '' for character type columns, and 0 for numeric columns.

The following tutorial exercise gives you some good examples:

mysql> INSERT INTO fyi_links (url, id) 
  VALUES ('sqa.fyicenter.com', 103);
Query OK, 1 row affected, 1 warning (0.01 sec)

mysql> INSERT INTO fyi_links (id) VALUES (110);
Query OK, 1 row affected, 1 warning (0.06 sec)

mysql> INSERT INTO fyi_links (url) 
   VALUES ('www.fyicenter.com');
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> SELECT id, url, notes, counts, DATE(created) 
   FROM fyi_links;
+-----+-------------------+-------+--------+---------------+
| id  | url               | notes | counts | DATE(created) |
+-----+-------------------+-------+--------+---------------+
| 101 | dev.fyicenter.com | NULL  |      0 | 2006-04-30    |
| 102 | dba.fyicenter.com | NULL  |      0 | 2006-07-01    |
| 103 | sqa.fyicenter.com | NULL  |   NULL | 2006-07-01    |
| 110 |                   | NULL  |   NULL | 2006-07-01    |
|   0 | www.fyicenter.com | NULL  |   NULL | 2006-07-01    |
+-----+-------------------+-------+--------+---------------+
5 rows in set (0.00 sec)

(Continued on next topic...)

  1. What Are DML Statements?
  2. How To Create a Testing Table?
  3. How To Insert a New Row into a Table?
  4. How To Specify Default Values in INSERT Statement?
  5. How To Omit Columns with Default Values in INSERT Statement?
  6. What Happens If Unique Value Constraints Are Violated?
  7. How To Insert Multiple Rows with One INSERT Statement?
  8. How To Update Values in a Table?
  9. How To Update Column Values on Multiple Rows?
  10. How To Use Existing Column Values in the SET Clause?
  11. Is the Order of Columns in the SET Clause Important?
  12. How To Use Values from Other Tables in UPDATE Statements?
  13. What Happens If the UPDATE Subquery Returns No Rows?
  14. What Happens If the UPDATE Subquery Returns Multiple Rows?
  15. How To Delete an Existing Row from a Table?
  16. How To Delete Multiple Rows from a Table?
  17. How To Delete All Rows in a Table?

MySQL Tutorials:

More...


Other Tutorials/FAQs:

More...


Related Resources:

More...


Selected Jobs:

More...