CENG301 DBMS - Session-7
CENG301 DBMS - Session-7
Session-7
Asst. Prof. Mustafa YENIAD
[email protected]
PostgreSQL Database Objects
Before access database data through SQL, let's get familiar with PostgreSQL Database
Objects:
• Schemas: Schemas are part of the ANSI SQL standard. They are the immediate next level
DBMS
of organization within each database. If you think of the database as a country, schemas
would be the individual states (or provinces, prefectures, or departments, depending on
the country).
• Most database objects first belong to a schema, which belongs to a database.
• When you create a new database, PostgreSQL automatically creates a schema named
public to store objects that you create.
• If you have few tables, using public would be fine but if you have thousands of tables,
you should organize them into different schemas.
Database Query - PostgreSQL Database Objects
• Tables: Tables are the workhorses of any database. In PostgreSQL, tables are first citizens of their
respective schemas, which in turn are citizens of the database. PostgreSQL tables have two
remarkable talents:
• First, they are inheritable. Table inheritance streamlines your database design and can save
you endless lines of looping code when querying tables with nearly identical structures.
• Second, whenever you create a table, PostgreSQL automatically creates an accompanying
custom data type.
DBMS
• Views: Almost all RDMS products offer views as a level of abstraction from tables.
• In a view, you can query multiple tables and present additional derived columns based on
complex calculations.
• Views are generally read-only, but PostgreSQL allows you to update the underlying data by
updating the view, provided that the view draws from a single table.
• To update data from views that join multiple tables, you need to create a trigger against
the view.
• PostgreSQL version 9.3 introduced materialized views*, which cache data to speed up
commonly used queries at the sacrifice of having the most up-to-date data.
• For example, if you need advanced text search in only one database, enable
fuzzystrmatch for that one only.
• When you enable extensions, you choose the schemas where all constituent objects will
reside. Accepting the default will place everything from the extension into the public schema,
littering it with potentially thousands of new objects
• It is recommended that you create a separate schema that will house all extensions.
• For an extension with many objects, it is suggested that you create a separate schema
devoted entirely to it.
• Optionally, you can append the name of any schemas you add to the search_path
variable of the database so you can refer to the function without having to prepend the
schema name.
Database Query - PostgreSQL Database Objects
• Functions: You can code your own custom functions to handle data manipulation,
perform complex calculations, or wrap similar functionality.
• PostgreSQL comes stocked with thousands of functions.
• PostgreSQL functions can return scalar values, arrays, single records, or sets of
records.
DBMS
• Operators: Operators are nothing more than symbolically named aliases such as = or &&
for
functions (you can invent your own).
Database Query - PostgreSQL Database Objects
• Foreign tables and foreign data wrappers: Foreign tables are virtual tables linked to data
outside a PostgreSQL database. Foreign data wrappers (FDWs) facilitate the magic
handshake between PostgreSQL and external data sources.
• Triggers and trigger functions: You will find triggers in all enterprise-level databases;
triggers detect datachange events. A trigger can run in response to particular types of
DBMS
statements or in response to changes to particular rows, and can fire before or after a
data-change event. In pgAdmin, to see which table triggers, drill down to the table level.
Pick the table of interest and look under triggers.
Trigger functions differ from regular functions in that they have access to special variables
that store the data both before and after the triggering event. This allows you to reverse
data changes made by the event during the execution of the trigger function. Because of
this, trigger functions are often used to write complex validation routines that are beyond
what can be implemented using check constraints.
Database Query - PostgreSQL Database Objects
• Catalogs: Catalogs are system schemas that store PostgreSQL builtin functions and
metadata. Every database contains two catalogs: pg_catalog, which holds all
functions, tables, system views, casts, and types packaged with PostgreSQL; and
information_schema, which offers views exposing metadata in a format dictated by
the ANSI SQL standard.
DBMS
All settings to finetune servers are kept in system tables that you are free to query
and modify. Go ahead and take a close look inside the pg_catalog schema. If you have
superuser privileges, you are at liberty to make updates to the pg_catalog directly.
The most commonly used views in the PostgreSQL information_schema are columns,
which list all table columns in a database; tables, which list all tables (including views)
in a database; and views, which list all views and the associated SQL to rebuild the
view.
Database Query - PostgreSQL Database Objects
• Types: Type is short for data type.
Every database product and every programming language has a set of types
that it understands: integers, characters, arrays, blobs, etc.
examples.
• Review: https://www.postgresql.org/docs/current/datatype.html
Database Query - PostgreSQL Database Objects
• Full text search (FTS): Full text search (FTS) is a natural language–
based search. This kind of search has some "intelligence" built in.
Unlike regular expression search, FTS can match based on the
semantics of an expression, not just its syntactical makeup. For
example, if you’re searching for the word running in a long piece of
DBMS
text, you may end up with run, running, ran, runner, jog, sprint,
dash, and so on…
Database Query - PostgreSQL Database Objects
• Casts: Casts prescribe how to convert from one data type to another. They are backed by
functions that actually perform the conversion. In PostgreSQL, you can create your own
casts and override or enhance the default casting behavior. For example, imagine you’re
converting zip codes (which are five digits long in the US) to character from integer. You
can define a custom cast that automatically prepends a zero when the zip is between
1000 and 9999.
DBMS
• Rules: Rules are instructions to rewrite an SQL prior to execution. Actually, there is not so
necessary to cover rules as they have fallen out of favor because triggers can accomplish
the same things.
Where to get help
• There will come a day when you need help (that day always arrives early).
• The favorite is the lively mailing list designed for helping all users with technical issues:
• If you are new to PostgreSQL, the best list to start with is the PGSQL General Mailing List.
• If you run into what appears to be a bug in PostgreSQL, report it at PostgreSQL Bug Reporting.
PostgreSQL Sample DB
• We will use the dvdrental (can be downloaded from AYBUZEM'
CENG301 course page) database to demonstrate the features of
PostgreSQL.
• ER Model diagram for the dvdrental DB is on the right:
• The DVD rental database includes:
• 15 tables
• 1 trigger
• 7 views
• 8 functions
• 1 domain
DBMS
• 13 sequences
• Additional info: Notation of ER diagram
• Cardinality refers to maximum number of times an instance in one
entity can relate to instances of another entity.
Figure: Relationship cardinality
Database Query - SQL
• As a remember, SQL stands for Structured Query Language; the term defines a
declarative programming language.
• As a user, we declare the result we want to obtain in terms of a data processing
pipeline that is executed against a known database model and a dataset.
• When working with SQL, as a developer we relatedly work with a type system and a
kind of relational algebra. We write code to retrieve and process the data we are
DBMS
"I will, in fact, claim that the difference between a bad programmer and a good one is
whether he considers his code or his data structures more important.
Bad programmers worry about the code.
Good programmers worry about data structures and their relationships." (Linus Torwalds)
Database Query - SQL
• Anyway, SQL is very popular and it is used in most applications written today.
• Every developer has seen some "SELECT … FROM … WHERE …" SQL query string in
one form or another and knows some parts of the very basics from SQL’89.
• Relational DBMS (RDMS) and SQL are forcing developers to think in terms of data
DBMS
structure, and to declare both the data structure and the data set we want to
obtain via our queries.
• The current SQL standard is SQL’2016 and it includes many advanced data
processing techniques. If your application is already using the SQL programming
language and SQL engine, then as a developer it’s important to fully understand
how much can be achieved in SQL, and what service is implemented by this run-
time dependency in your software architecture.
Database Query - SELECT
• One of the most common tasks, when you work with the database, is to query data from
tables by using the SELECT (one of the most complex) statement.
• Because of its complexity, we will break it down into many shorter and easy-to-
understand information so that you can learn about each clause faster:
• If you specify a list of columns, you need to place a comma (,) between two columns to separate them.
• If you want to select data from all the columns of the table, you can use an asterisk (*) shorthand
instead of specifying all the column names.
• The select list may also contain expressions or literal values.
2. Specify the table_name from which you want to query data after the FROM keyword.
• The FROM clause is optional. If you do not query data from any table, you can omit it.
• The semicolon (;) is not a part of the SQL statement. It is used to signal PostgreSQL the end of an SQL
statement ans is also used to separate two SQL statements.
• Note: SQL keywords are case-insensitive. It means that SELECT is equivalent to select or Select.
By convention, we will use all the SQL keywords in uppercase to make the queries easier to be read.
Database Query - SELECT
• Using SELECT statements to query data from dvdrental DB:
SELECT last_name FROM customer; # get the last names of all customers from the customer table
SELECT first_name, last_name, email
FROM customer; # query data from multiple columns example
SELECT * FROM customer; # to get all data from all columns of the given table
DBMS
• Important: It is not a good practice to use the asterisk (*) in the SELECT statement when you embed SQL statements in the application code
(Both the DB and the application will be slower to respond and less scalable).
SELECT email e-posta FROM customer; # make it shorter by removing the AS keyword
Database Query - SELECT
• Various arithmetic operations which can be done on PostgreSQL table
DBMS
SELECT first_name, last_name FROM actor ORDER BY last_name ASC; # sort by last names in ascending order
SELECT title, release_year FROM film ORDER BY title DESC; # sort by last names in ascending order
DBMS
SELECT first_name, last_name FROM customer ORDER BY first_name ASC last_name DESC;
SELECT first_name, LENGTH(first_name) len FROM actor ORDER BY len DESC; # sort by the lengths of the first name
SELECT actor_id, first_name, last_name FROM actor ORDER BY RANDOM(); # list all records by random order
• The DISTINCT clause is used in the SELECT statement to remove if duplicate rows from a result set:
SELECT DISTINCT
column_name
FROM table_name;
Database Query - SELECT
• The SELECT statement returns all rows from the tables.
To select rows that a specified condition, you use a WHERE clause:
SELECT select_list
FROM table_name
WHERE condition
ORDER BY sort_expression ASC | DESC | RANDOM()
DBMS
• To form the condition in the WHERE clause, you use comparison and logical operators:
Database Query - SELECT
SELECT first_name, last_name FROM customer WHERE last_name = 'Rice' AND first_name = 'Jamie' ;
SELECT first_name, last_name FROM customer WHERE first_name = 'Rice' OR first_name = 'Adam' ;
• If you want to match a string with any string in a list, you can use the IN operator:
SELECT first_name, last_name FROM customer WHERE first_name IN ('Adam','Ann','Rice');
DBMS
• To find a string that matches a specified pattern, you use the LIKE operator:
• The following example returns all customers whose first names start with the letter A:
• Find the customers whose first names start with Bra and last names are not Motley:
SELECT first_name, last_name FROM customer
WHERE first_name LIKE 'Bra%'
AND last_name <> 'Motley';
Note: You can use the != (exclamation mark and equal) operator and <> (less and greater) operator interchangeably.
Database Query - SELECT
• LIMIT is an optional clause of the SELECT statement that constrains the number of rows returned:
SELECT select_list
FROM table_name
WHERE condition
ORDER BY sort_expression ASC | DESC | RAMDOM()
DBMS
LIMIT row_count
• In case you want to skip a number of rows before returning the row_count rows, you use OFFSET clause
placed after the LIMIT clause as the following statement:
SELECT select_list
FROM table_name
WHERE condition
ORDER BY sort_expression ASC | DESC | RAMDOM()
LIMIT row_count OFFSET row_to_skip
Database Query - SELECT
• Get the last ten films sorted by release_year:
SELECT film_id, title, release_year FROM film
ORDER BY release_year DESC
LIMIT 10;
• To retrieve 5 films starting from the third one ordered by film_id:
SELECT film_id, title, release_year FROM film
DBMS
ORDER BY film_id
LIMIT 5 OFFSET 3;
Note: The LIMIT clause is widely used by many relational database management systems however, the LIMIT
clause is not a SQL-standard. To conform it with the SQL standard, PostgreSQL supports the FETCH clause {
FIRST | NEXT } [ row_count ] { ROW | ROWS } ONLY to retrieve a number of rows returned by a query:
• Select the first film sorted by titles in ascending order:
SELECT film_id, title FROM film ORDER BY title FETCH FIRST ROW ONLY;
• Select the first seven films sorted by titles:
SELECT film_id, title FROM film ORDER BY title FETCH FIRST 7 ROW ONLY;
Database Query - SELECT
• You use IN or NOT IN operators in the WHERE clause to check if a value matches any value in a list of values.
• Suppose you want to know the rental information of customer id 4 and 7:
• To get the total number of addresses (the total number of records), you use the COUNT(*) function as follows:
SELECT COUNT(*) FROM address;
• The COUNT() function can be applied to a specific column (returns the total count of the non-null values in that column):
SELECT COUNT(address2) FROM address;
• To calculate the average replacement cost of all films:
SELECT AVG(replacement_cost) FROM film;
• To calculate the minimum and maximum cost:
SELECT MIN(replacement_cost), MAX(replacement_cost) FROM film;
Database Query - SELECT - GROUP BY
• The GROUP BY clause divides the rows returned from the SELECT statement into groups.
For each group, you can apply an aggregate function e.g., SUM() to calculate the sum of items or
COUNT() to get the number of items in the groups.
SELECT column_1, column_2, ..., aggregate_function(column_3)
FROM table_name
GROUP BY column_1, column_2, …;
DBMS
• You can also use the GROUP BY clause without applying an aggregate function.
The following query gets data from the payment table and groups the result by customer id:
SELECT customer_id FROM payment GROUP BY customer_id;
• The GROUP BY clause is useful when it is used in conjunction with an aggregate function.
For example, to select the total amount that each customer has been paid, you use the GROUP BY clause to
divide the rows in the payment table into groups grouped by customer id. For each group, you calculate the
total amount using the SUM() function:
SELECT customer_id, SUM(amount) FROM payment GROUP BY customer_id;
• The same query with ORDER BY total amount :
SELECT customer_id, SUM(amount) FROM payment GROUP BY customer_id ORDER BY SUM(amount);
Database Query - SELECT - GROUP BY
• To find the number of payment transactions that each staff has processed, you group the rows in the payment table by
the values in the staff_id column and use the COUNT() function to get the number of transactions:
SELECT staff_id,
COUNT(payment_id)
FROM payment
GROUP BY staff_id;
• The following example uses multiple columns in the GROUP BY clause.
DBMS
(In this example, the GROUP BY clause divides the rows in the payment table by the values in the customer_id and staff_id columns. For
each group of (customer_id, staff_id), the SUM() calculates the total amount)
SELECT customer_id, staff_id, SUM(amount)
FROM payment
GROUP BY staff_id, customer_id
ORDER BY customer_id;
• Using PostgreSQL GROUP BY clause with a date column:
The payment_date is a timestamp column. To group payments by dates, you use the DATE() function to convert timestamps to dates first
and then group payments by the result date:
SELECT DATE(payment_date) AS paid_date, SUM(amount) AS sum_paid
FROM payment
GROUP BY DATE(payment_date)
ORDER BY sum_paid DESC;
Database Query - CAST (to Convert Types)
• There are many cases that you want to convert a value of one data type into another. PostgreSQL provides
you with the CAST operator that allows you to do this.
• To find the contact who does not have a phone number you may come up with the following statement:
SELECT customer_id
FROM rental
DBMS