Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a uniqueidentifier column and adding the default to generate new guid

I have the following SQL command:

ALTER TABLE dbo.UserProfiles ADD ChatId UniqueIdentifier NOT NULL, UNIQUE(ChatId), CONSTRAINT "ChatId_default" SET DEFAULT newid() 

I want to be able to make this column unique, and I want it to be able to generate a new guid every time a row is added to the table. This column is not an IDENTITY column because I already have one. This is something separate. How would I go about adding this column to a table with users already in it.

like image 376
anthonypliu Avatar asked Aug 15 '12 18:08

anthonypliu


People also ask

How do I create a new GUID in SQL?

-- If you want to generate a new Guid (uniqueidentifier) in SQL server the you can simply use the NEWID() function. -- This will return a new random uniqueidentifier e.g. You can directly use this with INSERT statement to insert new row in table.

What is the default value of Uniqueidentifier in SQL?

Default value is GUID in SQL Server table column.

How do I add a GUID?

GUIDs can be added to any table. If the table you want to edit participates in replication or offline mapping or contains a GUID, you must insert a unique value to the global ID or GUID column when you insert a new record to the table using SQL. To do this, you can use the newid() function.

Can Uniqueidentifier be duplicated?

Well, no: the nature of a uniqueidentifier is to not allow duplicate entries.

Which column constraints and properties can be used on the UNIQUEIDENTIFIER data?

All column constraints and properties, except IDENTITY, can be used on the uniqueidentifierdata type. Merge replication and transactional replication with updating subscriptions use uniqueidentifiercolumns to guarantee that rows are uniquely identified across multiple copies of the table. Converting uniqueidentifier Data

How to convert a UNIQUEIDENTIFIER value to a char data type?

The following example converts a uniqueidentifier value to a char data type. SQL. DECLARE @myid uniqueidentifier = NEWID (); SELECT CONVERT(CHAR(255), @myid) AS 'char'; The following example demonstrates the truncation of data when the value is too long for the data type being converted to. Because the uniqueidentifier type is limited ...

Why do we use GUIDs instead of identity columns?

The reason they probably chose GUIDs is due to their uniqueness across space and time. GUIDs do have benefits over identity columns but they come with quite a large overhead - 16 bytes compared to 4bytes for an integer.

How do I truncate the value of a UNIQUEIDENTIFIER?

DECLARE @myid uniqueidentifier = NEWID(); SELECT CONVERT(CHAR(255), @myid) AS 'char'; The following example demonstrates the truncation of data when the value is too long for the data type being converted to.


1 Answers

see this sample:

create table test (mycol UniqueIdentifier NOT NULL default newid(), name varchar(100)) insert into test (name) values ('Roger Medeiros') select * from test 

for add a not null field on a populated table you need this.

alter table test add mycol2 UniqueIdentifier NOT NULL default newid() with values  CREATE UNIQUE NONCLUSTERED INDEX IX_test ON dbo.test ( mycol ) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,    ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
like image 116
Roger Medeiros Avatar answered Oct 02 '22 00:10

Roger Medeiros