1

I am writing a forum for the company I work for. I am in the middle of creating the database tables and I have set up all my primary keys but I want user_name to be unique so no two users can have the same username when registering.

Could someone shed some light on how this can be done, I have a basic level of knowledge when it comes to SQL.

Thanks!

CodingNewbie
  • 37
  • 1
  • 1
  • 8

2 Answers2

0

create unique index. That will force unique values for that column.

CREATE UNIQUE INDEX index_name ON table_name (column_name)

Satish
  • 713
  • 1
  • 5
  • 18
  • I would have beaten you to be first answerer, but the answer is so short I had to lengthen it a few times, and then I had to fill out a captcha! – Kind Contributor Jan 17 '13 at 13:38
  • I have seen, while I have been creating them in SQL server management studio that you can do it by right-clicking on the field and going to indexes/keys I think it is but then I don't know the syntax to enter to make the field unique. – CodingNewbie Jan 17 '13 at 13:43
0

Create a unique Index on user_name.

In SQL Management Studio:

  1. right click the Table in question, left click Design
  2. Right click on some white-space (or the junction of the design table), left click Indexes/Keys
  3. Click the Add button to create a new index
  4. Make sure the new one is selected in the list on the left
  5. Click the Triple dot button in the value area for the columns property of the index
  6. Select user_name from the drop-down box
  7. Click OK
  8. Set Is Unique to True
  9. Click Close
  10. Press Ctrl+S to save (will run the appropriate background scripts)

Update:

From SQL Server 2005 How Create a Unique Constraint?

In SQL Server Management Studio Express:

  1. Right-click table, choose Modify
  2. Right-click field, choose Indexes/Keys...
  3. Click Add
  4. For Columns, select the field name you want to be unique.
  5. For Type, choose Unique Key.
  6. Click Close, Save the table.
Community
  • 1
  • 1
Kind Contributor
  • 17,547
  • 6
  • 53
  • 70