MySQL - TABLE 분리

조재민·2025년 4월 16일
post-thumbnail

TABLE 분리

  • 장점
    유지 보수가 좋음
  • 단점
    직관적이지 않음

위 table에 공통으로 들어가는 데이터인 author와 profile 의 데이터를 분리

author_id의 이름을 가진 table 과 topic의 이름을 가진 테이블로 분리

author_id 에는 id, name, profile 의 컬럼을 생성

위 table 을 topic 에 적용


코드

author table 생성 코드

CREATE TABLE `author` (
    ->   `id` int(11) NOT NULL AUTO_INCREMENT,
    ->   `name` varchar(20) NOT NULL,
    ->   `profile` varchar(200) DEFAULT NULL,
    ->   PRIMARY KEY (`id`)
    -> );

topic table 생성 코드

create table topic(
    -> `id` int(11) NOT NULL AUTO_INCREMENT,
    ->   `title` varchar(30) NOT NULL,
    ->   `description` text,
    ->   `created` datetime NOT NULL,
    ->   `author_id` int(11) DEFAULT NULL,
    ->   PRIMARY KEY (`id`)
    -> );    

author table 데이터 추가 코드

INSERT INTO author (id, name, profile) values(1, 'egoing', 'develepor');
INSERT INTO author (id, name, profile) VALUES (2,'duru','database administrator');
INSERT INTO author (id, name, profile) VALUES (3,'taeho','data scientist, developer');

topic table 데이터 추가 코드

INSERT INTO topic(id, title, description, created, author_id) values(1, 'My SQL', 'My SQL is...', '2018-1-1 12:10:11', 1);
IINSERT INTO topic(id, title, description, created, author_id) VALUES (2,'Oracle','Oracle is ...','2018-01-03 13:01:10',1);
INSERT INTO topic(id, title, description, created, author_id) VALUES (3,'SQL Server','SQL Server is ...','2018-01-20 11:01:10',2);
INSERT INTO topic(id, title, description, created, author_id) VALUES (4,'PostgreSQL','PostgreSQL is ...','2018-01-23 01:03:03',3);
INSERT INTO topic(id, title, description, created, author_id) VALUES (5,'MongoDB','MongoDB is ...','2018-01-30 12:31:03',1);
~~~
profile
“누군가는 너를 사랑하고 있다.”

0개의 댓글