Advertisement
akevintg

SQLearning(VIEW, SUBSTRING, CHARINDEX,etc)

Mar 23rd, 2015
604
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 11.00 KB | None | 0 0
  1. -- SQL LEARNING, (VIEW,LEN,SUBSTRING,ETC)
  2.  
  3. /*
  4. CREATE DATABASE OOVEO_Salon
  5. GO
  6. USE OOVEO_Salon
  7. */
  8.  
  9. -- Create MsCustomer
  10. CREATE TABLE MsCustomer(
  11.     CustomerId CHAR(5) PRIMARY KEY,
  12.     CustomerName VARCHAR(50),
  13.     CustomerGender VARCHAR(10),
  14.     CustomerPhone VARCHAR(13),
  15.     CustomerAddress VARCHAR(100),
  16.     CONSTRAINT cekIDCust CHECK(CustomerId LIKE 'CU[0-9][0-9][0-9]')
  17. )
  18.  
  19. -- Create MsStaff
  20. CREATE TABLE MsStaff(
  21.     StaffId CHAR(5) PRIMARY KEY,
  22.     StaffName VARCHAR(50),
  23.     StaffGender VARCHAR(10),
  24.     StaffPhone VARCHAR(13),
  25.     StaffAddress VARCHAR(100),
  26.     StaffSalary NUMERIC(11,2),
  27.     StaffPosition VARCHAR(20),
  28.     CONSTRAINT cekIDStaff CHECK(StaffId LIKE 'SF[0-9][0-9][0-9]')
  29. )
  30.  
  31. -- Create MsTreatmentType
  32. CREATE TABLE MsTreatmentType(
  33.     TreatmentTypeId CHAR(5) PRIMARY KEY,
  34.     TreatmentTypeName VARCHAR(50),
  35.     CONSTRAINT cekIDTType CHECK(TreatmentTypeId LIKE 'TT[0-9][0-9][0-9]')
  36. )
  37.  
  38. -- Create MsTreatment
  39. CREATE TABLE MsTreatment(
  40.     TreatmentId CHAR(5) PRIMARY KEY,
  41.     TreatmentTypeId CHAR(5) REFERENCES MsTreatmentType ON UPDATE CASCADE ON DELETE CASCADE,
  42.     TreatmentName VARCHAR(50),
  43.     Price NUMERIC(11,2),
  44.     CONSTRAINT cekIDTreat CHECK(TreatmentId LIKE 'TM[0-9][0-9][0-9]')
  45. )
  46.  
  47. -- Create HeaderSalonServices
  48. CREATE TABLE HeaderSalonServices(
  49.     TransactionId CHAR(5) PRIMARY KEY,
  50.     CustomerId CHAR(5) REFERENCES MsCustomer ON UPDATE CASCADE ON DELETE CASCADE,
  51.     StaffId CHAR(5) REFERENCES MsStaff ON UPDATE CASCADE ON DELETE CASCADE,
  52.     TransactionDate DATE,
  53.     PaymentType VARCHAR(20),
  54.     CONSTRAINT cekIDTrans CHECK(TransactionId LIKE 'TR[0-9][0-9][0-9]')
  55. )
  56.  
  57. -- Create DetailSalonServices
  58. CREATE TABLE DetailSalonServices(
  59.     TransactionId CHAR(5) REFERENCES HeaderSalonServices ON UPDATE CASCADE ON DELETE CASCADE,
  60.     TreatmentId CHAR(5) REFERENCES MsTreatment ON UPDATE CASCADE ON DELETE CASCADE,
  61.     PRIMARY KEY(TransactionId, TreatmentId)
  62. )
  63.  
  64. -- Insert Data
  65. INSERT INTO MsCustomer VALUES
  66. ('CU001', 'Franky', 'Male', '08566543338', 'Daan mogot baru Street no 6'),
  67. ('CU002', 'Ernalia Dewi', 'Female', '085264782135', 'Tanjung Duren Street no 185'),
  68. ('CU003', 'Elysia Chen', 'Female', '085754206611', 'Kebon Jeruk Street no 120'),
  69. ('CU004', 'Brando Kartawijaya', 'Male', '081170225561', 'Greenvil Street no 88'),
  70. ('CU005', 'Andy Putra', 'Male', '087751321421', 'Sunter Street no 42')
  71.  
  72. INSERT INTO MsStaff VALUES
  73. ('SF001', 'Dian Felita Tanoto', 'Female', '085265442222', 'Palmerah Street no 56', 15000000, 'Top Stylist'),
  74. ('SF002', 'Mellisa Pratiwi', 'Female', '085755552011', 'Kebon Jeruk Street no 151', 10000000, 'Top Stylist'),
  75. ('SF003', 'Livia Ashianti', 'Female', '085218542222', 'Kebon Jeruk Street no 19', 7000000, 'Stylist'),
  76. ('SF004', 'Indra Saswita', 'Male', '085564223311', 'Sunter Street no 91', 7000000, 'Stylist'),
  77. ('SF005', 'Ryan Nixon Salim', 'Male', '085710255522', 'Kebon Jeruk Street no 123', 3000000, 'Stylist')
  78.  
  79. INSERT INTO MsTreatmentType VALUES
  80. ('TT001', 'Hair Treatment'),
  81. ('TT002', 'Message / Spa'),
  82. ('TT003', 'Beauty Care'),
  83. ('TT004', 'Nail Treatment'),
  84. ('TT005', 'Body Treatment')
  85.  
  86. INSERT INTO MsTreatment VALUES
  87. ('TM001', 'TT001','Cutting by Stylist', 150000),
  88. ('TM002', 'TT001','Cutting by Top Stylist', 450000),
  89. ('TM003', 'TT001','Cutting Pony', 50000),
  90. ('TM004', 'TT001','Blow', 90000),
  91. ('TM005', 'TT001','Coloring', 480000),
  92. ('TM006', 'TT001','Highlight', 320000),
  93. ('TM007', 'TT001','Japanese Perm', 700000),
  94. ('TM008', 'TT001','Digital Perm', 1100000),
  95. ('TM009', 'TT001','Special Perm', 1100000),
  96. ('TM010', 'TT001','Rebonding Treatment', 1100000),
  97. ('TM011', 'TT002','Creambath', 150000),
  98. ('TM012', 'TT002','Hair Spa', 250000),
  99. ('TM013', 'TT002','Hair Mask', 250000),
  100. ('TM014', 'TT002','Hand Spa Reflexy', 200000),
  101. ('TM015', 'TT002','Reflexy', 250000),
  102. ('TM016', 'TT002','Back Theraphy Massage', 300000),
  103. ('TM017', 'TT003','Make Up', 500000),
  104. ('TM018', 'TT003','Make Up Wedding', 5000000),
  105. ('TM019', 'TT003','Facial', 300000),
  106. ('TM020', 'TT004','Manicure', 80000),
  107. ('TM021', 'TT004','Pedicure', 100000),
  108. ('TM022', 'TT004','Nail Extension', 250000),
  109. ('TM023', 'TT004','Nail Acrylic Infill', 340000),
  110. ('TM024', 'TT005','Japanese Treatment', 350000),
  111. ('TM025', 'TT005','Scalp Treatment', 250000),
  112. ('TM026', 'TT005','Crystal Treatment', 400000)
  113.  
  114. INSERT INTO HeaderSalonServices VALUES
  115. ('TR001', 'CU001', 'SF004', '2012/12/20', 'Credit'),
  116. ('TR002', 'CU002', 'SF005', '2012/12/20', 'Credit'),
  117. ('TR003', 'CU003', 'SF003', '2012/12/20', 'Cash'),
  118. ('TR004', 'CU004', 'SF005', '2012/12/20', 'Debit'),
  119. ('TR005', 'CU005', 'SF003', '2012/12/21', 'Debit'),
  120. ('TR006', 'CU001', 'SF005', '2012/12/21', 'Credit'),
  121. ('TR007', 'CU002', 'SF001', '2012/12/22', 'Cash'),
  122. ('TR008', 'CU003', 'SF002', '2012/12/22', 'Credit'),
  123. ('TR009', 'CU005', 'SF004', '2012/12/22', 'Debit')
  124.  
  125. INSERT INTO DetailSalonServices VALUES
  126. ('TR001', 'TM001'),
  127. ('TR001', 'TM005'),
  128. ('TR002', 'TM010'),
  129. ('TR002', 'TM015'),
  130. ('TR003', 'TM025'),
  131. ('TR003', 'TM009'),
  132. ('TR004', 'TM001'),
  133. ('TR004', 'TM006'),
  134. ('TR004', 'TM015'),
  135. ('TR004', 'TM016'),
  136. ('TR005', 'TM016'),
  137. ('TR006', 'TM006'),
  138. ('TR006', 'TM015'),
  139. ('TR007', 'TM002'),
  140. ('TR007', 'TM005'),
  141. ('TR008', 'TM002'),
  142. ('TR008', 'TM006'),
  143. ('TR009', 'TM005'),
  144. ('TR009', 'TM006')
  145.  
  146. -- Display Data
  147. SELECT * FROM MsCustomer
  148. SELECT * FROM MsStaff
  149. SELECT * FROM MsTreatment
  150. SELECT * FROM MsTreatmentType
  151. SELECT * FROM HeaderSalonServices
  152. SELECT * FROM DetailSalonServices
  153.  
  154. /*1. Create a view named ‘ViewBonus’ to display BonusId (obtained from CustomerId
  155. by replacing the first 2 characters with ‘BN’) and CustomerName where CustomerName
  156. is greater than 10 characters.
  157. (create view, stuff, len)*/
  158.  
  159. SELECT  *
  160. FROM MsCustomer
  161.  
  162. DROP VIEW ViewBonus
  163.  
  164. CREATE VIEW ViewBonus AS
  165. SELECT BonusId= STUFF(CustomerId,1,2,'BN'), CustomerName
  166. FROM msCustomer
  167. WHERE LEN(CustomerName)>10
  168.  
  169. SELECT *
  170. FROM ViewBonus
  171.  
  172. /*2. Create a view named ‘ViewCustomerData’ to display Name (obtained from the first
  173. name of CustomerName), Address (obtained from CustomerAddress) and Phone (obtained from
  174. CustomerPhone) where CustomerName is more than one word.
  175. (create view, substring, charindex)
  176. */
  177.  
  178. SELECT  *
  179. FROM MsCustomer
  180.  
  181. CREATE VIEW ViewCustomerData AS
  182. SELECT Name=SUBSTRING(CustomerName,1,CHARINDEX(' ',CustomerName,0)),[Address]=CustomerAddress, Phone=CustomerPhone
  183. FROM MsCustomer
  184. WHERE CHARINDEX(' ',CustomerName,0)>0
  185.  
  186. SELECT *
  187. FROM ViewCustomerData
  188.  
  189. /*3. Create a view named ‘ViewTreatment’ to display TreatmentName, TreatmentTypeName,
  190. Price (obtained from Price by adding ‘Rp. ’ in front of it) where TreatmentTypeName is
  191. ‘Hair Treatment’ and Price is between 450000 and 800000.
  192. (create view, cast, between)
  193. */
  194.  
  195. SELECT MTT.TreatmentTypeId,TreatmentId,TreatmentTypeName,TreatmentName,Price
  196. FROM MsTreatmentType MTT,MsTreatment MT
  197. WHERE MTT.TreatmentTypeId=MT.TreatmentTypeId
  198.  
  199. CREATE VIEW ViewTreatment AS
  200. SELECT TreatmentName,TreatmentTypeName,Price='Rp. '+CAST(Price AS VARCHAR)
  201. FROM MsTreatmentType MTT,MsTreatment MT
  202. WHERE MTT.TreatmentTypeId=MT.TreatmentTypeId AND PRICE BETWEEN 450000 AND 800000 AND TreatmentTypeName='Hair Treatment'
  203.  
  204. SELECT *
  205. FROM ViewTreatment
  206.  
  207. /*4. Create a view named ‘ViewTransaction’ to display StaffName, CustomerName, TransactionDate
  208. (obtained from TransactionDate with format ‘dd Mon yyyy’), and PaymentType where the transaction
  209.  happened between 21 and 25 and PaymentType is ‘Credit’.
  210. (create view, convert, day, between)
  211. */
  212.  
  213. SELECT *
  214. FROM HeaderSalonServices,MsStaff,MsCustomer
  215.  
  216. CREATE VIEW ViewTransaction AS
  217. SELECT StaffName,CustomerName,TransactionDate=CONVERT(VARCHAR,TransactionDate,103), PaymentType
  218. FROM MsStaff MS,MsCustomer MC, HeaderSalonServices HSS
  219. WHERE MS.StaffId=HSS.StaffId AND MC.CustomerId=HSS.CustomerId AND DAY(TransactionDate) BETWEEN 21 AND 25 AND PaymentType='Credit'
  220.  
  221. SELECT *
  222. FROM ViewTransaction
  223.  
  224. /*5. Create a view named ‘ViewBonusCustomer’ to display BonusId (obtained from CustomerId by
  225. replacing ‘CU’ with ‘BN’), Name (obtained from the last name of Customer with lowercase format),
  226. Day (obtained from name of the day when the transaction happened), and TransactionDate (obtained
  227. from TransactionDate with format ‘mm/dd/yyyy’) where CustomerName is more than one word and Name
  228. contains ‘a’ character.
  229. (create view, replace, lower, substring, charindex, len, datename, convert, like)
  230. */
  231.  
  232. SELECT *
  233. FROM HeaderSalonServices,MsCustomer
  234.  
  235. CREATE VIEW ViewBonusCustomer AS
  236. SELECT BonusId=REPLACE(MC.CustomerId,'CU','BN'),Name=LOWER(SUBSTRING(CustomerName,CHARINDEX(' ',CustomerName)+1,LEN(CustomerName))),
  237.     [DAY]=DATENAME(WEEKDAY,TransactionDate), TransactionDate=CONVERT(VARCHAR,TransactionDate,101)
  238. FROM HeaderSalonServices HSS, MsCustomer MC
  239. WHERE HSS.CustomerId=MC.CustomerId AND SUBSTRING(CustomerName,CHARINDEX(' ',CustomerName)+1,LEN(CustomerName)) LIKE '%a%'AND CHARINDEX(' ',CustomerName)>0
  240.  
  241. SELECT *
  242. FROM ViewBonusCustomer
  243.  
  244. /*6. Create a view named ‘ViewTransactionByLivia’ to display TransactionId, Date (obtained from
  245. TransactionDate with format ‘Mon dd, yyyy’), and TreatmentName where Staff that handle the
  246. transaction named ‘Livia Ashianti’ and date of transaction was occurred on 21.
  247. (create view, convert, like, day)
  248. */
  249.  
  250. SELECT *
  251. FROM HeaderSalonServices, DetailSalonServices, MsStaff, MsTreatment
  252.  
  253. CREATE VIEW ViewTransactionByLivia AS
  254. SELECT HSS.TransactionId, [DATE]=CONVERT(VARCHAR,TransactionDate,107), TreatmentName
  255. FROM HeaderSalonServices HSS, DetailSalonServices DSS, MsStaff MS, MsTreatment MT
  256. WHERE StaffName LIKE 'Livia Ashianti' AND DAY(TransactionDate)=21 AND HSS.TransactionId=DSS.TransactionId AND HSS.StaffId=MS.StaffId AND DSS.TreatmentId=MT.TreatmentId
  257.  
  258. SELECT *
  259. FROM ViewTransactionByLivia
  260.  
  261. /*7. Changing the view named ‘ViewCustomerData’ becomes displaying ID (obtained from the last 3
  262. digit characters of CustomerId), Name (obtained from CustomerName), Address (obtained from
  263. CustomerAddress), and Phone (obtained from CustomerPhone) where CustomerName is more than one word.
  264. (alter, right, charindex)
  265. */
  266.  
  267. SELECT *
  268. FROM ViewCustomerData
  269.  
  270. ALTER VIEW ViewCustomerData AS
  271. SELECT ID=RIGHT(CustomerId,3),Name=CustomerName,[Address]=CustomerAddress,Phone=CustomerPhone
  272. FROM MsCustomer
  273. WHERE CHARINDEX(' ',CustomerName)>1
  274.  
  275. SELECT *
  276. FROM ViewCustomerData
  277.  
  278. /*8.1 Create a view named ‘ViewCustomer’ to display CustomerId, CustomerName and CustomerGender.
  279. Then add the data to ViewCustomer with the following specifications:
  280.   8.2 Then display all data on MsCustomer table.
  281. */
  282.  
  283. SELECT *
  284. FROM MsCustomer
  285.  
  286. CREATE VIEW ViewCustomer AS
  287. SELECT CustomerId,CustomerName,CustomerGender
  288. FROM MsCustomer
  289.  
  290. --Direct Insert
  291. INSERT INTO MsCustomer(CustomerId,CustomerName,CustomerGender) VALUES ('CU006','Cristian','Male')
  292. --View Insert
  293. INSERT INTO ViewCustomer SELECT 'CU007','Alexander Kevin','Male'
  294.  
  295. SELECT *
  296. FROM MsCustomer
  297.  
  298. /*9. Delete data in view ‘ViewCustomerData’ that has ID ‘005’.
  299. Then display all data from MsCustomer table.*/
  300.  
  301. SELECT *
  302. FROM ViewCustomerData
  303.  
  304. DELETE FROM ViewCustomerData
  305. WHERE ID='005'
  306.  
  307. SELECT *
  308. FROM MsCustomer
  309.  
  310. /*10.   Delete the view ‘ViewCustomerData’. */
  311.  
  312. DROP VIEW ViewCustomerData
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement