As database admin for Postgres, you will need to log into the command line interface to create, update, and manage databases and their accompanying tables.
Logging into Postgres from the command line:
rob@laptop ~ $ psql -h localhost -U postgres
Password for user postgres:
psql (9.3.10)
SSL connection (cipher: DHE-RSA-AES256-GCM-SHA384, bits: 256)
Type "help" for help.
postgres=#
Postgres provides standard SQL commands for tables:
Postgres contains a variety of fields (columns). We'll be using the following types:
Postgres provides standard SQL commands for managing the data stored in tables. These commands do not change the structure of the tables.
STUDENTS | ||
---|---|---|
student_id | bigserial | |
first_name | varchar(100) | |
last_name | varchar(100) | |
GRADES | ||
student_id | bigint | |
course_id | bigint | |
grade | varchar(2) | |
COURSES | ||
course_id | bigserial | |
course_name | varchar(255) | |
course_description | varchar(1024) |
STUDENTS | ||
---|---|---|
PRIMARY KEY➞ | student_id | bigserial |
first_name | varchar(100) | |
last_name | varchar(100) |
STUDENTS | ||
---|---|---|
student_id | bigserial | |
first_name | varchar(100) | |
last_name | varchar(100) | |
GRADES | ||
PRIMARY KEY{ | student_id | bigint |
course_id | bigint | |
grade | varchar(2) |
STUDENTS | ||
---|---|---|
student_id | bigserial | |
first_name | varchar(100) | |
last_name | varchar(100) | |
GRADES | ||
student_id | bigint | |
course_id | bigint | |
grade | varchar(2) | |
COURSES | ||
PRIMARY KEY➞ | course_id | bigserial |
course_name | varchar(255) | |
course_description | varchar(1024) |
The yellow highlighted columns (fields) below form a relation. This relation ties the STUDENTS and GRADES tables together.
STUDENTS | ||
---|---|---|
student_id | bigserial | |
first_name | varchar(100) | |
last_name | varchar(100) | |
GRADES | ||
student_id | bigint | |
course_id | bigint | |
grade | varchar(2) | |
COURSES | ||
course_id | bigserial | |
course_name | varchar(255) | |
course_description | varchar(1024) |
The yellow highlighted columns (fields) below form a relation. This relation ties the GRADES and COURSES tables together.
STUDENTS | ||
---|---|---|
student_id | bigserial | |
first_name | varchar(100) | |
last_name | varchar(100) | |
GRADES | ||
student_id | bigint | |
course_id | bigint | |
grade | varchar(2) | |
COURSES | ||
course_id | bigserial | |
course_name | varchar(255) | |
course_description | varchar(1024) |
The WHERE clause is a powerful SQL command for defining a condition. This condition must be satisfied (evaluated to Boolean TRUE) before the appropriate action is taken. The WHERE clause can be applied to SELECT, INSERT, UPDATE, and DELETE commands. Examples:
The ORDER clause follows a WHERE clause. It only applies to SELECT statements. The ORDER clause provides a means of sorting the output in ASCending or DESCending order based on the columns SELECTed. Examples:
The DISTINCT clause is used in SELECT statements. It removes duplicate rows of data. Each row returned is thus unique or DISTINCT from each other. Example:
The BIGSERIAL datatype is an unsigned integer datatype that automatically increments its column value atomically when a new row of data is INSERTed. This is a very useful operation when the bigserial column needs to be unique and be the primary key. Primary keys, by definition, must be unique!
Since this operation is atomic, no race conditions can occur. This is very important when developing applications in highly scaled environments with many concurrent users!
Retrieving the last INSERTed serial number for an INSERT operation is easy. Here's how to do so:
Spring 2017, CS-160-03:
Lectures:
Assignments:
Handouts:
Programs: