0

Let me explain in the current context:

I have a tokens table which contains two columns: name and token.

For example, a user named "John" can be assigned an "updated" token.

However, I don't want to have two rows containing "John", "updated". However, there may be one line containing "Mathew", "updated", so primary keys cannot be used.

Is it possible to do this without executing two statements? Thank you in advance.

ookenos
  • 1
  • 1

3 Answers3

2

What you are looking at is making both name and token unique, you could add a line:

unique(name, token);  

Or making the primary key of the table being the pair (name, token) by writing this:

primary key(name, token);  

By doing this, you will only accept 1 unique row of both name and token.

CodeCop
  • 1
  • 2
  • 15
  • 37
0

You can use DISTINCT. For example:

select distinct name, token from tokens

DISTINCT is typed right after SELECT and applies to the whole row. It removes identical rows, showing a single one per combination.

The Impaler
  • 45,731
  • 9
  • 39
  • 76
0

Best solution would be to creat unique key from this columns. One unique key should be applied to both columns at the same time. It will prevent adding data or updating data in database which could cause duplication

Huseyn Zeynalov
  • 123
  • 2
  • 9