【问题标题】:How to wget info into Postgresql database and create a table for it?如何将信息输入 Postgresql 数据库并为其创建表?
【发布时间】:2017-12-26 06:32:00
【问题描述】:
我目前正在为自己做一个辅助项目,以便学习如何使用 postgresql 和读取数据库日志。该项目的目标是创建一个数据库,用于检查网站的关键字,数据库将记录从网站上找到或未找到该词的次数。每次找到该单词时,都会添加一个时间戳,告诉我该单词是在什么时间和日期找到的。
到目前为止,我已经创建了我的数据库,但我被困在创建表中,我不知道如何将信息放入表中。我正在 ubuntu linux 系统上构建这个 postgresql。
【问题讨论】:
标签:
sql
ddl
postgresql-9.5
【解决方案1】:
使用 SQL 创建表。
在 Postgres 10 和其他一些数据库中:
CREATE TABLE word_found_ (
id_ BIGINT -- 64-bit number for virtually unlimited number of records.
GENERATED ALWAYS AS IDENTITY -- Generate sequential number by default. Tag as NOT NULL.
PRIMARY KEY , -- Create index to enforce UNIQUE.
when_ TIMESTAMP WITH TIME ZONE. -- Store the moment adjusted into UTC.
DEFAULT CURRENT_TIMESTAMP , -- Get the moment when this current transaction began.
count_ INTEGER -- The number of times the target word was found.
) ;
在 Postgres 10 之前,使用 SERIAL 而不是 GENERATED ALWAYS AS IDENTITY。或者,在 Stack Overflow 上搜索有关使用 UUID 作为主键的信息,这些值由 ossp-uuid 扩展默认生成。
为您采集的每个样本插入一行。
INSERT INTO word_found_ ( count_ )
VALUES ( 42 )
;