COMP9311 Database Systems Lab4

1. Logging in

ssh [email protected]
输入password进入weill
ssh grieg
source /srvr/z1234567/env
cd /srvr/z1234567
mkdir lab04
cd lab04

2.Copy documents

grieg % cp /home/cs9311/web/17s2/labs/04/Accesses.sql /srvr/z5140515/lab04
grieg % cp /home/cs9311/web/17s2/labs/04/Hosts.sql /srvr/z5140515/lab04
grieg % cp /home/cs9311/web/17s2/labs/04/Sessions.sql /srvr/z5140515/lab04
grieg % cp /home/cs9311/web/17s2/labs/04/schema.sql /srvr/z5140515/lab04
grieg % cp /home/cs9311/web/17s2/labs/04/weblog.sql /srvr/z5140515/lab04

3. Setting up the PostgreSQL database

createdb weblog
psql weblog
\i schema.sql
\i Hosts.sql
\i Sessions.sql
\i Accesses.sql

If you don't want to look at all of the INSERT messages, and you're using Linux or Mac OSX, then you can do the following:

createdb weblog
% psql weblog -f schema.sql
  ... should produce CREATE TABLE messages ...
% (psql weblog -f Hosts.sql 2>&1) > .errs
  ... INSERT messages are added to file .errs ...
% (psql weblog -f Sessions.sql 2>&1) >> .errs
  ... INSERT messages are added to file .errs ...
% (psql weblog -f Accesses.sql 2>&1) >> .errs

4. Check database

psql weblog
--如果已经在weblog数据库中,则不用操作
\d
--查看所有建好的table
select count(*) from hosts;
--查询hosts中有多少项instances, count(*)是计数所有项
select count(*) from sessions;
select count(*) from accesses;

5. Exercieses

Q1:how many page accesses on March 2

create or replace view Q1(nacc) as
select count(page) as nacc
from accesses 
where accTime > '2005-03-02' and accTime < '2005-03-03'
;
--or
create or replace view Q1(nacc) as
     select count(acctime)
        AS nacc from accesses 
     where extract(month from acctime) = 3 
        and extract(day from acctime) = 2;
;

Q2: how many times was the MessageBoard search facility used?

create or replace view Q2(nsearches) as
select count(page) as nsearches
from accesses 
where page like '%messageboard%' and page like '%webcms%' and params like '%state=search%'
;

Q3: on which Tuba lab machines were there incomplete sessions?

create or replace view Q3(hostname) as
select distinct Hosts.hostname as hostname
from Hosts join Sessions on (Hosts.id = Sessions.host) 
where Sessions.complete = 'f' and Hosts.hostname like '%tuba%'
;
--postgre sql规定更改view的查询不能改变字段类型,会出现需要drop view view_name;重新创建才能运行的情况

Q4: min,avg,max bytes transferred in page accesses

create or replace view Q4(min,avg,max) as
select min(nbytes)::int as min, avg(nbytes)::int as avg, max(nbytes)::int as max
from accesses 
;

Q5: number of sessions from CSE hosts

create or replace view Q5(nhosts) as
select count(*) as nhosts
from Hosts h join Sessions s on (h.id = s.host)
where h.hostname like '%cse.unsw.edu.au'
;

Q6: number of sessions from non-CSE hosts

create or replace view Q6(nhosts) as
select count(*) as nhosts
from Hosts h join Sessions s on (h.id = s.host)
where h.hostname not like '%cse.unsw.edu.au'
;

Q7: session id and number of accesses for the longest session?

create or replace view Q7(session,length) as 
select session as session, seq as length
from accesses 
where seq = (select max(seq) from accesses)
;

Q8: frequency of page accesses

create or replace view Q8(page,freq) as
select page as page, count(page) as freq
from accesses
group by page
order by count(page) desc
;

Q9: frequency of module accesses

create or replace view Q9(module,freq) as
select substring(page from '^([a-z]+)(/|$)') as module, count(page) as freq
from accesses
group by substring(page from '^([a-z]+)(/|$)')
order by count(substring(page from '^([a-z]+)(/|$)')) desc
;
--substring(string from pattern), Extract substring matching POSIX regular expression. E.G. substring('Thomas' from '...$')

Q10: "sessions" which have no page accesses

create or replace view Q10(session) as
select id as session
from sessions s
    where not exists (select session from accesses where session = s.id)
;

6. Exit

\q
--退出psql
exit
--退出cse server

你可能感兴趣的:(COMP9311 Database Systems Lab4)