Jump to content

How to UNIQUE Two fields together in mysql?

- - - - -

  • Please log in to reply
2 replies to this topic

#1
lol33d

lol33d

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
hi guys

How to UNIQUE Two fields together in mysql?

table is MyISAM

i want unique 2 field together

for ex:
CREATE TABLE IF NOT EXISTS `tblmytt` (

  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,

  `user` varchar(25) NOT NULL,

  `res` char(20) NOT NULL,

  `ref` char(20) NOT NULL,

  PRIMARY KEY (`id`)

) ENGINE=MyISAM;

i want `user` and `res` unique

#Rec1
user: a

res: b
success
-------------------------------------
#Rec2
user: a

res: b
error
-------------------------------------
#Rec3
user: a

res: c
success
-------------------------------------
#Rec4
user: c

res: b
success
-------------------------------------
#Rec5
user: a

res: c
error
-------------------------------------
thank you

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
If table already exists:
CREATE UNIQUE INDEX index_name
ON table_name (column_name, column_name2)

During creation of the table I think you can add
UNIQUE (column_name, column_name2)


#3
lol33d

lol33d

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
thank you




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users