0

For example

create table Equipment (

Equipmenttype nvarchar(1,
Description nvarchar(10),
Primary Key (Equipmenttype)
)

Create table Room (

RoomID nvarchar(8),
Capacity numeric(3),
Roomtype(fk,nvarchar(2)

)

I want to create the following table...

create table RoomEquipment(
(RoomID here)
(Equipmenttype here)

but Equipmenttype is not a primary key.

Raibaz
  • 9,280
  • 10
  • 44
  • 65
salman12
  • 21
  • 1
  • 7

3 Answers3

2

You can create foreign key on columns that are PK or Unuque:

So you have to create a unique index on RoomID:

CREATE UNIQUE INDEX UI_Room_RoomID ON dbo.Room(RoomID); 

Now you can create a foreign key in table RoomEquipment.

Giorgi Nakeuri
  • 35,155
  • 8
  • 47
  • 75
2

A FOREIGN KEY constraint does not have to be linked only to a PRIMARY KEY constraint in another table; it can also be defined to reference the columns of a UNIQUE constraint in another table.

Source: Foreign Key to non-primary key

Community
  • 1
  • 1
Adish
  • 709
  • 4
  • 12
0

Equipmenttype is a primary key, in its own table Equipment, and RoomID should be a primary key for Room.

So RoomEquipment is a standard many-to-many relationship table.

Lorenzo Gatti
  • 1,260
  • 1
  • 10
  • 15