1

I would appreciate your help with the blocker I have. I created a new table with the statement CREATE TABLE copy_table AS SELECT * FROM original_table because I wanted the second table with similar columns as the original table.

However, the second table came without the UNIQUE constraint that was in the original table. So far, I can insert data into the mentors' table but anything that required UNIQUE constraint is not working.

I tried to add the UNIQUE constraints but it's throwing this error: syntax error at or near "UNIQUE"

Unedited code

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;

Edited code(Adding UNIQUE constraint)

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;

What am I doing wrong? Thanks.

1 Answer 1

2

CREATE TABLE AS does not allow to define any constraints on the new table.

Use ALTER TABLE just after the table is created, e.g.:

ALTER TABLE mentors ADD UNIQUE(mentor_id)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.