Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.86 KB | None | 0 0
  1. Question 1: Defining the Database
  2.  
  3. Banks:
  4. Primary key = (BankName, City)
  5. Justification: There can be banks with the same name in different cities so both attributes are needed to uniquely identify an entry
  6.  
  7. Foreign Key = NA
  8. Justification: Does not reference any other tables
  9.  
  10. CREATE TABLE Banks (BankName CHAR(25) NOT NULL, City CHAR(25) NOT NULL, NoAccounts INT, Security CHAR CONSTRAINT scr CHECK (Security IN (‘weak’,’good’, ‘very good’, ‘excellent’)), PRIMARY KEY (BankName, City));
  11.  
  12. Robberies:
  13. Primary key = (BankName, City, Date)
  14. Justification: The same bank can be robbed multiple times so the Date attribute is needed to uniquely identify an entry
  15.  
  16. Foreign Key = (BankName, City)
  17. Justification: Robberies references Banks so this foreign key is needed to uniquely identify the bank at which the robbery took place
  18.  
  19. CREATE TABLE Robberies (BankName CHAR(25) NOT NULL, City CHAR(25) NOT NULL, Date DATE NOT NULL CONSTRAINT rdRange CHECK (Date < ‘2017-10-01’), Amount INT CONSTRAINT raRange CHECK (Amount >= 0), PRIMARY KEY (BankName, City, Date), FOREIGN KEY (BankName, City) REFERENCES Banks ON DELETE NO ACTION ON UPDATE CASCADE);
  20.  
  21. Foreign Key actions justification:
  22.  
  23. (BankName, City): ON DELETE NO ACTION because if a Bank is deleted then the data of the Robberies would be deleted, and we want to prevent this
  24. ON UPDATE CASCADE because if the details of a Bank are updated then they should be updated in Robberies also
  25.  
  26. Plans:
  27. Primary key = (BankName, City, PlannedDate)
  28. Justification: The same bank can be robbed multiple times so the PlannedDate attribute is needed to uniquely identify an entry
  29.  
  30. Foreign Key = (BankName, City)
  31. Justification: Plans references Banks so this foreign key is needed to uniquely identify the bank at which the robbery will take place
  32.  
  33. CREATE TABLE Plans (BankName CHAR(25) NOT NULL, City CHAR(25) NOT NULL, PlannedDate DATE NOT NULL CONSTRAINT ppdRange CHECK (Date >= ‘2017-10-01’), NoRobbers INT, PRIMARY KEY (BankName, City, PlannedDate), FOREIGN KEY (BankName, City) REFERENCES Banks ON DELETE CASCADE ON UPDATE CASCADE);
  34.  
  35. Foreign Key actions justification:
  36.  
  37. (BankName, City): ON DELETE CASCADE because if a Bank is deleted then a Plan can no longer take place at the bank
  38. ON UPDATE CASCADE because if the details of a Bank are updated then they should be updated in Plans also
  39.  
  40.  
  41.  
  42. Robbers:
  43. Primary key = (RobberID)
  44. Justification: A Robber can be uniquely identified with a RobberID, whereas using the Nickname attribute as the primary key might not work as Robbers can have the same Nickname
  45.  
  46. Foreign Key = NA
  47. Justification: Does not reference any other tables
  48.  
  49. CREATE TABLE Robbers (RobberID INT NOT NULL CONSTRAINT rIdRange CHECK (RobberID >= 0 AND RobberID <= 1000), Nickname CHAR(25) NOT NULL, Age INT NOT NULL CONSTRAINT rAgeRange CHECK (Age >= 0 AND Age <=120), NoYears INT CONSTRAINT rNoYearsRange CHECK (NoYears <= Age), PRIMARY KEY (RobberID));
  50.  
  51. Skills:
  52. Primary key = (SkillID)
  53. Justification: A Skill can be uniquely identified with a SkillD
  54.  
  55. Foreign Key = NA
  56. Justification: Does not reference any other tables
  57.  
  58. CREATE TABLE Skills (SkillID INT NOT NULL CONSTRAINT SIdRange CHECK (SkillID >= 0 AND SkillID <= 100), Description CHAR(25) NOT NULL, PRIMARY KEY (SkillID));
  59.  
  60.  
  61. HasSkills:
  62. Primary key = (RobberID, SkillID)
  63. Justification: A Robber can be uniquely identified with a RobberID and a Skill can be uniquely identified with a SkillID so a Robbers Skills can be uniquely identified with a primary key combining these two attributes
  64.  
  65. Foreign Keys = (RobberID), (SkillID)
  66. Justification: HasSkills references Robbers and Skills, so RobbersID and SkillsID are needed as foreign keys
  67.  
  68. CREATE TABLE HasSkills (SkillID INT NOT NULL, RobberID INT NOT NULL, Preference INT CONSTRAINT hspRange CHECK (Preference IN (1,2,3)), Grade CHAR(2) CONSTRAINT hsgi CHECK (Grade IN (‘A’,’A+’,‘B’,’B+’,’C’,’C+’,NULL)), PRIMARY KEY (SkillID, RobberID), FOREIGN KEY (SkillID) REFERENCES Skills ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY (RobberID) REFERENCES Robbers ON DELETE CASCADE ON UPDATE CASCADE);
  69.  
  70. Foreign Key actions justification:
  71.  
  72. (RobberID): ON DELETE CASCADE because if a Robber is removed from Robbers then they shouldn’t be in HasSkills anymore as it is strictly for the Skills of Robbers
  73. ON UPDATE CASCADE because if the RobberID is updated in Robbers then it should also be updated in HasSkills
  74.  
  75. (SkillsID): ON DELETE CASCADE because if a Skill is removed from Skills then it shouldn’t be in HasSkills anymore
  76. ON UPDATE CASCADE because if the SkillID is updated in Skills then it should also be updated in HasSkills
  77.  
  78. HasAccounts:
  79. Primary key = (BankName, City, RobberID)
  80. Justification: An account is setup between a Robber and a Bank, so to uniquely identify an account the primary key is a combination of the primary keys of
  81.  
  82. Foreign Keys = (BankName, City), (RobberID)
  83.  
  84. CREATE TABLE HasAccounts (BankName CHAR(25) NOT NULL, City CHAR(25) NOT NULL, RobberID INT NOT NULL, PRIMARY KEY (BankName, City, RobberID), FOREIGN KEY (BankName, City) REFERENCES Banks ON DELETE CASCADE ON UPDATE CASCADE), FOREIGN KEY (RobberID) REFERENCES Robbers ON DELETE CASCADE ON UPDATE CASCADE);
  85.  
  86. Foreign Key actions justification:
  87.  
  88. (BankName, City): ON DELETE CASCADE because if a Bank is deleted then a Robber can no longer have an account with that Bank
  89. ON UPDATE CASCADE because if the details of a Bank are updated then they should be updated in HasAccounts also
  90.  
  91. (RobberID): ON DELETE CASCADE because if a Robber is removed from Robbers then they shouldn’t be in HasAccounts anymore as it is strictly for Robbers with bank accounts
  92. ON UPDATE CASCADE because if the RobberID is updated in Robbers then it should also be updated in HasAccounts
  93.  
  94. Accomplices:
  95. Primary key = (BankName, City, RobberID, RobberyDate)
  96. Foreign Keys = (BankName, City, RobberyDate), (RobberID)
  97.  
  98. CREATE TABLE Accomplices (RobberID INT NOT NULL, BankName CHAR(25) NOT NULL, City CHAR(25) NOT NULL, RobberyDate DATE NOT NULL CONSTRAINT adRange CHECK (RobberyDate < '2017-10-01'), Share INT CONSTRAINT asRange CHECK (Share >= 0), PRIMARY KEY (RobberID, BankName, City, RobberyDate), FOREIGN KEY (RobberID) REFERENCES Robbers ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY (BankName, City, RobberyDate) REFERENCES Robberies (BankName, City, Date) ON DELETE CASCADE ON UPDATE CASCADE);
  99.  
  100. Foreign Key actions justification:
  101.  
  102. (BankName, City, RobberyDate): ON DELETE CASCADE because if a robbery is removed from Robberies it should also be removed from Accomplices
  103. ON UPDATE CASCADE because if the details of a robbery in Robberies are updated then it should also be updated in Accomplices
  104.  
  105. (RobberID): ON DELETE CASCADE because if a Robber is removed from Robbers then they shouldn’t be an accomplice anymore
  106. ON UPDATE CASCADE because if the RobberID is updated in Robbers then it should also be updated in Accomplices
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement