== DB ERD
CREATE TABLE Users(
userId int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
email varchar(255) NOT NULL UNIQUE,
name varchar(50) NOT NULL,
nickname varchar(50) NOT NULL UNIQUE,
password varchar(255) NOT NULL,
profileImg varchar(255),
statusMessage varchar(255),
rating int(11) NOT NULL,
createdAt datetime NOT NULL,
updatedAt datetime NOT NULL
);
CREATE TABLE Likes(
likeId int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
userId int(11) NOT NULL,
likeItem varchar(30) NOT NULL,
createdAt datetime NOT NULL,
updatedAt datetime NOT NULL,
FOREIGN KEY (userId) REFERENCES Users(userId) ON DELETE CASCADE
);
CREATE TABLE Posts(
postId int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
userId int(11) NOT NULL,
title varchar(255) NOT NULL,
postImg varchar(255),
content varchar(1000),
maxMember TINYINT UNSIGNED NOT NULL,
startDate datetime NOT NULL,
endDate datetime NOT NULL,
place varchar(255),
bring varchar(255),
public TINYINT UNSIGNED NOT NULL,
createdAt datetime NOT NULL,
updatedAt datetime NOT NULL,
FOREIGN KEY (userId) REFERENCES Users(userId) ON DELETE CASCADE
);
CREATE TABLE Tags(
tagId int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
postId int(11) NOT NULL,
tag varchar(30) NOT NULL,
createdAt datetime NOT NULL,
updatedAt datetime NOT NULL,
FOREIGN KEY (postId) REFERENCES Posts(postId) ON DELETE CASCADE
);
CREATE TABLE Channels(
channelId int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
postId int(11) NOT NULL,
userId int(11) NOT NULL,
createdAt datetime NOT NULL,
updatedAt datetime NOT NULL,
FOREIGN KEY (postId) REFERENCES Posts(postId) ON DELETE CASCADE,
FOREIGN KEY (userId) REFERENCES Users(userId) ON DELETE CASCADE
);
CREATE TABLE Messages(
messageId int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
postId int(11) NOT NULL,
userId int(11),
message varchar(255) NOT NULL,
createdAt datetime NOT NULL,
updatedAt datetime NOT NULL,
FOREIGN KEY (postId) REFERENCES Posts(postId) ON DELETE CASCADE,
FOREIGN KEY (userId) REFERENCES Channels(userId) ON DELETE SET NULL,
);
# Messages에 유저가 나갔을 경우 postId를 기반으로 검색하는 SELECT 문
DELIMITER $$
CREATE FUNCTION MessageSelect (
IF ( userId IS NULL) THEN
SELECT "알수없음" AS nickname, ' ' AS profileImg, message
FROM Messages
WHERE postId = <입력 받은데이터>;
ELSE
SELECT u.nickname, u.profileImg, m.message
FROM Messages AS m
JOIN Users AS u
ON m.userId = u.userId
WHERE postId = <입력 받은데이터>;
END;
)
END $$
DELIMITER ;
-- 구현해야하는 남은 테이블
친구
친구 내역
친구 수신
== Sequelize 구현
$ npm init
$ npm install sequelize mysql2 sequelize-cli express mysql joi jsonwebtoken cors dotenv socket.io
$ npx sequelize init
$ npx sequelize model:generate --name Users --attributes email:string,name:string,nickname:string,password:string,profileImg:string,rating:integer
$ npx sequelize model:generate --name Likes --attributes userId:integer,likeItem:string
$ npx sequelize model:generate --name Posts --attributes userId:integer,title:string,postImg:string,content:string,maxMember:tinyint,startDate:date,endDate:date,place:string,bring:string,public:tinyint
$ npx sequelize model:generate --name Tags --attributes postId:integer,tag:string
$ npx sequelize model:generate --name Channels --attributes postId:integer,userId:integer
$ npx sequelize model:generate --name Messages --attributes postId:integer,userId:integer,message:string
== DB 임시 데이터 생성
# DB Users 생성
INSERT INTO Users (email, name, nickname, password, profileImg, rating) values
("dddd@naver.com","Chris John", "KKK", "!@#4qwer", NULL, 70);
INSERT INTO Users (email, name, nickname, password, profileImg, rating, createdAt, updatedAt) values
("qwer@naver.com","Riot", "Riot", "!@#4qwer", NULL, 70, NOW(), NOW() );
INSERT INTO Users (email, name, nickname, password, profileImg, rating, createdAt, updatedAt) values
("qqe22@naver.com","karis", "Karin", "!@#4qwer", NULL, 70, NOW(), NOW() );
INSERT INTO Users (email, name, nickname, password, profileImg, rating, createdAt, updatedAt) values
("ggenter@naver.com","Zero Two", "Kuran", "!@#4qwer", NULL, 70, NOW(), NOW() );
INSERT INTO Users (email, name, nickname, password, profileImg, rating, createdAt, updatedAt) values
("kkkk@naver.com","Metro", "Sawadicap", "!@#4qwer", NULL, 70, NOW(), NOW() );
# DB Likes 생성
INSERT INTO Likes (userId, likeItem) values (1, "Game");
INSERT INTO Likes (userId, likeItem) values (1, "EDM");
INSERT INTO Likes (userId, likeItem) values (1, "Travel");
INSERT INTO Likes (userId, likeItem) values (2, "Game");
INSERT INTO Likes (userId, likeItem) values (2, "Music");
INSERT INTO Likes (userId, likeItem) values (3, "Board");
INSERT INTO Likes (userId, likeItem) values (4, "Board");
INSERT INTO Likes (userId, likeItem) values (4, "Game");
INSERT INTO Likes (userId, likeItem) values (4, "walking");
# DB Posts 생성
INSERT INTO Posts (userId, title, postImg, content, maxMember, startDate, endDate, place, bring, public) values
(1, "Title Hello", NULL, "content!", 5, NOW(), NOW(), "Daegue", "No!", 0);
INSERT INTO Posts (userId, title, postImg, content, maxMember, startDate, endDate, place, bring, public) values
(1, "Board Game Group", NULL, "Like BoardGame", 3, NOW(), NOW(), "Seoul", "30$", 0);
INSERT INTO Posts (userId, title, postImg, content, maxMember, startDate, endDate, place, bring, public) values
(2, "GG!", NULL, "GG!", 10, NOW(), NOW(), "Online", "No", 0);
#DB Tags 생성
INSERT INTO Tags (postId, tag) values (1, "Hello");
INSERT INTO Tags (postId, tag) values (1, "No");
INSERT INTO Tags (postId, tag) values (1, "Play");
INSERT INTO Tags (postId, tag) values (2, "Game");
INSERT INTO Tags (postId, tag) values (3, "Board");
#DB Chanlles 생성
INSERT INTO Channels (postId, userId) values (1,1);
INSERT INTO Channels (postId, userId) values (1,2);
INSERT INTO Channels (postId, userId) values (1,3);
INSERT INTO Channels (postId, userId) values (2,2);
INSERT INTO Channels (postId, userId) values (2,3);
#DB Messages 생성
INSERT INTO Messages (postId, userId, message) values (1,1, "Hello");
INSERT INTO Messages (postId, userId, message) values (1,2, "Hello");
INSERT INTO Messages (postId, userId, message) values (1,1, "Who Are You??");
INSERT INTO Messages (postId, userId, message) values (1,3, "Hello");
INSERT INTO Messages (postId, userId, message) values (1,1, "Hello");
INSERT INTO Messages (postId, userId, message) values (1,2, "Watson");
'항해99 > 필기노트' 카테고리의 다른 글
[필기노트] SQL, 5개 Table 조회 (0) | 2021.07.29 |
---|---|
[필기노트] 카카오맵, API 구현 (0) | 2021.07.28 |
[필기노트] 2021-07-22 SQL, 태그 검색, 테스트코드 (0) | 2021.07.23 |
[필기노트] 2021-07-21 SQL, PROCEDURE (0) | 2021.07.22 |
[필기노트] 2021-07-20 SQL, TRIGGER (0) | 2021.07.21 |