Advertisement
Guest User

Untitled

a guest
Mar 17th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.74 KB | None | 0 0
  1. --Zad. 1
  2. --W bazie danych należy utworzyć schemat, którego nazwa będzie odpowiadać nazwisku wykonują-cego ćwiczenie
  3. --(zapisać zapytanie tworzące ten schemat).
  4. USE AdventureWorks2014
  5. GO
  6. CREATE SCHEMA Mandziejewska AUTHORIZATION dbo
  7. GO
  8.  
  9. --Zad. 2
  10. --W nowo utworzonym schemacie utworzyć tabele (zapisać skrypt create table), opisane w następujących podpunktach:
  11. --• DIM_CUSTOMER (CustomerID, FirstName, LastName, TerritoryName, CounrtyRegionCode, Group) – tabele źródłowe:
  12. --· Sales.SalesTerritory
  13. --· Sales.Customer
  14. --· Person.Person
  15.  
  16. USE AdventureWorks2014
  17. GO
  18. CREATE TABLE Mandziejewska.DIM_CUSTOMER(
  19. CustomerID int NOT NULL,
  20. FirstName nvarchar(50) NULL,
  21. LastName nvarchar(50) NULL,
  22. TerritoryName nvarchar(50) NULL,
  23. CountryRegionCode nvarchar(3) NOT NULL,
  24. [Group] nvarchar(50) NOT NULL);
  25. GO
  26.  
  27. --• DIM_PRODUCT (ProductID, Name, ListPrice, Color, Rating, SubCategoryName, Catego-ryName) – tabele źródłowe:
  28. --· Production.Product
  29. --· Production.ProductReview
  30. --· Production.ProductSubcategory
  31. --· Production.ProductCategory
  32.  
  33. USE AdventureWorks2014
  34. GO
  35. CREATE TABLE Mandziejewska.DIM_PRODUCT(
  36. ProductID int NOT NULL,
  37. [Name] nvarchar(50) NOT NULL,
  38. ListPrice money NOT NULL,
  39. Color nvarchar(15) NULL,
  40. Rating float NULL,
  41. SubCategoryName nvarchar(50) NULL,
  42. CategoryName nvarchar(50) NULL);
  43. GO
  44.  
  45. --• DIM_SALESPERSON (SalesPersonID, FirstName, LastName, Title, Gender, CountryRe-gionCode, Group, Age,
  46. --Seniority) – tabele źródłowe:
  47. --· HumanResources.Employee
  48. --· Person.Person
  49. --· Sales.SalesTerritory
  50. --· Sales.SalesPerson
  51. --Uwaga: Wiek sprzedawcy i staż pracy należy wyliczyć wykorzystując pole BirthDate i HireDate.
  52.  
  53. USE AdventureWorks2014
  54. GO
  55. CREATE TABLE Mandziejewska.DIM_SALESPERSON(
  56. SalesPersonID int NOT NULL,
  57. FirstName nvarchar(50) NOT NULL,
  58. LastName nvarchar(50) NOT NULL,
  59. Title nvarchar(8) NULL,
  60. Gender nchar(1) NOT NULL,
  61. CountryRegionCode nvarchar(3) NULL,
  62. [Group] nvarchar(50) NULL,
  63. Age int NOT NULL,
  64. Seniority int NOT NULL
  65. );
  66. GO
  67.  
  68. --• FACT_SALES (ProductID, CustomerID, SalesPersonID, OrderDate, ShipDate, OrderQty, UnitPrice,
  69. --UnitPriceDiscount, LineTotal) – tabele źródłowe:
  70. --· Sales.SalesOrderDetail
  71. --· Sales.SalesOrderHeader
  72. --Uwaga: Kolumny OrderDate oraz ShipDate przechowują dane typu całkowitego,
  73. --gdzie cztery pierwsze cyfry oznaczają rok, dwie następne miesiąc, a dwie ostatnie dzień.
  74. --Do pobrania po-szczególnych części daty użyć funkcji datepart.
  75.  
  76. USE AdventureWorks2014
  77. GO
  78. CREATE TABLE Mandziejewska.FACT_SALES(
  79. ProductID int NOT NULL,
  80. CustomerID int NOT NULL,
  81. SalesPersonID int NULL,
  82. OrderDate int NOT NULL,
  83. ShipDate int NULL,
  84. OrderQty smallint NOT NULL,
  85. UnitPrice money NOT NULL,
  86. UnitPriceDiscount money NOT NULL,
  87. LineTotal numeric(38,6) NOT NULL
  88. );
  89. GO
  90.  
  91. --Zad. 3. Wypełnić nowoutworzone tabele danymi znajdującymi się w tabelach źródłowych.
  92. --Do wy-pełnienia użyć instrukcji INSERT INTO.
  93. --Uwaga 1. Do tabeli DIM_PRODUCT należy także skopiować produkty, które nie mają przypi-sanej
  94. --podkategorii.
  95. --Uwaga 2. Do tabeli FACT_SALES należy skopiować transakcje, które nie mają sprzedawcy.
  96.  
  97. USE AdventureWorks2014
  98. GO
  99. INSERT INTO Mandziejewska.DIM_CUSTOMER
  100. SELECT
  101. C.CustomerID,
  102. P.FirstName,
  103. P.LastName,
  104. T.[Name],
  105. T.CountryRegionCode,
  106. T.[Group]
  107. FROM Sales.SalesTerritory T
  108. RIGHT JOIN Sales.Customer C ON T.TerritoryID=C.TerritoryID
  109. LEFT JOIN Person.Person P ON P.BusinessEntityID=C.CustomerID;
  110. GO
  111.  
  112. USE AdventureWorks2014
  113. GO
  114. INSERT INTO Mandziejewska.DIM_PRODUCT
  115. SELECT
  116. distinct pp.ProductID,
  117. pp.Name,
  118. pp.ListPrice,
  119. pp.Color,
  120. AVG(pr.Rating) OVER (Partition by pr.ProductID),
  121. ps.Name,
  122. pc.Name
  123. FROM [Production].[ProductReview] pr RIGHT JOIN [Production].[Product] pp ON pp.ProductID = pr.ProductID
  124. LEFT JOIN [Production].[ProductSubcategory] ps ON pp.ProductSubcategoryID = ps.ProductSubcategoryID
  125. LEFT JOIN [Production].[ProductCategory] pc ON pc.ProductCategoryID = ps.ProductCategoryID
  126.  
  127. --• DIM_SALESPERSON (SalesPersonID, FirstName, LastName, Title, Gender, CountryRe-gionCode, Group, Age,
  128. --Seniority) – tabele źródłowe:
  129. --· HumanResources.Employee
  130. --· Person.Person
  131. --· Sales.SalesTerritory
  132. --· Sales.SalesPerson
  133. --Uwaga: Wiek sprzedawcy i staż pracy należy wyliczyć wykorzystując pole BirthDate i HireDate.
  134.  
  135. USE AdventureWorks2014
  136. GO
  137. INSERT INTO Mandziejewska.DIM_SALESPERSON
  138. SELECT
  139. S.BusinessEntityID,
  140. P.FirstName,
  141. P.LastName,
  142. P.Title,
  143. H.Gender,
  144. T.CountryRegionCode,
  145. T.[Group],
  146. DATEDIFF(YY,H.BirthDate,GETDATE()),
  147. DATEDIFF(YY,H.HireDate, GETDATE())
  148. FROM Sales.SalesPerson S JOIN Person.Person P ON S.BusinessEntityID=P.BusinessEntityID
  149. JOIN HumanResources.Employee H ON H.BusinessEntityID=P.BusinessEntityID
  150. LEFT JOIN Sales.SalesTerritory T ON T.TerritoryID=S.TerritoryID;
  151.  
  152.  
  153. USE AdventureWorks2014
  154. GO
  155. INSERT INTO Mandziejewska.FACT_SALES
  156. SELECT
  157. D.ProductID,
  158. H.CustomerID,
  159. H.SalesPersonID,
  160. CAST(CONCAT
  161. (datepart(YY, H.OrderDate),
  162. CASE WHEN datepart(M, H.OrderDate) < 10 THEN '0' ELSE '' END,
  163. datepart(M, H.OrderDate),
  164. CASE WHEN datepart(D, H.OrderDate) < 10 THEN '0' ELSE '' END,
  165. datepart(D, H.OrderDate)
  166. )
  167. AS int),
  168.  
  169. CAST(CONCAT
  170. (datepart(YY, H.ShipDate),
  171. CASE WHEN datepart(M, H.ShipDate) < 10 THEN '0' ELSE '' END,
  172. datepart(M, H.ShipDate),
  173. CASE WHEN datepart(D, H.ShipDate) < 10 THEN '0' ELSE '' END,
  174. datepart(D, H.ShipDate)
  175. )
  176. AS int),
  177. D.OrderQty,
  178. D.UnitPrice,
  179. D.UnitPriceDiscount,
  180. D.LineTotal
  181. FROM Sales.SalesOrderDetail D JOIN Sales.SalesOrderHeader H ON D.SalesOrderID = H.SalesOrderID;
  182. GO
  183.  
  184. --Zad. 4. Dodać integralność referencyjną i klucze główne do tabel już zdefiniowanych.
  185.  
  186.  
  187. USE AdventureWorks2014
  188. GO
  189. ALTER TABLE Mandziejewska.DIM_CUSTOMER
  190. ADD CONSTRAINT pk_dim_customer PRIMARY KEY(CustomerID);
  191. GO
  192.  
  193. ALTER TABLE Mandziejewska.DIM_PRODUCT
  194. ADD CONSTRAINT pk_dim_product PRIMARY KEY(ProductID);
  195. GO
  196.  
  197. ALTER TABLE Mandziejewska.DIM_SALESPERSON
  198. ADD CONSTRAINT pk_dim_salesperson PRIMARY KEY (SalesPersonID);
  199.  
  200. ALTER TABLE Mandziejewska.FACT_SALES
  201. ADD FactSalesID int NOT NULL IDENTITY(1,1) PRIMARY KEY;
  202. GO
  203.  
  204.  
  205. ALTER TABLE Mandziejewska.FACT_SALES
  206. ADD CONSTRAINT fk_fact_product FOREIGN KEY (ProductID) REFERENCES Mandziejewska.DIM_Product(ProductID),
  207. CONSTRAINT fk_fact_customer FOREIGN KEY (CustomerID) REFERENCES Mandziejewska.DIM_CUSTOMER(CustomerID),
  208. CONSTRAINT fk_fact_salesperson FOREIGN KEY (SalesPersonID) REFERENCES Mandziejewska.DIM_SALESPERSON(SalesPersonID);
  209. GO
  210.  
  211. --Zad. 5
  212. --Przygotować instrukcję INSERT INTO, która sprawdzi poprawność integralności referencyjnej
  213. --oraz klucze główne.
  214.  
  215. --klucze główne
  216. INSERT INTO Mandziejewska.DIM_CUSTOMER VALUES(1,'Karol','Kowalski','New York','US','South America');
  217. --Violation of PRIMARY KEY constraint 'pk_dim_customer'. Cannot insert duplicate key in object 'Mandziejewska.DIM_CUSTOMER'. The duplicate key value is (1).
  218.  
  219. INSERT INTO Mandziejewska.DIM_PRODUCT VALUES(1,'bike','100','pink', 5, NULL, 'Bikes');
  220. --Violation of PRIMARY KEY constraint 'pk_dim_product'. Cannot insert duplicate key in object 'Mandziejewska.DIM_PRODUCT'. The duplicate key value is (1).
  221. INSERT INTO Mandziejewska.DIM_SALESPERSON VALUES(274,'Dawid','Nowak', 'Mr.','M',NULL,NULL,34,5);
  222. --Violation of PRIMARY KEY constraint 'pk_dim_salesperson'. Cannot insert duplicate key in object 'Mandziejewska.DIM_SALESPERSON'. The duplicate key value is (274).
  223. SET IDENTITY_INSERT Mandziejewska.FACT_SALES ON;
  224. INSERT INTO Mandziejewska.FACT_SALES(ProductID,CustomerID,SalesPersonID,OrderDate,ShipDate,OrderQty,UnitPrice,UnitPriceDiscount,LineTotal,FactSalesID) VALUES(10,220,125,20150503,NULL,1,50,0,30,4);
  225. --Violation of PRIMARY KEY constraint 'PK__FACT_SAL__AA8B53276D5C82BB'. Cannot insert duplicate key in object 'Mandziejewska.FACT_SALES'. The duplicate key value is (4).
  226.  
  227. -- integralność referencyjna
  228.  
  229. SET IDENTITY_INSERT Mandziejewska.FACT_SALES OFF;
  230.  
  231. INSERT INTO Mandziejewska.FACT_SALES VALUES(100000,333,NULL,20150503,NULL,1,20,0,10);
  232. --The INSERT statement conflicted with the FOREIGN KEY constraint "fk_fact_product". The conflict occurred in database "AdventureWorks2014", table "Mandziejewska.DIM_PRODUCT", column 'ProductID'.
  233.  
  234. INSERT INTO Mandziejewska.FACT_SALES VALUES(1,50119,NULL,20150503,NULL,1,20,0,10);
  235. --The INSERT statement conflicted with the FOREIGN KEY constraint "fk_fact_customer". The conflict occurred in database "AdventureWorks2014", table "Mandziejewska.DIM_CUSTOMER", column 'CustomerID'.
  236.  
  237. INSERT INTO Mandziejewska.FACT_SALES VALUES(1,1,291,20150503,NULL,1,20,0,0);
  238. --The INSERT statement conflicted with the FOREIGN KEY constraint "fk_fact_salesperson". The conflict occurred in database "AdventureWorks2014", table "Mandziejewska.DIM_SALESPERSON", column 'SalesPersonID'.
  239.  
  240.  
  241. --Zad 6
  242. --Przygotować instrukcję usuwającą każdą z tabel utworzonych w trakcie dotychczasowej pracy.
  243. --Uwaga: Instrukcja powinna być wykonana tylko pod warunkiem istnienia usuwanej tabeli. Należy sprawdzić, czy dana tabela istnieje,
  244. --używając instrukcji IF oraz informacji zawartych w widoku systemowym INFORMATION_SCHEMA.TABLES.
  245.  
  246. USE AdventureWorks2014
  247. GO
  248. IF(SELECT COUNT (*) FROM INFORMATION_SCHEMA.TABLES
  249. WHERE TABLE_SCHEMA='Mandziejewska' AND TABLE_NAME='FACT_SALES')>0
  250. DROP TABLE Mandziejewska.FACT_SALES;
  251. GO
  252.  
  253. IF(SELECT COUNT (*) FROM INFORMATION_SCHEMA.TABLES
  254. WHERE TABLE_SCHEMA='Mandziejewska' AND TABLE_NAME='DIM_CUSTOMER')>0
  255. DROP TABLE Mandziejewska.DIM_CUSTOMER;
  256. GO
  257. IF(SELECT COUNT (*) FROM INFORMATION_SCHEMA.TABLES
  258. WHERE TABLE_SCHEMA='Mandziejewska' AND TABLE_NAME='DIM_PRODUCT')>0
  259. DROP TABLE Mandziejewska.DIM_PRODUCT;
  260. GO
  261.  
  262. IF(SELECT COUNT (*) FROM INFORMATION_SCHEMA.TABLES
  263. WHERE TABLE_SCHEMA='Mandziejewska' AND TABLE_NAME='DIM_SALESPERSON')>0
  264. DROP TABLE Mandziejewska.DIM_SALESPERSON;
  265. GO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement