question

Abhiu avatar image
Abhiu asked

Story behind duplicate object creation ??

Hi Everyone, M trying to understand one concept , whenever we create one object (like table), table information get stored in sys.tables and respective column information is in sys.columns but there is no primary key defined in sys.tables , then how sql server understands we are creating same table again and gives us error , Table already exists Please help . Regards, Abhi kr (If possible please let me know how can i change my Screen Name :( )
sql server 2012
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Kev Riley avatar image
Kev Riley answered
The primary key will have an entry in `sys.objects`, with a type = 'PK'; and also entry in `sys.key_constraints`. The table itself will also have a row in `sys.objects`, and the `object_id` of the table will correspond to the `parent_object_id` of the primary key.
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Abhiu avatar image Abhiu commented ·
Hi Kev, correct me if m wrong , sys.objects have an entry with type = 'pk' only if we define any contraint like primary key , please help me with the below one , suppose i created a table t1 .. create table t1 ( id int ) once table got created , will have an entry in sys.tables ,sys.columns as well as in sys.objects , My question is if i again tries to create same the same table t1 it gives me an error stating like table already exists , How sql server understoods we have tried to create same table.(Whole flow i would like to understand) Thanks and Regards, Abhi
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.