Sunteți pe pagina 1din 14

Proiect la Baze de Date în Economie

Design Exterior

Giurgiuman Claudia-Teodora
IE, ID, an 2, Grupa 2
An Universitar 2021-2022
Scenariu:
La nivelul unei firme de design exterior, se dorește realizarea unei baze de date, pentru o mai
buna și eficientă gestiune a activității acesteia. Firma se ocupă de amenajarea exterioară atât a
spațiilor exterioare comune sau private (parcuri, grădini, terase etc.) cu mobilier, cât și a
clădirilor (locuințe, spații de birouri, sedii pentru firme etc.). Activitatea este extinsă la nivel
mondial iar vânzările pot fi efectuate atât către persoane juridice, cât și fizice.
Baza de date va cuprinde informații despre clienți (precum datele de identificare, comenzi,
adresele corespunzătoare), prețuri și costuri (acestea fiin calculate sezonier), furnizori (de ex. de
materii prime: lemn, fier etc.), locațiile proiectelor de design.

Diagrama ERD
Schema bazei de date:

Crearea Tabelelor
Tabel product
create table Product (Id int NOT NULL PRIMARY KEY, ProductName varchar(250),
CreatedAt varchar(250), Quantity int, UnitsInStock int, Discontinued boolean, DiscontinuedAt
varchar(250), CONSTRAINT MAX_UnitsInStock CHECK (UnitsInStock <= 30 and Quantity
<= 20));

Tabel season

create table Season (Id int NOT NULL PRIMARY KEY, Name varchar(250));
Tabel History

Create table History (Id int NOT NULL PRIMARY KEY, ProductId int NOT NULL, SeasonId
int NOT NULL, CreatedAt varchar(250), CompletedAt varchar(250), Price int, FOREIGN KEY
(ProductId) REFERENCES Product(Id), FOREIGN KEY (SeasonId) REFERENCES
Season(Id));

Tabel ProductType

create table ProductType (Id int NOT NULL PRIMARY KEY, ProductId int, Occurence
varchar(250), Description varchar(250), FOREIGN KEY (ProductId) REFERENCES
Product(Id));

Tabel BuyerType

create table BuyerType (Id int NOT NULL PRIMARY KEY, Type varchar(250));

Tabel Buyer

create table Buyer (Id int NOT NULL PRIMARY KEY, BuyerTypeId int NOT NULL, Name
varchar(250), SocialCapital int, AddedAt varchar(250), Discontinued boolean, DiscontinuedAt
varchar(250), FOREIGN KEY (BuyerTypeId) REFERENCES BuyerType(Id));

Tabel BuyerLocation

create table BuyerLocation (Id int NOT NULL PRIMARY KEY, BuyerId int NOT NULL, City
varchar(250), Region varchar(250), Country varchar(250), Street varchar(250), Number
varchar(250), FOREIGN KEY (BuyerId) REFERENCES Buyer(Id));

Tabel Sale

create table Sale (Id int NOT NULL PRIMARY KEY, ProductId int NOT NULL, BuyerId int
NOT NULL, CreatedAt varchar(250), Price decimal, FOREIGN KEY (ProductId)
REFERENCES Product(Id), FOREIGN KEY (BuyerId) REFERENCES Buyer(Id));
Tabel SupplierType

create table SupplierType (Id int NOT NULL PRIMARY KEY, Type varchar(250));

Tabel Supplier

create table Supplier (Id int NOT NULL PRIMARY KEY, SupplierTypeId int NOT NULL,
Name varchar(250), Location varchar(250), Price int, FOREIGN KEY (SupplierTypeId)
REFERENCES SupplierType(Id));

Tabel Supply

create table Supply (Id int NOT NULL PRIMARY KEY, ProductId int NOT NULL, SupplierId
int NOT NULL, CreatedAt varchar(250), Price decimal, FOREIGN KEY (ProductId)
REFERENCES Product(Id), FOREIGN KEY (SupplierId) REFERENCES
Buyer(Id),CONSTRAINT MIN_PriceTarget CHECK (Price >= 50));

Popularea datelor

Tabel Product

insert into Product (Id, ProductName, CreatedAt, Quantity, UnitsInStock, Discontinued,


DiscontinuedAt) values (1, 'Persian bed', '23.02.2022', 1, 15, false, '-');

insert into Product (Id, ProductName, CreatedAt, Quantity, UnitsInStock, Discontinued,


DiscontinuedAt) values (2, 'Persian armchair', '22.02.2022', 2, 5, false, '-');

insert into Product (Id, ProductName, CreatedAt, Quantity, UnitsInStock, Discontinued,


DiscontinuedAt) values (3, 'Elm collection', '10.01.2022', 20, 10, false, '-');

insert into Product (Id, ProductName, CreatedAt, Quantity, UnitsInStock, Discontinued,


DiscontinuedAt) values (4, 'Crate and barrel', '11.03.2021', 1, 2, false, '-');

insert into Product (Id, ProductName, CreatedAt, Quantity, UnitsInStock, Discontinued,


DiscontinuedAt) values (5, 'Surya collection', '23.02.2022', 15, 0, true, '09.07.2022');

insert into Product (Id, ProductName, CreatedAt, Quantity, UnitsInStock, Discontinued,


DiscontinuedAt) values (6, 'Jaipur living', '01.01.2022', 3, 0, true, '09.07.2022');
Tabel Season

insert into Season (Id, Name) values (1, 'Spring');

insert into Season (Id, Name) values (2, 'Summer');

insert into Season (Id, Name) values (3, 'Autumn');

insert into Season (Id, Name) values (4, 'Winter');

insert into Season (Id, Name) values (5, 'Spring-Summer');

insert into Season (Id, Name) values (6, 'Autumn-Winter');

Tabel History

insert into History (Id, ProductId, SeasonId, CreatedAt, CompletedAt, Price) values (1, 1, 1,
'23.03.2022', '24.03.2022', 100);

insert into History (Id, ProductId, SeasonId, CreatedAt, CompletedAt, Price) values (2, 2, 1,
'23.03.2022', '24.03.2022', 20);

insert into History (Id, ProductId, SeasonId, CreatedAt, CompletedAt, Price) values (3, 3, 5,
'23.03.2022', '24.07.2022', 10);

insert into History (Id, ProductId, SeasonId, CreatedAt, CompletedAt, Price) values (4, 2, 5,
'21.04.2022', '24.07.2022', 15);

insert into History (Id, ProductId, SeasonId, CreatedAt, CompletedAt, Price) values (5, 6, 2,
'15.06.2022', '16.06.2022', 45);

insert into History (Id, ProductId, SeasonId, CreatedAt, CompletedAt, Price) values (6, 5, 2,
'10.06.2022', '24.06.2022', 214);

Tabel Product type

insert into ProductType (Id, ProductId, Occurence, Description) values (1, 1, '12x', 'Lorem
ipsum');

insert into ProductType (Id, ProductId, Occurence, Description) values (2, 2, '1x', 'Lorem ipsum
lorem');

insert into ProductType (Id, ProductId, Occurence, Description) values (3, 3, '2x', 'Lorem ipsum
lorem ipsum');
insert into ProductType (Id, ProductId, Occurence, Description) values (4, 3, '5x', 'Lorem ipsum
lorem ipsum lorem');

insert into ProductType (Id, ProductId, Occurence, Description) values (5, 4, '20x', 'Lorem ipsum
lorem ipsum lorem ipsum');

insert into ProductType (Id, ProductId, Occurence, Description) values (6, 6, '5x', 'Lorem ipsum
lorem ipsum lorem ipsum lorem');

Tabel Buyer type

insert into BuyerType (Id, Type) values (1, 'PF');

insert into BuyerType (Id, Type) values (2, 'PJ');

insert into BuyerType (Id, Type) values (3, 'SRL');

insert into BuyerType (Id, Type) values (4, 'SA');

insert into BuyerType (Id, Type) values (5, 'Global');

insert into BuyerType (Id, Type) values (6, 'Other');

Tabel Buyer

insert into Buyer (Id, BuyerTypeId, Name, SocialCapital, AddedAt, Discontinued,


DiscontinuedAt) values (1, 1, 'John Smith', 30000, '12.02.2020', false, '-');

insert into Buyer (Id, BuyerTypeId, Name, SocialCapital, AddedAt, Discontinued,


DiscontinuedAt) values (2, 1, 'Michelle Davies', 65000, '12.05.2020', false, '-');

insert into Buyer (Id, BuyerTypeId, Name, SocialCapital, AddedAt, Discontinued,


DiscontinuedAt) values (3, 2, 'Mitchel and Co', 156000, '05.02.2018', false, '-');

insert into Buyer (Id, BuyerTypeId, Name, SocialCapital, AddedAt, Discontinued,


DiscontinuedAt) values (4, 2, 'Apple', 99999999, '12.02.2020', true, '20.07.2021');

insert into Buyer (Id, BuyerTypeId, Name, SocialCapital, AddedAt, Discontinued,


DiscontinuedAt) values (5, 3, 'Matsuke SRL', 99999999, '09.01.2018', false, '-');

insert into Buyer (Id, BuyerTypeId, Name, SocialCapital, AddedAt, Discontinued,


DiscontinuedAt) values (6, 6, 'Marshal Invetments', 99999999, '07.05.2022', true, '07.06.2022');
Tabel Buyer location

insert into BuyerLocation (Id, BuyerId, City, Region, Country, Street, Number) values (1, 1,
'Berlin', 'Bavaria', 'Germany', 'Vasentrop Street', '2');

insert into BuyerLocation (Id, BuyerId, City, Region, Country, Street, Number) values (2, 2,
'Washington', 'District of Columbia', 'USA', 'Independence Avenue', '2');

insert into BuyerLocation (Id, BuyerId, City, Region, Country, Street, Number) values (3, 3,
'New Orleans', 'Louisiana', 'USA', 'Loyola Avenue', '10');

insert into BuyerLocation (Id, BuyerId, City, Region, Country, Street, Number) values (4, 4,
'Cupertino', 'California', 'USA', 'Apple Park Way', '1');

insert into BuyerLocation (Id, BuyerId, City, Region, Country, Street, Number) values (5, 5,
'Osaka', 'Honshu', 'Japan', 'Sumiyoshi Ward', '9-12');

insert into BuyerLocation (Id, BuyerId, City, Region, Country, Street, Number) values (6, 6,
'Birmingham', 'West Midlands', 'UK', 'Henshaw Rd', '2-5');

Tabel Sale

insert into Sale (Id, ProductId, BuyerId, CreatedAt, Price) values (1, 1, 2, '12.02.2021', 234);

insert into Sale (Id, ProductId, BuyerId, CreatedAt, Price) values (2, 1, 3, '12.02.2021', 234);

insert into Sale (Id, ProductId, BuyerId, CreatedAt, Price) values (3, 3, 4, '12.02.2021', 234);

insert into Sale (Id, ProductId, BuyerId, CreatedAt, Price) values (4, 4, 5, '13.02.2021', 234);

insert into Sale (Id, ProductId, BuyerId, CreatedAt, Price) values (5, 5, 6, '13.02.2021', 234);

insert into Sale (Id, ProductId, BuyerId, CreatedAt, Price) values (6, 6, 1, '14.02.2021', 234);

Tabel Supplier type

insert into SupplierType (Id, Type) values (1, 'PF');

insert into SupplierType (Id, Type) values (2, 'PJ');

insert into SupplierType (Id, Type) values (3, 'SRL');

insert into SupplierType (Id, Type) values (4, 'SA');


insert into SupplierType (Id, Type) values (5, 'Global');

insert into SupplierType (Id, Type) values (6, 'Other');

Tabel Supplier

insert into Supplier (Id, SupplierTypeId, Name, Location, Price) values (1, 2, 'Fabrica de lemne',
'Brasov', 4563);

insert into Supplier (Id, SupplierTypeId, Name, Location, Price) values (2, 1, 'Apostol Ion',
'Craiova', 234);

insert into Supplier (Id, SupplierTypeId, Name, Location, Price) values (3, 2, 'Holver',
'Timisoara', 3453);

insert into Supplier (Id, SupplierTypeId, Name, Location, Price) values (4, 2, 'Mobirom', 'Iasi',
334);

insert into Supplier (Id, SupplierTypeId, Name, Location, Price) values (5, 2, 'Nord Arin',
'Milano', 452);

insert into Supplier (Id, SupplierTypeId, Name, Location, Price) values (6, 2, 'Staer', 'Galati',
768);

Tabel Supply

insert into Supply (Id, ProductId, SupplierId, CreatedAt, Price) values (1, 1, 1, '01.09.2015',
234.5);

insert into Supply (Id, ProductId, SupplierId, CreatedAt, Price) values (2, 2, 2, '01.09.2015',
3434.5);

insert into Supply (Id, ProductId, SupplierId, CreatedAt, Price) values (3, 3, 3, '01.09.2015',
436.78);

insert into Supply (Id, ProductId, SupplierId, CreatedAt, Price) values (4, 4, 4, '01.09.2015',
3267.3);

insert into Supply (Id, ProductId, SupplierId, CreatedAt, Price) values (5, 5, 5, '01.09.2015',
4364.5);

insert into Supply (Id, ProductId, SupplierId, CreatedAt, Price) values (6, 6, 6, '01.09.2015',
3552.5);
Modificări de structură

1. La crearea tabelelor BuyerLocation, Sale, SupplierType, BuyerType am adăugat


următoarele atribute:
 alter table BuyerLocation add Continent varchar(255);
 alter table Sale add CompletedAt varchar(255);
 alter table SupplierType add Description varchar(255);
 alter table BuyerType add Description varchar(255);
 alter table Sale add Notes varchar(255);

Modificări de conținut:

 update BuyerLocation set City = 'New York', Region = 'New Jersey', Street = '3rd Street'
where Id = 2;
 update Sale set Price = 340 where Id = 3;
 update Supply set ProductId = 2 where Id = 1;

View-uri

create view PreviousSalesAbove50 as

select p.ProductName, s.Name, h.CreatedAt, h.Price

from History h

inner join Product p on p.Id = h.ProductId

inner join Season s on s.Id = h.SeasonId

where h.Price > 50;

create view MostBoughtProduct as

select s.ProductId, count(s.ProductId) as occurence, p.ProductName

from Sale s

inner join Product p on p.Id = s.ProductId

group by s.ProductId

order by s.ProductId DESC


limit 1;

create view SumOfPricesFromPJSuppliers as

select sum(s.Price)

from Supply s

inner join Supplier sr on s.Id = sr.SupplierId

where sr.SupplierTypeId = 2;

create view SupplyItemsAbove1000 as

select p.ProductName, s.Price, sr.Name, sr.Price

from Supply s

inner join Supplier sr on sr.Id = s.SupplierId

inner join Product p on p.Id = s.ProductId

where s.Price > 1000;

Interogări

1. GetTotalCostPerSupplier

Select sum(s.Price), sr.Name

from Supplier sr

inner join Supply s on s.SupplierId = sr.Id

group by sr.Name;

2. GetTotalRevenuePerSeason

Select sum(Price), s.Name

from Season s
inner join History h on h.SeasonId = s.Id

group by s.Name;

3. GetSupplierTypeThatSoldMoreThan3Times

Select Id, Type

from SupplierType st

where 3 <= (Select count(SupplierTypeId) from Supplier where SupplierTypeId = st.Id);

4. GetProductWithPriceAboveAverageSupplierCosts

Select p.ProductName, s.Price

From Sale s

inner join Product p on s.ProductId = p.Id

where Price > (Select avg(Price) from Sale);

5. AppleTotalBuyValue

Select sum(Price) as TotalSum

From Sale

where BuyerId = 4;

6. MostNeededProduct

Select p.ProductName, count(s.ProductId) as Occurence

from Supply s

inner join Product p on s.ProductId = p.Id

group by s.ProductId

order by count(ProductId) DESC;


7. MostSellProduct

Select p.ProductName, count(s.ProductId) as Occurence

from Sale s

inner join Product p on s.ProductId = p.Id

group by s.ProductId

order by count(ProductId) DESC;

8. GetClientTypeThatBoughtMoreThan2Times

Select Id, Type

from BuyerType bt

where 2 <= (Select count(BuyerTypeId) from Buyer where BuyerTypeId = bt.Id);

9. GetLocationThatBoughtMoreThanTwoTimes

Select Id, Name

from Buyer b

where 2 <= (Select count(*) from BuyerLocation where BuyerId = b.Id);

10. GetProductsPurchasedAbove30

Select Id, ProductName

from Product p

where 30 <= (Select SUM(Price) from Supply s where ProductId = p.Id);


11. GetProductsThatHadSalesAbove30

Select Id, ProductName, UnitsInStock

from Product p

where 30 <= (Select SUM(Price) from History h where ProductId = p.Id);

12. GetProductsWithMoreThan2Sales

Select Id, ProductName, UnitsInStock

from Product p

where 2 <= (Select COUNT(ProductId) from History h where ProductId = p.Id)

13. FindAllDiscontinuedItemsWithUnitsInStock

Select ProductName, UnitsInStock

from Product

where Discontinued = 1 and UnitsInStock > 0;

S-ar putea să vă placă și