I have a database Class_Books which links ISBNs (from Books table) to Class_ID's (from Classes table). I'm changing my Books table so the primary key is a Book_ID (autoincrement INT) instead of the ISBN. Is there a way to update Class_Books so it uses Book_ID now?
+1
A:
MySQL supports a multi-table UPDATE syntax that makes this type of work a lot easier.
ALTER TABLE Class_Books ADD COLUMNS Book_Id INT; -- nullable
UPDATE Books b JOIN Class_Books cb ON b.ISBN = cb.ISBN
SET cb.Book_Id = b.Book_Id;
Then you can use ALTER TABLE to change the constraints, make cb.Book_Id NOT NULL
, and drop cb.ISBN.
PS: I recommend calling Class_Books a many-to-many table or an intersection table. The word relation means something different in relational theory, and it has nothing to do with relationships between tables.
Bill Karwin
2010-09-09 02:02:31
Is that the same thing as an *association* table? Or am I thinking of something else?
mlschechter
2010-09-09 02:11:06
@mlschechter: Sounds like a synonym to me
OMG Ponies
2010-09-09 02:17:21
Sure, just don't call it a *relation table* because that'd be like saying you're using a *number integer*. Sort of true in a way, but it sounds awkward and redundant.
Bill Karwin
2010-09-09 02:23:12
Fixed the question, and thanks.. it all works.
babonk
2010-09-09 02:25:23