Question

I have an sql table in which there are two column State and SongInState. Now I am having a scenario here, where State and SongInState columns can have any non unique value but any State can't have same SongInState value. Table structure

ID     State     SongInState
1      AR        Dream on
2      MN        Dream on
3      TX        Hard sun
4      AR        I got a name

See i can have same state and songInState but not SongInState can be repeated for Same state This should not be happen..

ID     State     SongInState
 1      AR        Dream on
 2      AR        Dream on

AR can't have the same song but another state can have it. Can i do that? I am using mySql and JAVA(just to inform). Any Help would be appreciated.

Was it helpful?

Solution

Create a multicomun unique index on State and SongInState

Put in the table create query

UNIQUE KEY `UniqueStateSong` (`State` ,  `SongInState`)

or if it's already created

ALTER TABLE  `mytable` ADD UNIQUE  `UniqueStateSong` (  `State` ,  `SongInState` )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top