|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Challenge\n", |
| 8 | + "\n", |
| 9 | + "Define a a sales history table partitioned by month of sale:\n", |
| 10 | + "\n", |
| 11 | + "- Months are numbers from 1 to 12\n", |
| 12 | + "- Attributes should include:\n", |
| 13 | + " - product ID\n", |
| 14 | + " - product name\n", |
| 15 | + " - product type\n", |
| 16 | + " - total units sold\n", |
| 17 | + " - month of sale\n", |
| 18 | + "- Create a primary key using month of sale and product ID" |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "markdown", |
| 23 | + "metadata": {}, |
| 24 | + "source": [ |
| 25 | + "# Solution" |
| 26 | + ] |
| 27 | + }, |
| 28 | + { |
| 29 | + "cell_type": "markdown", |
| 30 | + "metadata": {}, |
| 31 | + "source": [ |
| 32 | + "**Create table with partitioned by range**\n", |
| 33 | + "\n", |
| 34 | + "`CREATE TABLE sales_history(\n", |
| 35 | + " product_id INT NOT NULL,\n", |
| 36 | + "\tproduct_name CHARACTER VARYING(50) NOT NULL,\n", |
| 37 | + "\tproduct_type CHARACTER VARYING(50) NOT NULL,\n", |
| 38 | + "\ttotal_units_sold INT NOT NULL,\n", |
| 39 | + "\tmonth_of_sale INT NOT NULL,\n", |
| 40 | + "\tPRIMARY KEY(month_of_sale, product_id)\n", |
| 41 | + ") PARTITION BY RANGE(month_of_sale);`\n", |
| 42 | + "\n", |
| 43 | + "\n", |
| 44 | + "**create partition nodes tables**\n", |
| 45 | + "\n", |
| 46 | + "`CREATE TABLE sales_history_month_1 PARTITION OF sales_history\n", |
| 47 | + "FOR VALUES FROM (1) TO (2);`\n", |
| 48 | + "\n", |
| 49 | + "`CREATE TABLE sales_history_month_2 PARTITION OF sales_history\n", |
| 50 | + "FOR VALUES FROM (2) TO (3);`\n", |
| 51 | + "\n", |
| 52 | + "`CREATE TABLE sales_history_month_3 PARTITION OF sales_history\n", |
| 53 | + "FOR VALUES FROM (3) TO (4);`\n", |
| 54 | + "\n", |
| 55 | + "`CREATE TABLE sales_history_month_4 PARTITION OF sales_history\n", |
| 56 | + "FOR VALUES FROM (4) TO (5);`\n", |
| 57 | + "\n", |
| 58 | + "`CREATE TABLE sales_history_month_5 PARTITION OF sales_history\n", |
| 59 | + "FOR VALUES FROM (5) TO (6);`\n", |
| 60 | + "\n", |
| 61 | + "`CREATE TABLE sales_history_month_6 PARTITION OF sales_history\n", |
| 62 | + "FOR VALUES FROM (6) TO (7);`\n", |
| 63 | + "\n", |
| 64 | + "`CREATE TABLE sales_history_month_7 PARTITION OF sales_history\n", |
| 65 | + "FOR VALUES FROM (7) TO (8);`\n", |
| 66 | + "\n", |
| 67 | + "`CREATE TABLE sales_history_month_8 PARTITION OF sales_history\n", |
| 68 | + "FOR VALUES FROM (8) TO (9);`\n", |
| 69 | + "\n", |
| 70 | + "`CREATE TABLE sales_history_month_9 PARTITION OF sales_history\n", |
| 71 | + "FOR VALUES FROM (9) TO (10);`\n", |
| 72 | + "\n", |
| 73 | + "`CREATE TABLE sales_history_month_10 PARTITION OF sales_history\n", |
| 74 | + "FOR VALUES FROM (10) TO (11);`\n", |
| 75 | + "\n", |
| 76 | + "`CREATE TABLE sales_history_month_11 PARTITION OF sales_history\n", |
| 77 | + "FOR VALUES FROM (11) TO (12);`\n", |
| 78 | + "\n", |
| 79 | + "`CREATE TABLE sales_history_month_12 PARTITION OF sales_history\n", |
| 80 | + "FOR VALUES FROM (12) TO (13);`\n" |
| 81 | + ] |
| 82 | + }, |
| 83 | + { |
| 84 | + "cell_type": "code", |
| 85 | + "execution_count": null, |
| 86 | + "metadata": {}, |
| 87 | + "outputs": [], |
| 88 | + "source": [] |
| 89 | + } |
| 90 | + ], |
| 91 | + "metadata": { |
| 92 | + "kernelspec": { |
| 93 | + "display_name": "Python 3", |
| 94 | + "language": "python", |
| 95 | + "name": "python3" |
| 96 | + }, |
| 97 | + "language_info": { |
| 98 | + "codemirror_mode": { |
| 99 | + "name": "ipython", |
| 100 | + "version": 3 |
| 101 | + }, |
| 102 | + "file_extension": ".py", |
| 103 | + "mimetype": "text/x-python", |
| 104 | + "name": "python", |
| 105 | + "nbconvert_exporter": "python", |
| 106 | + "pygments_lexer": "ipython3", |
| 107 | + "version": "3.8.5" |
| 108 | + } |
| 109 | + }, |
| 110 | + "nbformat": 4, |
| 111 | + "nbformat_minor": 4 |
| 112 | +} |
0 commit comments