在PostgreSQL中创建表格

78

我不明白这个查询有什么问题?查询工具无法在PostgreSQL中创建表。

CREATE TABLE article (
article_id bigint(20) NOT NULL auto_increment,
article_name varchar(20) NOT NULL,
article_desc text NOT NULL,
date_added datetime default NULL,
PRIMARY KEY (article_id)
);

错误:在“(”附近语法错误 SQL状态:42601 字符:41 - user721588
1
我也遇到了错误:语法错误在或接近“(”处 第2行: article_id bigint(20)NOT NULL auto_increment, - mmmmmm
5个回答

151

首先,bigint(20) not null auto_increment 不起作用,只需使用 bigserial primary key。然后,在 PostgreSQL 中,datetimetimestamp。总之:

CREATE TABLE article (
    article_id bigserial primary key,
    article_name varchar(20) NOT NULL,
    article_desc text NOT NULL,
    date_added timestamp default NULL
);

4
为什么auto_increment是MySQL的一个特性,而Postgres使用serial列实现相同的目的。 - Brad Koch
4
是和不是。PostgreSQL提供了“serial”和“bigserial”。因为问题中包含“bigint(20)”,所以我在我的答案中选择了“bigserial”。在这种情况下,这是更好的匹配。 - A.H.
2
考虑到 OP 显然的用例是博客文章引擎,最大的串行大小为 20 亿条目(serial),除非他非常勤奋,否则应该足够了。但是,即便如此,bigserial 更接近于 bigint。 - fatal_error

8
-- Table: "user"

-- DROP TABLE "user";

CREATE TABLE "user"
(
  id bigserial NOT NULL,
  name text NOT NULL,
  email character varying(20) NOT NULL,
  password text NOT NULL,
  CONSTRAINT user_pkey PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE "user"
  OWNER TO postgres;

4

bigint(20) not null auto_increment 替换为 bigserial not null,并将 datetime 替换为 timestamp


9
创建表格将会成功。 - sega_sai

0
使用以下脚本创建带有前缀的 N 个表。 此代码使用 for 循环和变量创建以前缀 'sbtest' 开头的 10 个表,即 sbtest1、sbtest2...sbtest10。

create_table.sql

do $$
    DECLARE myvar integer;
begin
    for myvar in 1..10 loop
        EXECUTE format('CREATE TABLE sbtest%s (
        id SERIAL NOT NULL,
        k INTEGER NOT NULL,
        c CHAR(120) NOT NULL,
        pad CHAR(60) NOT NULL,
        PRIMARY KEY (id))', myvar);
    end loop;
end; $$

运行命令:psql -U 用户名 -d 数据库名 -f create_table.sql

命令:\d+ sbtest

id | k | c | pad
----+---+---+-----
(0 rows)

                                                   Table "public.sbtest1"
 Column |      Type      | Collation | Nullable |               Default               | Storage  | Stats
 target | Description
--------+----------------+-----------+----------+-------------------------------------+----------+------
--------+-------------
 id     | integer        |           | not null | nextval('sbtest1_id_seq'::regclass) | plain    |
        |
 k      | integer        |           | not null |                                     | plain    |
        |
 c      | character(120) |           | not null |                                     | extended |
        |
 pad    | character(60)  |           | not null |                                     | extended |
        |
Indexes:
    "sbtest1_pkey" PRIMARY KEY, btree (id)
Access method: heap

-5
请尝试这个:
CREATE TABLE article (
  article_id bigint(20) NOT NULL serial,
  article_name varchar(20) NOT NULL,
  article_desc text NOT NULL,
  date_added datetime default NULL,
  PRIMARY KEY (article_id)
);

在Postgres中,bigint(20)是无效的。此外,您不能同时指定bigintserial。请参阅已接受的答案以获取正确的语法。 - user330315

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接