Advertisement
robinkolk

Andmebaaside Projekteerimine: Projekt 1

Jan 12th, 2020
584
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --------------------------------------------------------------
  2. -- Database creation Script
  3.  
  4. -- Auto-Generated by QSEE-SuperLite (c) 2001-2004 QSEE-Technologies Ltd.
  5.  
  6. -- Verbose generation: ON
  7.  
  8. -- note: spaces within table/column names have been replaced by underscores (_)
  9.  
  10. -- Target DB: SQL2
  11.  
  12. -- Entity Model :Entity Relationship Diagram
  13.  
  14. -- To drop the tables generated by this script run -
  15. --   'C:\Users\Work\Desktop\GYM_PROJEKT_SQL_drop.sql'
  16.  
  17. --------------------------------------------------------------
  18.  
  19.  
  20.  
  21. --------------------------------------------------------------
  22. -- Table Creation --
  23.  
  24. -- Each entity on the model is represented by a table that needs to be created within the Database.
  25. -- Within SQL new tables are created using the CREATE TABLE command.
  26. -- When a table is created its name and its attributes are defined.
  27. -- The values of which are derived from those specified on the model.
  28. -- Certain constraints are sometimes also specified, such as identification of primary keys.
  29.  
  30. -- Create a Database table to represent the "CLIENT" entity.
  31. CREATE TABLE CLIENT(
  32.     Client_ID   INTEGER NOT NULL,
  33.     First_Name  VARCHAR(20) NOT NULL,
  34.     Last_Name   VARCHAR(20) NOT NULL,
  35.     Address VARCHAR(30) NOT NULL,
  36.     Phone_Number    INTEGER NOT NULL,
  37.     ID_Code INTEGER NOT NULL UNIQUE,
  38.     Email   VARCHAR(30),
  39.     -- Specify the PRIMARY KEY constraint for table "CLIENT".
  40.     -- This indicates which attribute(s) uniquely identify each row of data.
  41.     CONSTRAINT  pk_CLIENT PRIMARY KEY (Client_ID)
  42. );
  43.  
  44. -- Create a Database table to represent the "PAYMENT" entity.
  45. CREATE TABLE PAYMENT(
  46.     Payment_ID  INTEGER NOT NULL,
  47.     Client_ID   INTEGER NOT NULL,
  48.     Package_ID  INTEGER NOT NULL,
  49.     Payment_Date    DATE NOT NULL,
  50.     Credit_Card_Nr  INTEGER NOT NULL,
  51.     fk1_Client_ID   INTEGER NOT NULL,
  52.     FOREIGN KEY (Client_ID) REFERENCES CLIENT(Client_ID),
  53.     FOREIGN KEY (Package_ID) REFERENCES PACKAGE(Package_ID),
  54.     -- Specify the PRIMARY KEY constraint for table "PAYMENT".
  55.     -- This indicates which attribute(s) uniquely identify each row of data.
  56.     CONSTRAINT  pk_PAYMENT PRIMARY KEY (Payment_ID)
  57. );
  58.  
  59. -- Create a Database table to represent the "PACKAGE" entity.
  60. CREATE TABLE PACKAGE(
  61.     Package_ID  INTEGER NOT NULL,
  62.     Price   FLOAT(10),
  63.     Package_Name    VARCHAR(30) NOT NULL,
  64.     Package_Start_Date  TIMESTAMP(20) NOT NULL,
  65.     Package_End_Date    TIMESTAMP(20) NOT NULL,
  66.     fk1_Payment_ID  INTEGER NOT NULL,
  67.     fk1_Client_ID   INTEGER NOT NULL,
  68.     fk1_Package_ID  INTEGER NOT NULL,
  69.     -- Specify FK as unique to maintain 1:1 relationship
  70.     UNIQUE(fk1_Payment_ID,fk1_Client_ID,fk1_Package_ID),
  71.     -- Specify the PRIMARY KEY constraint for table "PACKAGE".
  72.     -- This indicates which attribute(s) uniquely identify each row of data.
  73.     CONSTRAINT  pk_PACKAGE PRIMARY KEY (Package_ID)
  74. );
  75.  
  76. -- Create a Database table to represent the "ACCESS_LOG" entity.
  77. CREATE TABLE ACCESS_LOG(
  78.     Client_ID   INTEGER NOT NULL,
  79.     Gym_ID  INTEGER NOT NULL,
  80.     Package_ID  INTEGER NOT NULL,
  81.     Time_Accessed_Gym   TIMESTAMP(20) NOT NULL,
  82.     fk1_Package_ID  INTEGER NOT NULL,
  83.     -- Specify FK as unique to maintain 1:1 relationship
  84.     UNIQUE(fk1_Package_ID),
  85.     fk2_Client_ID   INTEGER NOT NULL,
  86.     -- Specify FK as unique to maintain 1:1 relationship
  87.     UNIQUE(fk2_Client_ID),
  88.     fk3_Gym_ID  INTEGER NOT NULL,
  89.     -- Specify FK as unique to maintain 1:1 relationship
  90.     UNIQUE(fk3_Gym_ID),
  91.     FOREIGN KEY (Gym_ID) REFERENCES GYM(Gym_ID),
  92.     FOREIGN KEY (Package_ID) REFERENCES PACKAGE(Package_ID),
  93.     FOREIGN KEY (Client_ID) REFERENCES CLIENT(Client_ID)
  94.     -- Specify the PRIMARY KEY constraint for table "ACCESS_LOG".
  95.     -- This indicates which attribute(s) uniquely identify each row of data.
  96. );
  97.  
  98. -- Create a Database table to represent the "GYM" entity.
  99. CREATE TABLE GYM(
  100.     Gym_ID  INTEGER NOT NULL,
  101.     Gym_Name    VARCHAR(30) NOT NULL,
  102.     Address VARCHAR(40) NOT NULL UNIQUE,
  103.     Rooms   INTEGER NOT NULL,
  104.     fk1_Gym_ID  INTEGER NOT NULL,
  105.     -- Specify FK as unique to maintain 1:1 relationship
  106.     UNIQUE(fk1_Gym_ID),
  107.     -- Specify the PRIMARY KEY constraint for table "GYM".
  108.     -- This indicates which attribute(s) uniquely identify each row of data.
  109.     CONSTRAINT  pk_GYM PRIMARY KEY (Gym_ID)
  110. );
  111.  
  112. -- Create a Database table to represent the "EMPLOYEE" entity.
  113. CREATE TABLE EMPLOYEE(
  114.     Employee_ID INTEGER NOT NULL,
  115.     Occupation_ID   INTEGER NOT NULL,
  116.     Gym_ID  INTEGER NOT NULL,
  117.     Employee_Name   VARCHAR(40),
  118.     Employee_Start  DATE NOT NULL,
  119.     Employee_End    DATE NOT NULL,
  120.     fk1_Gym_ID  INTEGER NOT NULL,
  121.     fk2_Occupation_ID   INTEGER NOT NULL,
  122.     fk2_Employee_ID INTEGER NOT NULL,
  123.     -- Specify FK as unique to maintain 1:1 relationship
  124.     UNIQUE(fk2_Occupation_ID,fk2_Employee_ID),
  125.     -- Specify the PRIMARY KEY constraint for table "EMPLOYEE".
  126.     -- This indicates which attribute(s) uniquely identify each row of data.
  127.     FOREIGN KEY (Occupation_ID) REFERENCES OCCUPATION(Occupation_ID),
  128.     FOREIGN KEY (Gym_ID) REFERENCES GYM(Gym_ID),
  129.     CONSTRAINT  pk_EMPLOYEE PRIMARY KEY (Employee_ID)
  130. );
  131.  
  132. -- Create a Database table to represent the "OCCUPATION" entity.
  133. CREATE TABLE OCCUPATION(
  134.     Occupation_ID   INTEGER NOT NULL,
  135.     Employee_ID INTEGER NOT NULL,
  136.     Occupation_Name VARCHAR(30) NOT NULL,
  137.     Month_Salary    FLOAT(10),
  138.     -- Specify the PRIMARY KEY constraint for table "OCCUPATION".
  139.     -- This indicates which attribute(s) uniquely identify each row of data.
  140.     FOREIGN KEY (Employee_ID) REFERENCES EMPLOYEE(Employee_ID),
  141.     CONSTRAINT  pk_OCCUPATION PRIMARY KEY (Occupation_ID)
  142. );
  143.  
  144. -- Create a Database table to represent the "EQUIPMENT" entity.
  145. CREATE TABLE EQUIPMENT(
  146.     Equipment_ID    INTEGER NOT NULL,
  147.     Gym_ID  INTEGER NOT NULL,
  148.     Equipment_Name  VARCHAR(30) NOT NULL,
  149.     FOREIGN KEY (Gym_ID) REFERENCES GYM(Gym_ID),
  150.     -- Specify the PRIMARY KEY constraint for table "EQUIPMENT".
  151.     -- This indicates which attribute(s) uniquely identify each row of data.
  152.     CONSTRAINT  pk_EQUIPMENT PRIMARY KEY (Equipment_ID)
  153. );
  154.  
  155. -- Create a Database table to represent the "GYM_CONTACT" entity.
  156. CREATE TABLE GYM_CONTACT(
  157.     Gym_ID  INTEGER NOT NULL,
  158.     EMAIL   VARCHAR(30) NOT NULL,
  159.     Contact_Number  INTEGER NOT NULL,
  160.     -- Specify the PRIMARY KEY constraint for table "GYM_CONTACT".
  161.     -- This indicates which attribute(s) uniquely identify each row of data.
  162.     CONSTRAINT  pk_GYM_CONTACT PRIMARY KEY (Gym_ID)
  163. );
  164.  
  165.  
  166. --------------------------------------------------------------
  167. -- Alter Tables to add fk constraints --
  168.  
  169. -- Now all the tables have been created the ALTER TABLE command is used to define some additional
  170. -- constraints.  These typically constrain values of foreign keys to be associated in some way
  171. -- with the primary keys of related tables.  Foreign key constraints can actually be specified
  172. -- when each table is created, but doing so can lead to dependency problems within the script
  173. -- i.e. tables may be referenced before they have been created.  This method is therefore safer.
  174.  
  175. -- Alter table to add new constraints required to implement the "client_payment" relationship
  176.  
  177. -- This constraint ensures that the foreign key of table "PAYMENT"
  178. -- correctly references the primary key of table "CLIENT"
  179.  
  180. ALTER TABLE PAYMENT ADD CONSTRAINT fk1_PAYMENT_to_CLIENT FOREIGN KEY(fk1_Client_ID) REFERENCES CLIENT(Client_ID) ON DELETE RESTRICT ON UPDATE RESTRICT;
  181.  
  182. -- Alter table to add new constraints required to implement the "PACKAGE_PAYMENT" relationship
  183.  
  184. -- This constraint ensures that the foreign key of table "PACKAGE"
  185. -- correctly references the primary key of table "PAYMENT"
  186.  
  187. ALTER TABLE PACKAGE ADD CONSTRAINT fk1_PACKAGE_to_PAYMENT FOREIGN KEY(fk1_Payment_ID,fk1_Client_ID,fk1_Package_ID) REFERENCES PAYMENT(Payment_ID,Client_ID,Package_ID) ON DELETE RESTRICT ON UPDATE RESTRICT;
  188.  
  189. -- Alter table to add new constraints required to implement the "ACCESS_LOG_PACKAGE" relationship
  190.  
  191. -- This constraint ensures that the foreign key of table "ACCESS_LOG"
  192. -- correctly references the primary key of table "PACKAGE"
  193.  
  194. ALTER TABLE ACCESS_LOG ADD CONSTRAINT fk1_ACCESS_LOG_to_PACKAGE FOREIGN KEY(fk1_Package_ID) REFERENCES PACKAGE(Package_ID) ON DELETE RESTRICT ON UPDATE RESTRICT;
  195.  
  196. -- Alter table to add new constraints required to implement the "ACCESS_LOG_CLIENT" relationship
  197.  
  198. -- This constraint ensures that the foreign key of table "ACCESS_LOG"
  199. -- correctly references the primary key of table "CLIENT"
  200.  
  201. ALTER TABLE ACCESS_LOG ADD CONSTRAINT fk2_ACCESS_LOG_to_CLIENT FOREIGN KEY(fk2_Client_ID) REFERENCES CLIENT(Client_ID) ON DELETE RESTRICT ON UPDATE RESTRICT;
  202.  
  203. -- Alter table to add new constraints required to implement the "ACCESS_LOG_GYM" relationship
  204.  
  205. -- This constraint ensures that the foreign key of table "ACCESS_LOG"
  206. -- correctly references the primary key of table "GYM"
  207.  
  208. ALTER TABLE ACCESS_LOG ADD CONSTRAINT fk3_ACCESS_LOG_to_GYM FOREIGN KEY(fk3_Gym_ID) REFERENCES GYM(Gym_ID) ON DELETE RESTRICT ON UPDATE RESTRICT;
  209.  
  210. -- Alter table to add new constraints required to implement the "EMPLOYEE_GYM" relationship
  211.  
  212. -- This constraint ensures that the foreign key of table "EMPLOYEE"
  213. -- correctly references the primary key of table "GYM"
  214.  
  215. ALTER TABLE EMPLOYEE ADD CONSTRAINT fk1_EMPLOYEE_to_GYM FOREIGN KEY(fk1_Gym_ID) REFERENCES GYM(Gym_ID) ON DELETE RESTRICT ON UPDATE RESTRICT;
  216.  
  217. -- Alter table to add new constraints required to implement the "GYM_GYM_CONTACT" relationship
  218.  
  219. -- This constraint ensures that the foreign key of table "GYM"
  220. -- correctly references the primary key of table "GYM_CONTACT"
  221.  
  222. ALTER TABLE GYM ADD CONSTRAINT fk1_GYM_to_GYM_CONTACT FOREIGN KEY(fk1_Gym_ID) REFERENCES GYM_CONTACT(Gym_ID) ON DELETE RESTRICT ON UPDATE RESTRICT;
  223.  
  224.  
  225.  
  226. -- unable to generate alter table command to enforce foreign key constraints between EQUIPMENT and GYM due to missing primary keys
  227.  
  228. -- Alter table to add new constraints required to implement the "EMPLOYEE_OCCUPATION" relationship
  229.  
  230. -- This constraint ensures that the foreign key of table "EMPLOYEE"
  231. -- correctly references the primary key of table "OCCUPATION"
  232.  
  233. ALTER TABLE EMPLOYEE ADD CONSTRAINT fk2_EMPLOYEE_to_OCCUPATION FOREIGN KEY(fk2_Occupation_ID,fk2_Employee_ID) REFERENCES OCCUPATION(Occupation_ID,Employee_ID) ON DELETE RESTRICT ON UPDATE RESTRICT;
  234.  
  235.  
  236. --------------------------------------------------------------
  237. -- End of DDL file auto-generation
  238. --------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement