PostgreSQL是一個關係數據庫管理系統,用於管理存儲在關係中的數據的系統。開發面向對象的數據庫。sql
每一個表都是命名的行集合。給定表的每一行都有相同的命名列表,每列都是特定的數據類型。儘管列在每行中都有固定的順序,但重要的是記住SQL不能以任何方式保證表中行的順序。數據庫
表被分組到數據庫中,由單個PostgreSQL服務器實例管理的數據庫集合構成數據庫集羣。服務器
#函數
官方下載安裝,已同步onedrivepost
https://www.postgresql.org/學習
文檔:https://www.postgresql.org/docs/11編碼
系統是win10 64
1.控制面板-用戶帳戶-> 添加用戶postgres,並賦予管理員權限 spa
初始化:postgresql
initdb.exe -D d:\postgresql\data -E UTF-8 --locale=chs -U postgres -W對象
-D :指定數據庫簇的存儲目錄E:\pgsql\data
-E :指定DB的超級用戶的用戶名postgres
--locale:關於區域設置(chinese-simplified-china)
-U :默認編碼格式chs
-W :爲超級用戶指定密碼的提示
啓動服務:pg_ctl start
中止服務:pg_ctl stop
圖形化界面:pgAdmin 4。須要先建立鏈接
環境變量PATH添加:D:\Software\PostgreSQL\bin
環境變量PGDATA添加:D:\Software\PostgreSQL\data
數據庫由客戶端和服務器組成。
建立數據庫:createdb -h localhost -U postgres mydb 默認用戶名postgres
刪除數據庫:dropdb mydb
訪問數據庫:psql mydb。而後能夠執行sql語句
建立新表
CREATE TABLE weather ( city varchar(80), temp_lo int, temp_hi int, prcp real, date date );
PostgreSQL支持基礎類型
刪除表:DROP TABLE weather;
插入數據:insert into weather values('舊金山',0,0,null,'2019-01-06');
從平面文件加載:
COPY weather FROM'/home/user/weather.txt';
查詢:SELECT * FROM weather ORDER BY city, temp_lo;
刪除重複行:SELECT DISTINCT city FROM weather;
錶鏈接:
SELECT weather.city, weather.temp_lo, weather.temp_hi, weather.prcp, weather.date, cities.location FROM weather, cities WHERE cities.name = weather.city;
SELECT * FROM weather INNER JOIN cities ON (weather.city = cities.name);
SELECT * FROM weather LEFT OUTER JOIN cities ON (weather.city = cities.name);
聚合函數:max(),不能使用在where中。可是能夠結合GROUP BY HAVING
SELECT city, max(temp_lo)
FROM weather
WHERE city LIKE 'S%' -- (1)
GROUP BY city
HAVING max(temp_lo) < 40;
更新:
UPDATE weather SET temp_hi = temp_hi - 2, temp_lo = temp_lo - 2 WHERE date > '1994-11-28';
刪除:
DELETE FROM weather WHERE city = 'Hayward';
#