Skip to content

Commit

Permalink
codespell and black
Browse files Browse the repository at this point in the history
  • Loading branch information
MilagrosMarin committed May 13, 2024
1 parent 1860626 commit 29ea931
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 134 deletions.
26 changes: 14 additions & 12 deletions db-course/003-ForeignKeys.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Referential integrity"
"# Referential integrity\n"
]
},
{
Expand All @@ -13,40 +13,41 @@
"source": [
"## Referential integrity\n",
"\n",
"* Correcting matching of corresponding entities across the schema\n",
"* Relies on entity integrity\n",
"* Enfornced by foreign keys\n",
"- Correcting matching of corresponding entities across the schema\n",
"- Relies on entity integrity\n",
"- Enforced by foreign keys\n",
"\n",
"## Foreign keys\n",
"A foreign key is a column or several columns in the child table referencing the primary key column(s) in the parent table.\n",
"\n",
"* More generally, foreign keys can reference other sets of columns than the primary key. However, in common practice and in this class foreign keys will always reference the primary key in the referenced table.\n",
"A foreign key is a column or several columns in the child table referencing the primary key column(s) in the parent table.\n",
"\n",
"- More generally, foreign keys can reference other sets of columns than the primary key. However, in common practice and in this class foreign keys will always reference the primary key in the referenced table.\n",
"\n",
"## Effects of a foreign key constraint\n",
"\n",
"1. Restrict inserts into the child table if there is no match in parent.\n",
"2. Restrict deletes (and updates of primary key values) from the parent table when there is a match in child.\n",
"3. An index is created in the child table to speed up searches on the foreign key.\n",
"\n",
"As a result, the child table is prevented from having values in its foreign keys columns in the absence of entries in the parent table with matching primary key values.\n",
"\n",
"Importantly, unlike other types of links in other data models, no actual link is created between individual rows of both tables. Referential integrity is maintained by restricting dta manipulations."
"Importantly, unlike other types of links in other data models, no actual link is created between individual rows of both tables. Referential integrity is maintained by restricting dta manipulations.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Diagramming notation \n",
"## Diagramming notation\n",
"\n",
"- Entity-relationship diagram"
"- Entity-relationship diagram\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Examples"
"## Examples\n"
]
},
{
Expand Down Expand Up @@ -420,10 +421,11 @@
"metadata": {},
"source": [
"## Foreign keys have 4 effects\n",
"\n",
"0. The primary key of the parent becomes part of the child definition (the foreign key)\n",
"1. Restrict inserts into child table if no match in parent\n",
"2. Restrict deletes from parent if there is a matching child \n",
"3. Create an index in child to make searches fast on the value of the FK value."
"2. Restrict deletes from parent if there is a matching child\n",
"3. Create an index in child to make searches fast on the value of the FK value.\n"
]
},
{
Expand Down
42 changes: 21 additions & 21 deletions db-course/003-Indexes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@
"\n",
"In DataJoint, indexes are created by one of the three mechanisms:\n",
"\n",
"1. Primary key \n",
"2. Foreign key \n",
"1. Primary key\n",
"2. Foreign key\n",
"3. Explicitly defined indexes\n",
"\n",
"The first two mechanisms are obligatory. Every table has a primary key, which serves as an unique index. Therefore, restrictions by a primary key are very fast. Foreign keys create additional indexes unless a suitable index already exists."
"The first two mechanisms are obligatory. Every table has a primary key, which serves as an unique index. Therefore, restrictions by a primary key are very fast. Foreign keys create additional indexes unless a suitable index already exists.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's test this principle. Let's create a table with a 10,000 entries and compare lookup times:"
"Let's test this principle. Let's create a table with a 10,000 entries and compare lookup times:\n"
]
},
{
Expand Down Expand Up @@ -61,7 +61,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's say a mouse in the lab has a lab-specific ID but it also has a separate id issued by the animal facility."
"Let's say a mouse in the lab has a lab-specific ID but it also has a separate id issued by the animal facility.\n"
]
},
{
Expand Down Expand Up @@ -266,25 +266,25 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The indexed searches are much faster!"
"The indexed searches are much faster!\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To make searches faster on fields other than the primary key or a foreign key, you can add a secondary index explicitly. \n",
"To make searches faster on fields other than the primary key or a foreign key, you can add a secondary index explicitly.\n",
"\n",
"Regular indexes are declared as `index(attr1, ..., attrN)` on a separate line anywhere in the table declration (below the primary key divide). \n",
"Regular indexes are declared as `index(attr1, ..., attrN)` on a separate line anywhere in the table declration (below the primary key divide).\n",
"\n",
"Indexes can be declared with unique constraint as `unique index (attr1, ..., attrN)`."
"Indexes can be declared with unique constraint as `unique index (attr1, ..., attrN)`.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's redeclare the table with a unique index on `tag_id`."
"Let's redeclare the table with a unique index on `tag_id`.\n"
]
},
{
Expand Down Expand Up @@ -430,7 +430,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Now both types of searches are equally efficient!"
"Now both types of searches are equally efficient!\n"
]
},
{
Expand Down Expand Up @@ -469,15 +469,15 @@
"source": [
"%%timeit -n6 -r3\n",
"\n",
"# efficient! Uses the seconary index on tag_id\n",
"# efficient! Uses the secondary index on tag_id\n",
"(Mouse2() & {\"tag_id\": random.randint(0, 999_999)}).fetch()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's now imagine that rats in the `Rat` table are identified by the combination of lab the `lab_name` and `rat_id` in each lab:"
"Let's now imagine that rats in the `Rat` table are identified by the combination of lab the `lab_name` and `rat_id` in each lab:\n"
]
},
{
Expand Down Expand Up @@ -656,14 +656,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that dispite the fact that `rat_id` is in the index, search by `rat_id` alone are not helped by the index because it is not first in the index. This is similar to search for a word in a dictionary that orders words alphabetically. Searching by the first letters of a word is easy but searching by the last few letters of a word requires scanning the whole dictionary."
"Note that despite the fact that `rat_id` is in the index, search by `rat_id` alone are not helped by the index because it is not first in the index. This is similar to search for a word in a dictionary that orders words alphabetically. Searching by the first letters of a word is easy but searching by the last few letters of a word requires scanning the whole dictionary.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this table, the primary key is a unique index on the combination `(lab_id, rat_id)`. Therefore searches on these attributes or on `lab_id` alone are fast. But this index cannot help searches on `rat_id` alone:"
"In this table, the primary key is a unique index on the combination `(lab_id, rat_id)`. Therefore searches on these attributes or on `lab_id` alone are fast. But this index cannot help searches on `rat_id` alone:\n"
]
},
{
Expand Down Expand Up @@ -730,7 +730,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Pattern searches in strings can benefit from an index when the starting characters are specified."
"Pattern searches in strings can benefit from an index when the starting characters are specified.\n"
]
},
{
Expand Down Expand Up @@ -777,7 +777,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Similarly, searching by the date requires an inefficient full-table scan:"
"Similarly, searching by the date requires an inefficient full-table scan:\n"
]
},
{
Expand All @@ -803,7 +803,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"To speed up searches by the `rat_id` and `date_of_birth`, we can explicit indexes to `Rat`:"
"To speed up searches by the `rat_id` and `date_of_birth`, we can explicit indexes to `Rat`:\n"
]
},
{
Expand Down Expand Up @@ -878,7 +878,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Quiz: How many indexes does the table `Rat` have?"
"#### Quiz: How many indexes does the table `Rat` have?\n"
]
},
{
Expand All @@ -901,14 +901,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Three: primary key, rat_id, date_of_birth"
"Three: primary key, rat_id, date_of_birth\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Indexes in SQL"
"# Indexes in SQL\n"
]
},
{
Expand Down
8 changes: 4 additions & 4 deletions db-course/004-DatabaseSales.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -439,7 +439,7 @@
"\n",
"('S12_3891','1969 Ford Falcon','Classic Cars','1:12','Second Gear Diecast','Turnable front wheels; steering function; detailed interior; detailed engine; opening hood; opening trunk; opening doors; and detailed chassis.',1049,'83.05','173.02'),\n",
"\n",
"('S12_3990','1970 Plymouth Hemi Cuda','Classic Cars','1:12','Studio M Art Models','Very detailed 1970 Plymouth Cuda model in 1:12 scale. The Cuda is generally accepted as one of the fastest original muscle cars from the 1970s. This model is a reproduction of one of the orginal 652 cars built in 1970. Red color.',5663,'31.92','79.80'),\n",
"('S12_3990','1970 Plymouth Hemi Cuda','Classic Cars','1:12','Studio M Art Models','Very detailed 1970 Plymouth Cuda model in 1:12 scale. The Cuda is generally accepted as one of the fastest original muscle cars from the 1970s. This model is a reproduction of one of the original 652 cars built in 1970. Red color.',5663,'31.92','79.80'),\n",
"\n",
"('S12_4473','1957 Chevy Pickup','Trucks and Buses','1:12','Exoto Designs','1:12 scale die-cast about 20\\\" long Hood opens, Rubber wheels',6125,'55.70','118.50'),\n",
"\n",
Expand Down Expand Up @@ -517,7 +517,7 @@
"\n",
"('S18_4600','1940s Ford truck','Trucks and Buses','1:18','Motor City Art Classics','This 1940s Ford Pick-Up truck is re-created in 1:18 scale of original 1940s Ford truck. This antique style metal 1940s Ford Flatbed truck is all hand-assembled. This collectible 1940\\'s Pick-Up truck is painted in classic dark green color, and features rotating wheels.',3128,'84.76','121.08'),\n",
"\n",
"('S18_4668','1939 Cadillac Limousine','Vintage Cars','1:18','Studio M Art Models','Features completely detailed interior including Velvet flocked drapes,deluxe wood grain floor, and a wood grain casket with seperate chrome handles',6645,'23.14','50.31'),\n",
"('S18_4668','1939 Cadillac Limousine','Vintage Cars','1:18','Studio M Art Models','Features completely detailed interior including Velvet flocked drapes,deluxe wood grain floor, and a wood grain casket with separate chrome handles',6645,'23.14','50.31'),\n",
"\n",
"('S18_4721','1957 Corvette Convertible','Classic Cars','1:18','Classic Metal Creations','1957 die cast Corvette Convertible in Roman Red with white sides and whitewall tires. 1:18 scale quality die-cast with detailed engine and underbvody. Now you can own The Classic Corvette.',1249,'69.93','148.80'),\n",
"\n",
Expand All @@ -529,7 +529,7 @@
"\n",
"('S24_1578','1997 BMW R 1100 S','Motorcycles','1:24','Autoart Studio Design','Detailed scale replica with working suspension and constructed from over 70 parts',7003,'60.86','112.70'),\n",
"\n",
"('S24_1628','1966 Shelby Cobra 427 S/C','Classic Cars','1:24','Carousel DieCast Legends','This diecast model of the 1966 Shelby Cobra 427 S/C includes many authentic details and operating parts. The 1:24 scale model of this iconic lighweight sports car from the 1960s comes in silver and it\\'s own display case.',8197,'29.18','50.31'),\n",
"('S24_1628','1966 Shelby Cobra 427 S/C','Classic Cars','1:24','Carousel DieCast Legends','This diecast model of the 1966 Shelby Cobra 427 S/C includes many authentic details and operating parts. The 1:24 scale model of this iconic lightweight sports car from the 1960s comes in silver and it\\'s own display case.',8197,'29.18','50.31'),\n",
"\n",
"('S24_1785','1928 British Royal Navy Airplane','Planes','1:24','Classic Metal Creations','Official logos and insignias',3627,'66.74','109.42'),\n",
"\n",
Expand Down
14 changes: 7 additions & 7 deletions db-course/004-DatabaseUniversity.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Define"
"# Define\n"
]
},
{
Expand Down Expand Up @@ -1138,14 +1138,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Queries"
"# Queries\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Restriction"
"## Restriction\n"
]
},
{
Expand Down Expand Up @@ -1928,7 +1928,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Millenials\n",
"# Millennials\n",
"millennials = Student & 'date_of_birth between \"1981-01-01\" and \"1996-12-31\"'"
]
},
Expand Down Expand Up @@ -2607,7 +2607,7 @@
}
],
"source": [
"# Millenials who have never enrolled\n",
"# Millennials who have never enrolled\n",
"millennials - Enroll"
]
},
Expand Down Expand Up @@ -3623,7 +3623,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Join"
"## Join\n"
]
},
{
Expand Down Expand Up @@ -3690,7 +3690,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Aggr"
"## Aggr\n"
]
},
{
Expand Down
Loading

0 comments on commit 29ea931

Please sign in to comment.