Table design question

  • Hi,

    Below my sample table schema,

    Create table Company (CompanyId int primary key identity(1,1),CompanyName varchar(100));

    Create table Messages(MessageID int primary key identity(1,1),MessageName varchar(100));

    Create table Company_Messages(...........)

    I need to create relational table (Company_Messages) table based on company and Messages. The condition here is companyId and MessageID pair should not be repeated in the

    Company_Messages table. I can create this with companyId,MessageID as primary key. But how do i enforce the referential integrity here. CompId and Message ID should refrence their primaty key table.

    how do i design table for this scenario. Any suggestion please

  • Something like this? Or I misunderstood the question?

    CREATE TABLE Company_Messages(

    CompanyId INT,

    MessageID INT,

    CONSTRAINT PK_Company_Messages PRIMARY KEY (CompanyId, MessageId),

    CONSTRAINT FK_Company_Messages_Company FOREIGN KEY (CompanyId) REFERENCES Company(CompanyId),

    CONSTRAINT FK_Company_Messages_Messages FOREIGN KEY (MessageId) REFERENCES Messages(MessageId));

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Great, i am looking for this only. i had this in my mind but didn't know i can create like this.

    Thanks Luiz for confirming. appreciated.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply