在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)