我尝试将 SQLite 数据库转换为 Postgres,我不知道 DATETIME 是否等于 postgres 数据库
I try to convert SQLite database to Postgres, I dont know DATETIME equal for postgres database
我尝试将 SQLite 数据库转换为 Postgres,我不知道 Postgres 数据库的 DATETIME 是否相等
全行是:
CREATE TABLE log
(
id SERIAL PRIMARY KEY NOT NULL UNIQUE,
date DATETIME,
msg TEXT
);
SQLite does not have a storage class set aside for storing dates
and/or times. Instead, the built-in Date And Time Functions of SQLite
are capable of storing dates and times as TEXT, REAL, or INTEGER
values:
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS"). REAL as Julian
day numbers, the number of days since noon in Greenwich on November
24, 4714 B.C. according to the proleptic Gregorian calendar. INTEGER
as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
取自https://sqlite.org/datatype3.html
但是在 Postgres 中你可以使用 timestamp/timestamptz 数据类型,这在 here
上很容易证明
还有table代码好像很奇怪
id SERIAL PRIMARY KEY NOT NULL UNIQUE
为什么要同时使用主键和唯一键?
您可以在 Postgres 中使用以下代码
CREATE TABLE log
(
id SERIAL PRIMARY KEY,
date timestamp,
msg TEXT
);
您可以使用 Postgresql 的 DATE
or TIMESTAMP
data type,它也存储时间。
此外,您可以使用 VARCHAR(n)
而不是 TEXT
作为 msg
的数据类型,除非您不知道该列中存储的值的最大长度:
CREATE TABLE log (id SERIAL PRIMARY KEY, date DATE, msg VARCHAR(255));
我尝试将 SQLite 数据库转换为 Postgres,我不知道 Postgres 数据库的 DATETIME 是否相等 全行是:
CREATE TABLE log
(
id SERIAL PRIMARY KEY NOT NULL UNIQUE,
date DATETIME,
msg TEXT
);
SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS"). REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar. INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
取自https://sqlite.org/datatype3.html
但是在 Postgres 中你可以使用 timestamp/timestamptz 数据类型,这在 here
上很容易证明还有table代码好像很奇怪
id SERIAL PRIMARY KEY NOT NULL UNIQUE
为什么要同时使用主键和唯一键?
您可以在 Postgres 中使用以下代码
CREATE TABLE log
(
id SERIAL PRIMARY KEY,
date timestamp,
msg TEXT
);
您可以使用 Postgresql 的 DATE
or TIMESTAMP
data type,它也存储时间。
此外,您可以使用 VARCHAR(n)
而不是 TEXT
作为 msg
的数据类型,除非您不知道该列中存储的值的最大长度:
CREATE TABLE log (id SERIAL PRIMARY KEY, date DATE, msg VARCHAR(255));