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

Postgresql

This document provides a tutorial on getting started with PostgreSQL. It introduces three tables - books, publishers, and suppliers - as an example database. It demonstrates how to create and populate the tables, perform queries and aggregate functions, and relate data between tables by common fields. The tutorial also covers deleting, updating, and dropping tables as well as saving work and exiting PostgreSQL sessions.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views

Postgresql

This document provides a tutorial on getting started with PostgreSQL. It introduces three tables - books, publishers, and suppliers - as an example database. It demonstrates how to create and populate the tables, perform queries and aggregate functions, and relate data between tables by common fields. The tutorial also covers deleting, updating, and dropping tables as well as saving work and exiting PostgreSQL sessions.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Tutorial : PostgreSQL - Getting started

George Easaw

January 5, 2006

Abstract This is a simple PostgreSQL tutorial which will help you get started with PostgreSQL. For ner details pl refer to http://www.postgresql.org for the detailed documentation.

Example Case / Problem for Practical session on PostgreSQL RDBMS Let us work on a ctitious example. Books for the library are procured from dierent suppliers who in turn have arrangements with the publishers. We are thus working with three tables (classes) here, books, publishers and suppliers in the larger database called library database. Books table in the library are given the following attributes ( after normalising for second and third normalisation 1 2 ) accession number, title, authors,cost, copies, unit cost, purchase date, publisher code, supplier code. The Publisher table has publisher code, publisher name and publisher address. The supplier table has supplier code, name and address. It is assumed that you are working in the Mech Engg Software Lab and logging onto the server. ( if you are working at home, you need to load the postgreSQL software, available on the CVD itself) activate/start the postgreSQL server under services in x-windows in root. Logging In ssh [email protected], password: bemech123 su postgres, password : postgres123 Creating Database createdb librarydb If you wish to destroy the database, destroydb librarydb (less quotes) would do !! Entering interactive session psql librarydb Creating tables create table books(b accno int,b title varchar(20),b author varchar(20), b copies int, b cost real, b pdate date, b pcode int, b scode int); create table publishers(p code int, p name varchar(20), p address varchar(40));
private circulation among students of Informations Systems elective in the Mechanical Engineering / Industrial Engineering Departments in College of Engineering Goa. Distributed under Gnu Public Documentation License. This license gives you the right to use, distribute, store, record, transmit, retrieve, modify or even destroy the document in electronic or printed form without the written consent of the original author provided this same note is also kept on that document Dr. George Easaw teaches in the Mechanical Engineering Department at the College of Engineering, Goa 1 A key eld is an attribute which is a unique identier of a record. The second normal form says that no non-key eld can be a descriptor of a part of a multiple key eld. The third normal form says that no non-key eld can be a descriptor of another non-key eld. 2 The case of the letters is not important in PostgreSQL commands, except for the table names and eld names etc. A better practice is to do everything is lower case
For

create table suppliers(s code int, s name varchar(20), s address varchar(40)); (while designing tables take care to see that the same eld name is not repeated in another table, especially in the common elds for relating purpose.) Modifying / Altering structure of tables If you have to modify the design of the table or rename the eld names, use this. Alter table books rename column b pcode to b pubcode; If you want to alter the name of the table itself, alter table books rename to books1; Inserting / deleting / viewing records (Populating tables with records) values(1, Engg Mechanics, Fischer et al, 5, 180.50,01/21/2005, 3,2); delete from books where b accno = 3; delete from books; will delete all the records. (BE CAREFUL !!) update books set b name= PostgreSQL Guide where b accno=3; To check or query just type select * from books where b accno = 1 ; A simple select * from books; (less quotes) would give all the records entered. Entering large amounts of data data type etc. Create a le books input.txt in /tmp directory with the required insert into books

copy books from /tmp/books input.txt; Using Aggregate Functions select max(b cost) from books; gives the details of the book costing maximum. select avg(b cost) from books; gives the average cost of books. Relating between tables The beauty of RDBMS is that one can relate between dierent tables ( which is necessitated by the desire to reduce data redundancy and to improve data integrity, ie. after normalising the tables) as long as the dierent tables have one eld which is common to any other table, usually it is the codes. In our example it is the publisher code and supplier code in the books table which is common to the publisher code in the publisher table and the supplier code in the supplier table. If we need to retrieve the book name, authors, cost, publisher name and supplier name, we need to take the information from all the three dierent tables. select b name, b author, b cost, p name, s name, from books, publishers, suppliers where books.b pcode=publishers.p code and books.b scode=suppliers.s code; Deleting Tables drop books; Saving and Quitting There is no need to specially save any of the data as it is already saved. backslash q will quit the interactive session. exit will quit the postgres session and exit will quit from the remote connection to the mech engg server.

You might also like