2021년 4월 7일 (MySQL Crash Course)

Ji Taek Lim·2021년 4월 7일
2

JOIN에 대한 설명

https://futurists.tistory.com/17

shcema에 대한 설명

https://www.youtube.com/watch?v=3BZz8R7mqu0&t=8s

Learn DOTENV in 6 minutes in 2020

https://www.youtube.com/watch?v=zwcvXd3kGbw

https://www.youtube.com/watch?v=5WFyhsnU4Ik

MySQL Crash Course | Learn SQL

https://www.youtube.com/watch?v=9ylj9NR0Lcg

Redux Tutorial - Learn Redux from Scratch
https://www.youtube.com/watch?v=poQXNp9ItL4&t=40s

https://www.youtube.com/watch?v=WmGgxTpGs_8

Primary & Foreign Keys

https://www.youtube.com/watch?v=B5r8CcTUs5Y

https://www.youtube.com/watch?v=8kDs8QkFI2Y

https://www.mysqltutorial.org/mysql-foreign-key/

스키마를 만들어주는게 되게 중요하다

/* DO NOT TOUCH OR CHANGE BELOW SQL STATEMENT */
USE learnmysql;
/* DESIGN SCHEMA FOR REQUIREMENT */
CREATE TABLE `user` (
  `id` int PRIMARY KEY AUTO_INCREMENT,
  `name` varchar(255) not NULL,
  `email` varchar(255) not NULL
);
CREATE TABLE `content` (
  `id` int PRIMARY KEY AUTO_INCREMENT,
  `title` varchar(255) not NULL,
  `body` varchar(255) not NULL,
  `created_at` timestamp not NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE `content_category` (
  `id` int PRIMARY KEY AUTO_INCREMENT,
  `contentId` int NOT  NULL, 
  `categoryId` int NOT NULL
);
CREATE TABLE `category` (
  `id` int PRIMARY KEY AUTO_INCREMENT,
  `name` varchar(255) not NULL
);
CREATE TABLE `role` (
  `id` int PRIMARY KEY AUTO_INCREMENT,
  `name` varchar(255) not NULL
);

ALTER TABLE `content` ADD userId int;
ALTER TABLE `content` ADD FOREIGN KEY (`userId`) REFERENCES `user` (`id`);

ALTER TABLE `content_category` ADD FOREIGN KEY (`contentId`) REFERENCES `content` (`id`);
ALTER TABLE `content_category` ADD FOREIGN KEY (`categoryId`) REFERENCES `category` (`id`);

ALTER TABLE `user` ADD roleId int;
ALTER TABLE `user` ADD FOREIGN KEY (`roleId`) REFERENCES `role` (`id`);

이것은

----------------------------------------------------------------------------------------------
    TODO: Q 5-1-1. category 테이블의 구조를 보기위한 SQL을 작성해주세요.
        - 요구사항에 맞는 category 테이블을 작성해야만, 테스트를 통과합니다.
*/
const PART5_1_1 = `DESC category`;

/*
----------------------------------------------------------------------------------------------
    TODO: Q 5-1-2. content_category 테이블의 구조를 보기위한 SQL을 작성해주세요.
        - 요구사항에 맞는 content_category 테이블을 작성해야만, 테스트를 통과합니다.
*/
const PART5_1_2 = `DESC content_category`;

/*
----------------------------------------------------------------------------------------------
    TODO: Q 5-1-3. role 테이블의 구조를 보기위한 SQL을 작성해주세요.
        - 요구사항에 맞는 role 테이블을 작성해야만, 테스트를 통과합니다.
*/
const PART5_1_3 = `DESC role`;

/*
----------------------------------------------------------------------------------------------
    TODO: Q 5-1-4. user 테이블의 구조를 보기위한 SQL을 작성해주세요.
        - 요구사항에 맞는 user 테이블을 작성해야만, 테스트를 통과합니다.
*/
const PART5_1_4 = `DESC user`;

/*
----------------------------------------------------------------------------------------------
    TODO: Q 5-2-1. category 테이블에 존재하는 데이터에서 id, name을 찾는 SQL을 작성해주세요.
*/

const PART5_2_1 = `SELECT id, name FROM category`;

/*
----------------------------------------------------------------------------------------------
    TODO: Q 5-2-2. user의 name과 email 그리고 그 user가 속한 role name(컬럼명: roleName)을 찾기 위한 SQL을 작성해주세요.
        - 속한 role이 없더라도, user의 name과 email,role name을 모두 찾아야합니다.
*/
const PART5_2_2 = `SELECT user.name, user.email, role.name 
AS roleName 
FROM user 
LEFT JOIN role 
ON user.roleId = role.id`;

/*
----------------------------------------------------------------------------------------------
    TODO: Q 5-2-3. 어느 role에도 속하지 않는 user의 모든 컬럼 데이터를 찾기위한 SQL을 작성해주세요.
*/
const PART5_2_3 = `SELECT * FROM user WHERE roleId IS NULL`;

/*
----------------------------------------------------------------------------------------------
    TODO: content_category 테이블의 의미를 테이블 관계로서 이해하고 있는지 점검해주세요. 질문자체가 이해가 안된다면, Help-Desk에 질문해주세요.
----------------------------------------------------------------------------------------------    
*/

/*
----------------------------------------------------------------------------------------------
    TODO: Q 5-2-4. content_category 테이블에 존재하는 모든 칼럼의 데이터를 찾기위한 SQL을 작성해주세요.
*/
const PART5_2_4 = `SELECT * FROM content_category`;

/*
----------------------------------------------------------------------------------------------
    TODO: Q 5-2-5. jiSungPark이 작성한 content의 title을 찾기위한 SQL을 작성해주세요.   
*/

const PART5_2_5 = `SELECT content.title 
FROM content 
INNER JOIN user 
ON content.userId = user.id
WHERE user.name='jiSungPark'`;

/*
----------------------------------------------------------------------------------------------
    TODO: Q 5-2-6. JiSungPark이 작성한 content의 category name을 찾기위한 SQL을 작성해주세요.
*/
// Inner Join을 2번써야한다 // soccer 랑 health

// SELECT category.name FROM category
//     -> INNER JOIN content_category
//     -> ON category.id=content_category.categoryId
//     -> WHERE content_category.contentId= 1;

// mysql> SELECT category.name FROM category
//     -> INNER JOIN content_category
//     -> ON category.id=content_category.categoryId
//     -> INNER JOIN content
//     -> ON content.id = content_category.contentId
//     -> WHERE content.id=1;

const PART5_2_6 = `SELECT category.name FROM category
INNER JOIN content_category
ON category.id=content_category.categoryId
INNER JOIN content
ON content.id = content_category.contentId
INNER JOIN user
ON content.userId = user.id
WHERE user.name = "JiSungPark"`;

/*
----------------------------------------------------------------------------------------------
    TODO: Q 5-2-7. category의 name이 soccer인 content의 title, body, created_at을 찾기위한 SQL을 작성해주세요.
*/

// soccer 랑 my father가 같이 나와야함.

const PART5_2_7 = `SELECT ct.title, ct.body, ct.created_at 
FROM content AS ct
INNER JOIN content_category AS cc
ON ct.id = cc.contentId
INNER JOIN category AS cg
ON cc.categoryId=cg.id
WHERE cg.name="soccer"`;

// SELECT ct.title, ct.body, ct.created_at
// FROM content AS ct
// INNER JOIN content_category AS cc
// ON ct.id = cc.contentid
// INNER JOIN category AS cg
// ON cc.categoryid = cg.id
// WHERE cg.name='soccer'

/*
----------------------------------------------------------------------------------------------
    TODO: Q 5-2-8. category의 name이 soccer인 content의 title, body, created_at, user의 name을 찾기위한 SQL을 작성해주세요.
*/
const PART5_2_8 = `SELECT ct.title, ct.body, ct.created_at, u.name
FROM content AS ct
INNER JOIN user AS u
ON u.id = ct.userId 
INNER JOIN content_category AS cc
ON ct.id = cc.contentId
INNER JOIN category AS cg
ON cc.categoryId=cg.id
WHERE cg.name="soccer"
`;

/*
----------------------------------------------------------------------------------------------
    TODO: Q 5-2-9. duRiCha가 작성한 글의 개수 (컬럼명: ContentCount)를 출력하기 위한 SQL을 작성해주세요.
*/
const PART5_2_9 = `SELECT COUNT(*) AS ContentCount
FROM content AS c
INNER JOIN user AS u
ON u.id=c.userId
WHERE u.name="duRiCha"`;

/*
----------------------------------------------------------------------------------------------
    TODO: Q 5-2-10. 각 user(컬럼명: name)가 작성한 글의 개수 (컬럼명: ContentCount)를 출력하기 위한 SQL을 작성해주세요.
*/
const PART5_2_10 = `SELECT user.name AS name ,COUNT(content.userId) AS ContentCount
FROM user
LEFT JOIN content
ON user.id=content.userId
GROUP BY user.name`;

profile
임지택입니다.

0개의 댓글