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 @@ + + + +
+ + + +This homework gives pracice with the most important aspects of SQL we covered.
+ ++ 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. +
+ +
+
+ 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.
+git commit
- makes a new commit.git branch branchName
- makes a new branch.git branch -f branchName target
- forces the given branch to move to the target commit or branch.git checkout branchName
- changes to the given branch.git checkout -b branchName
- creates and changes to the given branch.git merge branchName
- merges the given branch in with the current one.