SQL - generate primary key from columns

Hi… I’m learning SQL right now… (learning by doing) And my goal is to generate a Primary Key which should be insert in the Column “ID”.

But my Main-Problem is to generate it, becouse:
It should be generatted out of other columns in the row to prevent multiple entries for the same “state” of row…

Like this:

| ID | column1 | column2 | column 3 |
---------------------------------
| 01 | Text1 | 05 | 3.04 |
| 02 | Text2 | 06 | 3.04 |
| 03 | Text1 | 05 | 5.10 |
| 01 | Text1 | 05 | 3.04 | -> “OH… This is the same as the first entry. I dont add this!”
-----------------------------------

So I want to calculate the ID from the contents of the columns 1 to 3

Is that possible in any way?

then you should use unique constrain, this constrain can also be used to verify unique together (so to speak) as demonstrated here:

https://stackoverflow.com/questions/635937/how-do-i-specify-unique-constraint-for-multiple-columns-in-mysql

then make the id’s unique and auto incremental like “normal”

1 Like

Thenks… I did this to my table… but now: How do I let (phpmyadmin) enter this unique key to the “ID-Colum”?

when you create the table, you can specify the constrains:

id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,

oh,… I missunderstood something… the unique key is just in background… i thought I have to generate a unique key for all the columns and then enter exactly this key to “ID”… but “ID” is just a primary which auto increment and unique key hasnt to be visible for any reason…
Thanks for your help :slight_smile:

There are two parts/layers here:

  1. the columns
  2. the constrains

A tool like phpmyadmin will often also include an option for you to see/inspect the constrains. Don’t ask me, haven’t used phpmyadmin in ages, you will have to google how to see the constrains.

1 Like