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 a2e266d..0000000 Binary files a/scripts/__pycache__/data_generator.cpython-311.pyc and /dev/null differ