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

Week_12_SQL2

The document outlines the agenda for a week 12 class on SQL, covering topics such as creating tables, keys, JOINs, indexing, and transactions. It includes administrative details about labs, quizzes, and upcoming assignments related to SQL. Additionally, it provides links to presentation slides and resources for further learning about SQL and database management.

Uploaded by

Yash Soni
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Week_12_SQL2

The document outlines the agenda for a week 12 class on SQL, covering topics such as creating tables, keys, JOINs, indexing, and transactions. It includes administrative details about labs, quizzes, and upcoming assignments related to SQL. Additionally, it provides links to presentation slides and resources for further learning about SQL and database management.

Uploaded by

Yash Soni
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

SQL - part

2
COMP 1238 - week 12
Scratchpad
Presentation slides:
https://docs.google.com/presentation/d/1v-pP8UzmGm_aYh_4zcTXq4t01
axURBWkPu9xbPzhQ_U

Slido Questions:
https://app.sli.do/event/sNtCNvpTqVsDuMsm6HGf6K

AtKlass: VBTD
COMP1238 - Intro to Data Management

Relational DBs & SQL


part 2
sli.do/COM
P1238

Week 12 - Tue, Nov 19


No chatter &
Start at: inappropriate content
in Slido, plese

16:05
COMP 1238 – Introduction to
Data Management

Starting Zoom recording


Admin
● Labs with Akansha all in Zoom starting this week
● Quiz today, 2 attempts, open till end of weekend
● Lab this week will be very similar to last week
● Home assignment
○ About SQL (similar to last and this week’s labs)
○ To be released this week
○ Discuss it next week
○ Due in about 2 weeks
Agenda for today
● Creating tables - DDL
● Keys - Primary and Foreign
● JOINs
● Indexing for fast search
● INSERT DELETE UPDATE - CRUD
● ERDs & cardinality
● Transactions and ACID principles
● (?) Junction tables
What’s a database
Defining table structure
CREATE TABLE

ALTER TABLE

RENAME TABLE

DROP TABLE

This part of SQL is often called DDL = Data Definition Language


Creating tables
CREATE TABLE assignments (
id INTEGER PRIMARY KEY AUTOINCREMENT,
course_id TEXT NOT NULL,
title TEXT NOT NULL,
status TEXT NOT NULL,
due_date TEXT,
);
Primary key
● Primary key - unique identifier or id of a record in the table
● It can NOT repeated (must be unique)
● Can NOT be NULL (empty)
● Can consist of several columns
○ (course_id, assignment_number) could be a valid key for the assignments table
For example:
○ (COMP1238, 3) and (COMP1010, 3) are reasonable unique identifiers for
assignments
○ Year + semester number like “2024-3” is a good unique identifier for a semester
Foreign keys define the relations between tables
Foreign keys as integrity constraints
CREATE TABLE assignments (
id INTEGER PRIMARY KEY AUTOINCREMENT,
course_id TEXT NOT NULL,
title TEXT NOT NULL,
status TEXT NOT NULL,
due_date TEXT, -- "YYYY-MM-DD"
FOREIGN KEY (course_id)
REFERENCES courses(course_id)
ON DELETE CASCADE
);
Joining tables in SELECT
SELECT a.*, c.lab_time
FROM courses c
JOIN assignments a
ON a.course_id = c.course_id
lookup
We are constantly referring to
rows by IDs - lookup by ID
must be fast

Scanning a million rows is not


acceptable

“Index” is a special structure


that allows for fast lookup
without scanning the whole
table. It works a lot like the
letter index in paper address
books
What’s a database
Save
Update the DB
UPDATE courses
SET lab_time = 'Mon 9am'
WHERE course_id = 'COMP1238'
Transactions
Database transaction is a unit or
work that must always complete in
full or not at all.

Partial completion is not


acceptable
Transaction example
Transfer money between two accounts

Alice’s account Bob’s account

Before $500 $500

Transfer $100 Subtract $100 Add $100

After $400 $600


Example of isolation problem
Alice and Bob want to send $100 each to Charlie (he had $0)

By chance this happens at exactly the same moment

Alice and Bob both run


SELECT balance FROM accounts WHERE user=’Charlie’
Both get $0
Both run
UPDATE accounts SET balance = $100 WHERE user = ‘Charlie’
Charlie ends up with $100 instead of $200
Many-to-one relationship
As assignment can have exactly one course assigned but a course can
have many assignments - this is called many-to-one relationship
Many-to-many example
Order #123
Tables: Products, Orders
Product Qtty
● A product can appear in many orders
● An order can contain many products Pencil 6

● We also need to specify the quantity Erazer 3


of the product in the order
Many-to-many solution
CREATE TABLE order_items
(
order_id INTEGER,
product_id INTEGER,
qtty INTEGER,
comment TEXT,
);
erDiagram
orders {
INTEGER order_id PK
DATE order_date
TET Other_fields
}
products {
INTEGER product_id PK
STRING product_name
DECIMAL price
TEXT Other_fields

}
order_items {
INTEGER order_id
INTEGER product_id
INTEGER qtty
TEXT comment
}
orders ||--o{ order_items : contains
products ||--o{ order_items : includes
Objects or Entities
We often call our records Objects or Entities
● A course is an Entity
● An assignment is an Entity
ERD - Entity Relationship Diagrams
First 2 videos of this playlist
QUESTIONS ?
Links and references
● https://www.reddit.com/r/SQL/ - SQL Subreddit, highly recommended
● ERDs
○ Entity Relationship Diagram (ERD) Tutorial - Part 1 (relationships)
○ Entity Relationship Diagram (ERD) Tutorial - Part 2 (keys)
● SQL Joins (with extra drama to keep it engaging)
○ Part of a longer playlist about SQL by Socratica
● SQL Exercises

Extra material
● Fast and fairly advanced DB & SQL tutorial part 1, part 2 - indexing
● SQLite and its weird new fork “libSQL” - recent news about SQLite
● ACID Properties in Databases With Examples
DRAFTS

You might also like