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 serial4 NOT NULL,
"name" varchar(50) NOT NULL,
productnumber varchar(25) NOT NULL,
color varchar(15) NULL,
listprice numeric(29,3) NOT NULL,
"size" varchar(5) NULL,
sizeunitmeasurecode bpchar NULL,
weightunitmeasurecode bpchar NULL,
weight numeric(8,2) NULL,
productline bpchar NULL,
"class" bpchar NULL,
"style" bpchar NULL,
productsubcategoryid int4 NULL,
productmodelid int4 NULL,
sellstartdate timestamp(6) NOT NULL,
sellenddate timestamp(6) NULL,
discontinueddate timestamp(6) NULL,
CONSTRAINT product_pkey PRIMARY KEY (productid)
);
CREATE INDEX idx_product_color ON public.product USING btree (color);
CREATE TABLE public.productsubcategory (
productsubcategoryid serial4 NOT NULL,
productcategoryid serial4 NOT NULL,
"name" varchar(54) NOT NULL,
CONSTRAINT productsubcategory_pkey PRIMARY KEY (productsubcategoryid)
);
CREATE TABLE public.productcategory (
productcategoryid serial4 NOT NULL,
"name" varchar(54) NOT NULL,
CONSTRAINT productcategory_pkey PRIMARY KEY (productcategoryid)
);
CREATE TABLE plan_status (
quarterid varchar(6) NOT NULL,
status varchar(10) NOT NULL,
modifieddatetime timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
author varchar(20) NOT NULL DEFAULT CURRENT_USER,
country varchar(5) NOT NULL,
CONSTRAINT plan_status_pk PRIMARY KEY (quarterid, country)
);
CREATE TABLE plan_data (
versionid varchar(1) NOT NULL,
country varchar(5) NOT NULL,
quarterid varchar(6) NOT NULL,
pcid int4 NOT NULL,
salesamt numeric(18,2) NULL,
CONSTRAINT planapp_data_pkey PRIMARY KEY (quarterid, country, pcid, versionid)
);
CREATE TABLE country_managers (
username varchar(30) NOT NULL,
country varchar(5) NOT NULL,
CONSTRAINT country_managers_pk PRIMARY KEY (username, country)
);
CREATE TABLE company (
id int4 NOT NULL GENERATED ALWAYS AS IDENTITY,
cname varchar(200) NOT NULL,
countrycode varchar(10) NULL,
city varchar(30) NULL,
CONSTRAINT d_company_pk PRIMARY KEY (id)
);
CREATE TABLE company_sales (
cid int4 NOT NULL,
salesamt numeric(18,2) NULL,
year int4 NULL,
quarter_yr int4 NULL,
qr varchar(6) NOT NULL,
categoryid int4 NOT NULL,
ccls varchar(1) NULL,
CONSTRAINT company_sales_pk PRIMARY KEY (qr, cid, categoryid)
);
CREATE TABLE company_abc (
cid int4 NOT NULL,
salestotal numeric NULL,
cls varchar(1) NULL,
year int4 NOT NULL,
CONSTRAINT company_abc_pk PRIMARY KEY (cid, year)
);
create view v_plan_edit as
select pd.country, pd.quarterid, pd.pcid, pd.salesamt, pd.versionid
from plan_data pd
where
pd.versionid ='P'
and
pd.country in (select country
from country_managers cm
where cm.username = current_user)
and
pd.quarterid in (select ps.quarterid
from plan_status ps
where ps.author = current_user and ps.status ='L');
create view v_plan as
select pd.country,
pd.pcid,
pd.quarterid,
pd.salesamt
FROM plan_data pd
WHERE pd.versionid ='A'
AND (pd.country IN (SELECT cm.country FROM country_managers cm
WHERE cm.username = CURRENT_USER)
or
pg_has_role(current_user, 'planadmin', 'member'))
AND (pd.quarterid IN ( SELECT ps.quarterid
FROM plan_status ps
WHERE ps.status ='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
plan_data SUID SUID
plan_status SUID SU
country_managers SUID S
v_plan_edit S SU
v_plan 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 country_managers table, which will associate managers with
certain countries they are responsible for.
Populate the country_managers 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 product2,country2. Product2 should combine data of product and
its category. Country2 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 product2 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!