Arrays in SQL feel needlessly complicated. The traditional way is to have a one-to-many relationship, aka create a whole new table for each array you create.
So for a social media app, you can't store a user's posts in your users
table. You have to make a new table:
create table posts(
postId int auto_increment primary key,
authorId int,
postContent varchar
);
This makes sense here, but what if we wanted each post to have multiple authors? We can't just do:
create table posts(
postId int auto_increment primary key,
authorId int array,
postContent varchar
);
Instead, we would need a weird table like this:
create table post_authors(
postAuthorId int auto_increment primary key,
postId int,
postAuthor int
);
This is the SQL way to handle arrays. But I don't like it. So sometimes I'll use JSON.stringify()
and store my arrays as strings. This makes searching a lot harder though.