Modeling Database Coding Practice

In this practice set, let's build a relational database for a typical social networking site.

In a social networking site, we have user, post, group, and comment entities.

Use Case:

  • user can create multiple posts. Each post can have only one user.
  • user can make multiple comments to a post. Each comment can have only one user.
  • post can have multiple comments. Each comment can have only one post.
  • user can be in multiple groups. Each group can have multiple users.

Answer all the questions at once. The database resets everytime you navigate out of the practice set

1. Write a query to represent the user entity type in the relational database. Below are the attributes of a user entity type.

attributedescription
idan integer to uniquely identify a user - key attribute
namea string of max length 250 characters
gendera string of max length 50 characters
email_ida string of max length 500 characters

2. We have created a user table in the database.

Now, let's write a query to represent the post entity type and its relation with user entity type.

Below are the attributes of the post entity type.

attributedescription
post_idan integer to uniquely identify a post - key attribute
contenta text field
published_atdatetime field

Note:

  • Create a table in such a way that if we delete a user from the user table, then the related posts in the post table must be automatically deleted.

3. We have created user and post tables in the database. Now, users want to comment on the posts. So, let's create a comment table.

Write a query to represent the comment entity type, and its relation with user and post entity types.

  • Attributes of a comment entity type are given below.
attributedescription
comment_idan integer to uniquely identify a comment - key attribute
contenta text field
commented_atdatetime field

Note:

Create a table in such a way that:

  • If we delete a user from the user table, then the related comments in the comment table must be automatically deleted.
  • Similarly, if we delete a post, then the comments related to the post must be automatically deleted.

4. Any social network application has groups with users of similar interests.

Now, let’s create a group_details table that stores the information about a group.

Write a query to represent the group_details entity type in the relational database. Below are the attributes of the entity type.

attributedescription
idan integer to uniquely identify a group - key attribute
namea string of max length 500 characters

5. A user can be in multiple groups, and a group can contain many users.

Now, let’s create user_group table to capture the many-to-many relationship between user and group entity types.

Below are the attributes of the relationship.

attributedescription
joined_ata datetime field
is_admina boolean field

Note:

Create this junction table in such a way that if we delete a user/group, then the related data in the user_group must be automatically deleted.


Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post

Contact Form