
正文
postgresql建表时的数据类型的简单介绍
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
四、PostgreSQL常用数据类型(精简)
备注:
(1)numeric 不指定长度,可以保留的整数位和小数位很大;
(2)numeric(6,4)表示精度(precision,所有数字位的个数)为6,标度(scale,小数点右边所有小数位的个数)为4,例如23.5141
备注:varchar不指定长度,可以存储最大长度(1GB)
备注:保留两位小数,超过位数采用四舍五入法进行截断
1.
相关问答
Q1: postgresql数据库表哪个类型代表数组
PostgreSQL的二进制数据类型为bytea,可最多保存2G的数据。在ADO、ODBC等接口,可通过带参数化的插入SQL语句上传二进制。 然而在某些接口、SQL语句无法进行参数绑定,或者某些语言没有二进制的类型,或者接口的不兼容等原因
Q2: postgresql数据类型test相当于oracle中的什么数据类型
postgresql
数据类型text类型相当于oracle中的varchar2(n)
这里的n需要自己指定
text类型在postgres中是没有长度限制的,这是二者的区别。
Q3: 怎样用postgresql建表,建数据库
PostgreSQL的CREATE TABLE语句是用来在任何指定的的数据库中创建一个新表。 yiibai.com
语法
CREATE TABLE语句的基本语法如下:
CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns )
);
CREATE TABLE是告诉数据库系统关键字,创建一个新的表。独特的名称或标识如下表CREATE TABLE语句。当前数据库中的表最初是空的,并且将所拥有的用户发出的命令。
然后在括号内来定义每一列的列表,在表中是什么样的数据类型。其语法变得更清晰,下面的例子。
实例
下面是一个例子,它创建了一个公司ID作为主键的表和NOT NULL的约束显示这些字段不能为NULL,同时创建该表的记录:
CREATE TABLE COMPANY(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL
);
让我们创建一个表,在随后的章节中,我们将在练习中使用:
CREATE TABLE DEPARTMENT(
ID INT PRIMARY KEY NOT NULL,
DEPT CHAR(50) NOT NULL,
EMP_ID INT NOT NULL
);
可以验证已成功创建使用\d命令,将用于列出了附加的数据库中的所有表。
testdb-# \d
以上PostgreSQL的表会产生以下结果:
List of relations
Schema | Name | Type | Owner
--------+------------+-------+----------
public | company | table | postgres
public | department | table | postgres
(2 rows)
使用\d表名来描述每个表如下所示:
testdb-# \d company
以上PostgreSQL的表会产生以下结果:
Table "public.company"
Column | Type | Modifiers
-----------+---------------+-----------
id | integer | not null
name | text | not null
age | integer | not null
address | character(50) |
salary | real |
join_date | date |
Indexes:
"company_pkey" PRIMARY KEY, btree (id)
Q4: PostgreSQL 数据类型介绍(五)OID的理解
那oid在哪儿?到底为什么会出现这种情况 ?
来看看postgres官网对 oid的介绍:
根据stackoverflow的高票用户的回答:
*OIDs basically give you a built-in, globally unique id for every row, contained in a system column (as opposed to a user-space column). That's handy for tables where you don't have a primary key, have duplicate rows, etc. For example, if you have a table with two identical rows, and you want to delete the oldest of the two, you could do that using the oid column.
In my experience, the feature is generally unused in most postgres-backed applications (probably in part because they're non-standard), and their use is essentially deprecated :
In PostgreSQL 8.1 default_with_oids is off by default; in prior versions of PostgreSQL, it was on by default.
The use of OIDs in user tables is considered deprecated, so most installations should leave this variable disabled. Applications that require OIDs for a particular table should specify WITH OIDS when creating the table. This variable can be enabled for compatibility with old applications that do not follow this behavior.
大意是你要是有个表没有用主键,这时候可以把oid充当为主键使用,当然这是没办法的办法。
总结: oid是给内部表做标识用的,不推荐使用。 建议将 default_with_oids 设置为off。 建表的时候,如果想使用主键,请自行建立。oid本身大小固定的,万一 行数超过了oid 的最大限制数(4 byte int),那就无法插入新行了。
关于postgresql建表时的数据类型和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







