如何为“ int”设置大小限制? PostgreSQL 9.5中的数据类型 [英] How can I set a size limit for an "int" datatype in PostgreSQL 9.5

查看:1073
本文介绍了如何为“ int”设置大小限制? PostgreSQL 9.5中的数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用MySQL使用SQL编写的PostgreSQL,我只是希望用这段有效的SQL代码创建一个表:

I am experimenting with PostgreSQL coming from SQL using MySQL and I simply wish to create a table with this piece of code which is valid SQL:

CREATE TABLE flat_10
(
  pk_flat_id INT(30) DEFAULT 1,
  rooms      INT(10) UNSIGNED NOT NULL,
  room_label CHAR(1) NOT NULL,

  PRIMARY KEY (flat_id)
);

我遇到错误

ERROR:    syntax error at or near "("
LINE 3:   pk_flat_id integer(30) DEFAULT 1,

我在网上进行了搜索,没有找到答案,我似乎无法在PostgreSQL手册中找到答案。我在做什么错了?

I have conducted searches on the web and found no answer and I cant seem to find an answer in the PostgreSQL manual. What am I doing wrong?

我明确地想对可以插入 pk_flat_id字段中的位数设置限制

I explicitly want to set a limit to the number of digits that can be inserted into the "pk_flat_id" field

推荐答案


我明确地想为可插入 pk_flat_id字段中的位数设置限制

您当前的表定义不会以任何方式强加大小限制,在MySQL中,<$对于在显示时显示在列显示宽度上的应用程序,c $ c> int 数据类型只是一个提示

Your current table definition does not impose a "size limit" in any way. In MySQL the parameter for the intdata type is only a hint for applications on the display width of the column when displaying it.

您可以将值2147483647存储在 int(1)没有任何问题。

You can store the value 2147483647 in an int(1) without any problems.

如果您想限制要存储在整数列中的值您可以使用检查约束:

If you want to limit the values to be stored in an integer column you can use a check constraint:

CREATE TABLE flat_10
(
  pk_flat_id bigint DEFAULT 1,
  rooms      integer NOT NULL,
  room_label CHAR(1) NOT NULL,

  PRIMARY KEY (flat_id), 
  constraint valid_number 
      check (pk_flat_id <= 999999999)
);

这篇关于如何为“ int”设置大小限制? PostgreSQL 9.5中的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆