creation of a composite KP

Asked

Viewed 85 times

0

I have two tables: tbl1 and tbl2. In the tbl1 I own a pk and in the tbl2 I own a FK which refers to PK of tbl1 and a pk. With this, I can make a pk composed for the tbl2 with these 2 fields ?

EX: table cliente field (id cliente (pk), nome, idade)

table produto field (id cliente(fk), id produto (pk), nome, valor)

would like to create a PK composed in the product table using the FK and the PK

I hope I have made clear my doubt :v .

1 answer

1


Yes, it is possible.

You need to remove the current PK Constraint and add a new PK.

alter table produto drop CONSTRAINT <nome_constraint>
alter table produto add primary key (id_cliente, id_produto)

To know the name of the Constraint you can use the query below:

select OBJECT_NAME(OBJECT_ID) AS NomeConstraint
FROM sys.objects
where OBJECT_NAME(parent_object_id)='produto'
and type_desc LIKE '%CONSTRAINT'
  • This is the best method

  • Thanks for the help

  • In another scenario I could use 2 FK to create a correct compound PK ?

  • @Gabrielsilva Sim

  • 1

    If the answer has been useful, give upvote. If you have solved the problem, mark it as correct. This helps future users with the same issue identify how to solve. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.