Skip to content

Commit

Permalink
vault backup: 2024-05-18 18:18:04
Browse files Browse the repository at this point in the history
Affected files:
content/.obsidian/plugins/recent-files-obsidian/data.json
content/.obsidian/workspace.json
content/postgresql/PostgreSQL Conflict.md
content/postgresql/PostgreSQL Constraints.md
content/postgresql/PostgreSQL Delete.md
content/postgresql/PostgreSQL Insert and Import.md
content/postgresql/PostgreSQL Sequence and UUID.md
content/postgresql/PostgreSQL Update.md
content/postgresql/PostgreSQL.md
  • Loading branch information
windsuzu committed May 18, 2024
1 parent 19bafd2 commit a94dc69
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 71 deletions.
36 changes: 18 additions & 18 deletions content/.obsidian/plugins/recent-files-obsidian/data.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
{
"recentFiles": [
{
"basename": "PostgreSQL Sequence and UUID",
"path": "postgresql/PostgreSQL Sequence and UUID.md"
"basename": "PostgreSQL Conflict",
"path": "postgresql/PostgreSQL Conflict.md"
},
{
"basename": "PostgreSQL",
"path": "postgresql/PostgreSQL.md"
},
{
"basename": "PostgreSQL Join",
"path": "postgresql/PostgreSQL Join.md"
},
{
"basename": "PostgreSQL Delete",
"path": "postgresql/PostgreSQL Delete.md"
Expand All @@ -28,14 +24,6 @@
"basename": "PostgreSQL Primary Key and Foreign Key",
"path": "postgresql/PostgreSQL Primary Key and Foreign Key.md"
},
{
"basename": "PostgreSQL Table",
"path": "postgresql/PostgreSQL Table.md"
},
{
"basename": "PostgreSQL Database",
"path": "postgresql/PostgreSQL Database.md"
},
{
"basename": "PostgreSQL Insert and Import",
"path": "postgresql/PostgreSQL Insert and Import.md"
Expand All @@ -44,10 +32,26 @@
"basename": "PostgreSQL Select",
"path": "postgresql/PostgreSQL Select.md"
},
{
"basename": "PostgreSQL Sequence and UUID",
"path": "postgresql/PostgreSQL Sequence and UUID.md"
},
{
"basename": "Connect PostgreSQL on WSL 2 (Ubuntu)",
"path": "postgresql/Connect PostgreSQL on WSL 2 (Ubuntu).md"
},
{
"basename": "PostgreSQL Database",
"path": "postgresql/PostgreSQL Database.md"
},
{
"basename": "PostgreSQL Table",
"path": "postgresql/PostgreSQL Table.md"
},
{
"basename": "PostgreSQL Join",
"path": "postgresql/PostgreSQL Join.md"
},
{
"basename": "Install PostgreSQL on WSL 2 (Ubuntu)",
"path": "postgresql/Install PostgreSQL on WSL 2 (Ubuntu).md"
Expand Down Expand Up @@ -195,10 +199,6 @@
{
"basename": "Well-known groups of people",
"path": "english-grammar/Well-known groups of people.md"
},
{
"basename": "The most ... in or of",
"path": "english-grammar/The most ... in or of.md"
}
],
"omittedPaths": [],
Expand Down
48 changes: 12 additions & 36 deletions content/.obsidian/workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,7 @@
"state": {
"type": "markdown",
"state": {
"file": "postgresql/PostgreSQL Sequence and UUID.md",
"mode": "source",
"source": false
}
}
},
{
"id": "fbaa50145f527248",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "postgresql/PostgreSQL Primary Key and Foreign Key.md",
"mode": "source",
"source": false
}
}
},
{
"id": "2592231430c39d6e",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "postgresql/PostgreSQL Constraints.md",
"file": "postgresql/PostgreSQL Conflict.md",
"mode": "source",
"source": false
}
Expand Down Expand Up @@ -126,7 +102,7 @@
"state": {
"type": "localgraph",
"state": {
"file": "postgresql/PostgreSQL Sequence and UUID.md",
"file": "postgresql/PostgreSQL Conflict.md",
"options": {
"collapse-filter": true,
"search": "",
Expand Down Expand Up @@ -176,7 +152,7 @@
"state": {
"type": "outline",
"state": {
"file": "postgresql/PostgreSQL Sequence and UUID.md"
"file": "postgresql/PostgreSQL Conflict.md"
}
}
},
Expand Down Expand Up @@ -217,7 +193,7 @@
"state": {
"type": "file-properties",
"state": {
"file": "postgresql/PostgreSQL Sequence and UUID.md"
"file": "postgresql/PostgreSQL Conflict.md"
}
}
},
Expand All @@ -227,7 +203,7 @@
"state": {
"type": "backlink",
"state": {
"file": "postgresql/PostgreSQL Sequence and UUID.md",
"file": "postgresql/PostgreSQL Conflict.md",
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical",
Expand All @@ -244,7 +220,7 @@
"state": {
"type": "outgoing-link",
"state": {
"file": "postgresql/PostgreSQL Sequence and UUID.md",
"file": "postgresql/PostgreSQL Conflict.md",
"linksCollapsed": false,
"unlinkedCollapsed": false
}
Expand Down Expand Up @@ -273,17 +249,19 @@
"active": "89b49aba37ae8848",
"lastOpenFiles": [
"postgresql/PostgreSQL.md",
"postgresql/PostgreSQL Join.md",
"postgresql/PostgreSQL Delete.md",
"postgresql/PostgreSQL Update.md",
"postgresql/PostgreSQL Sequence and UUID.md",
"postgresql/PostgreSQL Conflict.md",
"postgresql/PostgreSQL Constraints.md",
"postgresql/PostgreSQL Primary Key and Foreign Key.md",
"postgresql/PostgreSQL Table.md",
"postgresql/PostgreSQL Database.md",
"postgresql/PostgreSQL Insert and Import.md",
"postgresql/PostgreSQL Select.md",
"postgresql/PostgreSQL Sequence and UUID.md",
"postgresql/Connect PostgreSQL on WSL 2 (Ubuntu).md",
"postgresql/PostgreSQL Database.md",
"postgresql/PostgreSQL Table.md",
"postgresql/().md",
"postgresql/PostgreSQL Join.md",
"postgresql/Install PostgreSQL on WSL 2 (Ubuntu).md",
"index.md",
"postgresql",
Expand All @@ -298,8 +276,6 @@
"english-grammar/The - definite article.md",
"mocs/Installation.md",
"mocs/CSS MOC.md",
"mocs/React MOC.md",
"mocs/Web-Dev MOC.md",
"fish-shell",
"resume",
"attachments/avatar-jay.jpeg",
Expand Down
20 changes: 20 additions & 0 deletions content/postgresql/PostgreSQL Conflict.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
draft: false
date: 2024-05-18 17:48
tags:
- postgres
---



```sql
ON CONFLICT (id) DO NOTHING;
ON CONFLICT (id) DO UPDATE SET email = EXCLUDED.email;
ON CONFLICT (id) DO UPDATE SET email = EXCLUDED.email, gender = EXCLUDED.gender;
```




> [!info] References
> - [Learn PostgreSQL Tutorial - Full Course for Beginners (youtube.com)](https://www.youtube.com/watch?v=qw--VYLpxG4)
12 changes: 11 additions & 1 deletion content/postgresql/PostgreSQL Constraints.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
draft: false
date: 2024-05-17 23:32
date: 2024-05-18 17:36
tags:
- postgres
---
Expand All @@ -10,35 +10,45 @@ Other than [[PostgreSQL Primary Key and Foreign Key|primary and foreign keys]],
### NOT NULL

```sql
-- Define NOT NULL constraint when creating table
CREATE TABLE example (
name VARCHAR(100) NOT NULL
);

-- Define NOT NULL constriant by modifying table
ALTER TABLE example ALTER COLUMN name SET NOT NULL;

-- Remove NOT NULL constraint by modifying table
ALTER TABLE example ALTER COLUMN name DROP NOT NULL;
```
### UNIQUE

```sql
-- Define UNIQUE constraint when creating table
CREATE TABLE example (
email VARCHAR(150) UNIQUE
);

-- Define UNIQUE constraint by modifying table with named constraint
ALTER TABLE example ADD CONSTRAINT unique_email UNIQUE (email);
-- Define UNIQUE constraint by modifying table without naming constraint
ALTER TABLE example ADD UNIQUE (email);

-- Remove UNIQUE constraint by modifying table
ALTER TABLE example DROP CONSTRAINT unique_email;
```
### CHECK

```sql
-- Define CHECK constraint when creating table
CREATE TABLE example (
age INT CHECK (age >= 18)
);

-- Define CHECK constraint by modifying table with named constraint
ALTER TABLE example ADD CONSTRAINT check_age CHECK (age >= 18);

-- Remove CHECK constraint by modifying table
ALTER TABLE example DROP CONSTRAINT check_age;
```

Expand Down
7 changes: 4 additions & 3 deletions content/postgresql/PostgreSQL Delete.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
---
draft: false
date: 2024-05-16 18:17
date: 2024-05-18 17:47
tags:
- postgres
---

To delete all records in your table:

```sql
DELETE FROM person;
```

To delete one or more records using conditions:

```sql
DELETE FROM person WHERE id = 1;
```




> [!info] References
> - [Learn PostgreSQL Tutorial - Full Course for Beginners (youtube.com)](https://www.youtube.com/watch?v=qw--VYLpxG4)
4 changes: 2 additions & 2 deletions content/postgresql/PostgreSQL Insert and Import.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
draft: false
date: 2024-05-16 11:49
date: 2024-05-18 17:37
tags:
- postgres
---
Expand All @@ -25,7 +25,7 @@ INSERT INTO person (first_name, last_name, email, gender, date_of_birth) VALUES
Suppose you have a `.sql` file with 1000 insert statements, you can use `\i file_relative_path` to execute all the commands in the file.

```sql
# /mnt/c/Users/winds/Desktop/person.sql
-- /mnt/c/Users/winds/Desktop/person.sql

insert into person (first_name, last_name, email, gender, date_of_birth) values ('Alvan', 'Fearnall', null, 'Male', '2024-05-12');
insert into person (first_name, last_name, email, gender, date_of_birth) values ('Herbie', 'Tollit', null, 'Male', '2023-12-18');
Expand Down
25 changes: 24 additions & 1 deletion content/postgresql/PostgreSQL Sequence and UUID.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
draft: false
date: 2024-05-17 23:37
date: 2024-05-18 17:23
tags:
- postgres
---
Expand All @@ -15,10 +15,33 @@ ALTER SEQUENCE person_id_seq RESTART WITH 1;

### UUID

[UUID](https://en.wikipedia.org/wiki/Universally_unique_identifier) stands for Universally Unique Identifier and is a data type designed to store unique identifiers. A UUID is globally unique, which means it is unique even across different systems.

```sql
CREATE TABLE example (
id UUID PRIMARY KEY,
name VARCHAR(100)
);
```

Before inserting a record with a UUID, you have to install a UUID generator from `uuid-ossp`. You can install it with the following SQL command.

```sql
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
```

Now when you insert a record, you can call `uuid_generate_v4()` or [other versions can be found in the documentation](https://www.postgresql.org/docs/current/uuid-ossp.html) for your `id` field.

```sql
-- Inserting a single row with a generated UUID
INSERT INTO example (id, name) VALUES (uuid_generate_v4(), 'John');

-- Inserting multiple rows with generated UUIDs
INSERT INTO example (id, name)
VALUES
(uuid_generate_v4(), 'Alice'),
(uuid_generate_v4(), 'Bob');
```


> [!info] References
Expand Down
12 changes: 3 additions & 9 deletions content/postgresql/PostgreSQL Update.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
---
draft: false
date: 2024-05-16 18:16
date: 2024-05-18 17:47
tags:
- postgres
---

To update a single record in the database, use the following SQL statement:

```sql
UPDATE person SET first_name = 'Omar', last_name = 'Montana' WHERE id = 2011;
```
```sql
ON CONFLICT (id) DO NOTHING;
ON CONFLICT (id) DO UPDATE SET email = EXCLUDED.email;
ON CONFLICT (id) DO UPDATE SET email = EXCLUDED.email, gender = EXCLUDED.gender;
```




> [!info] References
> - [Learn PostgreSQL Tutorial - Full Course for Beginners (youtube.com)](https://www.youtube.com/watch?v=qw--VYLpxG4)
3 changes: 2 additions & 1 deletion content/postgresql/PostgreSQL.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
draft: false
date: 2024-05-17 22:56
date: 2024-05-18 17:46
tags:
- postgres
---
Expand All @@ -24,6 +24,7 @@ tags:
- [[PostgreSQL Select]]
- [[PostgreSQL Update]]
- [[PostgreSQL Delete]]
- [[PostgreSQL Conflict]]
- [[PostgreSQL Join]]

> [!info] References
Expand Down

0 comments on commit a94dc69

Please sign in to comment.