How to INSERT INTO table DEFAULT VALUES - sql

When you want to insert default values into a table, some databases allow this syntax:
INSERT INTO table DEFAULT VALUES;
ASE does not support this.
Using:
INSERT INTO table (col2, col3)
VALUES (DEFAULT, DEFAULT)
and skipping the identity column works for columns with constant default values, but not for computed columns including timestamp.
Introspecting the table for a column with a constant default and then just specifying DEFAULT for that column would work, unless it's a table with only an identity and computed columns, but no one is likely to use such tables.
Is there an easier way?

Skip columns with default values from the insert statement.
If a default value exists for the skipped column (or user-defined datatype of the column), it is entered.

Related

Possible to assign a default value on a NULL against table column in sql

I am wondering if there is a way that when creating a table that you can assign a default value to a column if the value is null.
I understand that you can use the syntax DEFAULT however this is only for when the value is absent. Is there a way similar to this that you can say when NULL it will add the default without using a trigger.
CREATE TABLE DBO.TESTS
(
TEST VARCHAR(100) DEFAULT(ISNULL('test',NULL)),
NUM INT
)
This is a test and the kind of thing i was looking at?
UPDATE:
Example input
INSERT INTO TESTS (TEST,NUM)
VALUES (NULL,1)
Where the "NULL" is i would like that to enter the value "test". But also if i was to do the following
INSERT INTO TESTS (NUM)
VALUES (1)
This would also enter the value of "test" into the column "TEST".
I hope this helps.
Yes, there is a way to do what you want. It is called a trigger. You would have to define a trigger that sets the value when a NULL is inserted into the column.
If you use an INSTEAD OF trigger, then you can still declare the column as NOT NULL. The trigger will take care of assigning a value so the constraint is not violated.
So, you can do this. Why you would want to do it is another question. Perhaps you are not familiar with the DEFAULT keyword that allows default values to be inserted using a VALUES() clause. This is explained in the documentation for INSERT.
You can add a default constraint to your table which will automatically add a set value for the column if the insert does not have a value for it:
CREATE TABLE DBO.TESTS
(
TEST VARCHAR(100),
NUM INT
)
ALTER TABLE [DBO].[TESTS] ADD CONSTRAINT [DF_TESTS_TEST] DEFAULT (N'default_value_goes_here') FOR [TEST]
INSERT INTO [DBO].[TESTS] VALUES (NULL, 1)
INSERT INTO [DBO].[TESTS] (num) VALUES (2)
Results
NULL 1
'default_value_goes_here' 2
If you want to check during an insert you can use COALESCE
DECLARE #insertValue VARCHAR(100)
SET #insertValue = NULL
INSERT INTO DBO.TESTS VALUES (COALESCE (#insertValue, 'defaultValue'), 1);
The bottom line is that the column should be non-nullable with a default value.
It's possible to replace null values with default values in a trigger for insert/update, but that doesn't make any sense - first because it means every update/insert will have to do that extra work, and second, because that would make it impossible to insert null to the column (unless the triggers are disabled) so why allow nulls in the first place? It's a mistake that will only be confusing for anyone attempting to use that table.
Think about it from the user side - when you send null to a nullable column, you expect it to be null, you don't expect it to contain a value.
If you run this insert statement:
INSERT INTO TESTS (TEST,NUM)
VALUES (NULL,1)
You expect the table to contain a row where Test is null and num = 1.
You do not expect the Test column to contain the default value.
When providing a value for a column, including NULL, that value will be used. NULL is still a value, just an unknown value. A DEFAULT value will only be used if no value is passed (which, as I just said, NULL is a value so doesn't count).
If you don't want a NULL in your table, then instead stop people supplying them by setting your column as NOT NULL:
CREATE TABLE dbo.TestTable (ID int, String varchar(100) NOT NULL DEFAULT 'test')
GO
--INSERT is successful, String has a value of 'test'
INSERT INTO dbo.TestTable (ID)
VALUES(1);
GO
--INSERT fails, String cannot have a value of NULL
INSERT INTO dbo.TestTable (ID,
String)
VALUES(2,NULL);
GO
SELECT *
FROM dbo.TestTable;
GO
DROP TABLE dbo.TestTable;
GO

SQL Server Insert with no specified columns

I have a table with an auto-generated ID column (and that's all!)
CREATE TABLE [dbo].[EmailGroup](
[EmailGroupGuid] [uniqueidentifier] NOT NULL
CONSTRAINT [PK_EmailGroup] PRIMARY KEY CLUSTERED ([EmailGroupGuid] ASC)
) ON [PRIMARY]
ALTER TABLE [dbo].[EmailGroup]
ADD CONSTRAINT [DF_EmailGroup_EmailGroupGuid] DEFAULT (newsequentialid()) FOR [EmailGroupGuid]
I want to INSERT into this table and extract the generated ID. but, I can't work out if it's possible. It seems to complain about the lack of values/columns.
DECLARE #Id TABLE (Id UNIQUEIDENTIFIER)
INSERT INTO EmailGroup
OUTPUT inserted.EmailGroupID INTO #Id
Is there any way to do this? I mean I could add a dummy column to the table and easily do this:
INSERT INTO EmailGroup (Dummy)
OUTPUT inserted.EmailGroupID INTO #Id
VALUES (1)
however I don't really want to.
I could also specify my own ID and insert that, but again, I don't really want to.
Though I'm not sure why would you need such a table, the answer to your question is to use the keyword DEFAULT:
INSERT INTO EmailGroup (EmailGroupGuid)
OUTPUT inserted.EmailGroupGuid INTO #Id
VALUES(DEFAULT);
Another option is to use DEFAULT VALUES, as shown in Pawan Kumar's answer.
The key difference between these two options is that specifying the columns list and using the keyword default gives you more control.
It doesn't seem much when the table have a single column, but if you will add columns to the table, and want to insert specific values to them, using default values will no longer be a valid option.
From Microsoft Docs on INSERT (Transact-SQL):
DEFAULT
Forces the Database Engine to load the default value defined for a column.
If a default does not exist for the column and the column allows null values, NULL is inserted.
For a column defined with the timestamp data type, the next timestamp value is inserted.
DEFAULT is not valid for an identity column.
DEFAULT VALUES
Forces the new row to contain the default values defined for each column.
So as you can see, default is column based, while default values is row based.
Please use this.
CREATE TABLE [dbo].[EmailGroup]
(
[EmailGroupGuid] [uniqueidentifier] NOT NULL CONSTRAINT [PK_EmailGroup] PRIMARY KEY CLUSTERED ([EmailGroupGuid] ASC)
) ON [PRIMARY]
ALTER TABLE [dbo].[EmailGroup]
ADD CONSTRAINT [DF_EmailGroup_EmailGroupGuid] DEFAULT (newsequentialid()) FOR [EmailGroupGuid]
DECLARE #Id TABLE (Id UNIQUEIDENTIFIER)
INSERT INTO EmailGroup
OUTPUT inserted.EmailGroupGuid INTO #Id DEFAULT VALUES
SELECT * FROM #Id
last 3 OUTPUTs from my Laptop
--92832040-7D52-E811-B049-68F728AE8695
--2B6ADC5F-7D52-E811-B049-68F728AE8695
--0140AF66-7D52-E811-B049-68F728AE8695

How to insert into a table that specifies a DEFAULT value for every column?

I have a table where all columns are auto-populated whenever an insertion happens:
CREATE TABLE …
(
ItemID INT NOT NULL IDENTITY(…),
DateCreated DATETIME2 NOT NULL DEFAULT GETDATE()
);
How do I write a SQL statement that inserts a new row into this table without having to manually provide any concrete values to insert?
(There is already a similar question, but it differs in that it's about a table with some non-DEFAULT columns for which a value must be manually provided.)
Use the DEFAULT VALUES option:
INSERT INTO IdentitySpecification
DEFAULT VALUES;

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?
INSERT INTO globalsettings DEFAULT VALUES;
you can find the description here: http://msdn.microsoft.com/en-us/library/ms174335.aspx
You could do something like this:
INSERT INTO globalsettings (Column1) VALUES (DEFAULT)
It will use the default value for Column1 and all other columns.
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.

Inserting a new row into a single column table in apache derby with generated id

I have the following table:
create table indices (
id int primary key generated by default as identity
);
how do I insert a new row?
I have already tried several things I have found, like:
insert into indices values (null);
insert into indices default values;
however that didn't work with derby.
Try "insert into indices values (default)"
You need a column to insert into. You have id which is an identity, which means it will already have a value when a new row is inserted. Add a new column and then fill that column in your insert statement.