note

資料庫(PostgreSQL)

MySQL比較多人用,但PostgreSQL業界風評比較好,SQLite是用來測試的玩具XD

下載12.9版本的PostgreSQL,怕太新deno沒支援

安裝完後加入系統路徑(環境變數裡面的path點開加到裡面)

postgreSQL預設port: 5432

操作資料: https://docs.postgresql.tw/tutorial/getting-started/accessing-a-database

創建資料庫(-U postgres使用預設使用者)

createdb -U postgres mydb

使用資料庫

psql -U postgres mydb

進入資料庫裏面操作 SELECT 每個指令都要有分號

SELECT 是操作基礎,可以SELECT 2 +2;,可以 SELECT version();,後面一定要有;

表格的註解是用–

創建表格 CREATE TABLE table

CREATE TABLE weather (
    city            varchar(80),   -- 添加初始說明列   變數存放類型
    temp_lo         int,           -- low temperature
    temp_hi         int,           -- high temperature
    prcp            real,          -- precipitation(%數)
    date            date		   -- 創建日期
);



CREATE TABLE cities (
    name            varchar(80),
    location        point          -- (x, y)   
);

刪除表格 DROP TABLE table

DROP TABLE tablename;

加入資料到表格 INSERT INTO table VALUES ?

-- 添加一列,後面填入欄位,欄位可以少,但不能多
INSERT INTO weather VALUES ('San Francisco', 46, 50, 0.25, '1994-11-27');


-- 添加資料到指定欄位
INSERT INTO weather (city, temp_lo, temp_hi, prcp, date)
    VALUES ('San Francisco', 43, 57, 0.0, '1994-11-29');
 
 
-- 可以不同順序,選擇性添加
INSERT INTO weather (date, city, temp_hi, temp_lo)
    VALUES ('1994-11-29', 'Hayward', 54, 37);
    
-- 加入位置  
INSERT INTO cities VALUES ('San Francisco', '(-194.0, 53.0)');

查詢資料庫 SELECT ? FROM table

-- 顯示全部表格
SELECT * FROM weather;

-- 查詢部分
SELECT city, temp_lo, temp_hi, prcp, date FROM weather;

-- 在查詢中加入運算式
SELECT city, (temp_hi+temp_lo)/2 AS temp_avg, date FROM weather;

-- 過濾條件 WHERE
SELECT * FROM weather
    WHERE city = 'San Francisco' AND prcp > 0.0;
    
-- 按照大小排序
SELECT * FROM weather
    ORDER BY city;   -- 後面加上DESC會到著過來排

交叉查詢 join

SELECT *
    FROM weather, cities
    WHERE city = name;
    
-- 指定條件查詢,city欄位(weather) = name欄位(cities) 就會加進來

SELECT city, temp_lo, temp_hi, prcp, date, location
    FROM weather, cities
    WHERE city = name;
    
-- 速寫法
SELECT *
    FROM weather INNER JOIN cities ON (weather.city = cities.name);

彙總查詢

SELECT max(temp_lo) FROM weather;  -- 查詢最大值

-- 查詢city而不是temp_lo
SELECT city FROM weather
    WHERE temp_lo = (SELECT max(temp_lo) FROM weather);
    
-- 這個查詢對每個城市都輸出一列的結果。
SELECT city, max(temp_lo)
    FROM weather
    GROUP BY city;

修改用UPDATE table SET condition WHERE condition

-- 在data 1994-11-28以後的資料,溫度全部-2
UPDATE weather
    SET temp_hi = temp_hi - 2,  temp_lo = temp_lo - 2
    WHERE date > '1994-11-28';
    

刪除用DELETE FROM table WHERE condition

DELETE FROM weather WHERE city = 'Hayward';  -- 把表格的city 欄位刪除

DELETE FROM weather -- 刪除全部表格

創建檢視表(當作一個資料表)

CREATE VIEW myview AS
    SELECT city, temp_lo, temp_hi, prcp, date, location
        FROM weather, cities
        WHERE city = name;
    
SELECT * FROM myview;

離開資料庫

\q

資料庫使用deno套件: https://deno.land/x/postgres@v0.14.2