221116 Node.js #5

김혜진·2022년 11월 16일
0

Node.js

목록 보기
5/13

MySQL


관계형 데이터베이스가 필요한 이유

  • Topic 테이블에 중복된 데이터들이 있다.
  • 리스트가 1만줄이라고 가정하자.
  • 중복 데이터가 1천개 정도 있다고 가정하자.
  • 중복된 데이터를 일일히 수정한다는 것은 매우 비효율적인 일이다.
    ex) lee ⇒ choi 1천개를 수동으로 변경
  • 현재 상태에서는 이러한 중복 문제를 해결할 수 없다.

Topic 테이블에서 중복된 컬럼을 별도의 테이블로 분리한다.

  • Author의 정보를 수정하면 Topic의 중복된 데이터가 자동으로 수정 적용된다.
  • author_id 1번이면 Author 테이블의 id 1번 ⇒ name, profile 수정
    lee ⇒ choi로 변경하면 author_id에 연결된 모든 1번의 값은 변경됨.

테이블 이름 바꾸기

rename table 현재 이름 to 바꿀 이름

테이블 생성

mysql> create table todo(
    -> id int(11) not null auto_increment,
    -> title varchar(100) not null,
    -> curdate text,
    -> writer_id int(11) default null,
    -> primary key(id));

mysql> create table writer(
    -> id int(11) not null auto_increment,
    -> name varchar(30) not null,
    -> profile varchar(30) default null,
    -> primary key(id));

데이터 삽입

  • writer 테이블
mysql> insert into writer (id, name, profile) values(1, 'lee', '개발자');

mysql> insert into writer (id, name, profile) values(2, 'kim', '기획자');

mysql> insert into writer (id, name, profile) values(3, 'park', '디자이너');

  • todo 테이블
mysql> insert into todo (title, curdate, writer_id) values('지하철타기', '2022.11.15', 1);

mysql> insert into todo (title, curdate, writer_id) values('걷기', '2022.11.16', 2);

mysql> insert into todo (title, curdate, writer_id) values('커피마시기', '2022.11.17', 3);

mysql> insert into todo (title, curdate, writer_id) values('이닦기', '2022.11.18', 1);

mysql> insert into todo (title, curdate, writer_id) values('공부하기', '2022.11.19', 1);

데이터 조인

select * from todo left join writer on todo.writer_id = writer.id;

select todo.id, title, curdate from todo left join writer on todo.writer_id = writer.id;

select todo.id as todo_id, title, curdate from todo left join writer on todo.writer_id = writer.id;

todo 테이블의 id 필드를 todo_id로 이름을 변경해서 출력


인터넷과 데이터베이스의 관계

인터넷 위에서 데이터베이스가 동작하면 굉장히 파워풀한 효과를 낼 수 있다.
MySQL은 기본적으로 인터넷을 활용할 수 있는 구조이다.

  • 인터넷이 사용되기 위해서 인터넷이 동작되기 위해서는 컴퓨터가 최소 몇 대 필요할까?

  • 인터넷은 여러 컴퓨터가 연결되어 형성된 컴퓨터 사회라고 할 수 있다.

  • 데이터베이스를 설치하면?

    우리가 다루는 데이터베이스는 서버인가 클라이언트인가?
    사용자는 무조건 클라이언트를 통해 서버에 접근할 수 있다.
    우리가 사용했던 mysql 클라이언트 mysql.exe
    Mysql 설치 시 mysql.exe가 제공되며 명령어를 통해 데이터베이스 서버를 제어할 수 있게 한다.


Node.js - MySQL 연결

npm install --save node-mysql

(실습 오류)

profile
알고 쓰자!

0개의 댓글