Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6364479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:10:07+00:00 2026-05-25T00:10:07+00:00

I’m working on an exercise that wants me to create a small twitter clone,

  • 0

I’m working on an exercise that wants me to create a small twitter clone, with users, tweets and following system. Well, i came up with the following database structure:

CREATE TABLE tweets (
  tweet_id INT NOT NULL AUTO_INCREMENT,
  tweet VARCHAR(140) NOT NULL,
  PRIMARY KEY (tweet_id)
) ENGINE=INNODB;


CREATE TABLE users (
  user_id INT NOT NULL AUTO_INCREMENT,
  user VARCHAR(255) NOT NULL,
  password VARCHAR(40) NOT NULL,
  email VARCHAR(255) NOT NULL,
  PRIMARY KEY (user_id)
) ENGINE=INNODB;

CREATE TABLE user_tweets (
  id INT NOT NULL AUTO_INCREMENT,
  id_user INT NOT NULL,
  id_tweet INT NOT NULL,
  PRIMARY KEY(id),
  FOREIGN KEY (id_tweet)
    REFERENCES tweets(tweeth_id)
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  FOREIGN KEY (id_user)
    REFERENCES users(user_id)) ENGINE=INNODB;

CREATE TABLE followers (
  id_user INT NOT NULL REFERENCES users (user_id),
  id_following INT NOT NULL REFERENCES users (user_id),
  PRIMARY KEY (id_user, id_following)
) ENGINE=INNODB;

Is it valid? Am i missing something? Also:

  • How do i select the tweets from a user?
  • How do i select the followers from a user?
  • How do i select the people a user is following?

I’m getting a little lost with the foreign key concept. 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:10:07+00:00Added an answer on May 25, 2026 at 12:10 am

    Building on the answer by @Karel,
    I’d use slightly different tables:

    CREATE TABLE tweets (
      tweet_id INT NOT NULL AUTO_INCREMENT,
      user_id INT NOT NULL
      tweet VARCHAR(140) NOT NULL,
      PRIMARY KEY (tweet_id),
      FOREIGN KEY user_id(user_id) REFERENCES users(user_id) 
      ON UPDATE CASCADE ON DELETE CASCADE
    ) ENGINE=INNODB;
    
    
    CREATE TABLE users (
      user_id INT NOT NULL AUTO_INCREMENT,
      user VARCHAR(255) NOT NULL,
      /*password VARCHAR(40) NOT NULL,*/<<--- NEVER STORE A PASSWORD IN THE CLEAR!
      passhash VARCHAR(40) NOT NULL,
      email VARCHAR(255) NOT NULL,
      PRIMARY KEY (user_id)
    ) ENGINE=INNODB;
    
    
    CREATE TABLE followers (
      id_user INT NOT NULL REFERENCES users (user_id),
      id_following INT NOT NULL REFERENCES users (user_id),
      PRIMARY KEY (id_user, id_following)
    ) ENGINE=INNODB;
    

    How do i select the tweets from a user?

    SELECT * FROM tweets WHERE user_id = 458
    

    How do i select the followers from a user?

    SELECT * FROM users 
    INNER JOIN followers ON (users.users_id = followers.id_user) 
    WHERE followers.id_following = 458
    

    How do i select the people a user is following?

    SELECT * FROM users
    INNER JOIN followers ON (followers.id_following = users.user_id)
    WHERE followers.id_user = 458
    

    Use a SHA2 hash to compare the password.
    And don’t forget to add a salt to the hashing to prevent rainbow attacks.

    SELECT user_id 
    FROM users 
    WHERE users.user = 'OralB' 
          AND users.passhash = SHA2(CONCAT(users.user,'secretToothbrush'),512)
    

    SHA1 is no longer secure, so I’d advice using SHA2 with a 512bit hash length.

    Links
    MySQL tutorial: http://www.tizag.com/mysqlTutorial/
    Foreign keys: http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
    SHA2: http://dev.mysql.com/doc/refman/5.5/en/encryption-functions.html#function_sha2
    Concat: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat
    Why salt: What is "salt" when relating to MYSQL sha1?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.