Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

INSERT a row only with default or null values

I have a table with a varying amount of columns of varying names.

All columns are one of the following:

  • identity columns,
  • NULL columns,
  • or have a default value

Now I want to insert into this table a new row and read its content.

I tried all the following:

INSERT INTO globalsettings() VALUES()
INSERT INTO globalsettings VALUES()
INSERT INTO globalsettings VALUES
INSERT INTO globalsettings

Did I miss the correct syntax, or won't I be able to insert an all-defaults row?

like image 972
Alexander Avatar asked Aug 06 '14 13:08

Alexander


People also ask

Can we insert rows with null values in SQL?

The SQL INSERT statement can also be used to insert NULL value for a column.

How do you set a row to null in SQL?

To set a specific row on a specific column to null use: Update myTable set MyColumn = NULL where Field = Condition. This would set a specific cell to null as the inner question asks.

How do you add values to a specific row?

If you want to add data to your SQL table, then you can use the INSERT statement. Here is the basic syntax for adding rows to your SQL table: INSERT INTO table_name (column1, column2, column3,etc) VALUES (value1, value2, value3, etc); The second line of code is where you will add the values for the rows.

How do you insert if row does not exist?

There are three ways you can perform an “insert if not exists” query in MySQL: Using the INSERT IGNORE statement. Using the ON DUPLICATE KEY UPDATE clause. Or using the REPLACE statement.


3 Answers

INSERT INTO globalsettings DEFAULT VALUES;

you can find the description here: http://msdn.microsoft.com/en-us/library/ms174335.aspx

like image 174
deterministicFail Avatar answered Oct 07 '22 20:10

deterministicFail


You could do something like this:

INSERT INTO globalsettings (Column1) VALUES (DEFAULT)

It will use the default value for Column1 and all other columns.

like image 1
JodyT Avatar answered Oct 07 '22 20:10

JodyT


The number of columns in the table must equal the number of values when if you decide not to use the column names in the statement. For example, if you have 4 columns where first is the identity, second and third are nullable, and fourth is a default int 0.

You can do

INSERT INTO globalSettings DEFAULT VALUES

OR

You can specify all values:

INSERT INTO globalSettings Values (NULL, NULL, 0)

OR

You can specify the columns and have the rest to the default null or 0.

INSERT INTO globalSettings(secondColumn) VALUES (Default)

OR

  INSERT INTO globalSettings(secondColumn) VALUES (null)

This will insert a row with 1,null,null,0

You can not insert into a table without specifying what you want to insert.

like image 1
King Decipher Avatar answered Oct 07 '22 21:10

King Decipher