在 Python 中将自动增量主键添加到 PostgreSQL table
Add Autoincrement primary key to PostgreSQL table in Python
我正在尝试将自动增量主键添加到从 pandas 数据框转换而来的 sql table。
将数据帧转换为sql并添加alter table命令以添加自动增量主键的代码如下
test.to_sql('test_sql',schema='test_schema',con=conn,if_exists='replace',index=False)
with conn:
conn.execute("ALTER TABLE test_schema.test_sql ADD XPK_test_sql id SERIAL PRIMARY KEY;")
我收到以下错误:
ProgrammingError: (psycopg2.errors.SyntaxError) syntax error at or near "SERIAL"
我检查了语法,似乎是正确的。我哪里错了?还有其他方法吗?
我不太确定您要做什么:该列是否已经存在并且您想将其设为主键,还是要创建一个新的主键列?
将主键约束添加到现有列
这些是 the docs 创建约束的语法:
ALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ]
action [, ... ]
...
where action is one of:
...
ADD table_constraint [ NOT VALID ]
...
and table_constraint is:
[ CONSTRAINT constraint_name ]
{ CHECK ( expression ) [ NO INHERIT ] |
UNIQUE ( column_name [, ... ] ) index_parameters |
PRIMARY KEY ( column_name [, ... ] ) index_parameters |
EXCLUDE [ USING index_method ] ( exclude_element WITH operator [, ... ] ) index_parameters [ WHERE ( predicate ) ] |
FOREIGN KEY ( column_name [, ... ] ) REFERENCES reftable [ ( refcolumn [, ... ] ) ]
[ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE referential_action ] [ ON UPDATE referential_action ] }
[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
...
index_parameters in UNIQUE, PRIMARY KEY, and EXCLUDE constraints are:
[ INCLUDE ( column_name [, ... ] ) ]
[ WITH ( storage_parameter [= value] [, ... ] ) ]
[ USING INDEX TABLESPACE tablespace_name ]
所以你需要这样写
ALTER TABLE table_name ADD CONSTRAINT constraint_name PRIMARY KEY (column_name)
添加 table table_name
的(现有)列 column_name
作为主键,新约束命名为 constraint_name
.
添加新的主键列
文档是 on the same page:
ADD COLUMN [ IF NOT EXISTS ]
This form adds a new column to the table, using the same syntax as CREATE TABLE.
CREATE TABLE docs IMO 的可读性不是很好,但这里是相关部分:
CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name ( [
{ column_name data_type [ COMPRESSION compression_method ] [ COLLATE collation ] [ column_constraint [ ... ] ]
| table_constraint
| LIKE source_table [ like_option ... ] }
[, ... ]
] )
...
where column_constraint is:
...
PRIMARY KEY index_parameters
...
index_parameters in UNIQUE, PRIMARY KEY, and EXCLUDE constraints are:
[ INCLUDE ( column_name [, ... ] ) ]
[ WITH ( storage_parameter [= value] [, ... ] ) ]
[ USING INDEX TABLESPACE tablespace_name ]
...
所以,这是您要查找的语法:
ALTER TABLE table_name ADD COLUMN column_name serial PRIMARY KEY
我正在尝试将自动增量主键添加到从 pandas 数据框转换而来的 sql table。
将数据帧转换为sql并添加alter table命令以添加自动增量主键的代码如下
test.to_sql('test_sql',schema='test_schema',con=conn,if_exists='replace',index=False)
with conn:
conn.execute("ALTER TABLE test_schema.test_sql ADD XPK_test_sql id SERIAL PRIMARY KEY;")
我收到以下错误:
ProgrammingError: (psycopg2.errors.SyntaxError) syntax error at or near "SERIAL"
我检查了语法,似乎是正确的。我哪里错了?还有其他方法吗?
我不太确定您要做什么:该列是否已经存在并且您想将其设为主键,还是要创建一个新的主键列?
将主键约束添加到现有列
这些是 the docs 创建约束的语法:
ALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ]
action [, ... ]
...
where action is one of:
...
ADD table_constraint [ NOT VALID ]
...
and table_constraint is:
[ CONSTRAINT constraint_name ]
{ CHECK ( expression ) [ NO INHERIT ] |
UNIQUE ( column_name [, ... ] ) index_parameters |
PRIMARY KEY ( column_name [, ... ] ) index_parameters |
EXCLUDE [ USING index_method ] ( exclude_element WITH operator [, ... ] ) index_parameters [ WHERE ( predicate ) ] |
FOREIGN KEY ( column_name [, ... ] ) REFERENCES reftable [ ( refcolumn [, ... ] ) ]
[ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE referential_action ] [ ON UPDATE referential_action ] }
[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
...
index_parameters in UNIQUE, PRIMARY KEY, and EXCLUDE constraints are:
[ INCLUDE ( column_name [, ... ] ) ]
[ WITH ( storage_parameter [= value] [, ... ] ) ]
[ USING INDEX TABLESPACE tablespace_name ]
所以你需要这样写
ALTER TABLE table_name ADD CONSTRAINT constraint_name PRIMARY KEY (column_name)
添加 table table_name
的(现有)列 column_name
作为主键,新约束命名为 constraint_name
.
添加新的主键列
文档是 on the same page:
ADD COLUMN [ IF NOT EXISTS ]
This form adds a new column to the table, using the same syntax as CREATE TABLE.
CREATE TABLE docs IMO 的可读性不是很好,但这里是相关部分:
CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name ( [
{ column_name data_type [ COMPRESSION compression_method ] [ COLLATE collation ] [ column_constraint [ ... ] ]
| table_constraint
| LIKE source_table [ like_option ... ] }
[, ... ]
] )
...
where column_constraint is:
...
PRIMARY KEY index_parameters
...
index_parameters in UNIQUE, PRIMARY KEY, and EXCLUDE constraints are:
[ INCLUDE ( column_name [, ... ] ) ]
[ WITH ( storage_parameter [= value] [, ... ] ) ]
[ USING INDEX TABLESPACE tablespace_name ]
...
所以,这是您要查找的语法:
ALTER TABLE table_name ADD COLUMN column_name serial PRIMARY KEY