深入浅出 PostgreSQL

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;
                               
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值