Skip to content

Commit

Permalink
Use tabs in all posts
Browse files Browse the repository at this point in the history
  • Loading branch information
peymanslh committed Sep 29, 2023
1 parent 69f3175 commit a6407f2
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,7 @@ typings/

.venv

__pycache__

# mac
.DS_Store
12 changes: 11 additions & 1 deletion content/posts/create-and-drop-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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;
Expand All @@ -41,3 +49,5 @@ DROP TABLE coffee CASCADE;
```
[PostgreSQL docs for more info.](https://www.postgresql.org/docs/16/sql-droptable.html)

{{< /tab >}}
{{< /tabs >}}
75 changes: 61 additions & 14 deletions content/posts/sample-database-structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,94 @@ 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
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 |[email protected] |john_doe | true
|1 |2023-03-07 12:45:12.231069+00 |John|Doe |[email protected] |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 |[email protected] |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 |[email protected] |2023-06-25 11:04:28 |116.0.78.131 |blog/ |Mozilla/5.0 (Windows; U; Windows CE)...
29 changes: 22 additions & 7 deletions content/posts/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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 >}}
Binary file removed scripts/__pycache__/data_generator.cpython-311.pyc
Binary file not shown.

0 comments on commit a6407f2

Please sign in to comment.