DB exam2

-- PART1 2‘
-- 1 
psql -U postgres -d postgres
-- 2
CREATE DATABASE testdb;
-- 3
CREATE USER Bob WITH PASSWORD '20171203';
ALTER USER Bob superuser;

CREATE USER Alice WITH PASSWORD '20171203' superuser;
-- 4
ALTER DATABASE testdb OWNER TO Bob;
-- 5 windows下bob要小写
psql -U bob -d postgres
-- 6
ALTER DATABASE testdb RENAME TO examdb;
-- 7
psql -U bob -d examdb
-- 8
CREATE TABLE family (id integer, member VARCHAR(20), name VARCHAR(20), age integer);
INSERT INTO family VALUES (1, 'dad', 'Chen', 44);
INSERT INTO family VALUES (2, 'mom', 'Alice', 40);
INSERT INTO family VALUES (3, 'grandpa', 'Tom', 73);
INSERT INTO family VALUES (4, 'grandma', 'Amy', 69);
INSERT INTO family VALUES (5, 'son', 'Bob', 12);
-- 9 
\d family

-- 10
ALTER TABLE family ADD PRIMARY KEY(id);
-- 11
ALTER TABLE family ALTER COLUMN member SET NOT NULL;
-- 12
ALTER TABLE family ADD COLUMN birthday DATE;
UPDATE family SET birthday = '1973-10-10' WHERE id = 1;
UPDATE family SET birthday = '1977-02-03' WHERE id = 2;
UPDATE family SET birthday = '1944-07-29' WHERE id = 3;
UPDATE family SET birthday = '1948-01-07' WHERE id = 4;
UPDATE family SET birthday = '2005-12-17' WHERE id = 5;
-- 13
UPDATE family SET age = age + 1;
-- 14
UPDATE family SET birthday = '1977-02-19' WHERE id = 2;
-- 15
select * from pg_indexes where tablename = 'family';
-- 16
ALTER TABLE family DROP CONSTRAINT family_pkey;
-- 17
CREATE INDEX idx_hash_tmp ON family USING hash(id);
select * from pg_indexes where tablename = 'family';
-- 18
DROP INDEX IF EXISTS idx_hash_tmp;
-- 19
ALTER TABLE family DROP COLUMN age;
select * from family;
-- 20 --P671 be care of your logged user is not bob to drop bob
-- ALTER DATABASE examdb OWNER TO postgres;
REASSIGN OWNED BY bob TO postgres;
DROP OWNED BY bob;
-- repeat the above commands in each database of the cluster
DROP ROLE bob;
\du
-- 43min -- 56min
-- 1-5 4‘ 6-9 5’
-- 1 号码有小坑
select c_name, c_address, c_phone from customer where c_phone SIMILAR TO '%((288)|(28-8)|(2-88))%';
-- 2 小坑DISTINCT
--select c_name,  count(*) as num
--from customer, orders where o_custkey = c_custkey and o_orderdate between '1997-01-01' and '1997-12-31'
--group by c_name;
select DISTINCT c_name from customer, orders where o_custkey = c_custkey and o_orderdate between '1997-01-01' and '1997-12-31';
--3
select p_name, num1, num2
from 
(select l_partkey, 
	count(*) filter (where l_shipdate between '1997-01-01' and '1997-12-31') as num1,
	count(*) filter (where l_shipdate between '1998-01-01' and '1998-12-31') as num2
	from lineitem
	group by l_partkey)as temp, part
where p_partkey = l_partkey and num1 < num2;
-- 4
select p_partkey, p_name, p_size
from part
where p_type != 'PROMO PLATED COPPER'
order by p_size asc
limit 3;
-- 5
select n_name, r_name, num
from
(select r_regionkey, count(*) as num
from nation, region, customer
where n_nationkey = c_nationkey and n_regionkey = r_regionkey
group by r_regionkey
having count(*) > 30000) as temp, nation, region
where n_regionkey = temp.r_regionkey and region.r_regionkey = temp.r_regionkey;

-- use DISTINCT or not is ok
select n_name, r_name, num
from
(select r_regionkey, count(DISTINCT(c_custkey)) as num
from nation, region, customer
where n_nationkey = c_nationkey and n_regionkey = r_regionkey
group by r_regionkey
having count(DISTINCT(c_custkey)) > 30000) as temp, nation, region
where n_regionkey = temp.r_regionkey and region.r_regionkey = temp.r_regionkey;

-- error
select n_name, r_name, count(c_custkey)
from region, nation, customer
where r_regionkey = n_regionkey and n_nationkey = c_nationkey
group by n_name, r_name
having count(c_custkey) > 30000;

-- 6
select DISTINCT(c_name), c_phone, n_name
from customer, nation
where not exists
(select o_custkey
from lineitem, orders, part
where l_orderkey = o_orderkey and p_partkey = l_partkey and p_name = 'linen pink saddle puff powder' and c_custkey = o_custkey) and c_nationkey = n_nationkey;

-- test
select c_name, p_name
from part, lineitem, orders, customer
where l_orderkey = o_orderkey and p_partkey = l_partkey and c_custkey = o_custkey and c_name like 'Customer#00000%' and p_name = 'linen pink saddle puff powder';
-- error
select c_name, c_phone, n_name
from customer, orders, lineitem, part
where c_custkey = o_custkey and o_orderkey = l_orderkey and l_partkey = p_partkey and p_name not in ('linen pink saddle puff powder');

-- 7
select c_name, c_address, c_comment
from orders, customer,
(select o_orderkey
from orders, lineitem
where l_orderkey = o_orderkey
group by o_orderkey
having count(*) > 5) as temp
where orders.o_orderkey = temp.o_orderkey and orders.o_custkey = c_custkey;

--
select c_name, c_phone, c_address, c_comment
from customer, lineitem, orders 
where c_custkey = o_custkey and o_orderkey = l_orderkey
group by c_name, c_phone, c_address, c_comment 
having count(l_orderkey) > 5;

-- 8 小坑,必须把s_name放在having里面
select s_name, round(avg(p_retailprice),2)
from part, supplier, partsupp
where p_partkey = ps_partkey and ps_suppkey = s_suppkey
group by s_name
having s_name = 'Supplier#000000038';

-- 9
select s_suppkey, s_name
select min(ps_supplycost)
from part, partsupp
where p_partkey = ps_partkey
group by p_type, p_size;

-- PART3 10’
-- 1 5min
select * from pg_indexes where tablename = 'orders';
-- DROP INDEX IF EXISTS orders_pkey;
ALTER TABLE orders DROP CONSTRAINT orders_pkey CASCADE;
CREATE INDEX idx_hash_ordkey ON orders USING hash(o_orderkey);
explain(analyze) select c_name from customer, orders where o_custkey = c_custkey and o_orderkey = 1200;
-- 18 3min
select * from pg_indexes where tablename = 'customer';
CREATE INDEX re_index_cphone ON customer USING btree (reverse((c_phone)::text) varchar_pattern_ops);
explain(analyze) select * from customer where reverse(c_phone) LIKE '2988%';




猜你喜欢

转载自blog.csdn.net/nemoyy/article/details/79044435
db2