Instagram Clone

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6
At a glance
Powered by AI
The document shows the SQL commands to create the database tables needed for a basic social media app like Instagram. Tables were created for users, photos, likes, comments, follows and tags with the appropriate columns and constraints.

Tables were created for users, photos, likes, comments, follows, and tags. The users table stores user profiles. The photos table stores images and links them to users. The likes, comments and follows tables track interactions between users and photos. The tags table allows photos to be tagged and linked together.

Two queries were written - one to find the oldest 5 users, and another to find the most popular registration days. The first orders users by creation date and limits to 5, while the second groups users by registration day, counts them, and orders/limits the results.

CREATE TABLE users (

id INTEGER AUTO_INCREMENT PRIMARY KEY,


username VARCHAR(255) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
);
create database instagram;

use instagram;
create table users(
id INTEGER AUTO_INCREMENT PRIMARY KEY ,
username VARCHAR(255) UNICODE NOT NULL ,
created_at TIMESTAMP DEFAULT NOW()
);
INSERT INTO users (username) VALUES
('Bogdan Danile'),
('Daniel Ioan'),
('CLAUDIA IOANA'),
('CLARA STEFANIA')

create table photos(


id INTEGER AUTO_INCREMENT PRIMARY KEY ,
image_url VARCHAR(255) NOT NULL ,
user_id INTEGER NOT NULL ,
created_at TIMESTAMP DEFAULT NOW(),
foreign key(user_id) REFERENCES users(id)
);

INSERT INTO photos(image_url,user_id) VALUES


('/URL.com',2),
('/url.com',3),
('/sadasd',3);

SELECT photos.image_url , users.username


FROM photos
JOIN users
ON photos.user_id = users.id
He
mai sus o sa ia toate liniile din photos doar partea cu image.url
si o sa uneasca de partea cu users.username unde id userului
din users corespunde cu user id din users
CREATE TABLE likes(
user_id INTEGER NOT NULL ,
photo_id INTEGER NOT NULL ,
created_at TIMESTAMP DEFAULT NOW(),
foreign key (user_id) references users(id),
foreign key (photo_id)references photos(id),
PRIMARY KEY (user_id,photo_id)

)
CREATE TABLE users (
id INTEGER AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
);

CREATE TABLE photos (


id INTEGER AUTO_INCREMENT PRIMARY KEY,
image_url VARCHAR(255) NOT NULL,
user_id INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT NOW(),
FOREIGN KEY(user_id) REFERENCES users(id)
);

CREATE TABLE comments (


id INTEGER AUTO_INCREMENT PRIMARY KEY,
comment_text VARCHAR(255) NOT NULL,
photo_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT NOW(),
FOREIGN KEY(photo_id) REFERENCES photos(id),
FOREIGN KEY(user_id) REFERENCES users(id)
);

CREATE TABLE likes (


user_id INTEGER NOT NULL,
photo_id INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT NOW(),
FOREIGN KEY(user_id) REFERENCES users(id),
FOREIGN KEY(photo_id) REFERENCES photos(id),
PRIMARY KEY(user_id, photo_id)
);

CREATE TABLE follows (


follower_id INTEGER NOT NULL,
followee_id INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT NOW(),
FOREIGN KEY(follower_id) REFERENCES users(id),
FOREIGN KEY(followee_id) REFERENCES users(id),
PRIMARY KEY(follower_id, followee_id)
);
CREATE TABLE tags (
id INTEGER AUTO_INCREMENT PRIMARY KEY,
tag_name VARCHAR(255) UNIQUE,
created_at TIMESTAMP DEFAULT NOW()
);

CREATE TABLE photo_tags (


photo_id INTEGER NOT NULL,
tag_id INTEGER NOT NULL,
FOREIGN KEY(photo_id) REFERENCES photos(id),
FOREIGN KEY(tag_id) REFERENCES tags(id),
PRIMARY KEY(photo_id, tag_id)
);
-- 1. Finding 5 oldest users
SELECT *
FROM users
ORDER BY created_at
LIMIT 5;

-- 2. Most Popular Registration Date


SELECT
DAYNAME(created_at) AS day,
COUNT(*) AS total
FROM users
GROUP BY day
ORDER BY total DESC
LIMIT 2;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy