Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL insert row with only default values

Tags:

sql

mysql

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.

like image 294
shelbypereira Avatar asked Feb 09 '23 08:02

shelbypereira


1 Answers

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 |   *** |
like image 107
Blag Avatar answered Feb 11 '23 22:02

Blag