CIS 112, Dr. Ladd
spacebar
to go to the next slide, esc
/menu to navigate
creates rules for defining and manipulating databases.
but it’s not the same as Postgres.
Postgres is the program, SQL is the language.
Using a relational approach, data “objects” can inherit values from other objects.
date | item | price | quantity |
---|---|---|---|
8-11-2023 | Apple | $3.99 | 2 |
8-14-2023 | CokeZero | $4.99 | 3 |
8-14-2023 | Apple | $3.99 | 5 |
Always use quotes for attributes and names!
search_path
.You should do this at the beginning of every SQL script in NotePad++. (Save files with .sql
extension.)
Make sure you include data types, constraints, and primary keys.
DROP TABLE removes the table and the CASCADE argument removes objects that depend on the table.
Use the Postgres Documentation for a complete guide.
You’ve already done this with PRIMARY KEY and NOT NULL!
or
A few examples:
-- Add a column
ALTER TABLE "cost" ADD COLUMN "weight" FLOAT(4);
-- Drop a column
ALTER TABLE "purchase" DROP COLUMN "quantity";
-- Rename a column
ALTER TABLE "purchase" RENAME COLUMN "date" TO "day";
-- Add (or remove) a constraint
ALTER TABLE "purchase" ADD FOREIGN KEY ("item") REFERENCES "cost";
More in the Postgres documentation.
This will only work if the other columns can be NULL!
Enter the command for inserting data into the purchase table.
HINT: You may need to look at the Postgres documentation for data types.
We’ll learn a lot more about SELECT later on…
DELETE FROM "purchase";
will delete ALL the data without warning.Delete CokeZero from the cost
table.
-- Change one or many rows
UPDATE "purchase" SET "quantity" = 6 WHERE "quantity" = 5;
-- Change all rows based on original value
UPDATE "cost" SET "price" = "price" * 100;
-- Change more than one column
UPDATE "cost" SET "item" = 'Banana', "price" = 599 WHERE "item" = 'Apple';
All this and more in the Postgres documentation.
INSERT INTO "cost" VALUES ('CokeZero', 4.99) RETURNING "item";
UPDATE "cost" SET "price" = "price" * 100 RETURNING "price";
As always, more in the Postgres documentation.
You need them for column names, but also for capitalization, combining different tables, etc.
In its simplest form, you can use it to do simple arithmetic or to get all rows from a table.
Other forms of type casting:
"quantity"::NUMERIC::INTEGER
CAST("quantity" AS NUMERIC)
CAST(CAST("quantity" AS NUMERIC) AS INTEGER))
ORDER BY can also be used with arithmetic expressions and newly created columns!
Use OFFSET to start somewhere other than the beginning (which is row 0):
It’s recommended to use LIMIT with ORDER BY always!
SELECT queries: https://www.postgresql.org/docs/14/queries-overview.html
ORDER BY and AS: https://www.postgresql.org/docs/14/queries-order.html
LIMIT and OFFSET: https://www.postgresql.org/docs/14/queries-limit.html
Remember to add type casts as needed!
You can use the full list from the Postgres documentation!
Use HAVING to limit groups.
https://www.postgresql.org/docs/14/queries-table-expressions.html#QUERIES-GROUP
There are several expressions for subqueries in the documentation.
Use foreign keys to guide you! This is ideal for optional relations.
Write queries to answer the following for the “music” schema:
The basics of JOIN syntax:
https://www.postgresql.org/docs/14/tutorial-join.html
Details on different JOIN statements:
https://www.postgresql.org/docs/14/queries-table-expressions.html#QUERIES-FROM
Consider the inner part of a venn diagram. Only the overlapping part is returned.
Let’s try to make venn diagrams for LEFT, RIGHT, and FULL OUTER JOINs.
Match foreign keys with ON or with USING.
You can do this without AS, too.