parent
e9c9f8d3cc
commit
1cda7cceb1
@ -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;
|
||||||
|
|
@ -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;
|
@ -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
|
After Width: | Height: | Size: 16 KiB |
@ -0,0 +1,79 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Homework 1: Bird database</title>
|
||||||
|
<link rel="stylesheet" href="../style.css">
|
||||||
|
<style>
|
||||||
|
img {
|
||||||
|
border: 1px solid black;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<div id="page">
|
||||||
|
<h1>Homework 1: Bird database</h1>
|
||||||
|
<p>This homework gives pracice with the most important aspects of SQL we covered.</p>
|
||||||
|
|
||||||
|
<h2>Assignment</h2>
|
||||||
|
<p>
|
||||||
|
Write commands to implement the database represented in the class diagram below in SQLite. You can use <a href="https://sql.js.org/examples/GUI/">this SQL interpreter</a> or a local installation of SQLite, at your discretion. You will be submitting <strong>all of the SQL commands</strong> to implement the database, as described below.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<h2>Database design</h2>
|
||||||
|
<p>
|
||||||
|
<img src="class diagram.png" width="550"><br>
|
||||||
|
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).
|
||||||
|
<ul>
|
||||||
|
<li>Create all tables
|
||||||
|
<ul>
|
||||||
|
<li>Use a primary key for each table (a junction table should get a two-column primary key if each pairing should happen at most once)</li>
|
||||||
|
<li>Declare foreign key constraints where appropriate</li>
|
||||||
|
<li>Declare an index for the SpotterID foreign key column of Sightings</li>
|
||||||
|
</ul>
|
||||||
|
</li>
|
||||||
|
<li>Fill tables with initial data
|
||||||
|
<ul>
|
||||||
|
<li>Put in at least 6 actual bird species</li>
|
||||||
|
<li>Put in the regions: Midwest, Northeast, South, West</li>
|
||||||
|
<li>Put in 3 spotters (one of them should be JD - the Math/CS department head who is a birder)</li>
|
||||||
|
<li>Put in at least 4 sightings; at least two of them should be by JD</li>
|
||||||
|
<li>Associate each bird with the regions it actually lives in</li>
|
||||||
|
</ul>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
Include the following query commands:
|
||||||
|
<ul>
|
||||||
|
<li>Select the lat, lon, date, and species name for each sighting made by JD.</li>
|
||||||
|
<li>Pick some specific bird species and write a query that selects all region names in which it lives.</li>
|
||||||
|
</ul>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
Include the following update command:
|
||||||
|
<ul>
|
||||||
|
<li>Update some bird's name to its scientific name.</li>
|
||||||
|
</ul>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
Include the following delete command:
|
||||||
|
<ul>
|
||||||
|
<li>Delete a given spotter by ID and NULL out his/her ID in all of the associated sightings. Do this as a transaction and specify OR ROLLBACK for the commands therein.</li>
|
||||||
|
</ul>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p>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 <a href="https://www.gaia-gis.it/fossil/libspatialite/index">add-ons</a> and <a href="https://www.linkedin.com/pulse/what-geospatial-index-gaurav-pandey">workarounds</a> (though that is not required for this assignment).</p>
|
||||||
|
|
||||||
|
<hr>
|
||||||
|
<p>Submit a text document with all of your SQL commands in Educat, by November 6, 2024.</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
@ -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;
|
||||||
|
}
|
Loading…
Reference in new issue