From a6407f286fc9e43f525b8d8b728bf21af46ae047 Mon Sep 17 00:00:00 2001 From: Peyman Salehi Date: Fri, 29 Sep 2023 17:09:37 +0330 Subject: [PATCH] Use tabs in all posts --- .gitignore | 2 + content/posts/create-and-drop-table.md | 12 ++- content/posts/sample-database-structure.md | 75 ++++++++++++++---- content/posts/setup.md | 29 +++++-- .../data_generator.cpython-311.pyc | Bin 6889 -> 0 bytes 5 files changed, 96 insertions(+), 22 deletions(-) delete mode 100644 scripts/__pycache__/data_generator.cpython-311.pyc diff --git a/.gitignore b/.gitignore index 2ff966f..1a6ec5a 100644 --- a/.gitignore +++ b/.gitignore @@ -63,5 +63,7 @@ typings/ .venv +__pycache__ + # mac .DS_Store diff --git a/content/posts/create-and-drop-table.md b/content/posts/create-and-drop-table.md index 537c93e..f0f1d3c 100644 --- a/content/posts/create-and-drop-table.md +++ b/content/posts/create-and-drop-table.md @@ -3,9 +3,11 @@ title: "Create and Drop Table" date: 2023-09-17T01:06:01+03:30 --- -## PostgreSQL ### Create table By using `CREATE TABLE` statement you can create a new table. + +{{< tabs tabTotal="1" >}} +{{< tab tabName="PostgreSQL" >}} ```sql -- Create a table called users CREATE TABLE users ( @@ -27,8 +29,14 @@ CREATE TABLE users ( [PostgreSQL create table](https://www.postgresql.org/docs/16/sql-createtableas.html) [PostgreSQL data types](https://www.postgresql.org/docs/8.1/datatype.html#DATATYPE-TABLE) +{{< /tab >}} +{{< /tabs >}} + ## Remove table Use `DROP TABLE` to destroy a table. + +{{< tabs tabTotal="1" >}} +{{< tab tabName="PostgreSQL" >}} ```sql -- drop table coffee DROP TABLE coffee; @@ -41,3 +49,5 @@ DROP TABLE coffee CASCADE; ``` [PostgreSQL docs for more info.](https://www.postgresql.org/docs/16/sql-droptable.html) +{{< /tab >}} +{{< /tabs >}} diff --git a/content/posts/sample-database-structure.md b/content/posts/sample-database-structure.md index acc9202..932ef06 100644 --- a/content/posts/sample-database-structure.md +++ b/content/posts/sample-database-structure.md @@ -4,15 +4,15 @@ date: 2023-09-15T16:23:58+03:30 draft: false --- -This is an overview of sample database that you imported from [setup]() post. +This is an overview of sample database that you imported from [setup](/posts/setup/) post. ## Overview We're assuming that we have an eCommerce website and have some tables -like, users, products, orders, and users_log. +like, users, product_categories, products, orders, and user_logs. ### Users table Users table have the following structure. - +table name: users |column |type | |---|---| id |int @@ -20,31 +20,78 @@ datetime_joined |datetime first_name |varchar last_name |varchar email |varchar -username |varchar +city |varchar active |boolean Sample data: -|id |datetime_joined |first_name |last_name |email |username |active | +|id |datetime_joined |first_name |last_name |email |city |active | |---|---|---|---|---|---|---| -|1 |2023-03-07 12:45:12.231069+00 |John|Doe |john@sbe.com |john_doe | true +|1 |2023-03-07 12:45:12.231069+00 |John|Doe |john@sbe.com |West Misty | true -### Products table -Products table have the following structure. +### Product categories +table name: product_categories +|column |type | +|---|---| +id |int +name |varchar +Sample data: +|id |name | +|---|---| +|1 |Oil Paint + + +### Products table +table name: products |column |type | |---|---| id |int created_at |datetime +product_code |varchar name |varchar +company |varchar price |int -category |foreign key -description |varchar -sale_price |int / nullable +category_id |int - foreign key +description |text Sample data: -|id |datetime_joined |first_name |last_name |email |username |active | -|---|---|---|---|---|---|---| -|1 |2023-03-07 12:45:12.231069+00 |John|Doe |john@sbe.com |john_doe | true +|id |created_at |product_code |name |company |price |category_id |description | +|---|---|---|---|---|---|---|---| +|1 |2023-09-01 23:59:14 |30248960 |teal paint |Burke, Payne and Oliver |427 |1 |Score finally... +### Orders table +table name: orders +|column |type | +|---|---| +id |int +created_at |datetime +order_code |varchar +product_id |int - foreign key +user_id |int - foreign key +quantity |int +total_price |int +delivered |boolean + +Sample data: +|id |created_at |order_code |product_id |user_id |quantity |total_price |delivered | +|---|---|---|---|---|---|---|---| +|1 |2022-10-08 17:50:42 |28e9fe61-8883-4933-bc48-b4cd292cf501 |17 |480 |6 |2532 |True + + +### User logs table +table name: user_logs +|column |type | +|---|---| +id |int +user_email |varchar +datetime |datetime +ip_address |varchar +page_visited |text +user_agent |text + +Sample data: +|id |user_email |datetime |ip_address |page_visited |user_agent | +|---|---|---|---|---|---| +|1 |marilyn05@hotmail.com |2023-06-25 11:04:28 |116.0.78.131 |blog/ |Mozilla/5.0 (Windows; U; Windows CE)... diff --git a/content/posts/setup.md b/content/posts/setup.md index 7846d4e..8746898 100644 --- a/content/posts/setup.md +++ b/content/posts/setup.md @@ -8,14 +8,13 @@ If you want to run the examples in the following posts, you can follow these instructions to setup your environment and import our sample data to work with. -## PostgreSQL -If you have PostgreSQL installed, you can skip step 1 and 2. +{{< tabs tabTotal="1" >}} +{{< tab tabName="PostgreSQL" >}} +If you have PostgreSQL installed, you can skip step 1. -### 1: Install docker and docker compose +### 1: Run PostgreSQL You can download and install docker and docker compose from [docker website](https://www.docker.com/). - -### 2: Run PostgreSQL Create a `docker-compose.yml` file: ```yaml version: '3' @@ -35,9 +34,25 @@ services: volumes: postgres_data: ``` -Then, run: +Then, run: ```bash docker compose up -d +# or +docker-compose up -d ``` -### 3: Import sample database +### 2: Import data +Run this command to import backup into your PostgreSQL database. +```bash +# download backup file +wget https://raw.githubusercontent.com/peymanslh/sql/main/scripts/backups/sbe.pgsql + +# import data +cat sbe.pgsql | docker exec -i `docker ps -q --filter name=postgres` psql -U sbe sbe +# or, if you don't use docker +psql -U sbe sbe < sbe.pgsql +``` +You can download `sbe.pgsql` backup file from [GitHub](https://github.com/peymanslh/sql/tree/main/scripts/backups) +or generate more data with scripts we provided in [`scripts` directory](https://github.com/peymanslh/sql/tree/main/scripts). +{{< /tab >}} +{{< /tabs >}} diff --git a/scripts/__pycache__/data_generator.cpython-311.pyc b/scripts/__pycache__/data_generator.cpython-311.pyc deleted file mode 100644 index a2e266d9deefe7ffb361a7ddaf16d1ff771b6ebf..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6889 zcmcH-S!^4}b(hN}mzPK#)IrLT2GZe^G__)l2Ao{_!*ETMr1@b$)wmxmPOo_ zw54p5wiGwX(K45`r}#;pWh95>l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