Adding keywords to db.
I have 3 tables.
Articles
-ArticleID PK
-ArticleTitle
-ArticleBody
Keywords
-KeywordID PK
-Keyword UNIQUE
Keyword_Article
-KeywordID PK
-ArticleID
Trying to figure out how to update the Keyword tables as efficiently as possible.
All I have is SQLBUDDY to work with for now.
I have had success with
INSERT INTO Keywords (KeywordID, Keyword)
VALUES (NULL,'test');
INSERT INTO Keyword_Article (KeywordID, ArticleID)
VALUES ('LAST_INSERT_ID()','2222');
But when I run into a keyword that already exists this obviously does not work.
Im guessing a need an if/else/then or is there some other way that this should be done.
I have read about stored procedures which could help stop the second table from populating if the first one fails but I cant seem to get them to work in SQLBUDDY.
eg.
BEGIN
INSERT INTO Keywords (KeywordID, Keyword)
VALUES (NULL,'test')
INSERT INTO Keyword_Article (KeywordID, ArticleID)
VALUES ('LAST_INSERT_ID()','2222');
COMMIT;
Keeps giving errors.
What is the best way to do this kind of multiple insert?
You can do something like this:
That will try to insert a new keyword, but ignore it if it already exists. The second query will always find the KeywordID whether it is new or not.