Composite primary key using a foreign key

1

Is it possible to create a composite primary key using a foreign key?

I have the table mov_estoque , where I thought of putting id_mov and id_prod as the composite key, however, id_prod is a foreign key of the products table.

I tried:

ALTER TABLE mov_estoque
DROP PRIMARY KEY, ADD PRIMARY KEY (id,id_pro);

But I get:

  

1832 - Can not change column 'id_pro': used in a foreign key constraint 'mov_estoque_ibfk_2'

    
asked by anonymous 15.02.2018 / 18:01

1 answer

2
ALTER TABLE NomeDaTabela
ADD CONSTRAINT PK_NomeDaPK PRIMARY KEY CLUSTERED (Coluna1, Coluna2)

If you want to add the PK during table creation, use this syntax:

CREATE TABLE NomeDaTabela (
   Coluna1 INT,
   Coluna2 INT,
   CONSTRAINT PK_NomeDaPK PRIMARY KEY (Coluna1, Coluna2)
)
    
15.02.2018 / 19:16