Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Primary Key in SQL Server Table Variable

Primary Key in SQL Server Table Variable

Scheduled Pinned Locked Moved Database
databasesql-serversysadminquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    John Gathogo
    wrote on last edited by
    #1

    Is there any benefit in setting a primary key in a table variable? Especially if you will perform SELECT and INSERT operations on it? Like, DECLARE @Tbl TABLE(Id INT PRIMARY KEY, Name NVARCHAR(36))

    L 1 Reply Last reply
    0
    • J John Gathogo

      Is there any benefit in setting a primary key in a table variable? Especially if you will perform SELECT and INSERT operations on it? Like, DECLARE @Tbl TABLE(Id INT PRIMARY KEY, Name NVARCHAR(36))

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      John Gathogo wrote:

      Is there any benefit in setting a primary key in a table variable?

      Yes, mainly to ensure referential integrity. That's probably an unsatisfying answer. Imagine that your table only has "name" defined in it. We could add "John" as a record, and have a list of things that John should do in some other table. Might look like this;

      TABLE Users (Name NVARCHAR(36))
      "John"
      "Peter"

      TABLE Tasks (Name NVARCHAR(36), ThingToDo NVARCHAR(50))
      "John", "Water the plants"
      "John", "Cook dinner"
      "Peter", "Write an article for CodeProject"

      We're in trouble as soon as someone adds another user with the firstname "John"; we would no longer be able to differentiate between the tasks of the first "John" and the second "John". Now, it's a "best practice" to define a set of column-names that make a record unique. We could add the [Birthdate] of the user to the "Users" table, that way we could distingiush between the first and the second Johnny. It's easier[^] to just add a generated identity and to use that as a primary key. A primary key is often accompanied by a clustered index. There's a heap of information on MSDN[^], if that is your kind of thing :)

      I are Troll :suss:

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups