diff --git a/Day 24-27 (database)/indexes.txt b/Day 24-27 (database)/indexes.txt new file mode 100644 index 0000000..f2b7494 --- /dev/null +++ b/Day 24-27 (database)/indexes.txt @@ -0,0 +1,39 @@ +Indexes +======= +* An index is a data structure (usually a B-tree) for fast column lookups. + +* You don't need to (and probably shouldn't) put an index on every column, but if for example a column is used in the WHERE clause of an SQL statement, you should consider adding an index to that column. + +* If a table is not indexed, queries may go slow because the database may have to do a full table scan (i.e. linear search). + +CREATE TABLE RandomData( + ID INTEGER PRIMARY KEY, + Num FLOAT NOT NULL +); + +INSERT INTO RandomData(Num) VALUES (RANDOM()); +INSERT INTO RandomData(Num) VALUES (RANDOM()); +INSERT INTO RandomData(Num) VALUES (RANDOM()); +INSERT INTO RandomData(Num) VALUES (RANDOM()); +INSERT INTO RandomData(Num) VALUES (RANDOM()); +INSERT INTO RandomData(Num) VALUES (RANDOM()); +INSERT INTO RandomData(Num) VALUES (RANDOM()); + +SELECT * FROM RandomData; + +INSERT INTO RandomData(Num) SELECT a.Num/b.Num AS Num FROM RandomData AS a JOIN RandomData AS b ON a.ID != b.ID; +INSERT INTO RandomData(Num) SELECT a.Num/b.Num AS Num FROM RandomData AS a JOIN RandomData AS b ON a.ID != b.ID; +INSERT INTO RandomData(Num) SELECT a.Num/b.Num AS Num FROM RandomData AS a JOIN RandomData AS b ON a.ID != b.ID; + +SELECT COUNT(*) FROM RandomData; +SELECT * FROM RandomData WHERE ID=1234567; /* SQLite automatically puts an index on PRIMARY KEY and UNIQUE columns */ +SELECT * FROM RandomData WHERE Num>=.998 AND Num<=.999; /* Not indexed; it takes a second. */ + +CREATE INDEX Idx_RandomData_Num ON RandomData(Num); /* Naming convention for an index: Idx_TableName_ColumnName; */ + +SELECT * FROM RandomData WHERE Num>=.998 AND Num<=.999; + +DROP INDEX Idx_RandomData_Num; + +SELECT * FROM RandomData LIMIT 10; + diff --git a/Day 24-27 (database)/transactions.txt b/Day 24-27 (database)/transactions.txt new file mode 100644 index 0000000..77f09c0 --- /dev/null +++ b/Day 24-27 (database)/transactions.txt @@ -0,0 +1,53 @@ +Transactions +============ +A transaction is a sequence of statements that are treated as one unit; either all of them occur or none of them. + +Syntax and features vary somewhat based on the DBMS. + +Classic example: moving money from one bank account to another. + +Autocommit mode: + Every statement is treated as a transaction of its own. + Most DBMSs have autocommit mode on by default. + +To do a multi-statement transaction: BEGIN...COMMIT + +Systems support various means to explicitly ROLLBACK a transaction using if statements, etc. + +SQLite Example: +--------------- +CREATE TABLE Customers( + CustomerID INTEGER PRIMARY KEY, + Name TEXT NOT NULL +); + +CREATE TABLE Accounts( + AccountID INTEGER PRIMARY KEY, + CustomerID INTEGER NOT NULL, + Balance INTEGER NOT NULL, + FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID), + CHECK (Balance >= 0) /* Enforces a constraint that no account may be overdrawn. */ +); + +INSERT INTO Customers (Name) VALUES("DrKow"); +INSERT INTO Accounts (CustomerID, Balance) VALUES(1,1000); +INSERT INTO Accounts (CustomerID, Balance) VALUES(1,2000); + +/* This statement is a single-line transaction; since it failed for one row, it will not modify any rows. */ +UPDATE Accounts SET Balance = Balance - 1500; + +BEGIN; + UPDATE OR ROLLBACK Accounts SET Balance = Balance + 50 WHERE AccountID=1; + UPDATE OR ROLLBACK Accounts SET Balance = Balance / 0 WHERE AccountID=1; /* This makes it crash, but the transaction is safely rolled back. */ + UPDATE OR ROLLBACK Accounts SET Balance = Balance - 50 WHERE AccountID=2; +COMMIT; + +/* The following command will also be rolled back, because the non-negative balance constraint would be violated. */ +BEGIN; + UPDATE OR ROLLBACK Accounts SET Balance = Balance + 5000 WHERE AccountID=1; + UPDATE OR ROLLBACK Accounts SET Balance = Balance - 5000 WHERE AccountID=2; +COMMIT; + +/* Note that the "OR ROLLBACK" part is required in SQLite; if you run the command above without it, 5000 will be added to one account without deducting it from the other. */ + +SELECT Name,AccountID,Balance FROM Customers JOIN Accounts ON Customers.CustomerID=Accounts.CustomerID; diff --git a/Homework/01/birdDatabase.puml b/Homework/01/birdDatabase.puml new file mode 100644 index 0000000..7a8fd96 --- /dev/null +++ b/Homework/01/birdDatabase.puml @@ -0,0 +1,35 @@ +'Created with https://www.planttext.com/ + +@startuml + +skin rose + +title Bird database + +class Species { + SpeciesID + Name +} + +class Regions { + RegionID + Name +} + +class Sightings { + SightingID + Latitude + Longitude + Date +} + +class Spotters { + SpotterID + Name +} + +Regions -right- Species: "0..* 0..*" +Species -right- Sightings: "1 0..*" +Sightings -right- Spotters: "0..* 0..1" + +@enduml diff --git a/Homework/01/class diagram.png b/Homework/01/class diagram.png new file mode 100644 index 0000000..8c5d3d1 Binary files /dev/null and b/Homework/01/class diagram.png differ diff --git a/Homework/01/index.html b/Homework/01/index.html new file mode 100644 index 0000000..2f148ff --- /dev/null +++ b/Homework/01/index.html @@ -0,0 +1,79 @@ + + + + + + + + Homework 1: Bird database + + + + + +
+

Homework 1: Bird database

+

This homework gives pracice with the most important aspects of SQL we covered.

+ +

Assignment

+

+ Write commands to implement the database represented in the class diagram below in SQLite. You can use this SQL interpreter or a local installation of SQLite, at your discretion. You will be submitting all of the SQL commands to implement the database, as described below. +

+ +

Database design

+

+
+ Implement the database as pictured above. Note that the many-to-many relationship between Species and Regions will require a junction table. Write the following in SQL (invent data as necessary). +

+

+ +

Note that in real life, it would likely be useful to have a geospatial index on latitude and longitude. SQLite doesn't support this, but there are add-ons and workarounds (though that is not required for this assignment).

+ +
+

Submit a text document with all of your SQL commands in Educat, by November 6, 2024.

+
+ + + + + \ No newline at end of file diff --git a/Homework/style.css b/Homework/style.css new file mode 100644 index 0000000..4387f32 --- /dev/null +++ b/Homework/style.css @@ -0,0 +1,75 @@ +.tab { + padding-left: 15px; +} + +body { + background-color: #ffffb0; + margin: 0; + padding: 0; +} + +#page { + margin-left: 30px; + margin-right: 30px; + margin-top: 10px; + margin-bottom: 10px; + background-color: #fffff0; + padding: 7px; + border-style: double; + border-color: #000000; + border-width: 3px; +} + +.underline { + text-decoration: underline; +} + +img { + max-width: 100%; + height: auto; + border: 1px solid black; +} + +/* This is so code samples look reasonable */ +pre { + -moz-tab-size: 4; + -o-tab-size: 4; + tab-size: 4; + overflow-x: auto; + border-width: 1px; + border-color: black; + border-style: solid; + background-color: white; + padding: 4px; +} + +blockquote { + border-width: 1px; + border-color: black; + border-style: solid; + background-color: white; + padding: 4px; +} + +.output { + border-width: 3px; + border-style: double; + border-color: white; + background-color: #000000; + color: #ffffff; + padding: 3px; +} + +img { + max-width: 100%; +} + +table { + border-collapse: collapse; +} + +td, th { + border: 1px solid black; + padding: 4px; + background-color: white; +} \ No newline at end of file diff --git a/Labs/02/index.html b/Labs/02/index.html index 3d11820..004e8d7 100644 --- a/Labs/02/index.html +++ b/Labs/02/index.html @@ -43,6 +43,7 @@