PostgreSQL
关系型数据库
官方网站:https://www.postgresql.org/
下载地址:https://www.enterprisedb.com/downloads/postgres-postgresql-downloads
Database
创建数据库
postgres=# \l
数据库列表
名称 | 拥有者 | 字元编码 | 校对规则 | Ctype | 存取权限
-----------+----------+----------+--------------------------------+--------------------------------+-----------------------
postgres | postgres | UTF8 | Chinese (Simplified)_China.936 | Chinese (Simplified)_China.936 |
template0 | postgres | UTF8 | Chinese (Simplified)_China.936 | Chinese (Simplified)_China.936 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | Chinese (Simplified)_China.936 | Chinese (Simplified)_China.936 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 行记录)
postgres=# create database mydb;
CREATE DATABASE
postgres=# \l
数据库列表
名称 | 拥有者 | 字元编码 | 校对规则 | Ctype | 存取权限
-----------+----------+----------+--------------------------------+--------------------------------+-----------------------
mydb | postgres | UTF8 | Chinese (Simplified)_China.936 | Chinese (Simplified)_China.936 |
postgres | postgres | UTF8 | Chinese (Simplified)_China.936 | Chinese (Simplified)_China.936 |
template0 | postgres | UTF8 | Chinese (Simplified)_China.936 | Chinese (Simplified)_China.936 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | Chinese (Simplified)_China.936 | Chinese (Simplified)_China.936 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 行记录)
使用数据库
postgres=# \c mydb;
您现在已经连接到数据库 "mydb",用户 "postgres".
删除数据库
postgres=# drop database mydb
DROP DATABASE
postgres=# \l
数据库列表
名称 | 拥有者 | 字元编码 | 校对规则 | Ctype | 存取权限
-----------+----------+----------+--------------------------------+--------------------------------+-----------------------
postgres | postgres | UTF8 | Chinese (Simplified)_China.936 | Chinese (Simplified)_China.936 |
template0 | postgres | UTF8 | Chinese (Simplified)_China.936 | Chinese (Simplified)_China.936 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | Chinese (Simplified)_China.936 | Chinese (Simplified)_China.936 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 行记录)
Table
创建表
create table posts (
id serial primary key,
title varchar(255) not null,
content text check(length(content) > 8),
is_draft boolean default true,
is_del boolean default false,
created_date timestamp default 'now'
);
表约束
not null : 不能为空
unique : 在所有数据中的值必须唯一
check : 字段设置条件
default : 默认值
primary key (not null, unique) : 主键
查看表
mydb=# \dt
关联列表
架构模式 | 名称 | 类型 | 拥有者
----------+---------+--------+----------
public | mytable | 数据表 | postgres
public | posts | 数据表 | postgres
mydb=# \d posts;
数据表 "public.posts"
栏位 | 类型 | 校对规则 | 可空的 | 预设
--------------+-----------------------------+----------+----------+-----------------------------------------------------------
id | integer | | not null | nextval('posts_id_seq'::regclass)
title | character varying(255) | | not null |
content | text | | |
is_draft | boolean | | | true
is_del | boolean | | | false
created_date | timestamp without time zone | | | '2020-09-19 11:30:36.484723'::timestamp without time zone
索引:
"posts_pkey" PRIMARY KEY, btree (id)
检查约束限制
"posts_content_check" CHECK (length(content) > 8)
删除表
mydb=# drop table mytable;
DROP TABLE
mydb=# \dt
关联列表
架构模式 | 名称 | 类型 | 拥有者
----------+-------+--------+----------
public | posts | 数据表 | postgres
表结构变更
-
alter table [tablename] …
-
create index …
-
drop index …
mydb=# \d users;