Back-To-School Deals - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Default values
How to auto-generate values in a database
13. NEWID()—exercise
Summary

Instruction

Good job! Now that you're familiar with the uniqueidentifier data type and the NEWID() function, let's practice using them. In the statement below, we've put NEWID() in the place where we'd usually write a value for the Id column:

INSERT INTO Post (Id, AuthorId, Title, Text, ModifiedDateTime) 
VALUES (NEWID(), '31E01D66-7473-4C6A-BE69-C2E92C430A65', N'A celebrity''s holiday', N'Lucy, a celebrity, is going to spend lots of time on holiday in Japan.', DEFAULT);

Whenever you call the NEWID() function, it will return a unique value. This is true no matter what table you're working with because the NEWID() function randomly generates GUIDs.

Exercise

We changed the data type of the Id column in the Author table to uniqueidentifier. Use NEWID() to generate the next value in the Id column. The new author's name is Anne Nowak.