Practical 1 – Introduction to PostgreSQL Learning Outcomes • Understand what PostgreSQL is. • Able to connect to PostgreSQL database server from a client application such as pgAdmin. • Able to load a sample database into the PostgreSQL database server. • Able to perform simple queries in PostgreSQL • SELECT, ORDER BY, WHERE, DISTINCT, IN, BETWEEN, LIKE, LIMIT Part 1 : What is PostgreSQL? PostgreSQL is a general purpose and object-relational database management system. PostgreSQL is free and open source software. It allows you to add custom functions developed using different programming languages such as C/C++, Java, etc. In PostgreSQL, you can define your own data types. Some featured companies who have built products and solutions using PostgreSQL are Apple, Fujitsu, Red Hat, Cisco, Juniper Network, etc. Part 2 : Connect to database using client pgAdmin Here, we will connect to PostgreSQL database server using pgAdmin GUI application. By using pgAdmin GUI application, you can interact with PostgreSQL database server via an intuitive user interface. The following illustrates how to connect to a database using pgAdmin: • First, launch the pgAdmin application. • Second, double-click the PostgreSQL 9.2 under the Servers item. pgAdmin will ask you for the password. Enter the password for the postgres user (password is postgres). Click OK button to log in to the PostgreSQL server. 1/10 • Third, choose the postgres database and click Execute Arbitrary SQL queries tool from the pgAdmin’s toolbar. • Fourth, enter the following statement: • Fifth, click the execute query button in the toolbar, pgAdmin will display the result in the output panel. Part 3 : Load a sample database – the DVD rental database The DVD rental database represents business processes of a DVD rental store. The DVD rental database has many objects including: 15 tables, 7 views, 8 functions, 1 domain, and 1 trigger. The 15 tables are as follows: Table name Brief description actor stores actors data including first name and last name. film stores films data such as title, release year, length, rating, etc. film_actor stores the relationships between films and actors. category stores film’s categories data. film_category stores the relationships between films and categories. store contains the stores data including manager staff and address. inventory stores inventory data. rental stores rental data. payment stores customer’s payments. 2/10 staff stores staff data. customer stores customers data. address stores address data for staff and customers. city stores the city names. country stores the country names. The relational database schema for the DVD Rental database is shown below. • Download the DVD Rental sample database from the LCMS page. The database file is in zipformat (dvdrental.zip) so you need to extract it to dvdrental.tar. 3/10 • In pgAdmin, right mouse click on the Databases and click the new Database.. menu item to create a new database. • Enter a name for the database and click the OK button. • Right mouse click on the dvdrental and choose the Restore… menu item. 4/10 • Provide the path to database file and click the Restore button • Wait for few seconds and click the Done button when the pgAdmin completes restoring the database. • Verify the loaded sample database 5/10 • Open the dvdrental database from object browser panel, you will see the tables in the schema and other database objects as the following picture: Part 4 : Explore server and database objects PostgreSQL provides many server and database objects. In order to leverage the features of each object that PostgreSQL provides effectively, it is important to understand what each object is and how to use it effectively. 6/10 Server service When you install a PostgreSQL instance, you will have a corresponding PostgreSQL server service. It is also known as PostgreSQL server. You can install multiple PostgreSQL servers on a physical server using different ports and having different locations to store data. Database A database is a container of other objects such as tables, views, functions, indexes, etc. You can create as many databases as you want inside a PostgreSQL server. Table The table is used to store the data. You can have many tables in a database. A special feature of PostgreSQL table is inheritance. Meanings a table (child table) can inherit from another table (parent table) so when you query data from the child table, the data from parent table is also showing up. 7/10 Schema A schema is a logical container of tables and other objects inside a database. Each PostgreSQL database may have multiple schemas. It is important to note that schema is a part of ANSI-SQL standard. Tablespace A tablespace is where PostgreSQL stores the data. PostgreSQL tablespace enables you to move your data to different physical location across drivers easily by using simple commands. By default, PostgreSQL provides two tablespaces: pg_defaultfor storing user’s data and pg_globalfor storing system data. View The view is a virtual table that is used to simplify complex queries and to apply security for a set of records. PostgreSQL also provides you with updatable views. 8/10 Function The function is a block reusable SQL code that returns a scalar value of a list of records. In PostgreSQL, functions can also return composite objects. Operator The operator is a symbolic function. PostgreSQL allows you to define custom operators. Cast Casts enable you to convert one data type into another data type. Casts actually backed by functions to perform the conversion. You can also create your own casts to override the default casting provided by PostgreSQL. Sequence Sequences are used to manage auto-increment columns that defined in a table as a serial column. Extension PostgreSQL introduced extension concept since version 9.1 to wrap other objects including types, casts, indexes, functions, etc into a single unit. The purpose of extensions is to make it easier to maintain. 9/10 Part 5 : Write and execute SQL queries 1. List names of all staff. 2. List names of all customers ordered by their last name. If two customers have the same last name, then the records are ordered by their first name. 3. List names of all customers whose last name starts with “Moo”. 4. List titles of all films whose description contains the word “panorama”. 5. List the different film categories. 6. List details of films whose length is less than 60 minutes. 7. List details of films whose length is between 60 and 120 minutes. 8. Give the number of films. 9. Give the number of japanese films. 10. List the title and release year of films starred actor Reese West. 11. List the title of films in the following four categories: Comedy, Action, Horror, Children 12. List the total amount paid by customer Douglas Graf. Source http://www.postgresqltutorial.com/ 10/10