0

I need some help with some SQL Server 2014 syntax.

I want to use the NEWID() function to create the UUID value in column CareplannersUUID. Each time a record is inserted, I want a new UUID value to appear in column CareplannersUUID.

But I am not sure about the syntax of the command. I think it is something like:

ALTER TABLE CareplannersMembers 
    ADD CONSTRAINT DF_table_id DEFAULT (NEWID()) FOR CareplannersUUID

I am wondering specifically, is there another value I should add for DF_table_id?

Notes about the table in question:

  • Table name: CareplannersMembers
  • UUID column name: CareplannersUUID

Thank you for your help.

Eric

I looked at the similar questions, but am not able to determine, from information presented there, whether or not I am using the correct syntax for my SQL Server statement.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Eric B
  • 29
  • 5
  • Possible duplicate of [Adding a uniqueidentifier column and adding the default to generate new guid](https://stackoverflow.com/questions/11974939/adding-a-uniqueidentifier-column-and-adding-the-default-to-generate-new-guid) – PM 77-1 Oct 15 '17 at 01:29
  • 1
    `NEWID()` is fine. However, if `CareplannersUUID` is a clustered key, you may want to consider `NEWSEQUENTIALID()` instead, although it has [potential privacy and hardware implications](https://learn.microsoft.com/en-us/sql/t-sql/functions/newsequentialid-transact-sql). – Bacon Bits Oct 15 '17 at 01:32
  • PM 77-1, I looked at that question, but I was not able to tell, from the information given there, if my syntax is correct, or if I need to supply another value for DF_table_id. I've been searching for more information about this, for much of today and yesterday, and I am a bit stuck. Bacon, I am not sure if this is an unclustered key, or not. I am guessing it is not. Does this syntax look OK to you? Thank you both for your help. Eric ALTER TABLE CareplannersMembers ADD CONSTRAINT DF_table_id DEFAULT (NEWID()) FOR CareplannersUUID – Eric B Oct 15 '17 at 01:55
  • alroc, it is true, I could set up a test database, and set up another version of my production code; I thought about doing this. It would take me some time to complete that task, probably more time than typing this question. I apologize for wasting your time. Wishing you a good evening. Eric – Eric B Oct 15 '17 at 01:59
  • 2
    @EricB why do you need a whole copy of your production code and database just to test the syntax of a default constraint? – alroc Oct 15 '17 at 02:06
  • 1
    `DF_table_id` will be the name of the constraint, this needs to be unique in a database schema. It generally will reference the actual table name and actual column name so `DF_CareplannersMembers_CareplannersUUID` – Martin Smith Oct 15 '17 at 09:51
  • Dear Martin, that's good to know! I'll give that a try later today, and then I'll report back here. Thank you. =) All best, Eric – Eric B Oct 15 '17 at 14:26
  • Dear Martin, that was exactly what I needed to know. I was able to execute: ALTER TABLE CareplannersMembers ADD CONSTRAINT DF_CareplannersMembers_CarePlannersUUID DEFAULT (NEWID()) FOR CareplannersUUID And the application now works properly. Thank you so much for your help. PM77, Bacon Bits, and alroc, thank you very much for your time and attention. I am very grateful for your expertise and generosity. Wishing you all a peaceful and productive week. Eric – Eric B Oct 16 '17 at 03:01

0 Answers0