PostgreSQL主键自动增量在C ++中崩溃 [英] PostgreSQL primary key auto increment crashes in C++
问题描述
使用C ++在PostgreSQL中创建整数主键自动增量字段的正确语法是什么?
我以
开头db-> ExecuteSQL(CREATE TABLE mytable(\mytableid\INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
这会进行编译,但进程崩溃,不会创建任何字段。
db-> ExecuteSQL(CREATE TABLE mytable(\mytableid\serial PRIMARY KEY NOT NULL,
这样可以正确地创建字段。
我需要
NOT NULL
或者这不是必需的serial
?
这是Postgres中创建主键字段最好的语法和方法吗?解决方案你不需要
NOT NULL
=http://www.postgresql.org/docs/current/interactive/ddl-constraints.html =nofollow>PRIMARY KEYS
。根据文档:
技术上,主键约束仅仅是
唯一约束和非空约束的组合。
此外,
serial
也意味着NOT NULL 。它不是数据类型本身,只是一个符号方便的整数NOT NULL
与附加的序列。
这是完美的语法:
CREATE TABLE mytable(mytableid serial PRIMARY KEY);
您不需要双引号列名称,只要您不想使用混合案例标识符,保留字或非法字符。我建议使用法律,小写标识符专门使你的代码不容易出错(和你的生活更简单)。
What is the correct syntax to create an integer primary key auto incremental field in PostgreSQL using C++?
I started with
db->ExecuteSQL("CREATE TABLE mytable (\"mytableid\" INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
This compiles but the process crashes and no field is created.
db->ExecuteSQL("CREATE TABLE mytable (\"mytableid\" serial PRIMARY KEY NOT NULL,
This works and does create the field correctly.
Do I need the
NOT NULL
or is this not necessary withserial
?
Is this the best syntax and method in Postgres for primary key field creation?解决方案You do not need the
NOT NULL
. It is implied when you define the columnPRIMARY KEYS
. Per documentation:Technically, a primary key constraint is simply a combination of a unique constraint and a not-null constraint.
In addition,
serial
also implies NOT NULL. It's not a data type per se, just a notational convenience forinteger NOT NULL
with an attached sequence.So this is perfect syntax:
CREATE TABLE mytable (mytableid serial PRIMARY KEY);
You don't need to double quote the column name as long as you don't want to use mixed case identifiers, reserved words or "illegal" characters. I would advise to use legal, lower case identifiers exclusively to make your code less error-prone (and your life simpler).
这篇关于PostgreSQL主键自动增量在C ++中崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!