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

**URL:** <https://forum.freecodecamp.org/t/i-need-help-with-my-sql-code-how-do-i-add-a-unique-constraint-to-a-table/467008>\
**Category:** General\
**Created:** [June 29, 2021, 4:03am UTC](https://forum.freecodecamp.org/t/i-need-help-with-my-sql-code-how-do-i-add-a-unique-constraint-to-a-table/467008 "2021-06-29T04:03:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![uwemneku](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/uwemneku/32/150257_2.png) [@uwemneku](https://forum.freecodecamp.org/u/uwemneku)\
**Post date:** [June 29, 2021, 4:03am UTC](https://forum.freecodecamp.org/t/i-need-help-with-my-sql-code-how-do-i-add-a-unique-constraint-to-a-table/467008/1 "2021-06-29T04:03:54Z")

</div>

I ran the following SQL code

```auto
-- 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

```auto
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

---

<div class="post-metadata">

**Author:** ![chichkenmomos](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/chichkenmomos/32/222348_2.png) [@chichkenmomos](https://forum.freecodecamp.org/u/chichkenmomos)\
**Post date:** [June 29, 2021, 6:31am UTC](https://forum.freecodecamp.org/t/i-need-help-with-my-sql-code-how-do-i-add-a-unique-constraint-to-a-table/467008/2 "2021-06-29T06:31:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![helanyu123](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/helanyu123/32/224182_2.png) [@helanyu123](https://forum.freecodecamp.org/u/helanyu123)\
**Post date:** [July 5, 2021, 7:46am UTC](https://forum.freecodecamp.org/t/i-need-help-with-my-sql-code-how-do-i-add-a-unique-constraint-to-a-table/467008/3 "2021-07-05T07:46:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [January 3, 2022, 7:47pm UTC](https://forum.freecodecamp.org/t/i-need-help-with-my-sql-code-how-do-i-add-a-unique-constraint-to-a-table/467008/4 "2022-01-03T19:47:07Z")

</div>

Closed on January 3, 2022, 7:47pm UTC

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