[postgres13.1] 基本操作 CRUD

### create table
$ psql -l
$ psql -U root test
test=> create table mybook(
id integer,
name varchar(10)
);
CREATE TABLE

### テーブル一覧
test=> \d
List of relations
Schema | Name | Type | Owner
——–+——–+——-+——-
public | mybook | table | root
(1 row)

### カラムの確認
test=> \d mybook;
Table “public.mybook”
Column | Type | Collation | Nullable | Default
——–+———————–+———–+———-+———
id | integer | | |
name | character varying(10) | | |

### テーブル削除
test=> drop table mybook;
DROP TABLE

primary keyは、create tableの際に、name varchar(10) primary key,
mysqlでいうauto_incrementは、generated always as identity とする。
id integer generated always as identity,

Postgres放置してたけど、SpringBootやるなら必須じゃんか。

### データの挿入
test=> CREATE TABLE products (
product_no integer,
name text,
price numeric
);
CREATE TABLE
test=> INSERT INTO products (product_no, name, price) VALUES (1, ‘melon’, 9.99);
INSERT 0 1
test=> select * from products;
product_no | name | price
————+——-+——-
1 | melon | 9.99
(1 row)

### データの更新
test=> UPDATE products SET price = 12.0 where product_no = 1;
UPDATE 1
test=> select * from products;
product_no | name | price
————+——-+——-
1 | melon | 12.0
(1 row)

### データの削除
test=> delete from products where product_no = 1;
DELETE 1
test=> select * from products;
product_no | name | price
————+——+——-
(0 rows)

PostgresのCRUDは一通りマスターした。
後はこれをSpringBootから操作するところをやる