What is the use of uniqueidentifier in SQL Server? - sql-server

I have a table like this:
Table1 (Table_ID Int Primary Key, Name Varchar(50), ROWID Uniqueidentifier)
and the default value for ROWID column is set using NEWID() function.
I understand that NEWID() function will generate a unique identifier for each row.
I am wondering why do we need a unique identifier for each row? If we want uniqueness for each and every row I can achieve that using my primary key. I am new to SQL. Please advise.
Thank you.

It can be useful if you're interfacing with other systems. However, if you're working in an isolated system, you can absolutely use your PK as the "unique" row id for the table.

uniqueidentifier is a CPU specific ID, it is GLOBALLY UNIQUE, it's very useful especially if you ever have to move your data as row_identity (integer) would start over on new DB SERVER..

Related

SQLSERVER - how to define a procedure for table id generation?

I'm application switching databases for my application from MySQL to SQLServer.
Today I implement a custom id generation strategy defined in an abstract class all POJOs use. This works, but I am only able to generate an id via the application.
With this database migration, I'd like, after creating the schema, define somewhere all the 'id' columns for all tables to use a procedure that returns a SELECT NEWID(); query.
Is this possible? How?
I like to define the ID columns with a default constraint:
create table a
(
id
uniqueidentifier not null
constraint [a.id.default.newid]
default( newid() )
constraint [a.id.primarykey]
primary key clustered,
--> other columns...
)
This way, you can either take an app-generated ID or let the database assign it, depending on your needs. Your primary key constraint (or unique constraint) enforces uniqueness...which is important if you allow incoming IDs from the apps.
In the scripts...where you don't necessarily have the need to generate the ID, don't specify a value...and the default kicks in. You can insert newid(), too...
insert a
select newid()
from b
...and SQL Server knows to call newid() for each row

Insert a value in database

I am having a column named no. If I insert the no in database same no should not insert, if it is stored in that table. please help me.
Thank you
Make the required column unique using UNIQUE constraint.
Your create table statement should be like:
CREATE TABLE table_name ( col_1 datatype UNIQUE, col_2 datatype, .....)
This makes col_1 unique.
set it as Unique column and auto-increment.
A look on Google returns Sql learning Basics and this
You can set your column auto increment property to on it will enable the column to add a unique incremental value for each new record other way to is add a primary key or unique key constraint to that particular column
Thanks

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)

how can I use GUID datatype in SQL Server 2008?

I want to build an employees table using SQL SERVER 2008 , and in my table I want to have an ID for each employee .
I heared about GUID and I kind of understood that its a data type , But I couldn't use it
could you please show me the way to use it ...
by the way , lets say I want something like this :
CREATE TABLE Employees (
ID guid PRIMARY KEY,
Name NVARCHAR(50) NOT NULL
)
How can I do it ?? because I want to benefit from it , but I couldn't find out how to do that
It's not called GUID in SQL Server. It's called uniqueidentifier
The type is called UNIQUEIDENTIFIER as others have pointed out already. But I think you absolutely must read this article before proceeding: GUIDs as PRIMARY KEYs and/or the clustering key
They are called uniqueidentifier
Don't use uniqueidentifier as primary key if clustered (which is the default for PKs)
Seriously: use a standard IDENTITY instead
You can also consider using NEWSEQUENCIALID as the default value for your ID column as it would be faster than using NEWID() generate the GUIDs.
BUT (from the same link above):-
If privacy is a concern, do not use this function. It is possible to guess the value of the next generated GUID and, therefore, access data associated with that GUID.
Practical demo, FWIW
DECLARE #guid1 AS uniqueidentifier
SET #guid1 = NEWID()
SELECT #guid1
The GUID in sql server is known by UNIQUEIDENTIFIER data type. below is the desired code.
CREATE TABLE Employees
(
Id UNIQUEIDENTIFIER PRIMARY KEY,
Name NVARCHAR (50) not null
)
GO

Composite key in SQL Server

I am using Sql Server with Composite key. In this composite key contains one identity column i.e Auto incrment value. i want to generate in this column with duplicate values. How can i do this. Please give me a solution for this.
Thanks with Regards
Saravanan.M
The identity column cannot(corrected based on feedback from #AlexKuznetsov) should not have duplicates within the column itself - it is generally meant to be a unique column and a provide non-identifying value for each row.
If you are asking how to put values into the identity column that already exist in another column, you have to do the following:
Set IDENTITY_INSERT Schema.TableName ON
Insert Into TableName (PK1, PK2, IdentityCol1, OtherCol1, OtherCol2)
SELECT FirstCol, SecondCol, SecondCol, OtherColumn1, OtherColumn2
FROM SomeOtherTable
Set IDENTITY_INSERT Schema.TableName OFF
note that PK2 and IdentityCol1 both get the same value
It would be good if you could provide more context around what you are wanting to do and why? There are some good reasons to use composite keys, but if you're already using an identity field, why not make that your primary key?
Your auto-incrementing identity column should be left untouched and should continue to uniquely identify your rows. It is generally good practice to always have an abstract identity column as your primary key.
If you have 2 other values in your data model which uniquely identify your row, they should be in 2 other columns. If one of them is an auto-incrementing number then you can generate the value either in a stored proc which is used for all insertions or in an insert trigger.
Although this is not quite an answer, several answerers have made one and the same mistake, claiming that "You cant have an identity column with duplicates". In fact, identities may easily be not unique if you do not enforce their uniqueness by an index or constraint, as follows:
CREATE TABLE identityTest(i INT IDENTITY(1,1));
GO
INSERT identityTest DEFAULT VALUES;
INSERT identityTest DEFAULT VALUES;
SET IDENTITY_INSERT identityTest ON;
INSERT INTO identityTest(i)
SELECT i FROM identityTest;
SET IDENTITY_INSERT identityTest OFF;
SELECT i FROM identityTest;
i
-----------
1
2
1
2
GO
DROP TABLE identityTest;

Resources