DB exam1

-- 9:50 zhengli daan
-- PART1
-- 8:10
psql -U postgres -d postgres
CREATE DATABASE bookdb WITH OWNER postgres;
CREATE USER xiaoyubei PASSWORD '15331324';
alter user xiaoyubei createdb;
psql -U xiaoyubei -d postgres
CREATE DATABASE examdb WITH OWNER xiaoyubei;
\du
\l

alter role xiaoyubei nocreatedb;

ALTER USER xiaoyubei superuser;
psql -U xiaoyubei -d examdb;
-- and insert data?
CREATE TABLE applestore(id integer NOT NULL, category VARCHAR(20), color VARCHAR(20), number integer NOT NULL, PRIMARY KEY(id));
CREATE TABLE pricetable(category VARCHAR(20), price integer, PRIMARY KEY(category));
ALTER TABLE applestore ADD CONSTRAINT fk_1 FOREIGN KEY(category) REFERENCES pricetable(category);

INSERT INTO applestore(id, category, color, number) VALUES (1, 'iphone', 'golden', 33);
INSERT INTO applestore(id, category, color, number) VALUES (2, 'iphone', 'silver', 10);
INSERT INTO applestore(id, category, color, number) VALUES (3, 'iphone', 'black', 2);
INSERT INTO applestore(id, category, color, number) VALUES (4, 'macbook', 'silver', 3);
INSERT INTO applestore(id, category, color, number) VALUES (5, 'macbook', 'gray', 6);
INSERT INTO pricetable(category, price) VALUES ('iphone', 4888);
INSERT INTO pricetable(category, price) VALUES ('macbook', 9888);

UPDATE applestore SET number = number -2 WHERE category = 'iphone';
DELETE FROM applestore WHERE number = 0;
-- be care of seq
DROP TABLE applestore;
DROP TABLE pricetable;
-- to another databases and user
\c postgres postgres
DROP DATABASE examdb;
drop user xiaoyubei;
-- 9:10

-- PART2
-- 9:20
-- 1. need tpch.pdf
-- Q1
select p_partkey, p_name from part order by p_retailprice desc limit 5;
-- Q2
select count(*) from part where p_partkey % 5 = 0 and p_partkey % 4 = 1;
-- Q3
select s_name, sum(ps_supplycost) from partsupp, supplier, region, nation where ps_suppkey = s_suppkey and s_nationkey = n_nationkey and n_regionkey = r_regionkey and n_name = 'CHINA' and r_name = 'ASIA' group by s_suppkey;
-- Q4 points out orderkey
select o_orderkey, c_name from orders, customer where o_custkey = c_custkey and o_orderpriority = '5-LOW' and o_orderdate between '1994-10-10' and '1995-10-10';
-- Q5
select s_name, p_name 
from part, partsupp, supplier, nation
where p_partkey = ps_partkey and ps_suppkey = s_suppkey and s_nationkey = n_nationkey and n_name = 'INDIA';

-- Q7
select c_name, max(l_extendedprice)
from customer, lineitem, orders
where c_custkey = o_custkey and l_orderkey = o_orderkey and o_orderdate > '1997-01-01'
group by c_name
having c_name = 'Customer#000000167';
-- Q8 
select l_shipmode, avg(l_tax)
from lineitem, part
where l_partkey = p_partkey and p_brand = 'Brand#42' 
group by l_shipmode
having l_shipmode != 'REG AIR';
-- Q9
select s_suppkey, s_name, n_name, num
from
(select s_suppkey, s_name, s_nationkey, count(p_partkey) as num
from supplier, partsupp, part
where p_partkey = ps_partkey and ps_suppkey = s_suppkey and p_type = 'SMALL PLATED BRASS'
group by s_suppkey
having count(p_partkey) > 3) as temp, nation
where n_nationkey = s_nationkey;
-- Q10
select c_custkey, c_name, num1, num2
from 
(select o_custkey, 
	count(*) filter (where o_orderdate between '1993-01-01' and '1993-12-31') as num1,
	count(*) filter (where o_orderdate between '1994-01-01' and '1994-12-31') as num2
	from orders
	group by o_custkey)as temp, customer
where c_custkey = o_custkey and num1 < num2;
-- 9:48


-- PART3
-- Q1
select * from pg_indexes where tablename = 'part';
ALTER TABLE part DROP CONSTRAINT part_Pkey CASCADE; 
-- DROP INDEX index_name;
CREATE INDEX hash_id1 ON part using hash(p_partkey);
explain(analyze) select p_name, p_type, p_size from part where p_partkey = 15999;

猜你喜欢

转载自blog.csdn.net/nemoyy/article/details/79044406
DB