[Gloucs] mysql insert query

Bob Wilkinson rwilkinson at messagelabs.com
Wed Nov 9 14:49:32 UTC 2011


On Sun, Nov 06, 2011 at 10:23:37PM +0000, Glyn Davies wrote:
> 'lo
> 
> I have a noddy mysql query which I'm sure someone can help me with. Am
> a complete novice with mysql and SQL in general.
> 
> Assume you have two tables. Hopefully I have described this okay.
> 
> Authors
> id - auto_num, primary key
> Name - Author's name
> 
> and
> 
> Books
> id - auto_num, primary key
> Title - Book title
> Author - Author.id, foreign key
> 
> Say you want to insert a book into the db.
> 
> Add the Author, get the id for that Author's entry, then add the book
> with the Author value being that previosly learnt.
> 
> First insert is easy as is getting the id.
> 
> But I'm failing to insert into the Books table due to foreign key
> restraints. Can I insert? Should it be an update?

Hi Glyn

I notice no one has replied (that I have seen). However, I think
that you do want an insert - something like

insert into Books values ("Janet and John learn C", 123) where 

123 is the ID of authors "Enid Blyton and Dennis Ritchie"

If you had an entry for "Janet and John learn C" and you get the
second edition which had an additional author, then you want to use
an update command if you want to replace the author id.

BTW - what error message did you see regarding foreign key
restraints?

Bob

______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email 
______________________________________________________________________



More information about the gloucs mailing list