[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 680
  • Last Modified:

View Tables with multiple foreign keys from a single table.

How to create a "ContactInformation" view table in order to see all data fields from the associated foreign key table, mainly in question 3 phone numbers FK_HomePhoneID, FK_WorkPhoneID, and FK_MobilePhoneID, these foreign keys are associated with the phone table PK_PhoneID.

See attachment for details
Note: Design partly taken from larger system.
create table Account (AccountID uniqueidentifier default newsequentialid() not null, AccountNumber varchar(128) null unique, UserID varchar(128) not null, Password nvarchar(128) not null, RegistrationDate datetime default getdate() not null, constraint PK_Account primary key (AccountID));
create table PhoneType (PhoneTypeID uniqueidentifier default newsequentialid() not null, PhoneTypeCode int not null unique, Name nvarchar(50) not null, constraint PK_PhoneType primary key (PhoneTypeID));
create table Phone (PhoneID uniqueidentifier default newsequentialid() not null, Number nvarchar(50) not null, Ext nvarchar(10) null, isPrimary bit default 0 not null, PhoneTypeID uniqueidentifier not null, constraint PK_Phone primary key (PhoneID));
create table AccountContactInformation (AccountContactInformationID uniqueidentifier default newsequentialid() not null, ProfileID uniqueidentifier not null unique, ContactInformationID uniqueidentifier not null unique, constraint PK_AccountContactInformation primary key (AccountContactInformationID));
create table CountryOfCitizenship (CountryOfCitizenshipCode varchar(2) not null, Name nvarchar(100) not null unique, constraint PK_CountryOfCitizenship primary key (CountryOfCitizenshipCode));
create table ContactInformation (ContactInformationID uniqueidentifier default newsequentialid() not null, FirstName nvarchar(100) not null, LastName nvarchar(100) not null, AddressID uniqueidentifier not null, CountryOfCitizenshipCode varchar(2) not null, AccountCurrencyID uniqueidentifier not null, HomePhoneID uniqueidentifier null, WorkPhoneID uniqueidentifier null, MobilePhoneID uniqueidentifier null, constraint PK_ContactInformation primary key (ContactInformationID));
create table Profile (ProfileID uniqueidentifier default newsequentialid() not null, AccountID uniqueidentifier not null unique, constraint PK_Profile primary key (ProfileID));
create table AccountPhone (AccountPhoneID uniqueidentifier default newsequentialid() not null, ProfileID uniqueidentifier not null, PhoneID uniqueidentifier not null unique, constraint PK_AccountPhone primary key (AccountPhoneID));
alter table Phone add constraint FK_Phone_PhoneTypeID_PhoneType foreign key (PhoneTypeID) references PhoneType (PhoneTypeID);
alter table ContactInformation add constraint FK_ContactInformation_HomePhoneID_Phone foreign key (HomePhoneID) references Phone (PhoneID);
alter table ContactInformation add constraint FK_ContactInformation_WorkPhoneID_Phone foreign key (WorkPhoneID) references Phone (PhoneID);
alter table ContactInformation add constraint FK_ContactInformation_MobilePhoneID_Phone foreign key (MobilePhoneID) references Phone (PhoneID);
alter table AccountContactInformation add constraint FK_AccountContactInformation_ContactInformationID_ContactInformation foreign key (ContactInformationID) references ContactInformation (ContactInformationID);
alter table ContactInformation add constraint FK_ContactInformation_CountryOfCitizenshipCode_CountryOfCitizenship foreign key (CountryOfCitizenshipCode) references CountryOfCitizenship (CountryOfCitizenshipCode);
alter table Profile add constraint FK_Profile_AccountID_Account foreign key (AccountID) references Account (AccountID);
alter table AccountContactInformation add constraint FK_AccountContactInformation_ProfileID_Profile foreign key (ProfileID) references Profile (ProfileID);
alter table AccountPhone add constraint FK_AccountPhone_PhoneID_Phone foreign key (PhoneID) references Phone (PhoneID);
alter table AccountPhone add constraint FK_AccountPhone_ProfileID_Profile foreign key (ProfileID) references Profile (ProfileID);

Open in new window

cphone.jpg
0
_AstroBoy
Asked:
_AstroBoy
1 Solution
 
dportasCommented:
Something like this:

SELECT C.ContactInformationID, H.Number HomeNumber, W.Number WorkNumber, M.Number MobileNumber
FROM ContactInformation C
LEFT JOIN Phone H
ON C.HomePhoneID = H.PhoneID
LEFT JOIN Phone W
ON C.WorkPhoneID = W.PhoneID
LEFT JOIN Phone M
ON C.MobilePhoneID = M.PhoneID ;

My preference would be to redesign something more like the following. This makes it easier to retrieve all the numbers for a contact (one join instead of three), you aren't limited to three numbers and it doesn't require you to duplicate a number if the number is more than one category of: Home/Work/Mobile.

CREATE TABLE dbo.ContactPhone
 (ContactInformationID UNIQUEIDENTIFIER NOT NULL
  REFERENCES ContactInformation (ContactInformationID),
  PhoneID UNIQUEIDENTIFIER NOT NULL
  REFERENCES Phone (PhoneID),
  IsHome BIT NOT NULL,
  IsWork BIT NOT NULL,
  IsMobile BIT NOT NULL,
  PRIMARY KEY (ContactInformationID, PhoneID));
0
 
Mark WillsTopic AdvisorCommented:
Here you go...  That diagram is quite involved... Can only see one use really for Phone Type and that is for Account Phone. Which I also have read as being mandatory (ie inner join).

The Home, Work, and Mobile could also have extension etc and an indicator as to preferred / primary phone - can do more on that if you want...

I think all the data relationships are there matching your diagram - not every column, but that is easy to add in.



SELECT      dbo.Account.AccountID, dbo.Profile.ProfileID,
            dbo.ContactInformation.HomePhoneID, isnull(HP.Number,'') as HomePhoneNo,
            dbo.ContactInformation.WorkPhoneID, isnull(WP.Number,'') as WorkPhoneNo,
            dbo.ContactInformation.MobilePhoneID, isnull(MP.Number,'') as MobilePhoneNo,
            dbo.AccountPhone.PhoneID, isnull(AP.Number,'') as AccountPhoneNo,
            dbo.PhoneType.Name as AccountPhoneType
 
FROM        dbo.ContactInformation 
INNER JOIN  dbo.AccountContactInformation ON dbo.ContactInformation.ContactInformationID = dbo.AccountContactInformation.ContactInformationID 
INNER JOIN  dbo.CountryOfCitizenship ON dbo.ContactInformation.CountryOfCitizenshipCode = dbo.CountryOfCitizenship.CountryOfCitizenshipCode 
INNER JOIN  dbo.Profile ON dbo.AccountContactInformation.ProfileID = dbo.Profile.ProfileID 
INNER JOIN  dbo.Account ON dbo.Profile.AccountID = dbo.Account.AccountID
INNER JOIN  dbo.AccountPhone on dbo.Profile.ProfileID = dbo.AccountPhone.ProfileID    -- if account phone, why profile id ?
INNER JOIN  dbo.Phone AP ON dbo.AccountPhone.PhoneID = AP.PhoneID 
INNER JOIN  dbo.PhoneType ON dbo.Phone.PhoneTypeID = dbo.PhoneType.PhoneTypeID        -- PhoneType does seem superfluous for the rest
LEFT JOIN   dbo.Phone HP ON dbo.ContactInformation.HomePhoneID = HP.PhoneID 
LEFT JOIN   dbo.Phone WP ON dbo.ContactInformation.WorkPhoneID = WP.PhoneID 
LEFT JOIN   dbo.Phone MP ON dbo.ContactInformation.MobilePhoneID = MP.PhoneID 

Open in new window

0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now