Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. -- You should determine which are the most fitting
  2. -- data types and sizes for all your table columns
  3. -- depending on your business context characteristics.
  4.  
  5. -- As one would expect, you are free to make use of
  6. -- your preferred (or required) naming conventions.
  7.  
  8. CREATE TABLE Person (
  9. PersonId INT NOT NULL,
  10. FirstName CHAR(30) NOT NULL,
  11. LastName CHAR(30) NOT NULL,
  12. GenderCode CHAR(3) NOT NULL,
  13. BirthDate DATE NOT NULL,
  14. CreatedDateTime DATETIME NOT NULL,
  15. --
  16. CONSTRAINT Person_PK PRIMARY KEY (PersonId),
  17. CONSTRAINT Person_AK UNIQUE (
  18. FirstName,
  19. LastName,
  20. GenderCode,
  21. BirthDate
  22. )
  23. );
  24.  
  25. CREATE TABLE Survey (
  26. SurveyNumber INT NOT NULL,
  27. Description CHAR(255) NOT NULL,
  28. CreatedDateTime DATETIME NOT NULL,
  29. --
  30. CONSTRAINT Survey_PK PRIMARY KEY (SurveyNumber),
  31. CONSTRAINT Survey_AK UNIQUE (Description)
  32. );
  33.  
  34. CREATE TABLE PersonSurvey (
  35. PersonId INT NOT NULL,
  36. SurveyNumber INT NOT NULL,
  37. RegisteredDateTime DATETIME NOT NULL,
  38. --
  39. CONSTRAINT PersonSurvey_PK PRIMARY KEY (PersonId, SurveyNumber),
  40. CONSTRAINT PersonSurveyToPerson_FK FOREIGN KEY (PersonId)
  41. REFERENCES Person (PersonId),
  42. CONSTRAINT PersonSurveyToSurvey_FK FOREIGN KEY (SurveyNumber)
  43. REFERENCES Survey (SurveyNumber)
  44. );
  45.  
  46. CREATE TABLE Question (
  47. QuestionNumber INT NOT NULL,
  48. Wording CHAR(255) NOT NULL,
  49. CreatedDateTime DATETIME NOT NULL,
  50. --
  51. CONSTRAINT Question_PK PRIMARY KEY (QuestionNumber),
  52. CONSTRAINT Question_AK UNIQUE (Wording)
  53. );
  54.  
  55. CREATE TABLE SurveyQuestion (
  56. SurveyNumber INT NOT NULL,
  57. QuestionNumber INT NOT NULL,
  58. PresentationOrder TINYINT NOT NULL,
  59. IsMandatory BIT NOT NULL,
  60. IntegratedDateTime DATETIME NOT NULL,
  61. --
  62. CONSTRAINT SurveyQuestion_PK PRIMARY KEY (SurveyNumber, QuestionNumber),
  63. CONSTRAINT SurveyQuestion_AK UNIQUE (
  64. QuestionNumber,
  65. SurveyNumber,
  66. PresentationOrder
  67. ),
  68. CONSTRAINT SurveyQuestionToSurvey_FK FOREIGN KEY (SurveyNumber)
  69. REFERENCES Survey (SurveyNumber),
  70. CONSTRAINT SurveyQuestionToQuestion_FK FOREIGN KEY (QuestionNumber)
  71. REFERENCES Question (QuestionNumber)
  72. );
  73.  
  74. CREATE TABLE Response (
  75. SurveyNumber INT NOT NULL,
  76. QuestionNumber INT NOT NULL,
  77. PersonId INT NOT NULL,
  78. Content TEXT NOT NULL,
  79. ProvidedDateTime DATETIME NOT NULL,
  80. --
  81. CONSTRAINT Response_PK PRIMARY KEY (SurveyNumber, QuestionNumber, PersonId),
  82. CONSTRAINT ResponseToPersonSurvey_FK FOREIGN KEY (PersonId, SurveyNumber)
  83. REFERENCES PersonSurvey (PersonId, SurveyNumber),
  84. CONSTRAINT ResponseToSurveyQuestion_FK FOREIGN KEY (SurveyNumber, QuestionNumber)
  85. REFERENCES SurveyQuestion (SurveyNumber, QuestionNumber)
  86. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement