I am new to sql and I was wondering how i can add a list to a database. Here is what I mean:
I have an a java class like this that i want to insert its info into a db:
public class Dog{
private int dog_id;
private String dog_name;
private ArrayList<String> dog_friends;
}
Every dog object will have a different amount size of friends. My question is how i can add this list to the db. I was planning on having 3 columns (id,name,list), I just dont know how to add a list.
What you have is, in the relational database world, called a one-to-many relationship, and it requires two database tables to represent the information.
In your first table, you’ll have
idandname; in your second table you’ll havedog_id,friend_idanddog_friend.There will be possibly many entries in the second table for every entry in the first table. When you insert a row in the second table, be sure to set
dog_idto match theidin the first table.With this structure, you can use
JOINs in your SQL query to get all the information at once, or query the tables independently, depending on your needs.