DEV Community

Chris Richmond
Chris Richmond

Posted on • Edited on

SQL Primary Keys

Problem:
You need to add a primary Key to a table.

Solution:
Heres a few types I've run across.

Manual Primary Key

CREATE TABLE [dbo].[HtmlDtos]([Id] [int] PRIMARY KEY)

Auto Sequential Int Primary Key

CREATE TABLE [dbo].[HtmlDtos]([Id] [int] IDENTITY(1,1) PRIMARY KEY)

Auto Sequential GUID PK

CREATE TABLE [dbo].[HtmlDtos] ([Id] UNIQUEIDENTIFIER DEFAULT NEWSEQUENTIALID() PRIMARY KEY)

Auto Random GUID PK

CREATE TABLE [dbo].[HtmlDtos]([Id] UNIQUEIDENTIFIER DEFAULT NEWID() PRIMARY KEY)

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay