Professional Documents
Culture Documents
DBMS Practical Section
DBMS Practical Section
https://www.javatpoint.com/dbms-tutorial
https://www.javatpoint.com/dbms-three-schema-architecture
* store
*Manipulate
* is Programming Language
*Easy to learn
*Very powerful
* started in 1974
## What is PostgreSQL
*Modern
*Open Source
*Oracle
*MySQL and SQL Server which need license from the Microsoft
* Postages download
** URL https://www.postgresql.org/download/
*Download
** URL https://www.enterprisedb.com/downloads/postgres-postgresql-downloads
sql shell
server[localhost]:
Database[postgres]: "you can type your own database name and prese enter"
Port{5432]:
Username[postgres]:
\l
\?
#help
\q
# To connect the new created database, you must use the following command
\c test1
# To delete a table
# To create the table we can get the data type from this URL
https://www.postgresql.org/docs/12/datatype.html
test1(# id int,
CREATE TABLE
\d
i.e
\d student
CREATE TABLE
test1=# \l
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', 'zelalem@gmail.com', 'Ethiopia');
INSERT 0 1
test1(# first_name,
test1(# last_name,
test1(# gender,
test1(# date_of_birth,
test1(# email,
test1(# country_of_birth)
INSERT 0 1
test1(# first_name,
test1(# last_name,
test1(# gender,
test1(# date_of_birth,
test1(# email,
test1(# country_of_birth)
INSERT 0 1
test1(# first_name,
test1(# last_name,
test1(# gender,
test1(# date_of_birth,
test1(# email,
test1(# country_of_birth)
INSERT 0 1
check a preview
download at the end and open using visual studio software is open source you can download using the
following url
https://code.visualstudio.com/docs/?dv=win
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
\i C:/Users/Adisu/Downloads/person.sql
# Select From
#To grave the data from a table we use the following command
# ORDERD BY
# assending or Disending
# Distinct
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
?column?
----------
(1 row)
?column?
----------
f
(1 row)
?column?
----------
(1 row)
?column?
----------
(1 row)
?column?
----------
(1 row)
?column?
----------
t
(1 row)
?column?
----------
(1 row)
?column?
----------
(1 row)
?column?
----------
(1 row)
IN
BETWEEN
test1=# select * from person where date_of_birth between Date '2019-01-01' and '2020-01-15';
GROUP BY
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
price money numeric(20, 2) not null "Make the data type / carrency none"
);
------+-------------+---------------+--------
1 | Eb | Savill | $3.22
max
-----------
$99990.62
(1 row)
min
------------
$100053.26
(1 row)
test1=# select make, model, min(price) from car2 group by make, model;
---------------+----------------------+------------
test1=# select make, model, max(price) from car2 group by make, model;
---------------+----------------------+------------
make | max
---------------+------------
GMC | $984886.08
Maybach | $958865.52
Lincoln | $950229.61
Honda | $975876.46
Ram | $700793.00
make | min
---------------+------------
GMC | $120963.75
Maybach | $212805.06
Lincoln | $11175.31
Honda | $100053.26
avg
---------------------
498548.356000000000
(1 row)
sum
--------------
498548356.00
(1 row)
round
--------
498548
(1 row)
---------------+-------+-----------
---------------+-------+-----------
make | max
---------------+-----------
GMC | 968898.71
Maybach | 481823.03
Lincoln | 949320.96
Honda | 875263.88
Spyker | 191167.69
Daewoo | 780076.84
make | round
---------------+--------
GMC | 541825
Maybach | 481823
Lincoln | 512964
Honda | 459939
Spyker | 191168
Daewoo | 516669
Ford | 479338
test1=# select 10 + 2;
?column?
----------
12
(1 row)
test1=# select 10 - 2;
?column?
----------
(1 row)
test1=# select 10 * 2;
?column?
----------
20
(1 row)
?column?
----------
30
(1 row)
?column?
----------
(1 row)
?column?
----------
100
(1 row)
?column?
----------
120
(1 row)
test1=# select 10 % 3;
?column?
----------
(1 row)
------+---------------+-------+-----------
test1=# select id, make, model, price, price * .10 from car;
test1=# select id, make, model, price, round(price * .10, 2) from car;
------+---------------+-------+-----------+----------
test1=# select id, make, model, price, round(price * .10, 2), round(price - (price * .10)) from car;
------+---------------+-------+-----------+----------+--------
test1=# select id, make, model, price, round(price * .10, 2), round((price - (price * .10)), 2) from car;
------+---------------+-------+-----------+----------+-----------
test1=# select id, make, model, price, round(price * .10, 2), round((price + (price * .10)), 2) from car;
------+---------------+-------+-----------+----------+------------
##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;
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;
------+---------------+-------+----------------+-------------+-----------------------
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;
------+---------------+-------+----------------+-------------------+------------------------
## AGE FUNCTION
--------------+------------------+--------+---------------+----------------------------------
--------------+------------------+--------+---------------+----------------------------------+---------------------------------
-----------------+----------------+--------+---------------+----------------------------------+----------------------------------------
--
----+------------+-----------+--------+---------------+------------------+-------
1 | Jeffie | Backshill | Male | 2013-01-29 | China |
(2 rows)
----+------------+-----------+--------+---------------+------------------+-------
(1 row)
----+------------+-----------+--------+---------------+------------------+-------
(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);
ALTER TABLE
test1=# select * from person where id = 1;
----+------------+-----------+--------+---------------+------------------+-------
(2 rows)
test1=# \d person
Table "public.person"
------------------+------------------------+-----------+----------+------------------------------------
DELETE 2
test1=# delete from person where gender = 'Female' and country_of_birth = 'China';
DELETE 77
ALTER TABLE
test1=# \d person
Table "public.person"
------------------+------------------------+-----------+----------+------------------------------------
Indexes:
------+-----------------+----------------+--------+---------------+----------------------------------+---------------------------------
-----
------+-----------------+----------------+--------+---------------+----------------------------------+---------------------------------
-----
## Unique Constraints
email | count
--------------------------------------+-------
sbotemanj9@dedecms.com | 1
kdracheb3@networksolutions.com | 1
mrosonepp@shareasale.com | 1
| 304
acottonge@ebay.com | 1
sbeamentrr@amazon.com | 1
smacascaidh5s@weebly.com | 1
gpridenn9@netlog.com | 1
acockarillk0@uol.com.br | 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', 'awadwell2@sogou.com');
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', 'awadwell2@sogou.com');
INSERT 0 1
test1=# select email, count(*) from person group by email having count(*) > 1;
email | count
---------------------+-------
| 304
awadwell2@sogou.com | 2
DELETE 1
test1=# \d person
Table "public.person"
------------------+------------------------+-----------+----------+------------------------------------
Indexes:
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', 'awadwell2@sogou.com');
INSERT 0 1
test1=# \d person
Table "public.person"
------------------+------------------------+-----------+----------+------------------------------------
Indexes:
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', 'awadwell2@sogou.com');
DELETE 1
ALTER TABLE
test1=# alter table person add constraint gender_constraint check(gender = 'Female' or gender =
'Male');
ALTER TABLE
test1=# \d person
Table "public.person"
------------------+------------------------+-----------+----------+------------------------------------
Indexes:
Check constraints:
test1=# select * from person where gender = 'Female' and country_of_birth = 'China';
test1=# select * from person where gender = 'Male' and country_of_birth = 'China';
-----+------------+-------------+--------+---------------+------------------+--------------------------------
## UPDATE RECORDS
----+------------+-----------+--------+---------------+------------------+----------------------
1 | Jeffie | Backshill | Male | 2013-01-29 | China | elu.getu03@gmail.com
(1 row)
## UPDATE RECORDS
----+------------+-----------+--------+---------------+------------------+----------------------
(1 row)
----+------------+-----------+--------+---------------+------------------+-------
(1 row)
test1=# update person set first_name = 'Addisu', last_name = 'Bole', gender = 'Male', country_of_birth =
'Ethiopia', email = 'addiusb@hu.edu.et' where id = 2;
UPDATE 1
----+------------+-----------+--------+---------------+------------------+-------------------
(1 row)
## ON conflict
## Do nothing
test1=# insert into person (id, first_name, last_name, gender, email, date_of_birth, country_of_birth)
test1-# values (2, 'Abishalom', 'Addisu', 'Male', 'abisha@hu.edu.et', Date '2010-10-15', 'Ethiopia');
test1=# insert into person (id, first_name, last_name, gender, email, date_of_birth, country_of_birth)
test1-# values (2, 'Abishalom', 'Addisu', 'Male', 'abisha@hu.edu.et', Date '2010-10-15', 'Ethiopia')
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', 'abisha@hu.edu.et', Date '2010-10-15', 'Ethiopia')
----+------------+-----------+--------+---------------+------------------+------------------
(1 row)
INSERT 0 1
----+------------+-----------+--------+---------------+------------------+------------------
(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', 'abisha@gmail.com', Date '2010-10-15', 'Ethiopia')
INSERT 0 1
----+------------+-----------+--------+---------------+------------------+------------------
(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', 'abisha@gmail.com', Date '2010-10-15', 'Ethiopia')
INSERT 0 1
(1 row)
test1=# \dt
List of relations
--------+----------+-------+----------
(4 rows)
DROP TABLE
test1=#
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
--------+-----------------+----------+----------
(7 rows)
test1=# \dt
List of relations
--------+----------+-------+----------
public | car | table | postgres
(4 rows)
----+------------+-----------+--------+----------------------+---------------+------------------+--------
(5 rows)
----+------------+--------------+-----------
(4 rows)
Table "public.person"
------------------+------------------------+-----------+----------+------------------------------------
car_id | bigint | | |
Indexes:
Foreign-key constraints:
UPDATE 1
----+------------+-----------+--------+----------------------+---------------+------------------+--------
(5 rows)
----+------------+--------------+-----------
(4 rows)
UPDATE 1
----+------------+-----------+--------+----------------------+---------------+------------------+--------
(5 rows)
## INNER JOINS
----+------------+-----------+--------+----------------------+---------------+------------------+--------
(5 rows)
----+------------+--------------+-----------
(4 rows)
----+------------+-----------+--------+----------------------+---------------+------------------+--------+----+------------+--------
------+-----------
(3 rows)
test1=# \x
-[ RECORD 1 ]----+---------------------
id |1
first_name | Addisu
last_name | Bole
gender | Male
email | addisub@hu.edu.et
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 | elu.getu03@gmail.com
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 | mershag@hu.edu.et
date_of_birth | 1956-01-12
country_of_birth | Ethiopia
car_id |3
id |3
make | Hyhonday
model | ltvyLsdhf
price | 89865.38
------------+------------+--------------+-----------
(3 rows)
##LEFT JOIN
----+------------+-----------+--------+----------------------+---------------+------------------+--------+----+------------+--------
------+-----------
(5 rows)
----+------------+-----------+--------+---------------------+---------------+------------------+--------
(2 rows)
----+------------+-----------+--------+---------------------+---------------+------------------+--------+----+------+-------+-------
(2 rows)
----+------------+------------+--------+--------------------------+---------------+------------------+--------
3 | Kidus | Menfese | Male | kidusm@aa.edu.et | 1988-01-12 | Ethiopia |
(12 rows)
----+------------+--------------+-----------
(12 rows)
UPDATE 1
----+------------+------------+--------+--------------------------+---------------+------------------+--------
(12 rows)
ERROR: update or delete on table "car" violates foreign key constraint "person_car_id_fkey" on table
"person"
###
\?
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
--------------------+-----------------+-------------------+----------------------------------------------------------------------
# UUID-ASS
it is globaly unique
CREATE EXTENSION
--------------------+-----------------+-------------------+----------------------------------------------------------------------
test1=# \df
List of functions
--------+--------------------+------------------+---------------------------+------
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
--------------------------------------+------------+-----------+--------+----------------------+---------------+------------------+---
------
(5 rows)
--------------------------------------+------------+--------------+-----------
(4 rows)
UPDATE 1
test1=# UPDATE person2 SET car_uid = 'd241e8a6-a0d5-490a-bded-36ef05118878' WHERE person_uid =
'14f2bfb7-a0a1-4ce7-8e2f-b7269db25813';
UPDATE 1
--------------------------------------+------------+-----------+--------+----------------------+---------------+------------------+---
-----------------------------------+--------------------------------------+------------+--------------+-----------
(3 rows)
-[ RECORD 1 ]----+-------------------------------------
person_uid | 0b63042f-8568-4a1d-8a40-f02f4d4fd90c
first_name | Addisu
last_name | Bole
gender | Male
email | addisub@hu.edu.et
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 | mershag@hu.edu.et
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 | elu.getu03@gmail.com
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
-[ 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 | addisub@hu.edu.et
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 | mershag@hu.edu.et
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 | elu.getu03@gmail.com
date_of_birth | 1992-01-12
country_of_birth | Ethiopia
make | Hyhonday
model | ltvyLsdhf
price | 89865.38
-[ 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 | addisub@hu.edu.et
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 | mershag@hu.edu.et
date_of_birth | 1956-01-12
country_of_birth | Ethiopia
make | Merichedis
model | Storeiaot
price | 678695.38
-[ RECORD 3 ]----+-------------------------------------
test1=# select * from person2
-[ RECORD 1 ]----+-------------------------------------
car_uid |
person_uid | 15afa68b-9a77-4d31-87cb-e4da37c13844
first_name | Zelalem
last_name | Bole
gender | Male
email | zelalem@astu.edu.et
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 | kidusm@aa.edu.et
date_of_birth | 1988-01-12
country_of_birth | Ethiopia
make |
model |
price |
"""" CONGRATULATION""""
HANDS ON LABORATORY