I need help with my SQL code. How do i add a unique constraint to a table?

I ran the following SQL code

-- create a table
CREATE TABLE DEPT (
  ID NUMBER(7) PRIMARY KEY,
  NAME VARCHAR2(25)
);
-- -- insert some values
INSERT INTO DEPT (ID , NAME) VALUES  (40, 'Sales') ;   

ALTER TABLE DEPT ADD Manager_ID NUMBER(5) ;
ALTER TABLE DEPT ADD City VARCHAR2(30);

This works fine but when i try to add a unique constraint to the city column with the following command

ALTER TABLE DEPT
ADD CONSTRAINT UC_ffg UNIQUE(city);

I get the following error
1 near “CONSTRAINT”: syntax error

How can i add a unique constraint to a column

capitalize the “C” in City. SQL is case sensitive when it comes to tables and column names

1 Like

Just a reminder, if your table has some data that doesn’t fit the constraints you gonna add, you will get an error.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.