1

Is it possible to make a procedure that update rows in a mysql table without a primary key?

I have a table like this, that correspond to teachers's schedules:

TEACHER_ID | WEEK_DAY |  HOUR | CLASS

1234567         2        11:30  MAT001
1234567         2        13:00  HIS001

I want to load a .csv file with their schedules and that the procedure finds if the rows on it already exist on the table, update them or, in the case that don't exist, insert them.

My table doesn't have a primary, because the system where we got the .csv files doesn't create one.

What I thought was combine the fields "TEACHER _ID","WEEK_DAY" and "HOUR" as a key when use "WHERE" for the update/insert on the table, but I can't figure out how to do it.

Can achieve what I want? Can you help me?

Thank you in advance for your answers!

(I apologize in advance for the mistakes on my grammar errors)

8
  • It's hard to help you without knowing exactly what you need, but wouldn't the whole row (i.e. every column) be the merge condition? Or are you not concerned about the class? Or is the class what you're trying to update? You could help by showing existing data and what you would expect to happen.
    – lc.
    Jul 12, 2013 at 1:43
  • There's nothing unique to identify rows, it's going to be a very daunting task.
    – DevlshOne
    Jul 12, 2013 at 1:43
  • 1
    Show your UPDATE query. Try to write it.
    – sectus
    Jul 12, 2013 at 1:44
  • 1
    Since I guess teacher classes can dissappear as well as change classes, it might be better to just clear the data and re-import it.
    – apartridge
    Jul 12, 2013 at 1:44
  • What is the uniqueness for each row? Is it the Class, Class+Hour, Class+Hour+Week_Day, Class+Teacher_ID? You could update without a primary key, but you would need to define what is distinct for each row, and that would not change on an Update.
    – Sean
    Jul 12, 2013 at 1:55

1 Answer 1

1

You can create a UNIQUE KEY on TEACHER_ID,WEEK_DAY,HOUR http://dev.mysql.com/doc/refman/5.0/en/create-index.html

CREATE UNIQUE KEY classes ON your_table_name (TEACHER_ID,WEEK_DAY,HOUR)`

Then you can use INSERT ... ON DUPLICATE KEY UPDATE http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

INSERT INTO your_table_name (TEACHER_ID,WEEK_DAY,HOUR,CLASS) VALUES (1234567,2,11:30,MAT001)
 ON DUPLICATE KEY UPDATE CLASS=MAT001;
2
  • Thank you! That's what I needed! But, How can I update the rows for all the teachers differently for each one? UPDATE CLASS=MAT001 set all the classes into MAT101.
    – e.vejar
    Jul 16, 2013 at 18:55
  • You would change it to the value of the 4th cell of your csv row. So depending on how you are setting it, it could be something like - VALUES (cell_1,cell_2,cell_3,cell_4) ON DUPLICATE KEY UPDATE CLASS=cell_4
    – Sean
    Jul 16, 2013 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.