user3732216
user3732216

Reputation: 1589

Unique Indexes for MySQL

I'm trying to find out what I'm missing for my syntax errors to get corrected so I can apply a unique index to a field in my table called vehicleDrive.

ALTER TABLE `vehicleDrive` ADD UNIQUE `name`;

I'm receiving this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

Upvotes: 1

Views: 958

Answers (4)

Vishnu Prasad
Vishnu Prasad

Reputation: 1

Here's the syntax:

alter table <table_name> 
  add unique index <index_name> (<column_name> (8000))

So try something like this:

ALTER TABLE vehicleDrive ADD UNIQUE INDEX index_unique_name (name)

Upvotes: 0

Dyrandz Famador
Dyrandz Famador

Reputation: 4525

try this:

ALTER TABLE `vehicleDrive` ADD UNIQUE (name);

SQL UNIQUE CONSTRAINT

Upvotes: 2

Gordon Linoff
Gordon Linoff

Reputation: 1269873

How about doing:

create unique index idx_vehicleDrive_name on vehicleDrive(name);

This also gives the index a name.

For your syntax, you need parentheses:

ALTER TABLE `vehicleDrive` ADD UNIQUE (name);

Upvotes: 1

TimoStaudinger
TimoStaudinger

Reputation: 42460

You may be missing some brackets around the column name:

ALTER TABLE vehicleDrive ADD UNIQUE (name)

Upvotes: 0

Related Questions