0% found this document useful (0 votes)
8 views

EX NO 4

The document outlines the creation and management of a rental database named 'rentaldb' using PostgreSQL. It includes the creation of various tables such as 'branch', 'staff', 'client', 'private_owner', 'viewing', and 'property_for_rent', along with examples of inserting, updating, and querying data. Additionally, it highlights the enforcement of constraints like primary keys, unique keys, and check constraints to maintain data integrity.

Uploaded by

takash2412
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

EX NO 4

The document outlines the creation and management of a rental database named 'rentaldb' using PostgreSQL. It includes the creation of various tables such as 'branch', 'staff', 'client', 'private_owner', 'viewing', and 'property_for_rent', along with examples of inserting, updating, and querying data. Additionally, it highlights the enforcement of constraints like primary keys, unique keys, and check constraints to maintain data integrity.

Uploaded by

takash2412
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

EX.

NO:4
postgres=# create database rentaldb;
CREATE DATABASE
postgres=# \connect rentaldb;
You are now connected to database "rentaldb" as user "postgres".
rentaldb=# create table branch(branch_no varchar(18) primary key,street
varchar(18) not null,city varchar(18) not null,postcode varchar(18));
CREATE TABLE
rentaldb=# insert into branch(branch_no,street,city,postcode) values('b005',
'23,deer st','london','sw14eh');
INSERT 0 1
rentaldb=# insert into branch(branch_no,street,city,postcode) values('b005',
'23,deer st','london','sw14eh');
ERROR: duplicate key value violates unique constraint "branch_pkey"
DETAIL: Key (branch_no)=(b005) already exists.
rentaldb=# \d+ branch
Table "public.branch"
Column | Type | Collation | Nullable | Default | Storage |
Compression | Stats target | Description
-----------+-----------------------+-----------+----------+---------+----------+
-------------+--------------+-------------
branch_no | character varying(18) | | not null | | extended |
| |
street | character varying(18) | | not null | | extended |
| |
city | character varying(18) | | not null | | extended |
| |
postcode | character varying(18) | | | | extended |
| |
Indexes:
"branch_pkey" PRIMARY KEY, btree (branch_no)
Access method: heap

rentaldb=# insert into branch(branch_no,street,city,postcode) values('b007',


'ab2350');
ERROR: INSERT has more target columns than expressions
LINE 1: insert into branch(branch_no,street,city,postcode) values('b...
^
rentaldb=# insert into branch(branch_no,street,city,postcode) values('b007',
'14 arguil st','aberdeen','ab2350');
INSERT 0 1
rentaldb=# insert into branch(branch_no,street,city,postcode) values('b003',
'163 main st','glasgow','g119qx');
INSERT 0 1
rentaldb=# insert into branch(branch_no,street,city,postcode) values('b004',
'163 main st','bristol','bs99in7');
INSERT 0 1
rentaldb=# insert into branch(branch_no,street,city,postcode) values('b002',
'56 clover road','london','nw106ev');
INSERT 0 1
rentaldb=# update branch set branch_no=upper(branch_no);
UPDATE 5
rentaldb=# select * from branch;
branch_no | street | city | postcode
-----------+----------------+----------+----------
B005 | 23,deer st | london | sw14eh
B007 | 14 arguil st | aberdeen | ab2350
B003 | 163 main st | glasgow | g119qx
B004 | 163 main st | bristol | bs99in7
B002 | 56 clover road | london | nw106ev
(5 rows)

rentaldb=# create table staff(staff_no varchar(18) primary key,fname varchar(18)


not null,lname varchar(18) not null,position varchar(18) not null,sex varchar(18)
not null,dob date not null,salary numeric(10,2) not null,branch_no varchar(18)
not null);
CREATE TABLE
rentaldb=# insert into
staff(staff_no,fname,lname,position,sex,dob,salary,branch_no)
values('SL21','john','white','manager','M','1945-10-01',30000,'B005');
INSERT 0 1
rentaldb=# insert into
staff(staff_no,fname,lname,position,sex,dob,salary,branch_no)
values('SL21','john','white','manager','M','1945-10-01',30000,'B005');
ERROR: duplicate key value violates unique constraint "staff_pkey"
DETAIL: Key (staff_no)=(SL21) already exists.
rentaldb=# insert into
staff(staff_no,fname,lname,position,sex,dob,salary,branch_no)
values('SG37','ann','beech','assistant','F','1960-11-10',12000,'B003');
INSERT 0 1
rentaldb=# alter table staff add constraint salary_check check(salary>=8000);
ALTER TABLE
rentaldb=# insert into staff(staff_no) values('B001');
ERROR: null value in column "fname" of relation "staff" violates not-null
constraint
DETAIL: Failing row contains (B001, null, null, null, null, null, null, null).
rentaldb=# insert into
staff(staff_no,fname,lname,position,sex,dob,salary,branch_no)
values('sl21','john','white','manager','M','1945-07-07',7000,'B005');
ERROR: new row for relation "staff" violates check constraint "salary_check"
DETAIL: Failing row contains (sl21, john, white, manager, M, 1945-07-07, 7000.00,
B005).
rentaldb=# alter table staff add constraint dob_constraint check(dob<'2000-01-01');
ALTER TABLE
rentaldb=# update staff set dob='2022-03-09';
ERROR: new row for relation "staff" violates check constraint "dob_constraint"
DETAIL: Failing row contains (SL21, john, white, manager, M, 2022-03-09, 30000.00,
B005).
rentaldb=# alter table staff add constraint staff_fk foreign key(branch_no)
references branch(branch_no);
ALTER TABLE
rentaldb=# \d+ staff;
Table "public.staff"
Column | Type | Collation | Nullable | Default | Storage |
Compression | Stats target | Description
-----------+-----------------------+-----------+----------+---------+----------+
-------------+--------------+-------------
staff_no | character varying(18) | | not null | | extended |
| |
fname | character varying(18) | | not null | | extended |
| |
lname | character varying(18) | | not null | | extended |
| |
position | character varying(18) | | not null | | extended |
| |
sex | character varying(18) | | not null | | extended |
| |
dob | date | | not null | | plain |
| |
salary | numeric(10,2) | | not null | | main |
| |
branch_no | character varying(18) | | not null | | extended |
| |
Indexes:
"staff_pkey" PRIMARY KEY, btree (staff_no)
Check constraints:
"dob_constraint" CHECK (dob < '2000-01-01'::date)
"salary_check" CHECK (salary >= 8000::numeric)
Foreign-key constraints:
"staff_fk" FOREIGN KEY (branch_no) REFERENCES branch(branch_no)
Access method: heap
rentaldb=# \d+ staff;
Table "public.staff"
Column | Type | Collation | Nullable | Default | Storage |
Compression | Stats target | Description
-----------+-----------------------+-----------+----------+---------+----------+
-------------+--------------+-------------
staff_no | character varying(18) | | not null | | extended |
| |
fname | character varying(18) | | not null | | extended |
| |
lname | character varying(18) | | not null | | extended |
| |
position | character varying(18) | | not null | | extended |
| |
sex | character varying(18) | | not null | | extended |
| |
dob | date | | not null | | plain |
| |
salary | numeric(10,2) | | not null | | main |
| |
branch_no | character varying(18) | | not null | | extended |
| |
Indexes:
"staff_pkey" PRIMARY KEY, btree (staff_no)
Check constraints:
"dob_constraint" CHECK (dob < '2000-01-01'::date)
"salary_check" CHECK (salary >= 8000::numeric)
Foreign-key constraints:
"staff_fk" FOREIGN KEY (branch_no) REFERENCES branch(branch_no)
Access method: heap

rentaldb=# create table client(client_no varchar(4) primary key,fname varchar(18),


lname varchar(18),tel_no varchar(18),preftype varchar(18),max_rent numeric(5,2),
email varchar(20));
CREATE TABLE
rentaldb=# alter table client alter column email type varchar(30);
ALTER TABLE
rentaldb=# \d+ client;
Table "public.client"
Column | Type | Collation | Nullable | Default | Storage |
Compression | Stats target | Description
-----------+-----------------------+-----------+----------+---------+----------+
-------------+--------------+-------------
client_no | character varying(4) | | not null | | extended |
| |
fname | character varying(18) | | | | extended |
| |
lname | character varying(18) | | | | extended |
| |
tel_no | character varying(18) | | | | extended |
| |
preftype | character varying(18) | | | | extended |
| |
max_rent | numeric(5,2) | | | | main |
| |
email | character varying(30) | | | | extended |
| |
Indexes:
"client_pkey" PRIMARY KEY, btree (client_no)
Access method: heap

rentaldb=# insert into client(client_no,fname,lname,tel_no,preftype,max_rent,email)


values('CR56','aline','stewart','0141-848-1845','flat',350,'[email protected]');
INSERT 0 1
rentaldb=# insert into client(client_no,fname,lname,tel_no,preftype,max_rent,email)
values('CR74','mike','stewart','01475-392178','house',750,'[email protected]');
INSERT 0 1
rentaldb=# insert into client(client_no,fname,lname,tel_no,preftype,max_rent,email)
values('CR62','mary','treager','01224-196720','flat',600,'[email protected]');
INSERT 0 1
rentaldb=# select * from client;
client_no| fname| lname | tel_no | preftype | max_rent | email
--------+-------+---------+---------------+----------+----------
+-------------------
CR56 | aline | stewart | 0141-848-1845 | flat | 350.00 |
[email protected]
CR74 | mike | stewart | 01475-392178 | house | 750.00 |
[email protected]
CR62 | mary | treager | 01224-196720 | flat | 600.00 |
[email protected]
(3 rows)

rentaldb=# create table private_owner(owner_no varchar(20) primary key,fname


varchar(20),lname varchar(20),address varchar(30),tell_no varchar(20),email
varchar(30),password varchar(10));
CREATE TABLE
rentaldb=# insert into private_owner(owner_no,fname,lname,address,tell_no,email,
password) values('CO87','carol','farrel','6 achray st.glasgow G32 9DX',
'0141-357-7419','[email protected]','*****');
INSERT 0 1
rentaldb=# insert into private_owner(owner_no,fname,lname,address,tell_no,email,
password) values('CO40','tina','murphy','6 63 well st glasgow G42','0141-943-1728',
'[email protected]','*****');
INSERT 0 1
rentaldb=# insert into private_owner(owner_no,fname,lname,address,tell_no,email,
password) values('CO93','tony','shaw','12 park pl glasgow G4 0QR','0141-225-7025',
'[email protected]','*****');
INSERT 0 1
rentaldb=# select * from private_owner;
owner_no | fname | lname | address | tell_no |
email | password
----------+-------+--------+-----------------------------+---------------+
-------------------+----------
CO87 | carol | farrel | 6 achray st.glasgow G32 9DX | 0141-357-7419 |
[email protected] | *****
CO40 | tina | murphy | 6 63 well st glasgow G42 | 0141-943-1728 |
[email protected] | *****
CO93 | tony | shaw | 12 park pl glasgow G4 0QR | 0141-225-7025 |
[email protected] | *****
(3 rows)
rentaldb=# update private_owner set address='63 well st glasgow G32 9DX' where
owner_no='CO87';
UPDATE 1
rentaldb=# select * from private_owner;
owner_no | fname | lname | address | tell_no |
email | password
----------+-------+--------+----------------------------+---------------+-
------------------+----------
CO40 | tina | murphy | 6 63 well st glasgow G42 | 0141-943-1728 |
[email protected] | *****
CO93 | tony | shaw | 12 park pl glasgow G4 0QR | 0141-225-7025 |
[email protected] | *****
CO87 | carol | farrel | 63 well st glasgow G32 9DX | 0141-357-7419 |
[email protected] | *****
(3 rows)

rentaldb=# select * from client;


client_no | fname | lname | tel_no | preftype | max_rent | email
-----------+-------+---------+---------------+----------+----------
+----------------
CR56 | aline | stewart | 0141-848-1845 | flat | 350.00 |
[email protected]
CR74 | mike | stewart | 01475-392178 | house | 750.00 |
[email protected]
CR62 | mary | treager | 01224-196720 | flat | 600.00 |
[email protected]
(3 rows)

rentaldb=# alter table client add constraint uniquemail unique(email);


ALTER TABLE
rentaldb=# create table viewing(client_no varchar(4),property_no varchar(4),
viewdate date,comment text);
CREATE TABLE
rentaldb=# alter table viewing add constraint pkc primary key(client_no,
property_no);
ALTER TABLE
rentaldb=# insert into viewing(client_no,property_no,viewdate,comment)
values('CR56'
,'PA14','2013-05-24','too small');
INSERT 0 1
rentaldb=# insert into viewing(client_no,property_no,viewdate,comment)
values('CR56'
,'PA14','2013-05-24','too small');
ERROR: duplicate key value violates unique constraint "pkc"
DETAIL: Key (client_no, property_no)=(CR56, PA14) already exists.
rentaldb=# insert into viewing(client_no,property_no,viewdate,comment)
values('CR76'
,'PG4','2013-04-20','too remote');
INSERT 0 1
rentaldb=# insert into viewing(client_no,property_no,viewdate) values('CR76','PG4',
'2013-05-26');
ERROR: duplicate key value violates unique constraint "pkc"
DETAIL: Key (client_no, property_no)=(CR76, PG4) already exists.
rentaldb=# select * from viewing;
client_no | property_no | viewdate | comment
-----------+-------------+------------+------------
CR56 | PA14 | 2013-05-24 | too small
CR76 | PG4 | 2013-04-20 | too remote
(2 rows)
rentaldb=# create table property_for_rent(property_no varchar,street varchar,city
varchar,postcode varchar,type varchar,rooms varchar,rent numeric,owner_no varchar,
staff_no varchar,branch_no varchar);
CREATE TABLE
rentaldb=# select * from property_for_rent;
property_no | street | city | postcode | type | rooms | rent | owner_no | staff_no

| branch_no
-------------+--------+------+----------+------+-------+------+----------
+----------
+-----------
(0 rows)

rentaldb=# \d+ property_for_rent;


Table "public.property_for_rent"
Column | Type | Collation | Nullable | Default | Storage |
Compression | Stats target | Description
-------------+-------------------+-----------+----------+---------+----------+-
------------+--------------+-------------
property_no | character varying | | | | extended |
| |
street | character varying | | | | extended |
| |
city | character varying | | | | extended |
| |
postcode | character varying | | | | extended |
| |
type | character varying | | | | extended |
| |
rooms | character varying | | | | extended |
| |
rent | numeric | | | | main |
| |
owner_no | character varying | | | | extended |
| |
staff_no | character varying | | | | extended |
| |
branch_no | character varying | | | | extended |
| |
Access method: heap

rentaldb=# \d+;
List of relations
Schema | Name | Type | Owner | Persistence | Access method |
Size | Description
--------+-------------------+-------+----------+-------------+---------------+-
-----------+-------------
public | branch | table | postgres | permanent | heap |
8192 bytes |
public | client | table | postgres | permanent | heap |
8192 bytes |
public | private_owner | table | postgres | permanent | heap |
8192 bytes |
public | property_for_rent | table | postgres | permanent | heap |
8192 bytes |
public | staff | table | postgres | permanent | heap |
8192 bytes |
public | viewing | table | postgres | permanent | heap |
16 kB |
(6 rows)

rentaldb=# \d+ viewing;


Table "public.viewing"
Column | Type | Collation | Nullable | Default | Storage |
Compression | Stats target | Description
-------------+----------------------+-----------+----------+---------+----------+-
------------+--------------+-------------
client_no | character varying(4) | | not null | | extended |
| |
property_no | character varying(4) | | not null | | extended |
| |
viewdate | date | | | | plain |
| |
comment | text | | | | extended |
| |
Indexes:
"pkc" PRIMARY KEY, btree (client_no, property_no)
Access method: heap

rentaldb=# alter table property_for_rent add constraint foreign_staff foreign key


(staff_no) references staff(staff_no);
ALTER TABLE
rentaldb=# alter table property_for_rent add constraint foreign_branch foreign key
(branch_no) references branch(branch_no);
ALTER TABLE
rentaldb=# insert into property_for_rent(property_no,street,city,postcode,type,
rooms,rent,owner_no,staff_no,branch_no) values('PL94','6 argyll st','london','NW2',
'flat',4,400,'C0B7','SL21','B005');
INSERT 0 1
^
rentaldb=# insert into property_for_rent(property_no,street,city,postcode,type,
rooms,rent,owner_no,staff_no,branch_no) values('PG4','6 lawrence st','glasgow',
'g119qx','flat',3,350,'C040','SG37','B003');
INSERT 0 1
rentaldb=# select * from property_for_rent;
property_no | street | city | postcode | type | rooms | rent | owner_no
|
staff_no | branch_no
-------------+---------------+---------+----------+------+-------+------+----------
+
----------+-----------
PL94 | 6 argyll st | london | NW2 | flat | 4 | 400 | C0B7
|
SL21 | B005
PG4 | 6 lawrence st | glasgow | g119qx | flat | 3 | 350 | C040
|
SG37 | B003
(2 rows)

You might also like