Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PostgreSQL中指定唯一约束_Postgresql - Fatal编程技术网

在PostgreSQL中指定唯一约束

在PostgreSQL中指定唯一约束,postgresql,Postgresql,我会感谢你对我的拦截器的帮助。我创建了一个新表,其中语句createtablecopy\u table作为SELECT*fromoriginal\u table,因为我希望第二个表具有与原始表类似的列 但是,第二个表没有原始表中的唯一约束。到目前为止,我可以将数据插入到导师表中,但任何需要唯一约束的操作都不起作用 我试图添加唯一约束,但它引发了以下错误: 位于或接近“UNIQUE”的语法错误 未编辑的代码 CREATE TABLE IF NOT EXISTS mentors AS SELECT

我会感谢你对我的拦截器的帮助。我创建了一个新表,其中语句
createtablecopy\u table作为SELECT*fromoriginal\u table
,因为我希望第二个表具有与原始表类似的列

但是,第二个表没有原始表中的唯一约束。到目前为止,我可以将数据插入到导师表中,但任何需要唯一约束的操作都不起作用

我试图添加唯一约束,但它引发了以下错误:
位于或接近“UNIQUE”的语法错误

未编辑的代码

CREATE TABLE IF NOT EXISTS mentors AS SELECT users.user_id AS mentor_id, 
first_name, last_name, email, address, password, bio, occupation, expertise, is_mentor, is_admin FROM users;
CREATE TABLE IF NOT EXISTS mentors AS SELECT users.user_id AS mentor_id(UNIQUE),first_name, last_name, email, address, password, bio, occupation, expertise, is_mentor, is_admin FROM users;
编辑的代码(添加唯一约束)

CREATE TABLE IF NOT EXISTS mentors AS SELECT users.user_id AS mentor_id, 
first_name, last_name, email, address, password, bio, occupation, expertise, is_mentor, is_admin FROM users;
CREATE TABLE IF NOT EXISTS mentors AS SELECT users.user_id AS mentor_id(UNIQUE),first_name, last_name, email, address, password, bio, occupation, expertise, is_mentor, is_admin FROM users;
我做错了什么?谢谢。

不允许在新表上定义任何约束

创建表格后立即使用,例如:

ALTER TABLE mentors ADD UNIQUE(mentor_id)