Objectives:
Try the following activities answering all the questions. Turn in the files with your table creations as well as your written answers.
Here's a shell session captured using the Unix script
command.
It demonstrates basic usage of psql
, which is a PostgreSQL command
line client:
# Initially creating a database, connecting to it within psql, and # creating a table within the database. You always connect to the # username database the first time you connect. The syntax of psql is: # # psql <database name> <database username> # # See the man page for psql for more information. phoenix:~ * psql Password: Welcome to psql, the PostgreSQL interactive terminal. Type: \copyright for distribution terms \h for help with SQL commands \? for help on internal slash commands \g or terminate with semicolon to execute query \q to quit template1=> -- This is an SQL comment. template1=> -- template1=> -- Notice the nice help meta-commands above. template1=> -- template1=> create database jillz; CREATE DATABASE template1=> -- Note that SQL statements are terminated with template1=> -- a semicolon. If you forget, use \g. template1=> -- template1=> -- Connect to the database I just created. template1=> -- template1=> \c jillz You are now connected to database jillz. jillz=> create table example ( jillz(> id integer, jillz(> name char(50) ); CREATE jillz=> -- jillz=> -- About to exit jillz=> \q
Employee
with the following schema:
Attribute Name | Domain | Comment |
Id | INTEGER | Primary key |
Name | CHAR(20) | Not null |
Salary | INTEGER | Greater than zero |
Department | CHAR(4) |
It may take a few tries to get the syntax down, so create the SQL
statement in a text file and use \i <file>
in psql
to execute it.
Department
with the following schema:
Attribute Name | Domain | Comment |
DeptId | CHAR(4) | Primary key |
Budget | INTEGER | Default is $100 |
MngrID | INTEGER | References an Employee Id |
Use \i
again.
SELECT * FROM <table_name>;You'll need this to examine the results of your experiments.
Employee
:
INSERT INTO Employee (Id, Salary) VALUES (12345, 100000);What happened? Why?
Department
:
INSERT INTO Department VALUES ('CS', 5000,34567);What happened? Why?
DeptID
into
Department
. DROP TABLE Employee ; What happened? Why?
Drop your two tables: DROP TABLE Employee CASCADE; DROP TABLE Department;
Answer question 3.9a on p67
Use\q
to exitpsql
.