SQLTeam.com | Weblogs | Forums

Sql create table


#1

I have to create 4 tables and link them
the tables are: Pharmaceutical companies ,pharmacies,stores and medicines
how should i assign the foreign keys ?


#2

Hi,
As long as you know the key of the linkage, then you can easily make the FK by adding the constraint. Or you can do it through the UI if its easier for you, you can simply create a DB diagram, and use drag and drop to create the FK.
Hope this helps


#3

You can use ALTER table statement to create foreignkey relations. Suppose you create primary key on pharmacy table and want to make it as foreign kry in stores table, you can use

ALTER TABLE pharmacies ADD CONSTRAINT PK_ph PRIMARY KEY CLUSTERED (pharmacy_id);

ALTER TABLE stores
ADD CONSTRAINT FK_med FOREIGN KEY (pharmacy_id)
REFERENCES oharmacies(pharmacy_id);

;


#4

thanks for the answers !
which realtions do you think i should create?
which tables should have foreing keys and which only primary in this 4 table example?


#5

That would depends on your table design, by the guess of it, maybe a company id, and store will have its own id along with the company id, then the company id is the FK.
Each medicine should have its id as well as store id, then link them by store id etc.

hope this helps