Skip to content

Commit

Permalink
vault backup: 2024-05-18 00:07:35
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 Constraint and Key.md
content/postgresql/PostgreSQL Constraints.md
content/postgresql/PostgreSQL Primary Key and Foreign Key.md
content/postgresql/PostgreSQL Sequence and UUID.md
content/postgresql/PostgreSQL Table.md
content/postgresql/PostgreSQL.md
  • Loading branch information
windsuzu committed May 17, 2024
1 parent 2701aef commit 19bafd2
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 90 deletions.
32 changes: 16 additions & 16 deletions content/.obsidian/plugins/recent-files-obsidian/data.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
{
"recentFiles": [
{
"basename": "PostgreSQL",
"path": "postgresql/PostgreSQL.md"
"basename": "PostgreSQL Sequence and UUID",
"path": "postgresql/PostgreSQL Sequence and UUID.md"
},
{
"basename": "PostgreSQL Constraint and Key",
"path": "postgresql/PostgreSQL Constraint and Key.md"
"basename": "PostgreSQL",
"path": "postgresql/PostgreSQL.md"
},
{
"basename": "PostgreSQL Table",
"path": "postgresql/PostgreSQL Table.md"
"basename": "PostgreSQL Join",
"path": "postgresql/PostgreSQL Join.md"
},
{
"basename": "PostgreSQL Delete",
"path": "postgresql/PostgreSQL Delete.md"
},
{
"basename": "PostgreSQL Join",
"path": "postgresql/PostgreSQL Join.md"
},
{
"basename": "PostgreSQL Update",
"path": "postgresql/PostgreSQL Update.md"
},
{
"basename": "PostgreSQL Sequence and UUID",
"path": "postgresql/PostgreSQL Sequence and UUID.md"
"basename": "PostgreSQL Constraints",
"path": "postgresql/PostgreSQL Constraints.md"
},
{
"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",
Expand Down Expand Up @@ -195,10 +199,6 @@
{
"basename": "The most ... in or of",
"path": "english-grammar/The most ... in or of.md"
},
{
"basename": "Anymore vs Any more",
"path": "english-grammar/Anymore vs Any more.md"
}
],
"omittedPaths": [],
Expand Down
28 changes: 14 additions & 14 deletions content/.obsidian/workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,31 @@
"state": {
"type": "markdown",
"state": {
"file": "postgresql/PostgreSQL.md",
"file": "postgresql/PostgreSQL Sequence and UUID.md",
"mode": "source",
"source": false
}
}
},
{
"id": "96a9372e3b208e7f",
"id": "fbaa50145f527248",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "postgresql/PostgreSQL Constraint and Key.md",
"file": "postgresql/PostgreSQL Primary Key and Foreign Key.md",
"mode": "source",
"source": false
}
}
},
{
"id": "479dfa44c76beee2",
"id": "2592231430c39d6e",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "postgresql/PostgreSQL Table.md",
"file": "postgresql/PostgreSQL Constraints.md",
"mode": "source",
"source": false
}
Expand Down Expand Up @@ -126,7 +126,7 @@
"state": {
"type": "localgraph",
"state": {
"file": "postgresql/PostgreSQL.md",
"file": "postgresql/PostgreSQL Sequence and UUID.md",
"options": {
"collapse-filter": true,
"search": "",
Expand Down Expand Up @@ -176,7 +176,7 @@
"state": {
"type": "outline",
"state": {
"file": "postgresql/PostgreSQL.md"
"file": "postgresql/PostgreSQL Sequence and UUID.md"
}
}
},
Expand Down Expand Up @@ -217,7 +217,7 @@
"state": {
"type": "file-properties",
"state": {
"file": "postgresql/PostgreSQL.md"
"file": "postgresql/PostgreSQL Sequence and UUID.md"
}
}
},
Expand All @@ -227,7 +227,7 @@
"state": {
"type": "backlink",
"state": {
"file": "postgresql/PostgreSQL.md",
"file": "postgresql/PostgreSQL Sequence and UUID.md",
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical",
Expand All @@ -244,7 +244,7 @@
"state": {
"type": "outgoing-link",
"state": {
"file": "postgresql/PostgreSQL.md",
"file": "postgresql/PostgreSQL Sequence and UUID.md",
"linksCollapsed": false,
"unlinkedCollapsed": false
}
Expand Down Expand Up @@ -272,13 +272,14 @@
},
"active": "89b49aba37ae8848",
"lastOpenFiles": [
"postgresql/PostgreSQL Constraint and Key.md",
"postgresql/PostgreSQL Table.md",
"postgresql/PostgreSQL.md",
"postgresql/PostgreSQL Delete.md",
"postgresql/PostgreSQL Join.md",
"postgresql/PostgreSQL Delete.md",
"postgresql/PostgreSQL Update.md",
"postgresql/PostgreSQL Sequence and UUID.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",
Expand All @@ -299,7 +300,6 @@
"mocs/CSS MOC.md",
"mocs/React MOC.md",
"mocs/Web-Dev MOC.md",
"mocs/JavaScript MOC.md",
"fish-shell",
"resume",
"attachments/avatar-jay.jpeg",
Expand Down
54 changes: 0 additions & 54 deletions content/postgresql/PostgreSQL Constraint and Key.md

This file was deleted.

46 changes: 46 additions & 0 deletions content/postgresql/PostgreSQL Constraints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
draft: false
date: 2024-05-17 23:32
tags:
- postgres
---

Other than [[PostgreSQL Primary Key and Foreign Key|primary and foreign keys]], we can define constraints on any column during table creation (CREATE TABLE) or table modification (ALTER TABLE). These constraints can be forcing uniqueness or custom conditions.

### NOT NULL

```sql
CREATE TABLE example (
name VARCHAR(100) NOT NULL
);

ALTER TABLE example ALTER COLUMN name SET NOT NULL;

ALTER TABLE example ALTER COLUMN name DROP NOT NULL;
```
### UNIQUE

```sql
CREATE TABLE example (
email VARCHAR(150) UNIQUE
);

ALTER TABLE example ADD CONSTRAINT unique_email UNIQUE (email);
ALTER TABLE example ADD UNIQUE (email);

ALTER TABLE example DROP CONSTRAINT unique_email;
```
### CHECK

```sql
CREATE TABLE example (
age INT CHECK (age >= 18)
);

ALTER TABLE example ADD CONSTRAINT check_age CHECK (age >= 18);

ALTER TABLE example DROP CONSTRAINT check_age;
```

> [!info] References
> - [Learn PostgreSQL Tutorial - Full Course for Beginners (youtube.com)](https://www.youtube.com/watch?v=qw--VYLpxG4)
57 changes: 57 additions & 0 deletions content/postgresql/PostgreSQL Primary Key and Foreign Key.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
draft: false
date: 2024-05-17 23:14
tags:
- postgres
---

### Primary Key

We can assign a primary key to a single column or to a combination of columns, either when creating a table (`CREATE TABLE`) or when altering a table (`ALTER TABLE`).

```sql
CREATE TABLE person (
id BIGSERIAL PRIMARY KEY,
name VARCHAR(100)
);

CREATE TABLE person (
id BIGSERIAL,
name VARCHAR(100),
PRIMARY KEY (id, name)
);

ALTER TABLE person ADD PRIMARY KEY (id);
ALTER TABLE person ADD PRIMARY KEY (id, name);
```

To remove a primary key from a table, you can use the `ALTER TABLE` statement to do it. Find the name of the primary key by using `\d table_name` before removing it.

```sql
ALTER TABLE person DROP CONSTRAINT person_pkey;
```
### Foreign Key

We can create a relationship between two tables by using a foreign key from the child table to create a link to the primary key of the parent table. Creating a relationship between two tables can enable us to perform some useful commands, like `JOIN`, to [[PostgreSQL Join|join two tables]].

```sql
CREATE TABLE person (
id BIGSERIAL PRIMARY KEY,
name VARCHAR(100)
);

CREATE TABLE car (
id BIGSERIAL PRIMARY KEY,
person_id BIGINT,
FOREIGN KEY (person_id) REFERENCES person (id)
);
```

To remove a foreign key from a table, you can also use the `ALTER TABLE` statement do to it.

```sql
ALTER TABLE car DROP CONSTRAINT car_person_id_fkey;
```

> [!info] References
> - [Learn PostgreSQL Tutorial - Full Course for Beginners (youtube.com)](https://www.youtube.com/watch?v=qw--VYLpxG4)
6 changes: 4 additions & 2 deletions content/postgresql/PostgreSQL Sequence and UUID.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
---
draft: false
date: 2024-05-16 18:15
date: 2024-05-17 23:37
tags:
- postgres
---

### BIGSERIAL

The `BIGSERIAL` type assigns the column an auto-incrementing id starting at 1. If you want to restart the id number from a specific number, you can use `ALTER SEQUENCE` as the following statement.

```sql
ALTER SEQUENCE person_id_seq RESTART WITH 0;
ALTER SEQUENCE person_id_seq RESTART WITH 1;
```

### UUID
Expand Down
9 changes: 7 additions & 2 deletions content/postgresql/PostgreSQL Table.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
draft: false
date: 2024-05-16 17:20
date: 2024-05-17 21:54
tags:
- postgres
---
Expand Down Expand Up @@ -85,7 +85,10 @@ And use `\d table_name` to see the details of the specified table.

### Update a table

To update a table, use `ALTER`. For example, you can update the column name by using `ALTER TABLE` with `RENAME TO`:
To update a table, use `ALTER`.
#### Rename a column

You can update the column name by using `ALTER TABLE` with `RENAME TO`:

```sql
ALTER TABLE table_name RENAME column_name TO column_new_name;
Expand All @@ -94,6 +97,8 @@ ALTER TABLE person RENAME id TO person_uid;
ALTER TABLE person RENAME gender TO sex;
```

#### Update the data type

Additionally, you can update the column type by using `ALTER TABLE` with `ALTER COLUMN` and `TYPE`:

```sql
Expand Down
5 changes: 3 additions & 2 deletions content/postgresql/PostgreSQL.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
draft: false
date: 2024-05-16 18:18
date: 2024-05-17 22:56
tags:
- postgres
---
Expand All @@ -14,7 +14,8 @@ tags:
## Table

- [[PostgreSQL Table]]
- [[PostgreSQL Constraint and Key]]
- [[PostgreSQL Primary Key and Foreign Key]]
- [[PostgreSQL Constraints]]
- [[PostgreSQL Sequence and UUID]]

## Data Manipulation
Expand Down

0 comments on commit 19bafd2

Please sign in to comment.