Backend/MYSQL

생활코딩( Database2 MySQL ) (9) - Relational Database(1)

코르시카 2021. 6. 2. 14:09

1. Relational Database

 

1-1) 관계형 데이터베이스의 특징

기존의 single database로 다룰 수 없는 복잡성을 다룰 때 사용하게 됨

> 위의 경우에 egoing - developer 항목이 중복이 되는데 이 해당 부분을 다른 table의  데이터로 기입하고

해당 데이터를 현재 table에서 이어주는 것이 더 효율적

> 따라서 다음과 같이 author 컬럼을 따로 빼서 작성하는 것이 중복을 피할 수 있는 방식

1-2) RDB 장점

  1. 중복사용되는 데이터를 1회만 변경해도 변경사항이 반영
  2. 중복되는 데이터를 방지
  3. 특정 필드를 따로 table로 관리하기 때문에 id값으로 유일자로 관리가 가능
    eg)  egoing / developer가 다른 사람이지만 2명이 있을 때 등...

1-3) RDB 단점

  1. 기존 table 분리 이전은 직관적으로 모든 데이터를 하나의 테이블에서 확인할 수 있음에도
    RDB로 작성 시, 표가 분리되는 등 비직관적임
    -> Join syntax를 사용하여 한번에 같이 볼 수 있음

 

2. 테이블 drop & create

> drop table syntax

DROP DATABASE <db_name>
DROP TABLE <tbl_name>

2-1) SQL script

-- use opentutorials schema
USE opentutorials;

-- drop existing tables
DROP TABLE topic;
DROP TABLE author;

--
-- Table structure for table `author`
--
 
CREATE TABLE `author` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL,
  `profile` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`id`)
);
 
--
-- Dumping data for table `author`
--
 
INSERT INTO `author` VALUES (1,'egoing','developer');
INSERT INTO `author` VALUES (2,'duru','database administrator');
INSERT INTO `author` VALUES (3,'taeho','data scientist, developer');
 
--
-- Table structure for table `topic`
--
 
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`)
);
 
--
-- Dumping data for table `topic`
--
 
INSERT INTO `topic` VALUES (1,'MySQL','MySQL is...','2018-01-01 12:10:11',1);
INSERT INTO `topic` VALUES (2,'Oracle','Oracle is ...','2018-01-03 13:01:10',1);
INSERT INTO `topic` VALUES (3,'SQL Server','SQL Server is ...','2018-01-20 11:01:10',2);
INSERT INTO `topic` VALUES (4,'PostgreSQL','PostgreSQL is ...','2018-01-23 01:03:03',3);
INSERT INTO `topic` VALUES (5,'MongoDB','MongoDB is ...','2018-01-30 12:31:03',1);

 

2-2) output

mysql> SELECT * FROM topic;
+----+------------+-------------------+---------------------+-----------+
| id | title      | description       | created             | author_id |
+----+------------+-------------------+---------------------+-----------+
|  1 | MySQL      | MySQL is...       | 2018-01-01 12:10:11 |         1 |
|  2 | Oracle     | Oracle is ...     | 2018-01-03 13:01:10 |         1 |
|  3 | SQL Server | SQL Server is ... | 2018-01-20 11:01:10 |         2 |
|  4 | PostgreSQL | PostgreSQL is ... | 2018-01-23 01:03:03 |         3 |
|  5 | MongoDB    | MongoDB is ...    | 2018-01-30 12:31:03 |         1 |
+----+------------+-------------------+---------------------+-----------+
5 rows in set (0.00 sec)

mysql> SELECT * FROM author;
+----+--------+---------------------------+
| id | name   | profile                   |
+----+--------+---------------------------+
|  1 | egoing | developer                 |
|  2 | duru   | database administrator    |
|  3 | taeho  | data scientist, developer |
+----+--------+---------------------------+
3 rows in set (0.00 sec)

 

 

 


참조

https://opentutorials.org/course/3161/19543

 

수업의 정상 - 생활코딩

수업의 정상 2018-02-12 01:26:25

opentutorials.org

https://opentutorials.org/course/3161/19544

 

관계형 데이터베이스의 필요성 - 생활코딩

관계형 데이터베이스의 필요성 2018-02-12 01:26:57

opentutorials.org

https://opentutorials.org/course/3161/19521

 

테이블 분리하기 - 생활코딩

테이블 분리하기 2018-02-10 15:56:41 -- -- Table structure for table `author` -- CREATE TABLE `author` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL, `profile` varchar(200) DEFAULT NULL, PRIMARY KEY (`id`) ); -- -- Dumping data

opentutorials.org

 

반응형