2011-10-14 108 views
0

我的問題是:請注意,我將MvrId放在很多表格中。你對下面的設想有什麼看法?我該如何改進它?如果你指給我一本書,我希望是一本書的一頁。 enter image description here 如果你想看到整個事情,你可以在名爲MedicalVariance的數據庫上執行這個腳本。數據庫設計SQL

USE MedicalVariance; 
    --This is a quick install script 
    --I guess you could even execute this from the front end but that would be overkill 
    --Because of the audience that will install this software are DBA's 

IF EXISTS 
(
--The query below will will evaluate to true if it finds a foreign key constraint. 
    SELECT 1 From INFORMATION_SCHEMA.TABLE_CONSTRAINTS 
    WHERE CONSTRAINT_TYPE LIKE 'FOREIGN KEY' 
) 
BEGIN 
    DECLARE @TableName  NVARCHAR(100) 
    DECLARE @ConstraintName NVARCHAR(100) 
    DECLARE @DynamicSQLEXEC NVARCHAR(300) 
    --DECLARE AND FEED THE CURSOR DATA 
    Declare ConstraintCursor CURSOR FAST_FORWARD FOR 
    -- Dont worry I wont drop your precious FOREIGN KEYS since the catalog must be MedicalVariance 
    SELECT TABLE_NAME,CONSTRAINT_NAME 
    FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS 
    WHERE CONSTRAINT_TYPE LIKE 'FOREIGN KEY' 
      AND 
      CONSTRAINT_CATALOG LIKE 'MedicalVariance' 

    --OPEN THE CURSOR 
    OPEN ConstraintCursor 
    FETCH NEXT FROM ConstraintCursor 
    INTO @TableName, @ConstraintName 

    --NOW IMPLEMENT THE LOGIC TO DROP ALL CONSTRAINTS 
    WHILE @@FETCH_STATUS =0 
    BEGIN 

     --DYNAMIC SQL IS A PAIN IF YOU THINK YOU GOT BETTER SYNTAX GO FOR IT 
     SET @DynamicSQLEXEC ='ALTER TABLE '-- the space is important 
     SET @DynamicSQLEXEC [email protected] + @TableName + ' ' 
     SET @DynamicSQLEXEC [email protected] + 'DROP CONSTRAINT ' 
     SET @DynamicSQLEXEC [email protected] + @ConstraintName 
     PRINT @DynamicSQLEXEC -- make sure this is correct sql syntax 
     EXEC(@DynamicSQLEXEC) 
     -----------------------Dynamic SQL ENDS------------------------------------- 
     FETCH NEXT FROM ConstraintCursor 
     INTO @TableName, @ConstraintName 
    END; 
    CLOSE ConstraintCursor 
    DEALLOCATE ConstraintCursor 

END; 
GO 


IF EXISTS 
    (
      SELECT 1 FROM Information_Schema.Tables 
      WHERE Table_Name = 'MvrMeds' 
    ) 
      BEGIN 

       DROP TABLE MvrMeds 
      END; 
      GO 
IF EXISTS 
    (
      SELECT 1 FROM Information_Schema.Tables 
      WHERE Table_Name = 'Mvr' 
    ) 
      BEGIN 
       DROP TABLE dbo.Mvr 
      END; 
      GO 
IF EXISTS 
    (
      SELECT 1 FROM Information_Schema.Tables 
      WHERE Table_Name = 'MvrMedsAdminRoute' 
    ) 
      BEGIN 
       DROP TABLE dbo.MvrMedsAdminRoute 
      END; 
      GO 
IF EXISTS 
    (
      SELECT 1 FROM Information_Schema.Tables 
      WHERE Table_Name = 'MvrMedsPrescribingErrors' 
    ) 
      BEGIN 
       DROP TABLE dbo.MvrMedsPrescribingErrors 
      END; 
      GO 
IF EXISTS 
    (
      SELECT 1 FROM Information_Schema.Tables 
      WHERE Table_Name = 'MvrMedsTranscribingErrors' 
    ) 
      BEGIN 
       DROP TABLE dbo.MvrMedsTranscribingErrors 
      END; 
      GO 
IF EXISTS 
    (
      SELECT 1 FROM Information_Schema.Tables 
      WHERE Table_Name = 'MvrMedsProductIssuesErrors' 
    ) 
      BEGIN 
       DROP TABLE dbo.MvrMedsProductIssuesErrors 
      END; 
      GO 
IF EXISTS 
    (
      SELECT 1 FROM Information_Schema.Tables 
      WHERE Table_Name = 'MvrMedsProcumentErrors' 
    ) 
      BEGIN 
       DROP TABLE dbo.MvrMedsProcumentErrors 
      END; 
      GO 
IF EXISTS 
    (
      SELECT 1 FROM Information_Schema.Tables 
      WHERE Table_Name = 'MvrMedsDispensingErrors' 
    ) 
      BEGIN 
       DROP TABLE dbo.MvrMedsDispensingErrors 
      END; 
      GO 
IF EXISTS 
    (
      SELECT 1 FROM Information_Schema.Tables 
      WHERE Table_Name = 'MvrMedsAdministrationErrors' 
    ) 
      BEGIN 
       DROP TABLE dbo.MvrMedsAdministrationErrors 
      END; 
      GO 

    IF EXISTS 
    (
      SELECT 1 FROM Information_Schema.Tables 
      WHERE Table_Name = 'MvrMedsDocumentationErrors' 
    ) 
      BEGIN 
       DROP TABLE dbo.MvrMedsDocumentationErrors 
      END; 
      GO 

    IF EXISTS 
    (
      SELECT 1 FROM Information_Schema.Tables 
      WHERE Table_Name = 'MvrEmployees' 
    ) 
      BEGIN 
       DROP TABLE dbo.MvrEmployees 
      END; 
      GO   

    IF EXISTS 
    (
      SELECT 1 FROM Information_Schema.Tables 
      WHERE Table_Name = 'MvrCommunicationErrors' 
    ) 
      BEGIN 
       DROP TABLE dbo.MvrCommunicationErrors 
      END; 
      GO 
-- The way I am putting MvrId in almost every table 
-- Do you recommend it or bless it as good desing? 


CREATE TABLE Mvr 
(
    MvrId INT NOT NULL PRIMARY KEY 
) 
    CREATE TABLE MvrMedsAdminRoute 
(
    MvrMedsAdminRouteId INT NOT NULL PRIMARY KEY, 
    MvrId INT 
) 
CREATE TABLE MvrMeds 
( 
    MvrMedsId INT NOT NULL PRIMARY KEY, 
    MvrId INT , 
    MvrMedsAdminRouteId INT , 
    CONSTRAINT MvrMeds_Mvr_FK FOREIGN KEY(MvrId) REFERENCES dbo.Mvr(MvrID), 
    CONSTRAINT MvrMeds_MvrMedsAdminRoute_FK FOREIGN KEY (MvrMedsAdminRouteId) REFERENCES dbo.MvrMedsAdminRoute(MvrMedsAdminRouteId) 
) 

CREATE TABLE MvrMedsPrescribingErrors 
(
    MvrPrescribingErrorId INT NOT NULL PRIMARY KEY, 
    MvrMedsId  INT , 
    MvrId INT 
    CONSTRAINT MvrMedsPrescribingErrors_MvrMeds_FK FOREIGN KEY (MvrMedsId) REFERENCES dbo.MvrMeds(MvrMedsId) 
) 

CREATE TABLE MvrMedsTranscribingErrors 
(
    MvrTranscribingErrorsId INT NOT NULL PRIMARY KEY, 
    MvrMedsId INT , 
    MvrId INT 
    CONSTRAINT MvrMedsTranscribingErrors_MvrMeds_FK FOREIGN KEY (MvrMedsId) REFERENCES dbo.MvrMeds(MvrMedsId) 
) 
CREATE TABLE MvrMedsProductIssuesErrors 
(
    MvrTranscribingErrorsId INT NOT NULL PRIMARY KEY, 
    MvrMedsId INT , 
    MvrId INT 
    CONSTRAINT MvrMedsProductIssuesErrors_MvrMeds_FK FOREIGN KEY (MvrMedsId) REFERENCES dbo.MvrMeds(MvrMedsId) 
) 

CREATE TABLE MvrMedsProcumentErrors 
(
    MvrProcumentErrorsId INT NOT NULL PRIMARY KEY, 
    MvrMedsId INT, 
    MvrId INT 
    CONSTRAINT MvrMedsOrderingProcumentErrors_MvrMeds_FK FOREIGN KEY (MvrMedsId) REFERENCES dbo.MvrMeds(MvrMedsId) 
) 
CREATE TABLE MvrMedsDispensingErrors 
(
    MvrDispensingErrorsId INT NOT NULL PRIMARY KEY, 
    MvrMedsId INT, 
    MvrId INT 
    CONSTRAINT MvrMedsDispensingErrors_MvrMeds_FK FOREIGN KEY (MvrMedsId) REFERENCES dbo.MvrMeds(MvrMedsId) 
) 

CREATE TABLE MvrMedsAdministrationErrors 
(
    MvrAdministrationErrorsId INT NOT NULL PRIMARY KEY, 
    MvrMedsId INT, 
    MvrId INT 
    CONSTRAINT MvrMedsAdministrationErrors_MvrMeds_FK FOREIGN KEY (MvrMedsId) REFERENCES dbo.MvrMeds(MvrMedsId) 
) 

CREATE TABLE MvrMedsDocumentationErrors 
(
    MvrDocumentationErrorsId INT NOT NULL PRIMARY KEY, 
    MvrMedsId INT, 
    MvrId INT 
    CONSTRAINT MvrMedsDocumentationErrors_MvrMeds_FK FOREIGN KEY (MvrMedsId) REFERENCES dbo.MvrMeds(MvrMedsId) 
) 


----EMPLOYEES 
--ONLY EMPLOYEES CAN BE PART OF MVR? 
CREATE TABLE MvrEmployees 
(
    MvrEmployeesId INT PRIMARY KEY, 
    MvrId INT, 
    CONSTRAINT MvrEmployees_Mvr_FK FOREIGN KEY (MvrId) REFERENCES dbo.Mvr(MvrId) 
) 

CREATE TABLE MvrCommunicationErrors 
(
    MvrCommunicationErrorsId INT NOT NULL PRIMARY KEY, 
    MvrEmployeesId INT, 
    MvrId INT, 
    CONSTRAINT MvrCommunicationErrors_MvrEmployees_FK FOREIGN KEY (MvrEmployeesId) REFERENCES dbo.MvrEmployees(MvrEmployeesId) 
) 
+2

如果你問一個問題,請不要轉儲大量的代碼,並問人們他們對它的看法。詢問其他人會理解的具體問題。 –

+0

如果每個表都與核心對象相關,那麼每個表中的特定字段沒有任何問題。除此之外,我不知道該說什麼...... – mellamokb

+0

謝謝,我想我問了一個具體的問題。問題是你如何看待像mellamokb這樣的每個桌子上放置一個特定領域的問題。該代碼僅供參考。 – hidden

回答

1

如果所有表中的MvrId表來自主鍵MvrId,你應該對所有MvrId列的外鍵約束。至少如果你想控制其他表中使用的MvrId

你還沒有具體說明爲什麼你爲所有這些表添加了MvrId,並且取決於你希望如何使用這些值,這可能是必要的,或者它可能是一個壞主意。

有必要的是,如果它添加一些關於存儲實體的信息,那麼如果可以使用與另一個表的關係來檢索信息,則這是不必要的。

例如:MvrMedsPrescribingErrorsMvrMeds與FK MvrMedsId的子表。如果您可以使用MvrMedsIdMvrMedsPrescribingErrors找到關聯MvrIdMvrMeds然後我不會MvrIdMvrMedsPrescribingErrors存儲。

但如果在另一方面,你可以存儲MvrId的在MvrMedsPrescribingErrors是不一樣的相關MvrMedsMvrId那麼它是完全必要的MvrIdMvrMedsPrescribingErrors

+0

事情是MvrId表單是在審查階段創建的。有人填寫表格併產生一個MvrId。然後一個月後有人評論MvrId並在記錄中放入更多數據。所以,如果我把一個約束,它會迫使我在同一時間插入記錄是嗎? – hidden

+0

@jvelez - 不,你只需要確保你先向Mvr添加一行。 –

+0

好的,謝謝。您的意見幫助我開發出了更好的設計。 – hidden