What does "default values" do in sqlserver? - sql-server

I have come across the following piece of T-SQL. Can somebody explain what it does.
INSERT #numbers default VALUES
The temporary table #numbers was created a few lines before with the following:
CREATE TABLE #numbers (num int identity primary key)
What does the "default values" bit do?

While I will never understand the need for the #numbers table, with just one column defined as identity, there is no other way to insert data into that table
INSERT #numbers default VALUES
will insert one row of data with the next IDENTITY value. The only alternate would be to use IDENTITY INSERT ON, which would be cumbersome in this case

The DEFAULT constraint is used to insert a default value into a column.
The default value will be added to all new records, if no other value is specified.
In your case, you just have an identity column which does not really correspond to a column with a specified default value.

Related

SQL Server creating a Table with an IDENTITY COLUMN - uniqueness

In SQL Server, I have created a Table with an ID column that I have made an IDENTITY COLUMN,
EmployeeID int NOT NULL IDENTITY(100,10) PRIMARY KEY
It is my understanding, when I use the IDENTITY feature, it auto increments the EmployeeID. What I don't know/not sure is:
Is that IDENTITY number created, unique?
Does SQL search the entire column in the table to confirm the number created does not already exist?
Can I override that auto increment number manually?
If I did manually override that number, would the number I enter be checked to make sure it is not a duplicate/existing ID number?
Thanks for any help provided.
Is that IDENTITY number created, unique?
Yes, Identity property is unique
Does SQL search the entire column in the table to confirm the number created does not already exist? \
It need not, what this property does is, just incrementing the old value
Can I override that auto increment number manually?
Yes, you can. You have to use SET IDENTITY_INSERT TABLENAME ON
If I did manually override that number, would the number I enter be checked to make sure it is not a duplicate/existing ID number?
No, that won't be taken care by SQL Server, you will have to ensure you have constraints to take care of this
Below is a simple demo to prove that
create table #temp
(
id int identity(1,1)
)
insert into #temp
default values
go 3
select * from #temp--now id column has 3
set identity_insert #temp on
insert into #temp (id)
values(4)
set identity_insert #temp off
select * from #temp--now id column has 4
insert into #temp
default values
go
select * from #temp--now id column has 5,next value from the last highest
Updating info from comments:
Identity column will allow gaps once you reseed them,also you can't update them

How to create a SQL Server table with a column and its values to be generated automatically as a GUID

I need to design a table in SQL Server having some columns, one of these columns (ID column and use sequential uniqueidentifier) should automatically populate its data when inserting other column data.
The values of the ID column should be generated automatically when insertion happens.
Please help me to do this, any help is appreciated.
NB: I am new to this step by step approach will be more helpful
Just create a table with a column ID of datatype uniqueidentifier and set it's default value to newsequentialid():
Then, when you go insert rows into that table, just omit the ID column from the list of columns you're inserted values into:
INSERT INTO dbo.YourTable(ColA, ColB, ....., ColX)
VALUES(.., .. ,. ...)
If you don't explicitly insert a value into ID, the default specification (newsequentialid()) will be used .
As per Marc_s's comment, you should use NEWSEQUENTIALID()
CREATE TABLE myTable (ColumnA uniqueidentifier DEFAULT NEWSEQUENTIALID());
See NEWSEQUENTIALID (Transact-SQL)

Insert a new row in an SQL table without assigning any values

I have an SSMS table that has one column that is a pk and set to auto increment. In my code I need to create a row and then pull the value of the ID.
I don't want to set the id, it needs to auto generate. Is there a way to do this without adding a second column to add data into?
Just use the phrase "DEFAULT VALUES" in place of a field list and values specification:
INSERT INTO [TableName] DEFAULT VALUES;
Test with:
CREATE TABLE #Test
(
ID INT IDENTITY(1, 1) NOT NULL PRIMARY KEY
);
INSERT INTO #Test DEFAULT VALUES;
SELECT SCOPE_IDENTITY();
For more info on this, check out the MSDN page for INSERT and do a "find" (i.e. Control-F) in your browser for "default values".

How to use default primary key as a sequence value when creating table for oracle database columns

We have a tool and this tool create some insert scripts to add rows in our plsql table.
this scripts is not modifiable and we can't see this scripts. so,
when data row comes (with insert script that we don't know structure), we should give a primary key. we can use trigger but we don't want do this for reasons of performance.
Below code doesn't work.
CREATE TABLE qname
(
qname_id integer NOT NULL default qname_id_seq.nextval PRIMARY KEY,
);
Any idea?
Thanks..
.nextval cannot be used in Default Value of table, This is achieved by Trigger and Sequence when you want serialized number that anyone can easily read/remember/understand. But if you don't want to manage ID Column (like qname_id) by this way, and value of this column is not much considerable, you can use SYS_GUID() at Table Creation to get Auto Increment like this.
CREATE TABLE qname
(qname_id RAW(16) DEFAULT SYS_GUID() PRIMARY KEY,
name VARCHAR2(30));
(or by Modifying Column)
Now your qname_id column will accept "globally unique identifier value".
you can insert value in table by ignoring emp_id column like this.
INSERT INTO qname (name) VALUES ('name value');
So, it will insert unique value to your qname_id Column.

How do I store a non-identity column value in SQL Server?

If I have a table with an identity column as the primary key, I would use scope_identity() to retrieve latest identity value inserted during scope. What if the primary key is not an identity column, but an nvarchar(256) with a default value of newid() to generate the value. After performing the insert, is there a function that I can use to retrieve that value and store it in some variable? I'll need to insert this value into other column ID's in other tables.
Here's an example of what I'm referring to. In the aspnet_Users table the userID is described like above, if I wanted to use the userID as a FK in my own table, would it be ok to use that autogenerated newid() value or is there a better way? If this is the best way how do I store it easily?
Thank you
The OUTPUT clause of the INSERT is what you are looking for. See this MSDN article.
basically:
DECLARE #newkey TABLE (keys varchar(25));
INSERT INTO actual_table (nonAutoIncId,other1,other2,other3)
OUTPUT INSERTED.nonAutoIncId INTO #newkey
VALUES ('NewId',1,2,3)

Resources