I am getting values of a business by the API and inserting it into a database.
SNO ID category
1 aaa Machine learning
2 aaa AI
3 bbb mobile
Where SNO is the primary key which is set to auto increment. Now after 2 days, for keeping my database up to date I need to get the new data from the API. So suppose that I come to know that ID ‘aaa’ now has one more category as “data structures”
Question: How can I update my table to reflect this new category? I am expecting something like
SNO ID category
1 aaa Machine learning
2 aaa AI
3 bbb mobile
4 aaa data structures
I don’t know the SNO as they are Auto Incremented. Deleting all the rows which has ID = “aaa” and then Inserting it again is one option but I am trying to avoid that as it might increase the overheads.
I am getting new values from the API,
So I am getting Machine learning, AI and Data structures ( all 3 ) If I use , so in my code when I iterate over the category variable 3 SQL Will be generated
INSERT INTO tablename (ID, category) VALUES ('aaa', ‘Machine learning’);
INSERT INTO tablename (ID, category) VALUES ('aaa', ‘AI’);
INSERT INTO tablename (ID, category) VALUES ('aaa', 'data structures');
So in this case 1st and 2nd insert statement will be duplicated, my table will have duplicate rows . .
I basically need to check if the ID and Category exists in the BD if they do not then INSERT ?( The Primary Key SNO is set to auto Increment)
+ Options
Field Type Null Key Default Extra
uuid varchar(50) NO NULL
categoryName varchar(50) NO NULL
sno int(5) NO PRI NULL