一个表里面只能存在一个主键,但是可以有多个外键。在pg中建议每个表都设置主键,但是这并不强制。
主键的创建方法:
1.表约束.
createtableproduct( product_nointeger,binteger,cinteger,pricenumeric,nametext,constraintproduct_no_pkeyprimarykey(product_no) );
上面的product_no_pkey是一个别名。
2.字段约束
createtableproduct( product_nointegerprimarykey,cinteger pricenumeric,nametext )
字段约束的创建是在你需要创建为主键的字段后面加上primary key就可以了,同样的你也可以给他起一个别名
createtableproduct( product_nointegerconstrainttest_keyprimarykey,--test_key在这里就是一个别名 binteger,nametext )
在官方文档上面有一句话:
Technically,a primary key constraint is simply a combination of a unique constraint and a not-null constraint.
翻译:从技术上来说, 主键就是唯一约束与非空约束的组合
createtableproduct3( product_nointegeruniquenotnull,nametext )
createtableproduct3( product_nointegerprimarykey,nametext )
上面两个是等价的。
createtableproduct3( product_nointegeruniquenotnull,bintegeruniquenotnull,bintegerprimarykey,255);">就好比是上的两个例子一样,第二例子是不能通过。
会提示错误:ERROR: multiple primary keys for table "product3" are not allowed
sql 状态: 42P16
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。