대외활동/UMC 4th

Ch4 SQL : RDS실습

oxdjww 2023. 5. 17. 01:35
728x90
반응형

 

UMC 4th - SpringBoot

 

 

DataGrip 다운로드: 데이터베이스 및 SQL용 크로스 플랫폼 IDE

 

www.jetbrains.com

공홈에서 데이터그립을 다운로드 해준다

 

실행 후, 좌측 상단 + 버튼을 통해 MySQL과 연결할 준비를 한다

 

Host : RDS엔드포인트

User : RDS 마스터 계정

Password : RDS 마스터 PW


이 때,,, 반드시 RDS 계정을 사용하도록 하자

필자는 EC2 계정으로 착각하여 며칠을 헤맸다


입력 후, Test Connection에 이런 팝업이 뜨면 바로 OK를 통해 접속해준다

 

그 후 Export해준다..

다음은 구글 및 티스토리에 떠돌아다니는 당근마켓 스키마이다

CREATE TABLE `User` (
    `userId` int AUTO_INCREMENT NOT NULL ,
    `name` varchar(20)  NOT NULL ,
    `nickname` varchar(30)  NOT NULL ,
    `email` varchar(50)  NOT NULL ,
    `phone` vatchar(20)  NOT NULL ,
    `password` text  NOT NULL ,
    `imgUrl` text  NULL ,
    `temperature` decimal  NOT NULL DEFAULT 36.5,
    `created` timestamp  NOT NULL DEFAULT current_timestamp,
    `updated` timestamp  NOT NULL DEFAULT current_timestamp,
    `status` varchar(10)  NOT NULL DEFAULT 'active',
    PRIMARY KEY (
        `userId`
    )
);

CREATE TABLE `Post` (
    `postId` int AUTO_INCREMENT NOT NULL ,
    `userId` int  NOT NULL ,
    `townId` int  NOT NULL ,
    `title` varchar(100)  NOT NULL ,
    `categoryId` int  NOT NULL ,
    `cost` int  NULL ,
    `content` text  NOT NULL ,
    `created` timestamp  NOT NULL DEFAULT current_timestamp,
    `updated` timestamp  NOT NULL DEFAULT current_timestamp,
    `status` varchar(10)  NOT NULL DEFAULT 'active',
    PRIMARY KEY (
        `postId`
    )
);

CREATE TABLE `PostImg` (
    `postImgId` int AUTO_INCREMENT NOT NULL ,
    `postId` int  NOT NULL ,
    `imageUrl` text  NOT NULL ,
    `created` timestamp  NOT NULL DEFAULT current_timestamp,
    `updated` timestamp  NOT NULL DEFAULT current_timestamp,
    `status` varchar(10)  NOT NULL DEFAULT 'active',
    PRIMARY KEY (
        `postImgId`
    )
);

CREATE TABLE `Category` (
    `categoryId` int AUTO_INCREMENT NOT NULL ,
    `name` varchar(20)  NOT NULL ,
    `imgUrl` text  NOT NULL ,
    `created` timestamp  NOT NULL DEFAULT current_timestamp,
    `updated` timestamp  NOT NULL DEFAULT current_timestamp,
    `status` varchar(10)  NOT NULL DEFAULT 'active',
    PRIMARY KEY (
        `categoryId`
    )
);

CREATE TABLE `Room` (
    `roomId` int  NOT NULL ,
    `sellerId` int  NOT NULL ,
    `buyerId` int  NOT NULL ,
    `postId` int  NOT NULL ,
    `created` timestamp  NOT NULL DEFAULT current_timestamp,
    `updated` timestamp  NOT NULL DEFAULT current_timestamp,
    `status` varchar(10)  NOT NULL DEFAULT 'active',
    PRIMARY KEY (
        `roomId`
    )
);

CREATE TABLE `Chat` (
    `chatId` int AUTO_INCREMENT NOT NULL ,
    `roomId` int  NOT NULL ,
    `message` text  NOT NULL ,
    `created` timestamp  NOT NULL DEFAULT current_timestamp,
    `updated` timestamp  NOT NULL DEFAULT current_timestamp,
    `status` varchat(10)  NOT NULL DEFAULT 'active',
    PRIMARY KEY (
        `chatId`
    )
);

CREATE TABLE `Town` (
    `townId` int  NOT NULL ,
    `name` varchar(20)  NOT NULL ,
    `city` varchar(20)  NOT NULL ,
    `district` varchar(20)  NOT NULL ,
    PRIMARY KEY (
        `townId`
    )
);

CREATE TABLE `Address` (
    `addressId` int  NOT NULL ,
    `userId` int  NOT NULL ,
    `townId` int  NOT NULL ,
    `created` timestamp  NOT NULL DEFAULT current_timestamp,
    `updated` timestamp  NOT NULL DEFAULT current_timestamp,
    `status` varchar(10)  NOT NULL DEFAULT 'active',
    PRIMARY KEY (
        `addressId`
    )
);

CREATE TABLE `Area` (
    `areaId` int  NOT NULL ,
    `userId` int  NOT NULL ,
    `addressId` int  NOT NULL ,
    `area` int  NOT NULL ,
    `created` timestamp  NOT NULL DEFAULT current_timestamp,
    `updated` timestamp  NOT NULL DEFAULT current_timestamp,
    `status` varchar(10)  NOT NULL DEFAULT 'active',
    PRIMARY KEY (
        `areaId`
    )
);

CREATE TABLE `Wishlist` (
    `wishlistId` int  NOT NULL ,
    `userId` int  NOT NULL ,
    `postId` int  NOT NULL ,
    `created` timestamp  NOT NULL DEFAULT current_timestamp,
    `updated` timestamp  NOT NULL DEFAULT current_timestamp,
    `status` varchar(10)  NOT NULL DEFAULT 'active',
    PRIMARY KEY (
        `WishlistId`
    )
);

ALTER TABLE `Post` ADD CONSTRAINT `fk_Post_userId` FOREIGN KEY(`userId`)
REFERENCES `User` (`userId`);

ALTER TABLE `Post` ADD CONSTRAINT `fk_Post_categoryId` FOREIGN KEY(`categoryId`)
REFERENCES `Category` (`categoryId`);

ALTER TABLE `PostImg` ADD CONSTRAINT `fk_PostImg_postId` FOREIGN KEY(`postId`)
REFERENCES `Post` (`postId`);

ALTER TABLE `Room` ADD CONSTRAINT `fk_Room_sellerId` FOREIGN KEY(`sellerId`)
REFERENCES `User` (`userId`);

ALTER TABLE `Room` ADD CONSTRAINT `fk_Room_buyerId` FOREIGN KEY(`buyerId`)
REFERENCES `User` (`userId`);

ALTER TABLE `Room` ADD CONSTRAINT `fk_Room_postId` FOREIGN KEY(`postId`)
REFERENCES `Post` (`postId`);

ALTER TABLE `Chat` ADD CONSTRAINT `fk_Chat_roomId` FOREIGN KEY(`roomId`)
REFERENCES `Room` (`roomId`);

ALTER TABLE `Address` ADD CONSTRAINT `fk_Address_userId` FOREIGN KEY(`userId`)
REFERENCES `User` (`userId`);

ALTER TABLE `Address` ADD CONSTRAINT `fk_Address_townId` FOREIGN KEY(`townId`)
REFERENCES `Town` (`townId`);

ALTER TABLE `Area` ADD CONSTRAINT `fk_Area_userId` FOREIGN KEY(`userId`)
REFERENCES `User` (`userId`);

ALTER TABLE `Area` ADD CONSTRAINT `fk_Area_addressId` FOREIGN KEY(`addressId`)
REFERENCES `Address` (`addressId`);

ALTER TABLE `Wishlist` ADD CONSTRAINT `fk_Wishlist_userId` FOREIGN KEY(`userId`)
REFERENCES `User` (`userId`);

ALTER TABLE `Wishlist` ADD CONSTRAINT `fk_Wishlist_postId` FOREIGN KEY(`postId`)
REFERENCES `Post` (`postId`);

 

그 후 다양한 쿼리 및 더미데이터를 반영하며 실습을 진행해보았다

 

ch5, ch6는 간단한 Java, JS실습이라 생략하겠다..!

 

감사합니다!

728x90
반응형