英文:
psql syntax errors when trying to run sql script to create a table
问题
I was provided a .sql
script containing a query to create a table. I trust the source of the script, so I am wondering if the problem is how/where I'm running it. I'm running it in psql, using a call like \i c:/Users/.../sql_script.sql
. Here is the code in the script, modified slightly to make it look generic:
CREATE TABLE dbo.mytable (
id int NOT NULL IDENTITY(1,1),
userID int NULL,
Probability DECIMAL(18,8) NULL,
Conv_Thresh1 int NULL,
Conv_Thresh2 int NULL,
DateAddedToDB date NULL
);
ALTER TABLE dbo.mytable ADD CONSTRAINT some_text_i_dont_understand PRIMARY KEY (id);
CREATE NONCLUSTERED INDEX i1 ON mytable (userID);
And here are the errors I'm seeing:
.sql:8: ERROR: syntax error at or near "IDENTITY"
LINE 2: id int NOT NULL IDENTITY(1,1),
^
ALTER TABLE
and
.sql:12: ERROR: syntax error at or near "NONCLUSTERED"
LINE 1: CREATE NONCLUSTERED INDEX i1 ON mytable ...
Is this a problem with the script itself? I see similar errors trying to run other "table creation query" sql scripts that have been provided to me, so I'm wondering if it's more about me not understanding psql/postgres. Thank you.
英文:
I was provided a .sql
script containing a query to create a table. I trust the source of the script, so I am wondering if the problem is how/where I'm running it. I'm running it in psql, using a call like \i c:/Users/.../sql_script.sql
. Here is the code in the script, modified slightly to make it look generic:
CREATE TABLE dbo.mytable (
id int NOT NULL IDENTITY(1,1),
userID int NULL,
Probability DECIMAL(18,8) NULL,
Conv_Thresh1 int NULL,
Conv_Thresh2 int NULL,
DateAddedToDB date NULL
);
ALTER TABLE dbo.mytable ADD CONSTRAINT some_text_i_dont_understand PRIMARY KEY (id);
CREATE NONCLUSTERED INDEX i1 ON mytable (userID);
And here are the errors I'm seeing:
.sql:8: ERROR: syntax error at or near "IDENTITY"
LINE 2: id int NOT NULL IDENTITY(1,1),
^
ALTER TABLE
and
.sql:12: ERROR: syntax error at or near "NONCLUSTERED"
LINE 1: CREATE NONCLUSTERED INDEX i1 ON mytable ...
Is this a problem with the script itself? I see similar errors trying to run other "table creation query" sql scripts that have been provided to me, so I'm wondering if it's more about me not understanding psql/postgres. Thank you
答案1
得分: 1
Q: 这是脚本本身的问题吗?
在某种程度上是的。这个脚本显然是为 Microsoft SQL Server 设计的,其语法和特性与 PostgreSQL 在许多方面不同。
英文:
Q: Is this a problem with the script itself?
In a way, yes. This script is apparently meant for Microsoft SQL Server, whose syntax and features differ from PostgreSQL in many aspects.
答案2
得分: 0
以下是翻译好的部分:
"该语法用于 MSSQL Server。对于 PostgreSQL,它可能会是这样的:
CREATE TABLE mytable (
id INT GENERATED ALWAYS AS IDENTITY
);
更多关于在 PostgreSQL 中使用 identity 的信息,请参阅 identity with postgres。"
英文:
The syntax looks for MSSQL Server. For postgres it would have been like:
CREATE TABLE mytable (
id INT GENERATED ALWAYS AS IDENTITY
);
More info on using identity with postgres.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论