Advertisement
exihs

Untitled

Jan 10th, 2024 (edited)
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 9.48 KB | None | 0 0
  1. /* Staff table */
  2. CREATE TABLE Staff
  3. (
  4. StaffID smallint,
  5. StaffName VarChar(70) NOT NULL,
  6. StaffContactNo Char(8) NOT NULL,
  7. StaffDateJoined smalldatetime NOT NULL,
  8. StaffRole VarChar(50) NOT NULL,
  9. CONSTRAINT PK_Staff PRIMARY KEY (StaffID)
  10. );
  11.  
  12. /*Condo table*/
  13. CREATE TABLE Condo
  14. (
  15. CondoID smallint,
  16. CondoName varchar(50) NOT NULL,
  17. CondoAddress varchar(150) NOT NULL,
  18. CONSTRAINT PK_Condo PRIMARY KEY (CondoID)
  19. );
  20.  
  21. /*Account table*/
  22. CREATE TABLE Account
  23. (
  24. AccID Char(10),
  25. AccName VarChar(70) NOT NULL,
  26. AccAddress VarChar(150) NULL,
  27. AccCtcNo Char(8) NULL,
  28. AccEmail VarChar(50) NULL,
  29. CondoID smallint NOT NULL,
  30. ApprovedBy smallint NOT NULL,
  31. CONSTRAINT PK_Acc PRIMARY KEY (AccID),
  32. CONSTRAINT FK_Acc_Condo FOREIGN KEY (CondoID) REFERENCES Condo(CondoID),
  33. CONSTRAINT FK_Acc_Staff FOREIGN KEY (ApprovedBy) REFERENCES Staff(StaffID)
  34. );
  35.  
  36. /*CondoMgmt table*/
  37. CREATE TABLE CondoMgmt
  38. (
  39. CondoMgmtID Char(10),
  40. ContactPerson VarChar(70) NOT NULL,
  41. CtcPersonMobile  Char(8) NOT NULL,
  42. CONSTRAINT PK_CondoMgmt PRIMARY KEY (CondoMgmtID),
  43. CONSTRAINT FK_CondoMgmt_AccID FOREIGN KEY (CondoMgmtID) REFERENCES Account(AccID)
  44. );
  45.  
  46. /*Announcement table*/
  47. CREATE TABLE Announcement
  48. (
  49. AnnID smallint,
  50. AnnText VarChar(200) NOT NULL,
  51. AnnStartDate smalldatetime NOT NULL,
  52. AnnEndDate smalldatetime NULL,
  53. CondoMgmtID Char(10) NOT NULL,
  54. CONSTRAINT PK_Announcement PRIMARY KEY (AnnID),
  55. CONSTRAINT FK_Announcement FOREIGN KEY (CondoMgmtID) REFERENCES CondoMgmt(CondoMgmtID)
  56. );
  57.  
  58.  
  59. /*Message table*/
  60. CREATE TABLE Message
  61. (
  62. MsgID smallint,
  63. MsgText VarChar(200) NOT NULL,
  64. MsgType VarChar(50) NOT NULL,
  65. PostedBy Char(10) NOT NULL,
  66. ReplyTo smallint NULL,
  67. CONSTRAINT PK_Message PRIMARY KEY (MsgID),
  68. CONSTRAINT FK_Message_PostedBy FOREIGN KEY (PostedBy) REFERENCES Account(AccID),
  69. CONSTRAINT FK_Message_ReplyTo FOREIGN KEY (ReplyTo) REFERENCES Message(MsgID),
  70. CONSTRAINT CHK_Message CHECK (MsgType IN ('Chatterbox', 'Garage Sale', 'Find a Buddy'))
  71. );
  72.  
  73. /*ContactCat table*/
  74. CREATE TABLE ContactCat
  75. (
  76. CtcCatID smallint,
  77. CtcCatDesc varchar(100) NULL,
  78. CONSTRAINT PK_ContactCat PRIMARY KEY (CtcCatID)
  79. );
  80.  
  81. /*Facility table*/
  82. CREATE TABLE Facility
  83. (
  84. FacID smallint,
  85. FacName varchar(50) NOT NULL,
  86. Deposit smallmoney NULL,
  87. CondoID smallint NOT NULL,
  88. CONSTRAINT PK_Facility PRIMARY KEY (FacID),
  89. CONSTRAINT FK_Facility_CondoID FOREIGN KEY (CondoID) REFERENCES Condo(CondoID)
  90. );
  91.  
  92. /*FacTimeSlot table*/
  93. CREATE TABLE FacTimeSlot
  94. (
  95. FacID smallint,
  96. TimeSlotSN smallint,
  97. SlotDesc VarChar(100) NOT NULL,
  98. CONSTRAINT PK_FacTimeSlot PRIMARY KEY (FacID,TimeSlotSN),
  99. CONSTRAINT FK_FacTimeSlot_FacID FOREIGN KEY (FacID) REFERENCES Facility(FacID)
  100. );
  101.  
  102.  
  103. /*FeedbkCat table*/
  104. CREATE TABLE FeedbkCat
  105. (
  106. FbkCatID smallint,
  107. FbkCatDesc varchar(100) NOT NULL,
  108. CONSTRAINT PK_FeedbkCat PRIMARY KEY (FbkCatID)
  109. );
  110.  
  111. /* Feedback table */
  112. CREATE TABLE Feedback
  113. (
  114. FbkID smallint,
  115. FbkDesc VarChar(100) NOT NULL,
  116. FbkDateTime smalldatetime NOT NULL,
  117. FbkStatus VarChar(20) NOT NULL,
  118. ByAccID Char(10) NOT NULL,
  119. FbkCatID smallint NOT NULL,
  120. CondoMgmtID Char(10) NOT NULL,
  121. CONSTRAINT PK_Feedback PRIMARY KEY(FbkID),
  122. CONSTRAINT FK_Feedback_ByAccID FOREIGN KEY(ByAccID) REFERENCES Account(AccID),
  123. CONSTRAINT FK_Feedback_FbkCatID FOREIGN KEY(FbkCatID) REFERENCES FeedbkCat(FbkCatID),
  124. CONSTRAINT FK_Feedback_FbkCatID FOREIGN KEY(CondoMgmtID) REFERENCES CondoMgmt(CondoMgmtID)
  125. );
  126.  
  127. /*ItemCategory table*/
  128. CREATE TABLE ItemCategory
  129. (
  130. ItemCatID smallint,
  131. ItemCatDesc varchar(100) NOT NULL,
  132. CONSTRAINT PK_ItemCategory PRIMARY KEY (ItemCatID)
  133. );
  134.  
  135. /*ItemRelated table*/
  136. CREATE TABLE ItemRelated
  137. (
  138. ItemID smallint,
  139. ItemDesc VarChar(100) NULL,
  140. ItemPrice smallmoney NULL,
  141. ItemStatus VarChar(9) NOT NULL,
  142. SaleOrRent Char(4) NOT NULL,
  143. ItemCatID smallint NOT NULL,
  144. CONSTRAINT PK_ItemRelated PRIMARY KEY (ItemID),
  145. CONSTRAINT FK_ItemRelated_ItemID FOREIGN KEY (ItemID) REFERENCES Message(MsgID),
  146. CONSTRAINT CHK_ItemRelated CHECK (ItemStatus IN ('Available', 'Sold', 'Reserved') AND SaleOrRent IN ('Rent','Sale')),
  147. CONSTRAINT FK_ItemRelated_ItemCatID FOREIGN KEY (ItemCatID) REFERENCES ItemCategory(ItemCatID)
  148. );
  149.  
  150. /*ItemPhoto table*/
  151. CREATE TABLE ItemPhoto
  152. (
  153. ItemID smallint,
  154. Photo varchar(100) NOT NULL,
  155. CONSTRAINT PK_ItemPhoto PRIMARY KEY (ItemID,Photo),
  156. CONSTRAINT FK_ItemPhoto_ItemID FOREIGN KEY (ItemID) REFERENCES ItemRelated(ItemID)
  157. );
  158.  
  159. /*BookSlot table*/
  160. CREATE TABLE BookSlot
  161. (
  162. FacID smallint,
  163. TimeSlotSN smallint NOT NULL,
  164. SlotDate smalldatetime NOT NULL DEFAULT(GETDATE()),
  165. SlotStatus VarChar(15) NOT NULL,
  166. CONSTRAINT PK_BookSlot PRIMARY KEY(FacID,TimeSlotSN,SlotDate),   
  167. CONSTRAINT FK_BookSlot FOREIGN KEY(FacID, TimeSlotSN) REFERENCES FacTimeSlot(FacID, TimeSlotSN),
  168. CONSTRAINT CHK_BookSlot CHECK (SlotStatus in ('Booked', 'Available', 'Maintenance'))
  169. );
  170.  
  171. /*Likes table*/
  172. CREATE TABLE Likes
  173. (
  174. AccID Char(10),
  175. MessageID smallint,
  176. CONSTRAINT PK_Likes PRIMARY KEY (AccID, MessageID),
  177. CONSTRAINT FK_Likes_AccID FOREIGN KEY (AccID) REFERENCES ACCOUNT(AccID),
  178. CONSTRAINT FK_Likes_MessageID FOREIGN KEY (MessageID) REFERENCES Message(MsgID)
  179. );
  180.  
  181. /*Owner table*/
  182. CREATE TABLE Owner
  183. (
  184. OwnerID Char(10),
  185. OwnStartDate smalldatetime NOT NULL,
  186. CheckedBy Char(10) NOT NULL,
  187. CONSTRAINT PK_Owner PRIMARY KEY (OwnerID),
  188. CONSTRAINT FK_Owner_OwnerID FOREIGN KEY (OwnerID) REFERENCES Account(AccID),
  189. CONSTRAINT FK_Owner_CheckedBy FOREIGN KEY (CheckedBy) REFERENCES CondoMgmt(CondoMgmtID)
  190. );
  191.  
  192. /*TempVehLabel table*/
  193. CREATE TABLE TempVehLabel
  194. (
  195. VehLblAppID smallint,
  196. TempStartDate smalldatetime NOT NULL,
  197. TempExpiryDate smalldatetime NOT NULL,
  198. CONSTRAINT PK_TempVehLabel PRIMARY KEY (VehLblAppID)
  199. );
  200.  
  201. /*Tenant table*/
  202. CREATE TABLE Tenant
  203. (
  204. TenantID Char(10),
  205. ContractStartDate smalldatetime NOT NULL,
  206. ContractEndDate smalldatetime NULL,
  207. VerifiedBy Char(10) NOT NULL,
  208. CONSTRAINT PK_Tenant PRIMARY KEY (TenantID),
  209. CONSTRAINT FK_Tenant_TenantID FOREIGN KEY (TenantID) REFERENCES Account(AccID),
  210. CONSTRAINT FK_Tenant_VerifiedBy FOREIGN KEY (VerifiedBy) REFERENCES CondoMgmt(CondoMgmtID)
  211. );
  212.  
  213. /* UsefulContact Table */
  214. CREATE TABLE UsefulContact
  215. (
  216. UsefulCtcID smallint,
  217. UsefulCtcName VarChar(50) NOT NULL,
  218. UsefulCtcDesc VarChar(100) NULL,
  219. UsefulCtcPhone Char(8) NOT NULL,
  220. CtcCatID smallint NOT NULL,
  221. CONSTRAINT PK_UsefulContact PRIMARY KEY (UsefulCtcID),
  222. CONSTRAINT FK_UsefulContact_CtcCatID FOREIGN KEY(CtcCatID) REFERENCES ContactCat(CtcCatID)
  223. );
  224.  
  225. /*CondoUsefulContact table*/
  226. CREATE TABLE CondoUsefulContact
  227. (
  228. CondoID smallint,
  229. UsefulCtcID smallint NOT NULL,
  230. CONSTRAINT PK_CondoUsefulContact PRIMARY KEY (CondoID,UsefulCtcID),
  231. CONSTRAINT FK_CondoUsefulContact_CondoID FOREIGN KEY (CondoID) REFERENCES Condo(CondoID),
  232. CONSTRAINT FK_CondoUsefulContact_UsefulCtcID FOREIGN KEY (UsefulCtcID) REFERENCES UsefulContact(UsefulCtcID)
  233. );
  234.  
  235. /*Vehicle table*/
  236. CREATE TABLE Vehicle
  237. (
  238. VehicleNo varchar(10),
  239. IUNo char(10) NOT NULL,
  240. Ownership varchar(50) NOT NULL,
  241. Make varchar(50) NOT NULL,
  242. Model varchar(50) NOT NULL,
  243. CONSTRAINT PK_Vehicle PRIMARY KEY (VehicleNo)
  244. );
  245.  
  246. /*Booking table*/
  247. CREATE TABLE Booking
  248. (
  249. BookingID smallint,
  250. BookingDate smalldatetime NOT NULL,
  251. BookingStatus VarChar(20) NOT NULL,
  252. AccID Char(10) NOT NULL,
  253. FacID smallint NOT NULL,
  254. TimeSlotSN smallint NOT NULL,
  255. SlotDate smalldatetime NOT NULL,
  256. CONSTRAINT PK_Booking PRIMARY KEY(BookingID),
  257. CONSTRAINT FK_Booking_FacIDTimeSlotSNSlotDate FOREIGN KEY (FacID,TimeSlotSN,SlotDate) REFERENCES BookSlot(FacID,TimeSlotSN,SlotDate),
  258. CONSTRAINT FK_Booking_AccID FOREIGN KEY (AccID) REFERENCES Account(AccID)
  259. );
  260.  
  261. /*VehicleLabel table*/
  262. CREATE TABLE VehicleLabel
  263. (
  264. VehLblAppID smallint,
  265. VehLblStatus Varchar(10) NOT NULL,
  266. VehLblNum Varchar(10) NULL,
  267. VehicleNo Varchar(10) NOT NULL,
  268. AppliedBy Char(10) NOT NULL,
  269. IssuedBy Char(10) NOT NULL,
  270. CONSTRAINT PK_VehicleLabel PRIMARY KEY(VehLblAppID),
  271. CONSTRAINT FK_VehicleLabel_VehicleNo FOREIGN KEY(VehicleNo) REFERENCES Vehicle(VehicleNo),
  272. CONSTRAINT FK_VehicleLabel_AppliedBy FOREIGN KEY(AppliedBy) REFERENCES Account(AccID),
  273. CONSTRAINT FK_VehicleLabel_IssuedBy FOREIGN KEY(IssuedBy) REFERENCES CondoMgmt(CondoMgmtID),
  274. CONSTRAINT CHK_VehicleLabel CHECK (VehLblStatus in ('Pending', 'Approved', 'Rejected'))
  275. );
  276.  
  277.  
  278.  
  279. /*Show the content in all tables*/
  280.  
  281. DECLARE @table varchar(20); /*Declare a variable It will be used to store the name of the current table in the loop*/
  282.  
  283. /*Declare the cursor CurrTable, get and assign all the table names*/
  284. /* Since we plan to iterate linearly, FAST_FORWARD is used. It makes the cursor read-only and only move forward. Also, it is better for performance */
  285. DECLARE CurrTable CURSOR FAST_FORWARD FOR SELECT name FROM SYSOBJECTS WHERE xtype = 'U'  
  286.  
  287. /*Activate the cursor and populate it with the data*/
  288. OPEN CurrTable
  289.  
  290. FETCH NEXT FROM CurrTable INTO @table /* Set table to the first item in the cursor*/
  291.  
  292. /* Start iterating through the cursor */
  293.  
  294. WHILE @@FETCH_STATUS = 0 /* if the status of the previous fetch was successful */
  295. BEGIN
  296.     /* Show the content of the table with a select statement*/
  297.     /* EXEC statement executes the statement. It's used for integrating the table variable into it*/
  298.     EXEC('SELECT * FROM ' + @table)
  299.  
  300.     /* Set table to the next item in the cursor*/
  301.     FETCH NEXT FROM CurrTable INTO @table  
  302. END
  303.  
  304. /* Deactivate the cursor and free it */
  305. CLOSE CurrTable  
  306.  
  307. /* Remove the cursor reference, it is no longer needed */
  308. DEALLOCATE CurrTable
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement