generated from jackyzha0/quartz
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
8 changed files
with
147 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
57
content/postgresql/PostgreSQL Primary Key and Foreign Key.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters