Question: Hello! Help with SQL , please! We have following database: CREATE TABLE public.product ( productid serial 4 NOT NULL, name varchar ( 5 0 )
Hello! Help with SQL please!
We have following database:
CREATE TABLE public.product
productid serial NOT NULL,
"name" varchar NOT NULL,
productnumber varchar NOT NULL,
color varchar NULL,
listprice numeric NOT NULL,
"size" varchar NULL,
sizeunitmeasurecode bpchar NULL,
weightunitmeasurecode bpchar NULL,
weight numeric NULL,
productline bpchar NULL,
"class" bpchar NULL,
"style" bpchar NULL,
productsubcategoryid int NULL,
productmodelid int NULL,
sellstartdate timestamp NOT NULL,
sellenddate timestamp NULL,
discontinueddate timestamp NULL,
CONSTRAINT productpkey PRIMARY KEY productid
;
CREATE INDEX idxproductcolor ON public.product USING btree color;
CREATE TABLE public.productsubcategory
productsubcategoryid serial NOT NULL,
productcategoryid serial NOT NULL,
"name" varchar NOT NULL,
CONSTRAINT productsubcategorypkey PRIMARY KEY productsubcategoryid
;
CREATE TABLE public.productcategory
productcategoryid serial NOT NULL,
"name" varchar NOT NULL,
CONSTRAINT productcategorypkey PRIMARY KEY productcategoryid
;
CREATE TABLE planstatus
quarterid varchar NOT NULL,
status varchar NOT NULL,
modifieddatetime timestamp NOT NULL DEFAULT CURRENTTIMESTAMP,
author varchar NOT NULL DEFAULT CURRENTUSER,
country varchar NOT NULL,
CONSTRAINT planstatuspk PRIMARY KEY quarterid country
;
CREATE TABLE plandata
versionid varchar NOT NULL,
country varchar NOT NULL,
quarterid varchar NOT NULL,
pcid int NOT NULL,
salesamt numeric NULL,
CONSTRAINT planappdatapkey PRIMARY KEY quarterid country, pcid, versionid
;
CREATE TABLE countrymanagers
username varchar NOT NULL,
country varchar NOT NULL,
CONSTRAINT countrymanagerspk PRIMARY KEY username country
;
CREATE TABLE company
id int NOT NULL GENERATED ALWAYS AS IDENTITY,
cname varchar NOT NULL,
countrycode varchar NULL,
city varchar NULL,
CONSTRAINT dcompanypk PRIMARY KEY id
;
CREATE TABLE companysales
cid int NOT NULL,
salesamt numeric NULL,
year int NULL,
quarteryr int NULL,
qr varchar NOT NULL,
categoryid int NOT NULL,
ccls varchar NULL,
CONSTRAINT companysalespk PRIMARY KEY qr cid, categoryid
;
CREATE TABLE companyabc
cid int NOT NULL,
salestotal numeric NULL,
cls varchar NULL,
year int NOT NULL,
CONSTRAINT companyabcpk PRIMARY KEY cid year
;
create view vplanedit as
select pdcountry, pdquarterid, pdpcid, pdsalesamt, pdversionid
from plandata pd
where
pdversionid P
and
pdcountry in select country
from countrymanagers cm
where cmusername currentuser
and
pdquarterid in select psquarterid
from planstatus ps
where psauthor currentuser and psstatus L;
create view vplan as
select pdcountry,
pdpcid,
pdquarterid,
pdsalesamt
FROM plandata pd
WHERE pdversionid A
AND pdcountry IN SELECT cmcountry FROM countrymanagers cm
WHERE cmusername CURRENTUSER
or
pghasrolecurrentuser, 'planadmin', 'member'
AND pdquarterid IN SELECT psquarterid
FROM planstatus ps
WHERE psstatus A;
create role planadmin;
create role planmanager;
Set up permissions for roles as written in the table below:
User role planadmin planmanager
DB object
all tables S S
plandata SUID SUID
planstatus SUID SU
countrymanagers SUID S
vplanedit S SU
vplan S S
Legend:
S select
U update
I insert
D delete
Create three users:
Administrator:
o ivan
Managers:
o sophie
o kirill
Sophie has access to data related to US and CA countries. Kirill works with sales data in FR GB DE
AU countries. Put this information in the countrymanagers table, which will associate managers with
certain countries they are responsible for.
Populate the countrymanagers table with the records on two managers sophie and kirill
having sophie responsible for US and CA and kirill for FR GB DE AU
Add two materialized views productcountry Product should combine data of product and
its category. Country view should be filled with unique codes of the countries where the shops are
located country codes can be taken from addresses of Main Office type
Allow managers and administrators to read from these views.
Fields of product are shown in the table. Each field to be included in the view is described below:
Field Description Rule
pcid Product category key Load from Productcategory.productcategoryid
productid Product key Load from Product.productid
pcname Category name Load
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
