- The GUID stands for a global unique identifier.
- It is a binary data type (16 bit).
- To create the GUID in SQL server we will use the NEWID () function.
- The advantage of a GUID is that unique across servers, tables, databases also.
Just follow the below tables to understand.
Create Database infosysdb
Go
Use infosysdb
Go
Create Table Infosysemployees
(
ID int primary key identity,
Name nvarchar ( 100)
)
Go
Insert Into Infosysemployees Values ('sai ')
Insert Into Infosysemployees Values ('kalyan ')
In the above table, we have used identity to generate the automatic id values. If you perform the below query you will get the result as follows.
Select * From infosysdb . dbo . Infosysemployees
Now we have create anothe table with different database .
Use wipro
Go
Create Table wiproemployees
(
ID int primary key identity,
Name nvarchar ( 100)
)
Go
Insert Into wiproemployees Values ('vijendhar ')
Insert Into wiproemployees Values ('sundeep ')
In the above table, we have used identity to generate the automatic id values. If you perform the below query you will get the result as follows.
Select * From wipro . dbo . wiproemployees
Now within the Wipro database, we gonna create another table called employees and we have inserted values in employees tables by using above created two tables as follows.
Create Database wipro
Go
(
)
Select * From wipro . dbo . wiproemployees
Select * From infosysdb . dbo . Infosysemployees
If you execute the above query you will get an error as follows.
Database 'wipro ' already exists. Choose a different database name.
Violation of PRIMARY KEY constraint 'PK__employee__3213E83FA68E7852'. Cannot insert duplicate key in object 'dbo . employees'. The duplicate key value is (1).
The statement has been terminated.
The reason for above error is that if you execute the tables with union all it allows duplicate values, but not between the tables and databases and servers.
No comments:
Post a Comment