# Lecture Tutorial URL
https://www.javatpoint.com/dbms-tutorial
https://www.javatpoint.com/dbms-three-schema-architecture
##PostgreSQL use SQL main Language
## what is Database ? is a place where we can
* store
*Manipulate
*Retrieve a data in a computer
## what is PostgreSQL and Relational Database
*PostgreSQL is actual database engine
*SQL is Structured Query Language
* is Programming Language
*Manage data held in a relational database
*Easy to learn
*Very powerful
* started in 1974
* used all over the internet
* Very essential programing Language
## How data is stored
* Store data in a table in the form of row and columns
## Relational database is a relation between one or more table
## What is PostgreSQL
* Advanced Open Source
*Object-Relational database management system
*Modern
*Open Source
## same other Database moment system
*Oracle
*MySQL and SQL Server which need license from the Microsoft
# Download the Postgres for window
*Open the Google search engine
* Postages download
** URL https://www.postgresql.org/download/
* Select for windows
*Download
* select the latest version
** URL https://www.enterprisedb.com/downloads/postgres-postgresql-downloads
* click the download button
sql shell
server[localhost]:
Database[postgres]: "you can type your own database name and prese enter"
Port{5432]:
Username[postgres]:
Password for user
"Use the password you inter during installation of postgreSQL"
\l
# To list the database which is perversely excite in the computer
\?
#help
\q
# quit the shell
#First command to create the database
CREATE DATABASE TEST1;
# To connect the new created database, you must use the following command
\c test1
#"Test1 is the new created database name"
drop database test1;
# A very dangerous command
# To delete a table
drop table student2;
# To create the table we can get the data type from this URL
https://www.postgresql.org/docs/12/datatype.html
# To create a table with out a constrent
test1=# create table student(
test1(# id int,
test1(# first_name varchar(50),
test1(# last_name varchar(50),
test1(# gender varchar(8),
test1(# date_of_birth date);
CREATE TABLE
# To list the created database
\d
# To discribe the created table and
check the column of the table
\d the name of the created table
i.e
\d student
#Creating a table with constraint
test1=# create table student1(
test1(# id bigserial not null primary key,
test1(# first_name varchar(50) not null,
test1(# last_name varchar(50) not null,
test1(# gender varchar(8) not null,
test1(# date_of_birth date not null,
test1(# email varchar(180),
test1(# country_of_birth varchar(60) not null);
CREATE TABLE
test1=# \l
# INSERT INTO A TABLE
#HOW TO INSERT RECORDE INTO TABLE
test1=# insert into student1(
test1(# first_name,
test1(# last_name,
test1(# gender,
test1(# date_of_birth,
test1(# email,
test1(# country_of_birth)
test1-# values('Zelalem', 'Bole', 'Male', date '1994-02-24', '[email protected]', 'Ethiopia');
INSERT 0 1
# To insert the value without email
test1=# insert into student1(
test1(# first_name,
test1(# last_name,
test1(# gender,
test1(# date_of_birth,
test1(# email,
test1(# country_of_birth)
test1-# values('Zeru', 'Bole', 'Male', date '1990-02-18', '', 'Ethiopia');
INSERT 0 1
# To insert addtional data
test1=# insert into student1(
test1(# first_name,
test1(# last_name,
test1(# gender,
test1(# date_of_birth,
test1(# email,
test1(# country_of_birth)
test1-# values('Ana', 'fre', 'Female', date '1988-01-28', '[email protected]', 'Canada');
INSERT 0 1
test1=# select * from student1;
test1=# insert into student1(
test1(# first_name,
test1(# last_name,
test1(# gender,
test1(# date_of_birth,
test1(# email,
test1(# country_of_birth)
test1-# values('Duye', 'Huawn', 'Male', date '1968-01-28', '', 'China');
INSERT 0 1
test1=# select * from student1;
# Generating 1000 rows with Mockaroo
URL https://www.mockaroo.com/
design a data, convert data format to SQL, give your own name, tick the include create table
check a preview
download at the end and open using visual studio software is open source you can download using the
following url
# download the visual studio from the following URL
https://code.visualstudio.com/docs/?dv=win
# After this install the visual studio
and open the downloaded data by using a visual studio and edit it based on the created table above and
make it's constraint the same.
after you finish this save a data and copy a source of data using the following command and change
backward slash to forward slash as follow
To restore the created database
\i C:/Users/Adisu/Downloads/person.sql
## Now are with SQL query commands
# Select From
#To grave the data from a table we use the following command
select * from person;
# ORDERD BY
select * from person order by first_name;
# assending or Disending
select * from person order by id asc;
select * from person order by id desc;
# Distinct
select country_of_birth from person;
select country_of_birth from person order by country_of_birth;
select distinct country_of_birth from person order by country_of_birth;
select first_name from person order by first_name;
select distinct first_name from person order by first_name;
select distinct country_of_birth from person order by country_of_birth;
select country_of_birth from person order by country_of_birth;
select distinct country_of_birth from person order by country_of_birth desc;
select distinct country_of_birth from person order by country_of_birth asc;
#Where Clause and AND
select * from person where gender = 'Male';
select * from person where gender = 'Female';
select * from person where gender = 'Male' and country_of_birth = 'China';
select * from person where gender = 'Female' and country_of_birth = 'Ethiopia';
select * from person where gender = 'Male' and country_of_birth = 'Ethiopia';
select * from person where gender = 'Male' and (country_of_birth = 'Ethiopia' or country_of_birth =
'China');
select * from person where gender = 'Female' and (country_of_birth = 'Ethiopia' or country_of_birth =
'China');
select * from person where gender = 'Male' and (country_of_birth = 'Japan' or country_of_birth
='China') and last_name = 'Gorbell';
# COMPARISON OPERATORS
it is work with arthimetic, logical and comparative opration
test1=# select 1=1;
?column?
----------
(1 row)
test1=# select 1=2;
?column?
----------
f
(1 row)
test1=# select 1>4;
?column?
----------
(1 row)
test1=# select 1>=4;
?column?
----------
(1 row)
test1=# select 1<=4;
?column?
----------
(1 row)
test1=# select 1<>4;
?column?
----------
t
(1 row)
test1=# select 'A'='A';
?column?
----------
(1 row)
test1=# select 'Addisu'='addisu';
?column?
----------
(1 row)
test1=# select 'Addisu'='Addisu';
?column?
----------
(1 row)
# Limit, Offset and Fetch
test1=# select * from person limit 10;
test1=# select * from person offset 2 limit 10;
test1=# select * from person offset 5 fetch first 10 row only;
IN
select * from person where country_of_birth = 'China';
test1=# select * from person where country_of_birth = 'China' or country_of_birth = 'France' or
country_of_birth = 'Ethiopia';
with same improvement
test1=# where country_of_birth in ('China', 'Brazil', 'France');
test1=# select * from person where country_of_birth in ('China', 'France', 'Brazil')
test1-# order by country_of_birth;
BETWEEN
test1=# select * from person where date_of_birth between Date '2019-01-01' and '2020-01-15';
LIKE and ILIKE
test1=# select * from person where email like '%.com';
test1=# select * from person where email like '%.org';
test1=# select * from person where email like '%google.%';
test1=# select * from person where email like '%.net';
test1=# select * from person where email like '________@%';
GROUP BY
test1=# select distinct country_of_birth from person;
test1=# select country_of_birth from person group by country_of_birth;
test1=# select country_of_birth, count(*) from person group by country_of_birth;
test1=# select country_of_birth, count(*) from person group by country_of_birth order by
country_of_birth;
GROUP BY HAVING
test1=# select country_of_birth, count(*) from person group by country_of_birth having count(*) > 10
order by country_of_birth;
# create the new database from mockaroo.com which is car data base
id row number bigserial not null primary key
make car make varchar(100) not null,
model car model varchar(100) not null,
price money numeric(20, 2) not null "Make the data type / carrency none"
);
test1=# select * from car;
id | make | model | price
------+-------------+---------------+--------
1 | Eb | Savill | $3.22
2 | Malachi | Quincey | $1.85
3 | Ricca | Yakobowitz | $9.55
4 | Llewellyn | Asker | $8.40
# Calculating Min, Max and Average round and Sum
let as identfy the most expencive car.
test1=# select max(price) from car;
test1=# select max(price) from car2;
test1=# select max(price) from car2;
max
-----------
$99990.62
(1 row)
test1=# select min(price) from car2;
min
------------
$100053.26
(1 row)
test1=# select make, model, min(price) from car2 group by make, model;
make | model | min
---------------+----------------------+------------
Pontiac | GTO | $329208.30
Chevrolet | Sonic | $164790.01
Toyota | Land Cruiser | $162809.84
Mercedes-Benz | S-Class | $228636.89
test1=# select make, model, max(price) from car2 group by make, model;
make | model | max
---------------+----------------------+------------
Pontiac | GTO | $329208.30
Chevrolet | Sonic | $164790.01
Toyota | Land Cruiser | $499588.74
test1=# select make, max(price) from car2 group by make;
test1=# select make, max(price) from car2 group by make;
make | max
---------------+------------
GMC | $984886.08
Maybach | $958865.52
Lincoln | $950229.61
Honda | $975876.46
Ram | $700793.00
test1=# select make, min(price) from car2 group by make;
make | min
---------------+------------
GMC | $120963.75
Maybach | $212805.06
Lincoln | $11175.31
Honda | $100053.26
test1=# select avg(price) from car;
avg
---------------------
498548.356000000000
(1 row)
test1=# select sum(price) from car;
sum
--------------
498548356.00
(1 row)
test1=# select round(AVG(price)) from car;
round
--------
498548
(1 row)
test1=# select make, model,min(price) from car group by make,model;
make | model | min
---------------+-------+-----------
Volvo | 2004 | 89615.70
Subaru | 1993 | 487488.90
Lexus | 1994 | 160161.17
Mitsubishi | 2001 | 514788.33
Saturn | 1994 | 93578.28
test1=# select make, model,max(price) from car group by make,model;
make | model | max
---------------+-------+-----------
Volvo | 2004 | 872887.32
Subaru | 1993 | 487488.90
Lexus | 1994 | 160161.17
Mitsubishi | 2001 | 914461.73
Saturn | 1994 | 93578.28
Oldsmobile | 2000 | 539889.05
Mercedes-Benz | 2004 | 110417.11
Suzuki | 1992 | 715673.38
test1=# select make, max(price) from car group by make;
make | max
---------------+-----------
GMC | 968898.71
Maybach | 481823.03
Lincoln | 949320.96
Honda | 875263.88
Spyker | 191167.69
Daewoo | 780076.84
test1=# select make, round(avg(price)) from car group by make;
make | round
---------------+--------
GMC | 541825
Maybach | 481823
Lincoln | 512964
Honda | 459939
Spyker | 191168
Daewoo | 516669
Ford | 479338
# BASICS OF ARITHMETIC OPERATORS
Mathematics that deal behayn caliculating the stastics
discaunt made on car price.
test1=# select 10 + 2;
?column?
----------
12
(1 row)
test1=# select 10 - 2;
?column?
----------
(1 row)
test1=# select 10 * 2;
?column?
----------
20
(1 row)
test1=# select 10 * 2 + 10;
?column?
----------
30
(1 row)
test1=# select 10 * 2 / 10;
?column?
----------
(1 row)
test1=# select 10 ^2;
?column?
----------
100
(1 row)
test1=# select 5!;
?column?
----------
120
(1 row)
test1=# select 10 % 3;
?column?
----------
(1 row)
ARITHMETIC OPERATORS ROUND
test1=# select * from car;
id | make | model | price
------+---------------+-------+-----------
1 | Infiniti | 2003 | 859791.35
2 | Chevrolet | 2012 | 564639.98
3 | Mazda | 2003 | 174224.96
4 | Daewoo | 2001 | 253261.28
5 | Volkswagen | 2000 | 469737.50
test1=# select id, make, model, price, price * .10 from car;
id | make | model | price | ?column?
------+---------------+-------+-----------+------------
1 | Infiniti | 2003 | 859791.35 | 85979.1350
2 | Chevrolet | 2012 | 564639.98 | 56463.9980
3 | Mazda | 2003 | 174224.96 | 17422.4960
4 | Daewoo | 2001 | 253261.28 | 25326.1280
5 | Volkswagen | 2000 | 469737.50 | 46973.7500
6 | Infiniti | 2005 | 13187.63 | 1318.7630
test1=# select id, make, model, price, round(price * .10, 2) from car;
id | make | model | price | round
------+---------------+-------+-----------+----------
1 | Infiniti | 2003 | 859791.35 | 85979.14
2 | Chevrolet | 2012 | 564639.98 | 56464.00
3 | Mazda | 2003 | 174224.96 | 17422.50
4 | Daewoo | 2001 | 253261.28 | 25326.13
5 | Volkswagen | 2000 | 469737.50 | 46973.75
test1=# select id, make, model, price, round(price * .10, 2), round(price - (price * .10)) from car;
id | make | model | price | round | round
------+---------------+-------+-----------+----------+--------
1 | Infiniti | 2003 | 859791.35 | 85979.14 | 773812
2 | Chevrolet | 2012 | 564639.98 | 56464.00 | 508176
3 | Mazda | 2003 | 174224.96 | 17422.50 | 156802
4 | Daewoo | 2001 | 253261.28 | 25326.13 | 227935
5 | Volkswagen | 2000 | 469737.50 | 46973.75 | 422764
6 | Infiniti | 2005 | 13187.63 | 1318.76 | 11869
7 | Saab | 1999 | 18152.43 | 1815.24 | 16337
8 | Volkswagen | 1985 | 577799.52 | 57779.95 | 520020
9 | Isuzu | 2006 | 336875.04 | 33687.50 | 303188
test1=# select id, make, model, price, round(price * .10, 2), round((price - (price * .10)), 2) from car;
id | make | model | price | round | round
------+---------------+-------+-----------+----------+-----------
1 | Infiniti | 2003 | 859791.35 | 85979.14 | 773812.22
2 | Chevrolet | 2012 | 564639.98 | 56464.00 | 508175.98
3 | Mazda | 2003 | 174224.96 | 17422.50 | 156802.46
4 | Daewoo | 2001 | 253261.28 | 25326.13 | 227935.15
5 | Volkswagen | 2000 | 469737.50 | 46973.75 | 422763.75
test1=# select id, make, model, price, round(price * .10, 2), round((price + (price * .10)), 2) from car;
id | make | model | price | round | round
------+---------------+-------+-----------+----------+------------
1 | Infiniti | 2003 | 859791.35 | 85979.14 | 945770.49
2 | Chevrolet | 2012 | 564639.98 | 56464.00 | 621103.98
3 | Mazda | 2003 | 174224.96 | 17422.50 | 191647.46
4 | Daewoo | 2001 | 253261.28 | 25326.13 | 278587.41
5 | Volkswagen | 2000 | 469737.50 | 46973.75 | 516711.25
##Alias
column name round and round is not correct let as use alias to give column name.
test1=# select id, make, model, price As Original_price, round(price * .10, 2) As ten_percent,
round((price - (price * .10)), 2) As discount_after_10_percent from car;
id | make | model | original_price | ten_percent | discount_after_10_percent
------+---------------+-------+----------------+-------------+---------------------------
1 | Infiniti | 2003 | 859791.35 | 85979.14 | 773812.22
2 | Chevrolet | 2012 | 564639.98 | 56464.00 | 508175.98
3 | Mazda | 2003 | 174224.96 | 17422.50 | 156802.46
4 | Daewoo | 2001 | 253261.28 | 25326.13 | 227935.15
5 | Volkswagen | 2000 | 469737.50 | 46973.75 | 422763.75
test1=# select id, make, model, price As Original_price, round(price * .10, 2) As ten_percent,
round((price + (price * .10)), 2) As Additional_10_percent from car;
id | make | model | original_price | ten_percent | additional_10_percent
------+---------------+-------+----------------+-------------+-----------------------
1 | Infiniti | 2003 | 859791.35 | 85979.14 | 945770.49
2 | Chevrolet | 2012 | 564639.98 | 56464.00 | 621103.98
3 | Mazda | 2003 | 174224.96 | 17422.50 | 191647.46
4 | Daewoo | 2001 | 253261.28 | 25326.13 | 278587.41
5 | Volkswagen | 2000 | 469737.50 | 46973.75 | 516711.25
test1=# select id, make, model, price As Original_price, round(price * .10, 2) As ten_percent_value,
round((price + (price * .10)), 2) As Additional_ten_percent from car;
id | make | model | original_price | ten_percent_value | additional_ten_percent
------+---------------+-------+----------------+-------------------+------------------------
1 | Infiniti | 2003 | 859791.35 | 85979.14 | 945770.49
2 | Chevrolet | 2012 | 564639.98 | 56464.00 | 621103.98
3 | Mazda | 2003 | 174224.96 | 17422.50 | 191647.46
4 | Daewoo | 2001 | 253261.28 | 25326.13 | 278587.41
5 | Volkswagen | 2000 | 469737.50 | 46973.75 | 516711.25
## AGE FUNCTION
test1=# select first_name, last_name, gender, date_of_birth, country_of_birth from person;
first_name | last_name | gender | date_of_birth | country_of_birth
--------------+------------------+--------+---------------+----------------------------------
Rip | Gorbell | Male | 2019-09-23 | China
Chantalle | Landor | Female | 2019-08-06 | China
Elsy | Armal | Female | 2019-10-24 | Brazil
Dennie | Crathorne | Male | 2019-06-24 | China
Haywood | Pickance | Male | 2020-02-24 | Portugal
Alyse | Cabotto | Female | 2019-10-28 | Indonesia
Vidovik | Shaylor | Male | 2019-04-20 | China
test1=# select first_name, last_name, gender, date_of_birth, country_of_birth, Age(now(),
date_of_birth) As age from person;
first_name | last_name | gender | date_of_birth | country_of_birth | age
--------------+------------------+--------+---------------+----------------------------------+---------------------------------
Rip | Gorbell | Male | 2019-09-23 | China | 6 mons 8 days 04:55:24.716655
Chantalle | Landor | Female | 2019-08-06 | China | 7 mons 26 days
04:55:24.716655
Elsy | Armal | Female | 2019-10-24 | Brazil | 5 mons 8 days 04:55:24.716655
Dennie | Crathorne | Male | 2019-06-24 | China | 9 mons 7 days
04:55:24.716655
Haywood | Pickance | Male | 2020-02-24 | Portugal | 1 mon 6 days
04:55:24.716655
Alyse | Cabotto | Female | 2019-10-28 | Indonesia | 5 mons 4 days
04:55:24.716655
Vidovik | Shaylor | Male | 2019-04-20 | China | 11 mons 11 days
04:55:24.716655
test1=# select first_name, last_name, gender, date_of_birth, country_of_birth, Age(now(),
date_of_birth) As age from person;
first_name | last_name | gender | date_of_birth | country_of_birth | age
-----------------+----------------+--------+---------------+----------------------------------+----------------------------------------
--
Jeffie | Backshill | Male | 2013-01-29 | China | 7 years 2 mons 3 days
05:54:29.763163
Page | Crouse | Female | 2010-10-07 | China | 9 years 5 mons 25 days
05:54:29.763163
Anabelle | Wadwell | Female | 2010-12-26 | Sweden | 9 years 3 mons 6 days
05:54:29.763163
Kriste | McFall | Female | 2012-12-13 | Yemen | 7 years 3 mons 19 days
05:54:29.763163
Joey | Vizor | Female | 2012-08-13 | Japan | 7 years 7 mons 19 days
05:54:29.763163
Staford | Antrack | Male | 2008-04-12 | Haiti | 11 years 11 mons 19 days
05:54:29.763163
Jacynth | Messenbird | Female | 2009-03-21 | China | 11 years 11 days
05:54:29.763163
Aldus | Duffyn | Male | 2018-10-19 | Philippin
## UNDERSTANDING PRIMARY KEY
test1=# select * from person limit 2;
id | first_name | last_name | gender | date_of_birth | country_of_birth | email
----+------------+-----------+--------+---------------+------------------+-------
1 | Jeffie | Backshill | Male | 2013-01-29 | China |
2 | Page | Crouse | Female | 2010-10-07 | China |
(2 rows)
##understanding Primary key
To drop the primary key construnt by alter
test1=# alter table person drop construnt person_pkey;
test1=# select * from person where id = 4;
id | first_name | last_name | gender | date_of_birth | country_of_birth | email
----+------------+-----------+--------+---------------+------------------+-------
4 | Kriste | McFall | Female | 2012-12-13 | Yemen |
(1 row)
test1=# select * from person where id = 1;
id | first_name | last_name | gender | date_of_birth | country_of_birth | email
----+------------+-----------+--------+---------------+------------------+-------
1 | Jeffie | Backshill | Male | 2013-01-29 | China |
(1 row)
test1=# insert into person (id, first_name , last_name, gender, date_of_birth, country_of_birth, email )
values (1, 'Jeffie', 'Backshill', 'Male', '1/29/2013', 'China', null);
ERROR: duplicate key value violates unique constraint "person_pkey"
DETAIL: Key (id)=(1) already exists.
test1=# Alter table person drop constraint person_pkey;
ALTER TABLE
test1=# select * from person where id = 1;
id | first_name | last_name | gender | date_of_birth | country_of_birth | email
----+------------+-----------+--------+---------------+------------------+-------
1 | Jeffie | Backshill | Male | 2013-01-29 | China |
1 | Jeffie | Backshill | Male | 2013-01-29 | China |
(2 rows)
test1=# \d person
Table "public.person"
Column | Type | Collation | Nullable | Default
------------------+------------------------+-----------+----------+------------------------------------
id | bigint | | not null | nextval('person_id_seq'::regclass)
first_name | character varying(80) | | not null |
last_name | character varying(80) | | not null |
gender | character varying(10) | | not null |
date_of_birth | date | | not null |
country_of_birth | character varying(80) | | not null |
email | character varying(150) | | |
test1=# alter table person add primary key(id);
ERROR: could not create unique index "person_pkey"
DETAIL: Key (id)=(1) is duplicated.
test1=# delete from person where id = 1;
DELETE 2
test1=# delete from person where gender = 'Female' and country_of_birth = 'China';
DELETE 77
test1=# alter table person add primary key(id);
ALTER TABLE
test1=# \d person
Table "public.person"
Column | Type | Collation | Nullable | Default
------------------+------------------------+-----------+----------+------------------------------------
id | bigint | | not null | nextval('person_id_seq'::regclass)
first_name | character varying(80) | | not null |
last_name | character varying(80) | | not null |
gender | character varying(10) | | not null |
date_of_birth | date | | not null |
country_of_birth | character varying(80) | | not null |
email | character varying(150) | | |
Indexes:
"person_pkey" PRIMARY KEY, btree (id)
test1=# select * from person;
id | first_name | last_name | gender | date_of_birth | country_of_birth | email
------+-----------------+----------------+--------+---------------+----------------------------------+---------------------------------
-----
2 | Page | Crouse | Female | 2010-10-07 | China |
3 | Anabelle | Wadwell | Female | 2010-12-26 | Sweden |
[email protected] 4 | Kriste | McFall | Female | 2012-12-13 | Yemen |
5 | Joey | Vizor | Female | 2012-08-13 | Japan |
[email protected]test1=# select * from person order by id;
id | first_name | last_name | gender | date_of_birth | country_of_birth | email
------+-----------------+----------------+--------+---------------+----------------------------------+---------------------------------
-----
1 | Jeffie | Backshill | Male | 2013-01-29 | China |
2 | Page | Crouse | Female | 2010-10-07 | China |
3 | Anabelle | Wadwell | Female | 2010-12-26 | Sweden |
[email protected] 4 | Kriste | McFall | Female | 2012-12-13 | Yemen |
5 | Joey | Vizor | Female | 2012-08-13 | Japan | [email protected]
## Unique Constraints
it alows to have unique values from a given columns
test1=# select email, count(*) from person group by email;
email | count
--------------------------------------+-------
[email protected] | 1
[email protected] | 1
[email protected] | 1
| 304
[email protected] | 1
[email protected] | 1
[email protected] | 1
[email protected] | 1
[email protected] | 1
test1=# select email, count(*) from person group by email having count(*) > 1;
email | count
-------+-------
| 304
test1=# insert into person (id, first_name , last_name, gender, date_of_birth, country_of_birth, email )
values (3, 'Eleni', 'Wadwell', 'Female', '12/26/2010', 'Sweden', '[email protected]');
ERROR: duplicate key value violates unique constraint "person_pkey"
DETAIL: Key (id)=(3) already exists.
test1=# insert into person (id, first_name , last_name, gender, date_of_birth, country_of_birth, email )
values (1001, 'Eleni', 'Wadwell', 'Female', '12/26/2010', 'Sweden', '[email protected]');
INSERT 0 1
test1=# select email, count(*) from person group by email having count(*) > 1;
email | count
---------------------+-------
| 304
[email protected] | 2
test1=# alter table peson add constraint unique_email_address unique(email);
ERROR: relation "peson" does not exist
test1=# alter table person add constraint unique_email_address unique(email);
ERROR: could not create unique index "unique_email_address"
DETAIL: Key (email)=([email protected]) is duplicated.
test1=# delete from person where id = 1001;
DELETE 1
test1=# alter table person add constraint unique_email_address unique(email);
ALTER TABLE
test1=# \d person
Table "public.person"
Column | Type | Collation | Nullable | Default
------------------+------------------------+-----------+----------+------------------------------------
id | bigint | | not null | nextval('person_id_seq'::regclass)
first_name | character varying(80) | | not null |
last_name | character varying(80) | | not null |
gender | character varying(10) | | not null |
date_of_birth | date | | not null |
country_of_birth | character varying(80) | | not null |
email | character varying(150) | | |
Indexes:
"person_pkey" PRIMARY KEY, btree (id)
"unique_email_address" UNIQUE CONSTRAINT, btree (email)
test1=# Alter table person drop constraint unique_email_address;
ALTER TABLE
test1=# insert into person (id, first_name , last_name, gender, date_of_birth, country_of_birth, email )
values (1001, 'Eleni', 'Wadwell', 'Female', '12/26/2010', 'Sweden', '[email protected]');
INSERT 0 1
test1=# \d person
Table "public.person"
Column | Type | Collation | Nullable | Default
------------------+------------------------+-----------+----------+------------------------------------
id | bigint | | not null | nextval('person_id_seq'::regclass)
first_name | character varying(80) | | not null |
last_name | character varying(80) | | not null |
gender | character varying(10) | | not null |
date_of_birth | date | | not null |
country_of_birth | character varying(80) | | not null |
email | character varying(150) | | |
Indexes:
"person_pkey" PRIMARY KEY, btree (id)
test1=# insert into person (id, first_name , last_name, gender, date_of_birth, country_of_birth, email )
values (1001, 'Eleni', 'Wadwell', 'Female', '12/26/2010', 'Sweden', '[email protected]');
ERROR: duplicate key value violates unique constraint "unique_email_address"
DETAIL: Key (email)=([email protected]) already exists.
test1=# delete from person where id = 1001;
DELETE 1
test1=# alter table person add unique (email);
ALTER TABLE
test1=# alter table person add constraint gender_constraint check(gender = 'Female' or gender =
'Male');
ALTER TABLE
test1=# \d person
Table "public.person"
Column | Type | Collation | Nullable | Default
------------------+------------------------+-----------+----------+------------------------------------
id | bigint | | not null | nextval('person_id_seq'::regclass)
first_name | character varying(80) | | not null |
last_name | character varying(80) | | not null |
gender | character varying(10) | | not null |
date_of_birth | date | | not null |
country_of_birth | character varying(80) | | not null |
email | character varying(150) | | |
Indexes:
"person_pkey" PRIMARY KEY, btree (id)
Check constraints:
"gender_constraint" CHECK (gender::text = 'Female'::text OR gender::text = 'Male'::text)
test1=# select * from person where gender = 'Female' and country_of_birth = 'China';
test1=# select * from person where gender = 'Male' and country_of_birth = 'China';
id | first_name | last_name | gender | date_of_birth | country_of_birth | email
-----+------------+-------------+--------+---------------+------------------+--------------------------------
1 | Jeffie | Backshill | Male | 2013-01-29 | China |
35 | Leonidas | Morde | Male | 2017-09-13 | China |
59 | Drud | Gouldsmith | Male | 2012-10-18 | China | [email protected]
91 | Stavro | Gedling | Male | 2010-12-08 | China |
104 | Wilbur | Malatalant | Male | 2018-02-02 | China |
119 | Alexis | Elesander | Male | 2017-04-02 | China | [email protected]
## UPDATE RECORDS
update evry singl row
test1=# update person set email = '[email protected]' where id = 1;
test1=# select * from person where id = '1';
id | first_name | last_name | gender | date_of_birth | country_of_birth | email
----+------------+-----------+--------+---------------+------------------+----------------------
1 | Jeffie | Backshill | Male | 2013-01-29 | China | [email protected]
(1 row)
## UPDATE RECORDS
update evry singl row
test1=# select * from person where id = '1';
id | first_name | last_name | gender | date_of_birth | country_of_birth | email
----+------------+-----------+--------+---------------+------------------+----------------------
1 | Jeffie | Backshill | Male | 2013-01-29 | China | [email protected]
(1 row)
test1=# select * from person where id = 2;
id | first_name | last_name | gender | date_of_birth | country_of_birth | email
----+------------+-----------+--------+---------------+------------------+-------
2 | Page | Crouse | Female | 2010-10-07 | China |
(1 row)
test1=# update person set first_name = 'Addisu', last_name = 'Bole', gender = 'Male', country_of_birth =
'Ethiopia', email = '[email protected]' where id = 2;
UPDATE 1
test1=# select * from person where id = 2;
id | first_name | last_name | gender | date_of_birth | country_of_birth | email
----+------------+-----------+--------+---------------+------------------+-------------------
2 | Addisu | Bole | Male | 2010-10-07 | Ethiopia | [email protected]
(1 row)
## ON conflict
## Do nothing
how to deal with dupilicate key error or exception handiling
test1=# insert into person (id, first_name, last_name, gender, email, date_of_birth, country_of_birth)
test1-# values (2, 'Abishalom', 'Addisu', 'Male', '[email protected]', Date '2010-10-15', 'Ethiopia');
ERROR: duplicate key value violates unique constraint "person_pkey"
DETAIL: Key (id)=(2) already exists.
test1=# insert into person (id, first_name, last_name, gender, email, date_of_birth, country_of_birth)
test1-# values (2, 'Abishalom', 'Addisu', 'Male', '[email protected]', Date '2010-10-15', 'Ethiopia')
test1-# ON conflict (id) Do nothing;
INSERT 0 0
test1=# insert into person (id, first_name, last_name, gender, email, date_of_birth, country_of_birth)
test1-# values (2, 'Abishalom', 'Addisu', 'Male', '[email protected]', Date '2010-10-15', 'Ethiopia')
test1-# ON conflict (email) Do nothing;
ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification
## Upset to change one cele in the table.
test1=# select * from person where id = 2;
id | first_name | last_name | gender | date_of_birth | country_of_birth | email
----+------------+-----------+--------+---------------+------------------+------------------
2 | Addisu | Bole | Male | 2010-10-07 | Ethiopia | [email protected]
(1 row)
INSERT 0 1
test1=# select * from person where id = 2;
id | first_name | last_name | gender | date_of_birth | country_of_birth | email
----+------------+-----------+--------+---------------+------------------+------------------
2 | Addisu | Bole | Male | 2010-10-07 | Ethiopia | [email protected]
(1 row)
test1=# insert into person (id, first_name, last_name, gender, email, date_of_birth, country_of_birth)
test1-# values (2, 'Abishalom', 'Addisu', 'Male', '[email protected]', Date '2010-10-15', 'Ethiopia')
test1-# ON conflict (id) Do update set email = excluded.email;
INSERT 0 1
test1=# select * from person where id = 2;
id | first_name | last_name | gender | date_of_birth | country_of_birth | email
----+------------+-----------+--------+---------------+------------------+------------------
2 | Addisu | Bole | Male | 2010-10-07 | Ethiopia | [email protected]
(1 row)
## Do update set email = excluded.email
test1=# insert into person (id, first_name, last_name, gender, email, date_of_birth, country_of_birth)
test1-# values (2, 'Abishalom', 'Addisu', 'Male', '[email protected]', Date '2010-10-15', 'Ethiopia')
test1-# ON conflict (id) Do update set email = excluded.email,
test1-# last_name = excluded.last_name, first_name = excluded.first_name;
INSERT 0 1
test1=# select * from person where id = 2;
id | first_name | last_name | gender | date_of_birth | country_of_birth | email
----+------------+-----------+--------+---------------+------------------+------------------
2 | Abishalom | Addisu | Male | 2010-10-07 | Ethiopia | [email protected]
(1 row)
## ADDING RELATIONSHIPS BETWEEN TABLES
let as add relationships between car and person.
test1=# \dt
List of relations
Schema | Name | Type | Owner
--------+----------+-------+----------
public | car | table | postgres
public | person | table | postgres
public | student | table | postgres
public | student1 | table | postgres
(4 rows)
test1=# drop table person;
DROP TABLE
test1=#
test1=# drop table car;
DROP TABLE
test1=# \i C:/Users/Adisu/Downloads/person-car.sql
CREATE TABLE
CREATE TABLE
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
test1=# \d
List of relations
Schema | Name | Type | Owner
--------+-----------------+----------+----------
public | car | table | postgres
public | car_id_seq | sequence | postgres
public | person | table | postgres
public | person_id_seq | sequence | postgres
public | student | table | postgres
public | student1 | table | postgres
public | student1_id_seq | sequence | postgres
(7 rows)
test1=# \dt
List of relations
Schema | Name | Type | Owner
--------+----------+-------+----------
public | car | table | postgres
public | person | table | postgres
public | student | table | postgres
public | student1 | table | postgres
(4 rows)
test1=# select * from person;
id | first_name | last_name | gender | email | date_of_birth | country_of_birth | car_id
----+------------+-----------+--------+----------------------+---------------+------------------+--------
1 | Addisu | Bole | Male | [email protected] | 1986-01-12 | Ethiopia |
2 | Mersha | G/hiwot | Female | [email protected] | 1956-01-12 | Ethiopia |
3 | Kidus | Menfese | Male | [email protected] | 1988-01-12 | Ethiopia |
4 | Zelalem | Bole | Male | [email protected] | 0192-01-12 | Ethiopia |
5 | Eleni | Getnet | Female | [email protected] | 1992-01-12 | Ethiopia |
(5 rows)
test1=# select * from car;
id | make | model | price
----+------------+--------------+-----------
1 | Toyota | extorureruer | 107865.38
2 | Merichedis | Storeiaot | 678695.38
3 | Hyhonday | ltvyLsdhf | 89865.38
4 | Isuzu | obamamod | 997865.38
(4 rows)
## UPDATINIG FOREIGN KEYS COLUMNS
test1=# \d person
Table "public.person"
Column | Type | Collation | Nullable | Default
------------------+------------------------+-----------+----------+------------------------------------
id | bigint | | not null | nextval('person_id_seq'::regclass)
first_name | character varying(100) | | not null |
last_name | character varying(100) | | not null |
gender | character varying(8) | | not null |
email | character varying(150) | | |
date_of_birth | date | | not null |
country_of_birth | character varying(120) | | not null |
car_id | bigint | | |
Indexes:
"person_pkey" PRIMARY KEY, btree (id)
"person_car_id_key" UNIQUE CONSTRAINT, btree (car_id)
Foreign-key constraints:
"person_car_id_fkey" FOREIGN KEY (car_id) REFERENCES car(id)
## UPDATE FOREIGN KEYS COLUMNS
test1=# update person set car_id = 1 where id = 1;
UPDATE 1
test1=# select * from person;
id | first_name | last_name | gender | email | date_of_birth | country_of_birth | car_id
----+------------+-----------+--------+----------------------+---------------+------------------+--------
2 | Mersha | G/hiwot | Female | [email protected] | 1956-01-12 | Ethiopia |
3 | Kidus | Menfese | Male | [email protected] | 1988-01-12 | Ethiopia |
4 | Zelalem | Bole | Male |
[email protected] | 0192-01-12 | Ethiopia |
5 | Eleni | Getnet | Female | [email protected] | 1992-01-12 | Ethiopia |
1 | Addisu | Bole | Male | [email protected] | 1986-01-12 | Ethiopia | 1
(5 rows)
test1=# select * from car;
id | make | model | price
----+------------+--------------+-----------
1 | Toyota | extorureruer | 107865.38
2 | Merichedis | Storeiaot | 678695.38
3 | Hyhonday | ltvyLsdhf | 89865.38
4 | Isuzu | obamamod | 997865.38
(4 rows)
test1=# update person set car_id = 3 where id = 2;
UPDATE 1
test1=# select * from person;
id | first_name | last_name | gender | email | date_of_birth | country_of_birth | car_id
----+------------+-----------+--------+----------------------+---------------+------------------+--------
3 | Kidus | Menfese | Male | [email protected] | 1988-01-12 | Ethiopia |
4 | Zelalem | Bole | Male | [email protected] | 0192-01-12 | Ethiopia |
5 | Eleni | Getnet | Female | [email protected] | 1992-01-12 | Ethiopia |
1 | Addisu | Bole | Male | [email protected] | 1986-01-12 | Ethiopia | 1
2 | Mersha | G/hiwot | Female |
[email protected] | 1956-01-12 | Ethiopia | 3
(5 rows)
## INNER JOINS
test1=# select * from person;
id | first_name | last_name | gender | email | date_of_birth | country_of_birth | car_id
----+------------+-----------+--------+----------------------+---------------+------------------+--------
3 | Kidus | Menfese | Male | [email protected] | 1988-01-12 | Ethiopia |
4 | Zelalem | Bole | Male | [email protected] | 0192-01-12 | Ethiopia |
1 | Addisu | Bole | Male | [email protected] | 1986-01-12 | Ethiopia | 1
2 | Mersha | G/hiwot | Female | [email protected] | 1956-01-12 | Ethiopia | 3
5 | Eleni | Getnet | Female | [email protected] | 1992-01-12 | Ethiopia | 2
(5 rows)
test1=# select * from car;
id | make | model | price
----+------------+--------------+-----------
1 | Toyota | extorureruer | 107865.38
2 | Merichedis | Storeiaot | 678695.38
3 | Hyhonday | ltvyLsdhf | 89865.38
4 | Isuzu | obamamod | 997865.38
(4 rows)
test1=# select * from person
test1-# join car on person.car_id = car.id;
id | first_name | last_name | gender | email | date_of_birth | country_of_birth | car_id | id |
make | model | price
----+------------+-----------+--------+----------------------+---------------+------------------+--------+----+------------+--------
------+-----------
1 | Addisu | Bole | Male | [email protected] | 1986-01-12 | Ethiopia | 1| 1|
Toyota | extorureruer | 107865.38
5 | Eleni | Getnet | Female | [email protected] | 1992-01-12 | Ethiopia | 2| 2|
Merichedis | Storeiaot | 678695.38
2 | Mersha | G/hiwot | Female | [email protected] | 1956-01-12 | Ethiopia | 3| 3|
Hyhonday | ltvyLsdhf | 89865.38
(3 rows)
test1=# \x
Expanded display is on.
test1=# select * from person
test1-# join car on person.car_id = car.id;
-[ RECORD 1 ]----+---------------------
id |1
first_name | Addisu
last_name | Bole
gender | Male
email | [email protected]
date_of_birth | 1986-01-12
country_of_birth | Ethiopia
car_id |1
id |1
make | Toyota
model | extorureruer
price | 107865.38
-[ RECORD 2 ]----+---------------------
id |5
first_name | Eleni
last_name | Getnet
gender | Female
email | [email protected]
date_of_birth | 1992-01-12
country_of_birth | Ethiopia
car_id |2
id |2
make | Merichedis
model | Storeiaot
price | 678695.38
-[ RECORD 3 ]----+---------------------
id |2
first_name | Mersha
last_name | G/hiwot
gender | Female
email | [email protected]
date_of_birth | 1956-01-12
country_of_birth | Ethiopia
car_id |3
id |3
make | Hyhonday
model | ltvyLsdhf
price | 89865.38
test1=# select person.first_name, car.make, car.model, car.price
test1-# from person
test1-# join car on person.car_id = car.id;
first_name | make | model | price
------------+------------+--------------+-----------
Addisu | Toyota | extorureruer | 107865.38
Eleni | Merichedis | Storeiaot | 678695.38
Mersha | Hyhonday | ltvyLsdhf | 89865.38
(3 rows)
##LEFT JOIN
to includ a people that do not have car to
test1=# select * from person
test1-# left join car on car.id = person.car_id;
id | first_name | last_name | gender | email | date_of_birth | country_of_birth | car_id | id |
make | model | price
----+------------+-----------+--------+----------------------+---------------+------------------+--------+----+------------+--------
------+-----------
1 | Addisu | Bole | Male | [email protected] | 1986-01-12 | Ethiopia | 1| 1|
Toyota | extorureruer | 107865.38
5 | Eleni | Getnet | Female | [email protected] | 1992-01-12 | Ethiopia | 2| 2|
Merichedis | Storeiaot | 678695.38
2 | Mersha | G/hiwot | Female |
[email protected] | 1956-01-12 | Ethiopia | 3| 3|
Hyhonday | ltvyLsdhf | 89865.38
4 | Zelalem | Bole | Male | [email protected] | 0192-01-12 | Ethiopia | | |
| |
3 | Kidus | Menfese | Male | [email protected] | 1988-01-12 | Ethiopia | | |
| |
(5 rows)
test1=# select * from person where car_id is null;
id | first_name | last_name | gender | email | date_of_birth | country_of_birth | car_id
----+------------+-----------+--------+---------------------+---------------+------------------+--------
3 | Kidus | Menfese | Male | [email protected] | 1988-01-12 | Ethiopia |
4 | Zelalem | Bole | Male | [email protected] | 0192-01-12 | Ethiopia |
(2 rows)
test1=# select * from person
test1-# left join car on car.id = person.car_id
test1-# where car.* is null;
id | first_name | last_name | gender | email | date_of_birth | country_of_birth | car_id | id |
make | model | price
----+------------+-----------+--------+---------------------+---------------+------------------+--------+----+------+-------+-------
4 | Zelalem | Bole | Male | [email protected] | 0192-01-12 | Ethiopia | | | |
|
3 | Kidus | Menfese | Male | [email protected] | 1988-01-12 | Ethiopia | | | |
|
(2 rows)
test1=# select * from person;
id | first_name | last_name | gender | email | date_of_birth | country_of_birth | car_id
----+------------+------------+--------+--------------------------+---------------+------------------+--------
3 | Kidus | Menfese | Male | [email protected] | 1988-01-12 | Ethiopia |
4 | Zelalem | Bole | Male | [email protected] | 0192-01-12 | Ethiopia |
1 | Addisu | Bole | Male | [email protected] | 1986-01-12 | Ethiopia | 1
2 | Mersha | G/hiwot | Female | [email protected] | 1956-01-12 | Ethiopia | 3
5 | Eleni | Getnet | Female | [email protected] | 1992-01-12 | Ethiopia | 2
6 | Staford | Antrack | Male | | 2008-04-12 | Haiti |
7 | Jacynth | Messenbird | Female | [email protected] | 2009-03-21 | China |
8 | Aldus | Duffyn | Male | [email protected] | 2018-10-19 | Philippines |
9 | Merline | Gascoyen | Female | [email protected] | 2009-06-21 | Indonesia |
10 | Christie | Reeve | Male | [email protected] | 2014-02-10 | Poland |
11 | Durant | Sumption | Male | [email protected] | 2019-10-16 | Portugal |
12 | Hirsch | Adamek | Male | [email protected] | 2009-03-16 | Liechtenstein |
(12 rows)
test1=# select * from car;
id | make | model | price
----+------------+--------------+-----------
1 | Toyota | extorureruer | 107865.38
2 | Merichedis | Storeiaot | 678695.38
3 | Hyhonday | ltvyLsdhf | 89865.38
4 | Isuzu | obamamod | 997865.38
5 | Volkswagen | 2000 | 469737.50
6 | Infiniti | 2005 | 13187.63
7 | Saab | 1999 | 18152.43
8 | Volkswagen | 1985 | 577799.52
9 | Isuzu | 2006 | 336875.04
10 | Dodge | 1995 | 374024.64
11 | Subaru | 2001 | 327004.32
12 | Toyota | 1996 | 981970.55
(12 rows)
test1=# update person set car_id = 10 where id = 8;
UPDATE 1
test1=# select * from person;
id | first_name | last_name | gender | email | date_of_birth | country_of_birth | car_id
----+------------+------------+--------+--------------------------+---------------+------------------+--------
3 | Kidus | Menfese | Male | [email protected] | 1988-01-12 | Ethiopia |
4 | Zelalem | Bole | Male | [email protected] | 0192-01-12 | Ethiopia |
1 | Addisu | Bole | Male | [email protected] | 1986-01-12 | Ethiopia | 1
2 | Mersha | G/hiwot | Female | [email protected] | 1956-01-12 | Ethiopia | 3
5 | Eleni | Getnet | Female | [email protected] | 1992-01-12 | Ethiopia | 2
6 | Staford | Antrack | Male | | 2008-04-12 | Haiti |
7 | Jacynth | Messenbird | Female | [email protected] | 2009-03-21 | China |
9 | Merline | Gascoyen | Female | [email protected] | 2009-06-21 | Indonesia |
10 | Christie | Reeve | Male | [email protected] | 2014-02-10 | Poland |
11 | Durant | Sumption | Male | [email protected] | 2019-10-16 | Portugal |
12 | Hirsch | Adamek | Male | [email protected] | 2009-03-16 | Liechtenstein |
8 | Aldus | Duffyn | Male | [email protected] | 2018-10-19 | Philippines | 10
(12 rows)
## DELETING RECORDS WITH FOREIGN KEY
test1=# delete from car where id = 10;
ERROR: update or delete on table "car" violates foreign key constraint "person_car_id_fkey" on table
"person"
DETAIL: Key (id)=(10) is still referenced from table "person".
###
EXPORTING QUERY RESULTS TO CSV FORMAT
\?
test1=# \copy (select * from person left join car ON car.id =person.car_id) To
'/Users/Adisu/Desktop/result.csv' DELIMITER ',' CSV HEADER;
COPY 20
#SERIAL SEQUENCE
bigserial is special data type outo increament number that number is an integer.
# EXTENSIONS
now you can see a list of all extension.
test1=# select * from pg_available_extensions;
name | default_version | installed_version | comment
--------------------+-----------------+-------------------+----------------------------------------------------------------------
adminpack | 2.0 | | administrative functions for PostgreSQL
amcheck | 1.2 | | functions for verifying relation integrity
autoinc | 1.0 | | functions for autoincrementing fields
bloom | 1.0 | | bloom access method - signature file based index
btree_gin | 1.3 | | support for indexing common datatypes in GIN
btree_gist | 1.5 | | support for indexing common datatypes in GiST
# UUID-ASS
is generate universally unque identifiers(uuids)
understanding uuid data type
https://en.wikipedia.org/wiki/Universally_unique_identifier
it is globaly unique
we must install the extension if not exist
test1=# create extension if not exists "uuid-ossp";
CREATE EXTENSION
test1=# select * from pg_available_extensions;
name | default_version | installed_version | comment
--------------------+-----------------+-------------------+----------------------------------------------------------------------
adminpack | 2.0 | | administrative functions for PostgreSQL
amcheck | 1.2 | | functions for verifying relation integrity
autoinc | 1.0 | | functions for autoincrementing fields
bloom | 1.0 | | bloom access method - signature file based index
btree_gin | 1.3 | | support for indexing common datatypes in GIN
btree_gist | 1.5 | | support for indexing common datatypes in GiST
citext | 1.6 | | data type for case-insensitive character strings
test1=# \df
List of functions
Schema | Name | Result data type | Argument data types | Type
--------+--------------------+------------------+---------------------------+------
public | uuid_generate_v1 | uuid | | func
public | uuid_generate_v1mc | uuid | | func
public | uuid_generate_v3 | uuid | namespace uuid, name text | func
public | uuid_generate_v4 | uuid | | func
public | uuid_generate_v5 | uuid | namespace uuid, name text | func
public | uuid_nil | uuid | | func
public | uuid_ns_dns | uuid | | func
public | uuid_ns_oid | uuid | | func
public | uuid_ns_url | uuid | | func
public | uuid_ns_x500 | uuid | | func
test1=# select uuid_generate_v4();
uuid_generate_v4
--------------------------------------
6db42457-198d-43a4-9b07-3937fa5c0440
(1 row)
test1=# \i C:/Users/Adisu/Downloads/person-car2.sql;
CREATE TABLE
CREATE TABLE
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
test1=# select * from person2;
person_uid | first_name | last_name | gender | email | date_of_birth |
country_of_birth | car_uid
--------------------------------------+------------+-----------+--------+----------------------+---------------+------------------+---
------
0b63042f-8568-4a1d-8a40-f02f4d4fd90c | Addisu | Bole | Male | [email protected] | 1986-
01-12 | Ethiopia |
c7e620ac-f09f-48c1-9483-57b073bfe8b0 | Mersha | G/hiwot | Female | [email protected] |
1956-01-12 | Ethiopia |
f6955beb-dbfa-4442-9985-88a05197ca2d | Kidus | Menfese | Male | [email protected] | 1988-
01-12 | Ethiopia |
15afa68b-9a77-4d31-87cb-e4da37c13844 | Zelalem | Bole | Male | [email protected] | 0192-
01-12 | Ethiopia |
14f2bfb7-a0a1-4ce7-8e2f-b7269db25813 | Eleni | Getnet | Female | [email protected] |
1992-01-12 | Ethiopia |
(5 rows)
test1=# select * from car2;
car_uid | make | model | price
--------------------------------------+------------+--------------+-----------
efc1bd86-46a1-4294-be7a-368495257729 | Toyota | extorureruer | 107865.38
a839ba0a-621b-4827-ba66-8d942c632488 | Merichedis | Storeiaot | 678695.38
d241e8a6-a0d5-490a-bded-36ef05118878 | Hyhonday | ltvyLsdhf | 89865.38
ab9547af-9ed6-4700-8808-baafe4507825 | Isuzu | obamamod | 997865.38
(4 rows)
test1=# UPDATE person2 SET car_uid = 'a839ba0a-621b-4827-ba66-8d942c632488' WHERE person_uid
= 'c7e620ac-f09f-48c1-9483-57b073bfe8b0';
UPDATE 1
test1=# UPDATE person2 SET car_uid = 'd241e8a6-a0d5-490a-bded-36ef05118878' WHERE person_uid =
'14f2bfb7-a0a1-4ce7-8e2f-b7269db25813';
UPDATE 1
test1=# select * from person2
test1-# join car2 on person2.car_uid = car2.car_uid;
person_uid | first_name | last_name | gender | email | date_of_birth |
country_of_birth | car_uid | car_uid | make | model | price
--------------------------------------+------------+-----------+--------+----------------------+---------------+------------------+---
-----------------------------------+--------------------------------------+------------+--------------+-----------
0b63042f-8568-4a1d-8a40-f02f4d4fd90c | Addisu | Bole | Male | [email protected] | 1986-
01-12 | Ethiopia | efc1bd86-46a1-4294-be7a-368495257729 | efc1bd86-46a1-4294-be7a-
368495257729 | Toyota | extorureruer | 107865.38
c7e620ac-f09f-48c1-9483-57b073bfe8b0 | Mersha | G/hiwot | Female | [email protected] |
1956-01-12 | Ethiopia | a839ba0a-621b-4827-ba66-8d942c632488 | a839ba0a-621b-4827-ba66-
8d942c632488 | Merichedis | Storeiaot | 678695.38
14f2bfb7-a0a1-4ce7-8e2f-b7269db25813 | Eleni | Getnet | Female | [email protected] |
1992-01-12 | Ethiopia | d241e8a6-a0d5-490a-bded-36ef05118878 | d241e8a6-a0d5-490a-bded-
36ef05118878 | Hyhonday | ltvyLsdhf | 89865.38
(3 rows)
test1=# select * from person2
test1-# join car2 on person2.car_uid = car2.car_uid;
-[ RECORD 1 ]----+-------------------------------------
person_uid | 0b63042f-8568-4a1d-8a40-f02f4d4fd90c
first_name | Addisu
last_name | Bole
gender | Male
email | [email protected]
date_of_birth | 1986-01-12
country_of_birth | Ethiopia
car_uid | efc1bd86-46a1-4294-be7a-368495257729
car_uid | efc1bd86-46a1-4294-be7a-368495257729
make | Toyota
model | extorureruer
price | 107865.38
-[ RECORD 2 ]----+-------------------------------------
person_uid | c7e620ac-f09f-48c1-9483-57b073bfe8b0
first_name | Mersha
last_name | G/hiwot
gender | Female
email | [email protected]
date_of_birth | 1956-01-12
country_of_birth | Ethiopia
car_uid | a839ba0a-621b-4827-ba66-8d942c632488
car_uid | a839ba0a-621b-4827-ba66-8d942c632488
make | Merichedis
model | Storeiaot
price | 678695.38
-[ RECORD 3 ]----+-------------------------------------
person_uid | 14f2bfb7-a0a1-4ce7-8e2f-b7269db25813
first_name | Eleni
last_name | Getnet
gender | Female
email | [email protected]
date_of_birth | 1992-01-12
country_of_birth | Ethiopia
car_uid | d241e8a6-a0d5-490a-bded-36ef05118878
car_uid | d241e8a6-a0d5-490a-bded-36ef05118878
make | Hyhonday
model | ltvyLsdhf
price | 89865.38
test1=# select * from person2
test1-# join car2 using(car_uid);
-[ RECORD 1 ]----+-------------------------------------
car_uid | efc1bd86-46a1-4294-be7a-368495257729
person_uid | 0b63042f-8568-4a1d-8a40-f02f4d4fd90c
first_name | Addisu
last_name | Bole
gender | Male
email | [email protected]
date_of_birth | 1986-01-12
country_of_birth | Ethiopia
make | Toyota
model | extorureruer
price | 107865.38
-[ RECORD 2 ]----+-------------------------------------
car_uid | a839ba0a-621b-4827-ba66-8d942c632488
person_uid | c7e620ac-f09f-48c1-9483-57b073bfe8b0
first_name | Mersha
last_name | G/hiwot
gender | Female
email | [email protected]
date_of_birth | 1956-01-12
country_of_birth | Ethiopia
make | Merichedis
model | Storeiaot
price | 678695.38
-[ RECORD 3 ]----+-------------------------------------
car_uid | d241e8a6-a0d5-490a-bded-36ef05118878
person_uid | 14f2bfb7-a0a1-4ce7-8e2f-b7269db25813
first_name | Eleni
last_name | Getnet
gender | Female
email | [email protected]
date_of_birth | 1992-01-12
country_of_birth | Ethiopia
make | Hyhonday
model | ltvyLsdhf
price | 89865.38
test1=# select * from person2
test1-# left join car2 using(car_uid);
-[ RECORD 1 ]----+-------------------------------------
car_uid | efc1bd86-46a1-4294-be7a-368495257729
person_uid | 0b63042f-8568-4a1d-8a40-f02f4d4fd90c
first_name | Addisu
last_name | Bole
gender | Male
email | [email protected]
date_of_birth | 1986-01-12
country_of_birth | Ethiopia
make | Toyota
model | extorureruer
price | 107865.38
-[ RECORD 2 ]----+-------------------------------------
car_uid | a839ba0a-621b-4827-ba66-8d942c632488
person_uid | c7e620ac-f09f-48c1-9483-57b073bfe8b0
first_name | Mersha
last_name | G/hiwot
gender | Female
email | [email protected]
date_of_birth | 1956-01-12
country_of_birth | Ethiopia
make | Merichedis
model | Storeiaot
price | 678695.38
-[ RECORD 3 ]----+-------------------------------------
test1=# select * from person2
test1-# left join car2 using(car_uid)
test1-# where car2.* is null;
-[ RECORD 1 ]----+-------------------------------------
car_uid |
person_uid | 15afa68b-9a77-4d31-87cb-e4da37c13844
first_name | Zelalem
last_name | Bole
gender | Male
email | [email protected]
date_of_birth | 0192-01-12
country_of_birth | Ethiopia
make |
model |
price |
-[ RECORD 2 ]----+-------------------------------------
car_uid |
person_uid | f6955beb-dbfa-4442-9985-88a05197ca2d
first_name | Kidus
last_name | Menfese
gender | Male
email | [email protected]
date_of_birth | 1988-01-12
country_of_birth | Ethiopia
make |
model |
price |
"""" CONGRATULATION""""
WE ARE COMPLETED DATABASE MANAGMENT SYSTEM
HANDS ON LABORATORY