Index on #tmp without PK

  • Hello! 
    If I create a index on a #temp table, without create a Primary Key, the index will work ?

    It´s necessary create PK to use the index, on a #temp ?

    Thank you!

  • JoseACJr - Thursday, February 21, 2019 5:00 AM

    Hello! 
    If I create a index on a #temp table, without create a Primary Key, the index will work ?

    It´s necessary create PK to use the index, on a #temp ?

    Thank you!

    create table #temp1 (id int)
    create index ix_temp1_id on #temp1 (id)

    Muthukkumaran Kaliyamoorthy
    https://www.sqlserverblogforum.com/

  • JoseACJr - Thursday, February 21, 2019 5:00 AM

    Hello! 
    If I create a index on a #temp table, without create a Primary Key, the index will work ?

    It´s necessary create PK to use the index, on a #temp ?

    Thank you!

    Yes, you don't need a primary key.
    It's no different to creating an index on a normal table.

  • You can, but you shouldn't.  If you're creating only one index on a temp table, make it a clustered index not a non-clus index; the key columns don't have to qualify to be "primary key", i.e., there can be dup values and/or null values in clus index columns.

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply