I'm going to try to describe this situation as best as I can, but if
anything is unclear please let me know. Some of this database was
already in place before I arrived on the scene, and I don't really
have the buyin to change it.
There is a table of certificates that a person can hold. A person may
hold certificates of various types. I need to further group a person's
certificates together so that (for example) renewal dates can be
synchronized. There are only certain types of certificates that can be
linked together, which is defined in another set of tables. Here's a
first-go of the model. I've eliminated irrelevant columns from some of
the tables.
-- This table already exists as-is
CREATE TABLE dbo.Person (
ID INT NOT NULL IDENTITY,
FirstName VARCHAR(30) NULL,
LastName VARCHAR(30) NULL)
GO
ALTER TABLE dbo.Person
ADD CONSTRAINT PK_Person PRIMARY KEY (ID)
GO
-- This table already exists as-is
CREATE TABLE dbo.Certificate (
ID INT NOT NULL IDENTITY,
PersonID INT NOT NULL,
CertificateType ID INT NOT NULL) -- The table this FKs to is
just an ID and description. Not included here for brevity reasons
GO
ALTER TABLE dbo.Certificate
ADD CONSTRAINT PK_Certificate PRIMARY KEY (ID)
GO
ALTER TABLE dbo.Certificate
ADD CONSTRAINT FK_CertificateP erson FOREIGN KEY (PersonID) REFERENCES
dbo.Person (ID)
GO
-- This table is new and represents which types of certificates can be
linked and synchronized
CREATE TABLE dbo.CCRTypeCert ificateType (
CCRTypeID INT NOT NULL, -- This FKs to a table that is
mostly just an ID and description
CertificateType ID INT NOT NULL)
GO
ALTER TABLE dbo.CCRTypeCert ificateType
ADD CONSTRAINT PK_CCRTypeCerti ficateType PRIMARY KEY (CCRTypeID,
CertificateType ID)
GO
-- This table defines which actual certificates are linked together
CREATE TABLE dbo.CCRCertific ate (
CCRTypeID INT NOT NULL,
PersonID INT NOT NULL,
PrimaryCertific ationID INT NOT NULL, -- This is necessary
because a person can let a certification lapse and then start a new
one of the same type as the old
CertificationID INT NOT NULL )
GO
ALTER TABLE dbo.CCRCertific ate
ADD CONSTRAINT PK_CCRCertifica te PRIMARY KEY (CCRTypeID, PersonID,
PrimaryCertific ationID, CertificationID )
GO
Other tables hang off of the CCRCertificate table. The PersonID and
CertificationID in the same table seems to violate Boyce-Codd Normal
Form, so I've been asked to remove the PersonID (not that BCNF was the
reason given of course, but it's "duplicate data" since the
PrimaryCertific ationID should already determine the PersonID).
What I'd like to do is:
1. Constrain the CCRCertificate table (or use additional table(s) if
necessary) so that all certifications linked as CertificationID are
from the same person.
2. Require that at most 1 certificate of each type in
CCRTypeCertific ateType for a CCRType is added to CCRCertificate. In
other words, if the CCRTypeCertific ateType table indicates that a
CCRType 1 is made up of CertificationTy pe 1 and CertificationTy pe3,
the CCRCertificate table should only include at most one certification
that has a CertificationTy peID of 1 and one that has an ID of 3 if the
CCRTypeID is 1.
I hope this isn't too confusing. Let me know if there's anything that
I can make clearer.
Thanks!
-Tom.
P.S. The SQL here is for illustrative purposes. I'm on a laptop that
doesn't have SQL Server installed, so I can't test the SQL at the
moment. Since I'm not asking for any code that might need to be tested
I hope this isn't an issue.
anything is unclear please let me know. Some of this database was
already in place before I arrived on the scene, and I don't really
have the buyin to change it.
There is a table of certificates that a person can hold. A person may
hold certificates of various types. I need to further group a person's
certificates together so that (for example) renewal dates can be
synchronized. There are only certain types of certificates that can be
linked together, which is defined in another set of tables. Here's a
first-go of the model. I've eliminated irrelevant columns from some of
the tables.
-- This table already exists as-is
CREATE TABLE dbo.Person (
ID INT NOT NULL IDENTITY,
FirstName VARCHAR(30) NULL,
LastName VARCHAR(30) NULL)
GO
ALTER TABLE dbo.Person
ADD CONSTRAINT PK_Person PRIMARY KEY (ID)
GO
-- This table already exists as-is
CREATE TABLE dbo.Certificate (
ID INT NOT NULL IDENTITY,
PersonID INT NOT NULL,
CertificateType ID INT NOT NULL) -- The table this FKs to is
just an ID and description. Not included here for brevity reasons
GO
ALTER TABLE dbo.Certificate
ADD CONSTRAINT PK_Certificate PRIMARY KEY (ID)
GO
ALTER TABLE dbo.Certificate
ADD CONSTRAINT FK_CertificateP erson FOREIGN KEY (PersonID) REFERENCES
dbo.Person (ID)
GO
-- This table is new and represents which types of certificates can be
linked and synchronized
CREATE TABLE dbo.CCRTypeCert ificateType (
CCRTypeID INT NOT NULL, -- This FKs to a table that is
mostly just an ID and description
CertificateType ID INT NOT NULL)
GO
ALTER TABLE dbo.CCRTypeCert ificateType
ADD CONSTRAINT PK_CCRTypeCerti ficateType PRIMARY KEY (CCRTypeID,
CertificateType ID)
GO
-- This table defines which actual certificates are linked together
CREATE TABLE dbo.CCRCertific ate (
CCRTypeID INT NOT NULL,
PersonID INT NOT NULL,
PrimaryCertific ationID INT NOT NULL, -- This is necessary
because a person can let a certification lapse and then start a new
one of the same type as the old
CertificationID INT NOT NULL )
GO
ALTER TABLE dbo.CCRCertific ate
ADD CONSTRAINT PK_CCRCertifica te PRIMARY KEY (CCRTypeID, PersonID,
PrimaryCertific ationID, CertificationID )
GO
Other tables hang off of the CCRCertificate table. The PersonID and
CertificationID in the same table seems to violate Boyce-Codd Normal
Form, so I've been asked to remove the PersonID (not that BCNF was the
reason given of course, but it's "duplicate data" since the
PrimaryCertific ationID should already determine the PersonID).
What I'd like to do is:
1. Constrain the CCRCertificate table (or use additional table(s) if
necessary) so that all certifications linked as CertificationID are
from the same person.
2. Require that at most 1 certificate of each type in
CCRTypeCertific ateType for a CCRType is added to CCRCertificate. In
other words, if the CCRTypeCertific ateType table indicates that a
CCRType 1 is made up of CertificationTy pe 1 and CertificationTy pe3,
the CCRCertificate table should only include at most one certification
that has a CertificationTy peID of 1 and one that has an ID of 3 if the
CCRTypeID is 1.
I hope this isn't too confusing. Let me know if there's anything that
I can make clearer.
Thanks!
-Tom.
P.S. The SQL here is for illustrative purposes. I'm on a laptop that
doesn't have SQL Server installed, so I can't test the SQL at the
moment. Since I'm not asking for any code that might need to be tested
I hope this isn't an issue.
Comment