diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/.gradle/7.4/checksums/checksums.lock b/.gradle/7.4/checksums/checksums.lock new file mode 100644 index 0000000..572ab13 Binary files /dev/null and b/.gradle/7.4/checksums/checksums.lock differ diff --git a/.gradle/7.4/dependencies-accessors/dependencies-accessors.lock b/.gradle/7.4/dependencies-accessors/dependencies-accessors.lock new file mode 100644 index 0000000..6b6ef6b Binary files /dev/null and b/.gradle/7.4/dependencies-accessors/dependencies-accessors.lock differ diff --git a/.gradle/7.4/dependencies-accessors/gc.properties b/.gradle/7.4/dependencies-accessors/gc.properties new file mode 100644 index 0000000..e69de29 diff --git a/.gradle/7.4/executionHistory/executionHistory.lock b/.gradle/7.4/executionHistory/executionHistory.lock new file mode 100644 index 0000000..4632ab3 Binary files /dev/null and b/.gradle/7.4/executionHistory/executionHistory.lock differ diff --git a/.gradle/7.4/fileChanges/last-build.bin b/.gradle/7.4/fileChanges/last-build.bin new file mode 100644 index 0000000..f76dd23 Binary files /dev/null and b/.gradle/7.4/fileChanges/last-build.bin differ diff --git a/.gradle/7.4/fileHashes/fileHashes.lock b/.gradle/7.4/fileHashes/fileHashes.lock new file mode 100644 index 0000000..d41ea3c Binary files /dev/null and b/.gradle/7.4/fileHashes/fileHashes.lock differ diff --git a/.gradle/7.4/gc.properties b/.gradle/7.4/gc.properties new file mode 100644 index 0000000..e69de29 diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock new file mode 100644 index 0000000..d4205ec Binary files /dev/null and b/.gradle/buildOutputCleanup/buildOutputCleanup.lock differ diff --git a/.gradle/buildOutputCleanup/cache.properties b/.gradle/buildOutputCleanup/cache.properties new file mode 100644 index 0000000..d480163 --- /dev/null +++ b/.gradle/buildOutputCleanup/cache.properties @@ -0,0 +1,2 @@ +#Thu May 16 16:36:19 KST 2024 +gradle.version=7.4 diff --git a/.gradle/vcs-1/gc.properties b/.gradle/vcs-1/gc.properties new file mode 100644 index 0000000..e69de29 diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..fb7f4a8 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..ce1c62c --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,16 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..fdc392f --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..f50fada --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/com/jungmin/migrations/schema.sql b/src/main/java/com/jungmin/migrations/schema.sql index 1136c54..c1c2a4c 100644 --- a/src/main/java/com/jungmin/migrations/schema.sql +++ b/src/main/java/com/jungmin/migrations/schema.sql @@ -1,46 +1,48 @@ -CREATE TABLE Users ( - user_id BIGINT AUTO_INCREMENT PRIMARY KEY, - username VARCHAR(255) NOT NULL UNIQUE, - email VARCHAR(255) NOT NULL UNIQUE, - password VARCHAR(255) NOT NULL, - created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP +CREATE TABLE `Tags` ( + `tag_id` BIGINT PRIMARY KEY AUTO_INCREMENT, + `name` VARCHAR(255) NOT NULL UNIQUE ); -CREATE TABLE Boards ( - board_id BIGINT AUTO_INCREMENT PRIMARY KEY, - name VARCHAR(255) NOT NULL, - description TEXT +CREATE TABLE `Users` ( + `user_id` BIGINT PRIMARY KEY AUTO_INCREMENT, + `username` VARCHAR(255) NOT NULL UNIQUE, + `email` VARCHAR(255) NOT NULL UNIQUE, + `password` VARCHAR(255) NOT NULL, + `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP ); -CREATE TABLE Posts ( - post_id BIGINT AUTO_INCREMENT PRIMARY KEY, - title VARCHAR(255) NOT NULL, - content TEXT NOT NULL, - user_id BIGINT, - board_id BIGINT, - created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (user_id) REFERENCES Users(user_id), - FOREIGN KEY (board_id) REFERENCES Boards(board_id) +CREATE TABLE `Boards` ( + `board_id` BIGINT PRIMARY KEY AUTO_INCREMENT, + `name` VARCHAR(255) NOT NULL, + `description` TEXT ); -CREATE TABLE Comments ( - comment_id BIGINT AUTO_INCREMENT PRIMARY KEY, - content TEXT NOT NULL, - user_id BIGINT, - post_id BIGINT, - created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (user_id) REFERENCES Users(user_id), - FOREIGN KEY (post_id) REFERENCES Posts(post_id) +CREATE TABLE `Posts` ( + `post_id` BIGINT PRIMARY KEY AUTO_INCREMENT, + `title` varchar(255) NOT NULL, + `content` TEXT NOT NULL, + `user_id` BIGINT, + `board_id` BIGINT, + `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, + FOREIGN KEY (`user_id`) REFERENCES `Users` (`user_id`), + FOREIGN KEY (`board_id`) REFERENCES `Boards` (`board_id`) ); -CREATE TABLE Tags ( - tag_id BIGINT AUTO_INCREMENT PRIMARY KEY, - name VARCHAR(255) NOT NULL UNIQUE +CREATE TABLE `Comments` ( + `comment_id` BIGINT PRIMARY KEY AUTO_INCREMENT, + `content` TEXT NOT NULL, + `user_id` BIGINT, + `post_id` BIGINT, + `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, + FOREIGN KEY (`user_id`) REFERENCES `Users` (`user_id`), + FOREIGN KEY (`post_id`) REFERENCES `Posts` (`post_id`) ); -CREATE TABLE Post_Tags ( - post_id BIGINT NOT NULL, - tag_id BIGINT NOT NULL, - FOREIGN KEY (post_id) REFERENCES Posts(post_id), - FOREIGN KEY (tag_id) REFERENCES Tags(tag_id) -); \ No newline at end of file +CREATE TABLE `Post_Tags` ( + `post_id` BIGINT, + `tag_id` BIGINT, + FOREIGN KEY (`post_id`) REFERENCES `Posts` (`post_id`), + FOREIGN KEY (`tag_id`) REFERENCES `Tags` (`tag_id`) +); + + diff --git a/src/main/java/com/jungmin/script/Part1.java b/src/main/java/com/jungmin/script/Part1.java index 868c8cd..6b014d7 100644 --- a/src/main/java/com/jungmin/script/Part1.java +++ b/src/main/java/com/jungmin/script/Part1.java @@ -3,55 +3,55 @@ public class Part1 { /* ---------------------------------------------------------------------------------------------- - TODO: 유어클래스의 requirement를 참조하여, migration/schema.sql에 알맞은 테이블을 구성해주세요. + TODO: requirement를 참조하여, migration/schema.sql에 알맞은 테이블을 구성해주세요. */ /* ---------------------------------------------------------------------------------------------- TODO: Q 1-1. 현재 있는 데이터베이스에 존재하는 모든 테이블 정보를 보기위한 SQL을 작성해주세요. */ - public static final String PART1_1 = "show tables"; + public static final String PART1_1 = "SHOW TABLES"; /* ---------------------------------------------------------------------------------------------- TODO: Q 1-2. Users 테이블의 구조를 보기위한 SQL을 작성해주세요. - 요구사항에 맞는 Users 테이블을 작성해야만, 테스트를 통과합니다. */ - public static final String PART1_2 = "DESCRIBE Users;"; + public static final String PART1_2 = "DESC Users"; /* ---------------------------------------------------------------------------------------------- TODO: Q 1-3. Posts 테이블의 구조를 보기위한 SQL을 작성해주세요. - 요구사항에 맞는 Posts 테이블을 작성해야만, 테스트를 통과합니다. */ - public static final String PART1_3 = "desc posts"; + public static final String PART1_3 = "DESC Posts"; /* ---------------------------------------------------------------------------------------------- TODO: Q 1-4. Tags 테이블의 구조를 보기위한 SQL을 작성해주세요. - 요구사항에 맞는 Tags 테이블을 작성해야만, 테스트를 통과합니다. */ - public static final String PART1_4 = "desc Tags"; + public static final String PART1_4 = "DESC Tags"; /* ---------------------------------------------------------------------------------------------- TODO: Q 1-5. Post_Tags 테이블의 구조를 보기위한 SQL을 작성해주세요. - 요구사항에 맞는 Post_Tags 테이블을 작성해야만, 테스트를 통과합니다. */ - public static final String PART1_5 = "desc Post_Tags"; + public static final String PART1_5 = "DESC Post_tags"; /* ---------------------------------------------------------------------------------------------- TODO: Q 1-6. Comments 테이블의 구조를 보기위한 SQL을 작성해주세요. - 요구사항에 맞는 Comments 테이블을 작성해야만, 테스트를 통과합니다. */ - public static final String PART1_6 = "desc Comments"; + public static final String PART1_6 = "DESC Comments"; /* ---------------------------------------------------------------------------------------------- TODO: Q 1-7. Boards 테이블의 구조를 보기위한 SQL을 작성해주세요. - 요구사항에 맞는 Boards 테이블을 작성해야만, 테스트를 통과합니다. */ - public static final String PART1_7 = "desc Boards"; + public static final String PART1_7 = "DESC Boards"; } diff --git a/src/main/java/com/jungmin/script/Part2.java b/src/main/java/com/jungmin/script/Part2.java index 50b1214..8c91835 100644 --- a/src/main/java/com/jungmin/script/Part2.java +++ b/src/main/java/com/jungmin/script/Part2.java @@ -11,124 +11,129 @@ public class Part2 { ---------------------------------------------------------------------------------------------- TODO: Q 2-2. users 테이블에 존재하는 모든 데이터에서 username 컬럼만을 확인하기 위한 SQL을 작성해주세요. */ - public static final String PART2_2 = "SELECT username FROM Users"; + public static final String PART2_2 = "SELECT u.username FROM Users u"; /* ---------------------------------------------------------------------------------------------- TODO: Q 2-3. users 테이블에 데이터를 추가하기 위한 SQL을 작성해주세요.") - 원하는 username, email, password를 사용하시면 됩니다. */ - public static final String PART2_3 = "INSERT INTO Users (username, email, password, created_at) VALUES ('newuser', 'newuser@example.com', 'newpassword', NOW())"; + public static final String PART2_3 = "INSERT INTO Users(username, email, password) VALUES('이름', 'abc@naver.com', '123123')"; /* ---------------------------------------------------------------------------------------------- TODO: Q 2-4. users 테이블에서 특정 조건을 가진 데이터를 찾기위한 SQL을 작성해주세요. - 조건 : username이 luckykim이여야 합니다. */ - public static final String PART2_4 = "SELECT * FROM Users WHERE username = 'luckykim'"; + public static final String PART2_4 = "SELECT * FROM Users u WHERE u.username = 'luckykim'"; /* ---------------------------------------------------------------------------------------------- TODO: Q 2-5. users 테이블에서 특정 조건을 가진 데이터를 찾기위한 SQL을 작성해주세요. - 조건 : username이 luckykim이 아니여야 합니다. */ - public static final String PART2_5 = "SELECT * FROM Users WHERE username <> 'luckykim'"; + public static final String PART2_5 = "SELECT u.username FROM Users u WHERE u.username <> 'luckykim'"; /* ---------------------------------------------------------------------------------------------- TODO: Q 2-6. posts 테이블에 존재하는 모든 데이터에서 title 컬럼만을 찾기 위한 SQL을 작성해주세요. */ - public static final String PART2_6 = "SELECT title FROM Posts"; + public static final String PART2_6 = "SELECT p.title FROM Posts p"; /* ---------------------------------------------------------------------------------------------- TODO: Q 2-7. posts의 title과 그 게시글을 작성한 user의 username을 찾기 위한 SQL을 작성해주세요. - 저자가 없더라도, posts의 title을 모두 찾아야합니다. */ - public static final String PART2_7 = "SELECT p.title, u.username FROM Posts p LEFT JOIN Users u ON p.user_id = u.user_id"; + public static final String PART2_7 = "SELECT p.title, u.username FROM Posts p LEFT JOIN Users u ON u.user_id = p.user_id"; /* ---------------------------------------------------------------------------------------------- TODO: Q 2-8. posts의 title과 그 게시글을 작성한 user의 username을 찾기 위한 SQL을 작성해주세요. - 저자가 있는 posts의 title만 찾아야합니다. */ - public static final String PART2_8 = "SELECT p.title, u.username FROM Posts p INNER JOIN Users u ON p.user_id = u.user_id"; + public static final String PART2_8 = "SELECT p.title, u.username FROM Posts p JOIN Users u ON u.user_id = p.user_id"; /* ---------------------------------------------------------------------------------------------- TODO: Q 2-9. posts 테이블의 데이터를 수정하기 위한 SQL을 작성해주세요. - title이 'New Tech Trends'인 posts 데이터에서 content를 'Updated content'로 수정해야합니다. */ - public static final String PART2_9 = "UPDATE Posts SET content = 'Updated content' WHERE title = 'New Tech Trends'"; + public static final String PART2_9 = "UPDATE Posts p SET p.title = 'Updated content' WHERE p.title = 'New Tech Trends'"; /* ---------------------------------------------------------------------------------------------- TODO: Q 2-10. posts 테이블의 데이터를 추가하기 위한 SQL을 작성해주세요. - luckykim이 작성한 게시글을 추가해주세요. 제목과 본문은 자유입니다. (참고: luckykim의 아이디는 1입니다.) */ - public static final String PART2_10 = "INSERT INTO Posts (title, content, user_id, board_id, created_at) VALUES ('New Post', 'This is a new post by luckykim.', 1, 1, CURRENT_TIMESTAMP)"; + public static final String PART2_10 = "INSERT INTO Posts (title, content, user_id, board_id) " + + "VALUES ('title', 'content', 1, 10);"; /* ---------------------------------------------------------------------------------------------- TODO: Q 2-11. 어느 board에도 속하지 않는 post의 모든 데이터를 찾기위한 SQL을 작성해주세요.") */ - public static final String PART2_11 = "SELECT * FROM Posts WHERE board_id IS NULL"; + public static final String PART2_11 = "SELECT * FROM Posts p WHERE p.board_id IS NULL"; /* ---------------------------------------------------------------------------------------------- TODO: Q 2-12. users 테이블의 user_id가 1인 유저가 작성한 post의 title을 찾기위한 SQL을 작성해주세요. */ - public static final String PART2_12 = "SELECT title FROM Posts WHERE user_id = 1"; + public static final String PART2_12 = "SELECT p.title FROM Posts p WHERE p.user_id = 1"; /* ---------------------------------------------------------------------------------------------- TODO: Q 2-13. users 테이블의 user_id가 1인 유저가 작성한 post의 board name을 찾기위한 SQL을 작성해주세요. */ - public static final String PART2_13 = "SELECT b.name FROM Posts p INNER JOIN Boards b ON p.board_id = b.board_id WHERE p.user_id = 1"; + public static final String PART2_13 = "SELECT b.name FROM Boards b JOIN Posts p ON b.board_id = p.board_id WHERE p.user_id = 1"; /* ---------------------------------------------------------------------------------------------- TODO: Q 2-14. board의 name이 'General Discussion'인 post의 title, content, created_at을 찾기위한 SQL을 작성해주세요. */ - public static final String PART2_14 = "SELECT p.title, p.content, p.created_at FROM Posts p INNER JOIN Boards b ON p.board_id = b.board_id WHERE b.name = 'General Discussion'"; + public static final String PART2_14 = "SELECT p.title, p.content, p.created_at FROM Posts p JOIN Boards b ON b.board_id = p.board_id WHERE b.name = 'General Discussion'"; /* ---------------------------------------------------------------------------------------------- TODO: Q 2-15. luckykim이 작성한 comment의 개수 (컬럼명: CommentCount)를 출력하기 위한 SQL을 작성해주세요. */ - public static final String PART2_15 = "SELECT COUNT(*) AS CommentCount FROM Comments WHERE user_id = 1"; + public static final String PART2_15 = "SELECT COUNT(c.comment_id) as CommentCount FROM Comments c JOIN Users u ON c.user_id = u.user_id WHERE u.username = 'luckykim'"; /* ---------------------------------------------------------------------------------------------- TODO: Q 2-16. 각 user(컬럼명: username)가 작성한 comment의 개수 (컬럼명: CommentCount)를 출력하기 위한 SQL을 작성해주세요. - 단, 0개의 comment를 작성한 유저도 모두 출력해야 합니다. */ - public static final String PART2_16 = "SELECT u.username, COUNT(c.comment_id) AS CommentCount FROM Users u LEFT JOIN Comments c ON u.user_id = c.user_id GROUP BY u.username"; + public static final String PART2_16 = "SELECT u.username as username, COUNT(c.comment_id) as CommentCount FROM Comments c LEFT JOIN Users u ON c.user_id = u.user_id GROUP BY u.username"; /* ---------------------------------------------------------------------------------------------- TODO: Q 2-17. 각 board의 name과 해당 board에 속한 post의 개수 (컬럼명: PostCount)를 출력하기 위한 SQL을 작성해주세요. */ - public static final String PART2_17 = "SELECT b.name, COUNT(p.post_id) AS PostCount FROM Boards b LEFT JOIN Posts p ON b.board_id = p.board_id GROUP BY b.name"; + public static final String PART2_17 = "SELECT b.name, COUNT(p.post_id) as PostCount FROM Boards b LEFT JOIN Posts p ON b.board_id = p.board_id GROUP BY b.name"; /* ---------------------------------------------------------------------------------------------- TODO: Q 2-18. 각 board의 name과 해당 board에 속한 posts 테이블의 content의 평균 길이 (컬럼명: AvgLength)를 출력하기 위한 SQL을 작성해주세요.") */ - public static final String PART2_18 = "SELECT b.name, AVG(CHAR_LENGTH(p.content)) AS AvgLength FROM Boards b LEFT JOIN Posts p ON b.board_id = p.board_id GROUP BY b.name"; + public static final String PART2_18 = "SELECT b.name, AVG(CHAR_LENGTH(p.content)) as AvgLength FROM Boards b LEFT JOIN Posts p ON b.board_id = p.board_id GROUP BY b.name"; /* ---------------------------------------------------------------------------------------------- TODO: Q 2-19. 각 board의 name과 해당 board에 속한 posts 테이블의 content의 평균 길이 (컬럼명: AvgLength)를 출력하기 위한 SQL을 작성해주세요. - 단, content가 null인 경우를 제외해야 합니다. */ - public static final String PART2_19 = "SELECT b.name, AVG(CHAR_LENGTH(p.content)) AS AvgLength FROM Boards b LEFT JOIN Posts p ON b.board_id = p.board_id GROUP BY b.name HAVING AvgLength IS NOT NULL "; + public static final String PART2_19 = "SELECT b.name, AVG(CHAR_LENGTH(p.content)) as AvgLength FROM Boards b JOIN Posts p ON b.board_id = p.board_id GROUP BY b.name HAVING AvgLength IS NOT NULL"; /* ---------------------------------------------------------------------------------------------- TODO: Q 2-20. 각 tag의 name과 해당 tag가 달린 post의 개수 (컬럼명: PostCount)를 출력하기 위한 SQL을 작성해주세요. - 단, post에 할당되지 않은 tag의 이름도 모두 출력해야 합니다.") */ - public static final String PART2_20 = "SELECT t.name, COUNT(pt.post_id) AS PostCount FROM Tags t LEFT JOIN Post_Tags pt ON t.tag_id = pt.tag_id GROUP BY t.name"; + public static final String PART2_20 = "SELECT t.name, COUNT(pt.post_id) AS PostCount " + + "FROM Tags t " + + "LEFT JOIN Post_Tags pt ON t.tag_id = pt.tag_id " + + "GROUP BY t.name " + + "ORDER BY t.name"; }