my table
create table coursedept (did (pk,fk,varchar(50),notnull,
(cid (pk,fk,varchar(50),notnull) )
how to add pk and fk for same column
ive already created did(department table ) and cid(course table) with primary key i need to combined these two table
coursedept(did ,cid)
pls help me how to do it

Sachin SinghRank #66 · 26.2K reputationPosted Dec 18, 2022, 1:57 PM EST
see,
there can be unlimited number of foreign keys in a table, so did and cid can easily be marked as foreign key.
second, yes we can have two primary keys we call them composite key.
Your bridge table should look like this
create table CourseDept { DId INT NOT NULL, CId INT NOT NULL, PRIMARY KEY (DId,CId), FOREIGN KEY (DId) REFERENCES Department(DId) FOREIGN KEY (CId) REFERENCES Course(CId) }Deepak TewatiaRank #143 · 15.9K reputationPosted Dec 17, 2022, 11:46 AM EST
Hi Jaya Prakash,
you can use the following query:
ALTER TABLE coursedept ADD PRIMARY KEY (did), -- add primary key ADD FOREIGN KEY (cid) REFERENCES course(cid); -- add foreign keyMahesh ChandRank #1 · 393.3K reputationPosted Dec 18, 2022, 12:15 AM EST
Here is the detailed tutorial on this topic: Constraints in SQL Server (c-sharpcorner.com)
https://www.c-sharpcorner.com/UploadFile/f0b2ed/constraints-in-sql-server/
Rajeev KumarRank #1,334 · 1.3K reputationPosted Feb 22, 2023, 12:36 AM EST
CREATE TABLE coursedept ( DId NVARCHAR(10) NOT NULL, CId NVARCHAR(10) NOT NULL, PRIMARY KEY (DId,CId), FOREIGN KEY (DId) REFERENCES Department(DId), FOREIGN KEY (CId) REFERENCES Course(CId))