5

I would like to insert a row with only the default values (which I will then update later since I need the ID autoincremented field)

This works in SQL Server (How to insert a record with only default values?)

insert into myTable DEFAULT  VALUES;

But how can I accomplish this in MySQL:

I also tried:

insert into myTable;

which fails. I know I can work around with the standard insert syntax, but there are a lot of columns in my table so a simple syntax if it exists would be helpful.

1

1 Answer 1

10

This will do it :

INSERT INTO `myTable` (`id`)
VALUES 
    (null),
    (null);
-- or 
INSERT INTO `myTable` () 
VALUES();

SQL Fiddle

MySQL 5.6 Schema Setup:

CREATE TABLE Table1
    (`id` int AUTO_INCREMENT PRIMARY KEY, `title` varchar(5) DEFAULT '***')
;

INSERT INTO Table1
    (`id`, `title`)
VALUES
    (1, 'hi'),
    (2, 'hello')
;

INSERT INTO Table1
    (`id`)
VALUES
    (null),
    (null)
;
INSERT INTO Table1 () VALUES();

Query 1:

SELECT * from Table1

Results:

| id | title |
|----|-------|
|  1 |    hi |
|  2 | hello |
|  3 |   *** |
|  4 |   *** |
|  5 |   *** |
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.