Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mySQL - linking two columns as unique 1

Status
Not open for further replies.

soundmind

Technical User
Jun 30, 2003
16
US
Hi y'all,

Is there a way to link two columns within a table to be unique?

For example, the columns in a table "users" are:
user_id(primary key, auto_increment), user_name, date

I'd like to have user_name and date linked to each other so there could be only one entry per user_name per day. Is this possible in mySQL?

Thank you!!!

 
Don't use "date" as the name of a MySQL column. "date" is a MySQL reserved word.

If the table already exists, to add a UNIQUE index combining the user_name and user_date columns (
ALTER TABLE tablename
ADD UNIQUE indexname (user_name, user_date)


To add the UNIQUE index to a table at table creation (
CREATE TABLE tablename
(
user_id int unsigned auto_increment primary key,
user_name varchar(25) not null,
user_date date,
UNIQUE indexname (user_name, user_date)
}

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top