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

查看:17
本文介绍了如何为“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 中,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天全站免登陆