PostgreSQL - MySQL 中声明的表的等效版本?
我在 MySQL 中声明了下表:
CREATE TABLE IF NOT EXISTS `ci_sessions` (
`session_id` varchar(40) COLLATE utf8_bin NOT NULL DEFAULT '0',
`ip_address` varchar(16) COLLATE utf8_bin NOT NULL DEFAULT '0',
`user_agent` varchar(150) COLLATE utf8_bin NOT NULL,
`last_activity` int(10) unsigned NOT NULL DEFAULT '0',
`user_data` text COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`session_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
假设我的数据库已经使用 UTF-8 编码,那么 postgres 中该表的等效定义是什么?
I have the following table declared in MySQL:
CREATE TABLE IF NOT EXISTS `ci_sessions` (
`session_id` varchar(40) COLLATE utf8_bin NOT NULL DEFAULT '0',
`ip_address` varchar(16) COLLATE utf8_bin NOT NULL DEFAULT '0',
`user_agent` varchar(150) COLLATE utf8_bin NOT NULL,
`last_activity` int(10) unsigned NOT NULL DEFAULT '0',
`user_data` text COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`session_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
What would be an equivalent definition of the table in postgres, assuming my database is already using UTF-8 encoding?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
恕我直言,修饰符 IF NOT EXISTS 刚刚被添加到最新的 PostgreSQL 版本之一。
编辑:根据无名马的推荐,我将第四个属性从decimal(10)更改为int4。但我也更喜欢这里的时间戳(0)。如果您需要微秒精度,则仅使用时间戳而不带 (0)。
The modifier IF NOT EXISTS has IMHO just been added to one of the newest PostgreSQL versions.
EDIT: As unnamed horse recommended, I changed the 4th attribute from decimal(10) to int4. But I would prefer a timestamp(0) here, also. Use just timestamp without the (0) if you need microsecond precision here.
除了 Daniel 已经说过的内容之外:
如果您需要查询 IP 地址,最好使用本机 inet 数据类型:
http://www.postgresql.org/docs/current/static /datatype-net-types.html
列名last_activity 建议使用时间戳列或类似的列。我强烈建议使用
timestamp
数据类型。如果有所不同,那么
integer
比十进制更适合MySQL 的int(10)
。请注意,MySQL 中的 int(10) 并没有定义值范围,它只是用于显示目的的提示!
In addition to what Daniel has already said:
If you need to query on the IP address it might be better to use the native inet datatype for that:
http://www.postgresql.org/docs/current/static/datatype-net-types.html
The column name last_activity suggests a timestamp column or something similar. I would strongly recommend to use the
timestamp
data type for that.If it is something different, then
integer
is a better match to MySQL'sint(10)
than decimal.Note that int(10) in MySQL does not define the value range, it's merely a hint for displaying purposes!