Advertisement
Guest User

Untitled

a guest
Jun 24th, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.40 KB | None | 0 0
  1.  
  2. create database databejz
  3. use databejz
  4.  
  5. create table proizvodi
  6. (
  7. ProizvodID int not null identity(1,1) constraint PK_ProizvodID  primary key,
  8.  Sifra nvarchar (25) not null constraint UQ_sifra unique,
  9.     Naziv nvarchar (50) not null,
  10.     Kategorija nvarchar (50) null,
  11.  Podkategorija  nvarchar (50) null,
  12.  Boja   nvarchar (15) not null,
  13.  Cijena decimal not null,
  14.  StanjeZaliha   int not null
  15. );
  16.  
  17. create table prodaja
  18. (
  19. ProdajaID int not null identity(1,1) constraint PK_ProdajaID primary key,  
  20.  ProizvodID int not null constraint FK_Proizvodi_ProizvodID foreign key references proizvodi(ProizvodID),  
  21.  Godina  int not null,  
  22. Mjesec int not null,   
  23.  UkupnoProdano int not null,
  24. UkupnoPopust decimal not null,
  25. UkupnoIznos decimal not null
  26.  
  27.  
  28. );
  29.  
  30. insert into proizvodi
  31. (Sifra,Naziv,Kategorija,Podkategorija,Boja,Cijena,StanjeZaliha)
  32. values('ssds','sako',null,null,'roza',34,15)
  33.  
  34. insert into prodaja
  35. (ProizvodID,Godina,Mjesec, UkupnoProdano,UkupnoPopust,UkupnoIznos)
  36. values(1,2014,3,33, 0.9, 30),(1,2014,7,33, 0.9, 30),(1,2015,3,33, 0.9, 30)
  37. --------------------------------------
  38. --------------------------------------
  39. --------------------------------------
  40.  
  41. --code below this line throws error about duplicate key
  42. --Violation of PRIMARY KEY constraint 'PK_ProizvodID'. Cannot insert duplicate key in object 'dbo.proizvodi'. The duplicate key value --is (747).
  43.  
  44. set identity_insert proizvodi ON
  45. insert into proizvodi
  46. (ProizvodID,Sifra,Naziv,Kategorija,Podkategorija,Boja,Cijena,StanjeZaliha)
  47. select distinct p.ProductID, p.ProductNumber,p.[Name], pc.[Name],psc.[Name], p.Color,p.ListPrice,pii.Quantity
  48. from AdventureWorks2017.Production.Product as p
  49. join AdventureWorks2017.Production.ProductSubcategory as psc on psc.ProductSubcategoryID= p.ProductSubcategoryID
  50. join AdventureWorks2017.Production.ProductCategory as pc on pc.ProductCategoryID= psc.ProductCategoryID
  51. inner join AdventureWorks2017.Production.ProductInventory as pii on pii.ProductID= p.ProductID
  52. inner join AdventureWorks2017.Sales.SalesOrderDetail as sod on sod.ProductID= p.ProductID
  53. inner join AdventureWorks2017.Sales.SalesOrderHeader as soh on soh.SalesOrderID= sod.SalesOrderID
  54. inner join AdventureWorks2017.Sales.SalesTerritory as st on st.TerritoryID= soh.TerritoryID
  55. inner join AdventureWorks2017.Production.[Location] as l on l.LocationID= pii.LocationID
  56. where st.[Group] ='Europe'
  57. order by p.ProductID
  58. set identity_insert proizvodi OFF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement