๐ฏ ์๊ตฌ์ฌํญ ๋ถ์
*์ข์์๋ฅผ User ํ ์ด๋ธ์ ๋ฃ์ด๋๊ณ ๊ด๊ณ ๋งคํ์ ๋ ๋ฒ ์ด์์ผ๋ก ํ ๊ฒ์ธ๊ฐ
โ ์ข์์ ์, ๋๊ธ ์, ํด์ํ๊ทธ ์ฌ๋ถ, ์์นํ๊ทธ ์ฌ๋ถ
โ ๋ํ๋ฐฉ์ ์ฐธ์ฌ์ค์ธ ์ ์ ๋ชฉ๋ก, ์ต๊ทผ์ ๋์ฐฉํ ๋ฉ์์ง (์๋ฆผ)
์นด์นด์คํก ์ฑํ ๋ฐฉ ๊ด๋ฆฌ์ ์ ์ฌํ๊ฒ
โ ๊ฒ์๋ฌผ ๊ณต์ , ์ฌ์ฉ์ ํ๋กํ ๊ณต์ ๋ฑ์ ๊ธฐ๋ฅ
๋๋๊ธ๋ ๊ณ ๋ ค โ ์๊ธฐ์์ ๊ณผ ์ผ๋์ผ ๊ด๊ณ (๋ถ๋ชจ๋๊ธ๋ฒํธ; parent_comment)
User
-
userId PK int
nickname UNIQUE varchar(20)
password varchar(25)
username varchar(20)
profileImgUrl text
totalFollower int
totalFollow int
birth varchar(20)
website NULL text
email NULL varchar(50)
phone NULL varchar(30)
gender int
status int
Story
-
storyId PK int
storyImg NULL int FK >-< Media.mediaId
userId int FK >- User.userId
content text
layout NULL int
isPrivate int
isHighLight int
createdAt timestamp
placeTag NULL int FK >- PlaceTag.tagId
hashTag NULL int FK >-< HashTag.tagId
userTag NULL int FK >-< UserTag.tagUser
StoryReply
-
replyId PK int
storyId int FK >- Story.storyId
dmRoom int FK >- DM.roomId
storyEmoji int
postUser int FK >- User.userId
replyUser int FK >- User.userId
StoryHighlight
-
highlightId PK int
thumbnail text
storyId int FK >-< Story.storyId
userId int FK >- User.userId
StoryHeart
-
userId int FK >- User.userId
storyId int FK >- Story.storyId
Media
-
mediaId PK int
mediaUrl text
Post
-
postId PK int
postImg int FK >-< Media.mediaId
postImgUrl text
order int
createdAt timestamp
updatedAt timestamp
writer int FK >- User.userId
placeTag NULL varchar(50) FK >- PlaceTag.tagId
hashTag NULL varchar(50) FK >-< HashTag.tagId
userTag NULL int FK >-< UserTag.tagUser
totalLikes int
totalComments int
PostLike
-
postId int FK >- Post.postId
userId int FK >- User.userId
PostComment
-
commentId PK int
content text
commentOrder int
commentDepth int
isDeleted int
createdAt timestamp
updatedAt timestamp
writer int FK >- User.userId
PostCommentLike
-
postId int FK >- Post.postId
userId int FK >- User.userId
DM
-
roomId PK int
dialog text
chatUser int FK >-< User.userId
HashTag
-
tagId PK int
tagName UNIQUE varchar(50)
PlaceTag
-
tagId PK int
place varchar(50)
address NULL text
UserTag
-
userId int FK - User.userId
tagUser int FK >- User.userId
Alert
-
alertId PK int
content varchar(50)
alertType int
time timestamp
isChecked int
userId int FK >- User.userId
Reels
-
reelsId PK int
writer FK >- User.userId
videoContent text
desc User;
INSERT INTO User(userId, nickname, password, username, profileImgUrl, totalFollower, totalFollow, birth, website, email, phone, gender, status)
VALUES(NULL, "abc_def", "1234567", "๋๋ ๋ฏธ", "url", 0, 0, "2000๋
1์ 1์ผ", NULL, NULL, "010-1234-5678", 0, 0);
INSERT INTO User(userId, nickname, password, username, profileImgUrl, totalFollower, totalFollow, birth, website, email, phone, gender, status)
VALUES(NULL, "jj_qwe", "1234567", "์๋
", "url", 0, 0, "2000๋
1์ 1์ผ", NULL, NULL, "010-1234-5678", 0, 0);
INSERT INTO User(userId, nickname, password, username, profileImgUrl, totalFollower, totalFollow, birth, website, email, phone, gender, status)
VALUES(NULL, "ccc_gf", "1234567", "ํ๋ณตํด", "url", 0, 0, "2000๋
1์ 1์ผ", NULL, NULL, "010-1234-5678", 0, 0);
desc Post;
INSERT INTO Post(postId, postImg, postImgUrl, createdAt, updatedAt, writer, placeTag, hashTag, userTag, totalLikes, totalComments)
VALUES(NULL, 1, "img_link", now(), now(), 1, NULL, NULL, NULL, 0, 0);
INSERT INTO Post(postId, postImg, postImgUrl, createdAt, updatedAt, writer, placeTag, hashTag, userTag, totalLikes, totalComments)
VALUES(NULL, 1, "img_link2", now(), now(), 1, NULL, NULL, NULL, 0, 0);
INSERT INTO Post(postId, postImg, postImgUrl, createdAt, updatedAt, writer, placeTag, hashTag, userTag, totalLikes, totalComments)
VALUES(NULL, 1, "img_link3", now(), now(), 3, NULL, NULL, 1, 0, 0);
DELIMITER $$
DROP PROCEDURE IF EXISTS loopInsert$$
CREATE PROCEDURE loopInsert()
BEGIN
DECLARE i INT DEFAULT 1;
WHILE i <= 500 DO
INSERT INTO Post (postId, postImg, postImgUrl, createdAt, updatedAt, writer, placeTag, hashTag, userTag, totalLikes, totalComments)
VALUES(NULL, 1, concat('image',i), now(), now(), 1, NULL, NULL, NULL, 0, 0);
SET i = i + 1;
END WHILE;
END$$
DELIMITER $$
CALL loopInsert;
desc Story;
INSERT INTO Story(storyId, storyImg, userId, content, layout, isPrivate, isHighLight, createdAt, placeTag, hashTag, userTag)
VALUES (NULL, 1, 1, "Hello!", NULL, 0, 0, now(), 1, NULL, NULL);
INSERT INTO Story(storyId, storyImg, userId, content, layout, isPrivate, isHighLight, createdAt, placeTag, hashTag, userTag)
VALUES (NULL, 1, 2, "Hello!", NULL, 0, 0, now(), 1, NULL, 1);
INSERT INTO Story(storyId, storyImg, userId, content, layout, isPrivate, isHighLight, createdAt, placeTag, hashTag, userTag)
VALUES (NULL, 1, 2, "Hello!", NULL, 0, 0, now(), 1, NULL, 1);
desc HashTag;
INSERT INTO HashTag (tagId, tagName)
VALUES (NULL, "#ํด์ํ๊ทธ")
INSERT INTO HashTag (tagId, tagName)
VALUES (NULL, "#ํ
์คํธ")
desc UserTag;
INSERT INTO UserTag(userId, tagUser)
VALUES(3, 1);
desc Media;
INSERT INTO Media(mediaId, mediaUrl)
VALUES(1, "https://www.naver.com");
desc PostLike;
INSERT INTO PostLike(postId, userId)
VALUES (3, 1);
INSERT INTO PostLike(postId, userId)
VALUES (3, 2);
INSERT INTO PostLike(postId, userId)
VALUES (3, 3);
INSERT INTO PostLike(postId, userId)
VALUES (4, 1);
*์ข์์ ์์ผ๋ก ๊ฒ์๋ฌผ ์ ๋ ฌํด๋ณด๊ธฐ
INSERT INTO PlaceTag(tagId, place, address)
VALUES (NULL, "์์ธ์(Seoul)", "์ฉ์ฐ๊ตฌ ํ๋จ๋");
desc StoryHighlight
๋ก๊ทธ์ธ ์ฑ๊ณต ์ ์ ์ ๋ง์ดํ์ด์ง๋ก ๋์ด์ค๊ธฐ
# ์ค์ ์๋น์ค์์๋ ๋๋ค์, ์ ํ๋ฒํธ, ์ด๋ฉ์ผ์ฃผ์ ๋ชจ๋ ๋ก๊ทธ์ธ ์ ์์ด๋๋ก ์ฌ์ฉ๊ฐ๋ฅํ๋ค.
# ์์ด๋์ ๋น๋ฐ๋ฒํธ์ ๋ชจ๋ ์ผ์นํ๋ค๋ฉด ๋ก๊ทธ์ธ ์ฑ๊ณต์ผ๋ก ์ฒ๋ฆฌํ๊ณ ๋ง์ดํ์ด์ง๋ก ๋์ด๊ฐ๋ค.
SELECT nickname, username, profileImgUrl, totalFollower, totalFollow
FROM User
WHERE (nickname='abc_def' OR phone='010-6410-6452' OR email=?) AND password='1234567';
๊ฒ์๋ฌผ ์ข์์ ๋ชฉ๋ก ์กฐํ - Post3์ ์ข์์ ๋ชฉ๋ก์ ์กฐํํ๊ณ totalLikes๋ฅผ 1์ฆ๊ฐ์ํจ๋ค.
# where๋ฌธ์ผ๋ก Post3์ ์ข์์ ๋ชฉ๋ก์ ์กฐํํ๋ค.
SELECT *
FROM Post LEFT OUTER JOIN PostLike
ON Post.postId = PostLike.postId
WHERE Post.postId=3;
๊ฒ์๋ฌผ ์ข์์ ๊ฐ์ ์กฐํ - Post3์ ์ข์์ ๊ฐ์๋ฅผ ์กฐํํ๊ณ Post์ totalLikes ํ๋ ๊ฐ์ ์์ ํ๋ค.
# ์ง๊ณํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๊ฒ์๋ฌผ์ ์ข์์ ๊ฐ์๋ฅผ ๊ตฌํ๋ค.
# Post์ ์ข์์ ๊ฐ์์ธ totalLikes๋ฅผ ๋ชฉ๋ก ๊ฐ์๋ก ์ค์ ํ๋ค.
SELECT COUNT(*)
FROM Post LEFT OUTER JOIN PostLike
ON Post.postId = PostLike.postId
WHERE Post.postId=3;
๊ฒ์๋ฌผ ์์น ์กฐํ
SELECT place, address, postId FROM Post
RIGHT OUTER JOIN PlaceTag
ON Post.placeTag = PlaceTag.tagId
WHERE Post.postId=3;
๊ฒ์๋ฌผ / ์คํ ๋ฆฌ ์์ - ๊ฒ์๋ฌผ์์ ํด์ํ๊ทธ๋ฅผ ์ถ๊ฐํ๋ค.
UPDATE Post SET HashTag=1
WHERE Post.postId=?;
๊ฒ์๋ฌผ / ์คํ ๋ฆฌ ์ญ์ - ๋ ๋์ค์(์ต๊ทผ์) ์ฌ๋ฆฐ ์คํ ๋ฆฌ๋ถํฐ ์ญ์ ํ ์ ์๋ค.
# 2๊ฐ์ ์คํ ๋ฆฌ๋ฅผ ๊ฒ์ํ ์ฌ์ฉ์2๊ฐ ์ต๊ทผ์ ์
๋ก๋ํ ์คํ ๋ฆฌ๋ฅผ ์ญ์ ํ๋ค.
DELETE FROM Story
WHERE Story.userId=2
ORDER BY createdAt DESC LIMIT 1; # ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ ํ ๊ฐ์ฅ ์์ ์์นํ ๋ ์ฝ๋๋ฅผ ์ญ์
ํด์ํ๊ทธ๋ก ๊ฒ์๋ฌผ ๊ฒ์ํ๊ธฐ (โ#ํ ์คํธโ๋ผ๋ ํ๊ทธ๊ฐ ์๋ ๊ฒ์๋ฌผ ๋ชฉ๋ก ์กฐํ)
SELECT postId, postImg, postImgUrl, tagName from Post
LEFT OUTER JOIN HashTag
ON Post.hashTag = HashTag.tagId
WHERE HashTag.tagId=2
์์ ์ด ํ๊ทธ๋ ๊ฒ์๋ฌผ ์กฐํ
SELECT postId, postImg, postImgUrl, userTag from Post
LEFT OUTER JOIN UserTag
ON Post.userTag = UserTag.tagUser AND Post.writer != UserTag.userId
WHERE Post.writer = 3
๊ฒ์๋ฌผ ์ ๋ก๋ ์๊ฐ ์กฐํ
# ์ฌ์ฉ์1์ด ์์ฑํ ๊ฒ์๋ฌผ์ ์
๋ก๋ ์๊ฐ ๋ชฉ๋ก ์กฐํ
SELECT postId, createdAt from Post
LEFT OUTER JOIN User
ON Post.writer = User.userId
WHERE User.userId=1
์คํ ๋ฆฌ ํ์ด๋ผ์ดํธ ๋ชฉ๋ก ์กฐํ
# ์ฌ์ฉ์1์ ํ์ด๋ผ์ดํธ ๋ชฉ๋ก์ ์คํ ๋ฆฌ1,2,3์ด ์๋ ๊ฒฝ์ฐ