Database/SQL

why SQL? & Relational Databases

metamong 2022. 3. 30.

๐Ÿงก Structured Query Language ๐Ÿงก

๐Ÿฆ„ 1970๋…„๋Œ€์— ๋“ฑ์žฅํ–ˆ์œผ๋ฉด ๋‹ค์†Œ '์˜ฌ๋“œํ•˜๋‹ค'๋Š” ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€์ ธ๋‹ค ์ค„ ์ˆ˜ ์žˆ๊ฒ ์ง€๋งŒ, ์ „ ์„ธ๊ณ„ ๊ธฐ์—…๋“ค - ํŽ˜์ด์Šค๋ถ, ์ธ์Šคํƒ€, ์—์–ด๋น„์•ค๋น„, ์šฐ๋ฒ„ ๋“ฑ๋“ฑ -์ด ๋ณด์œ ํ•œ ๋Œ€์šฉ๋Ÿ‰์˜ data๋Š” database์— ์ €์žฅ๋จ - ์ด data๋ฅผ ์ ‘๊ทผํ•˜๋ ค๋ฉด SQL์„ ์‚ฌ์šฉํ•ด์•ผ ํ•จ (๋”ฐ๋ผ์„œ sql ๊ณต๋ถ€๋Š” ๊ทธ๋ƒฅ ํ•„์ˆ˜๋‹ค ํ•„์ˆ˜ ๐Ÿ‹๏ธ‍โ™€๏ธ) ๊ธฐ์—…๊ณต๊ณ ๋งŒ ๋ณธ๋‹ค ํ•˜๋”๋ผ๋„ python, R๋ณด๋‹ค๋„ ๋” ๋งŽ์ด ์š”๊ตฌํ•˜๋Š” SQL!

 

SQL is

→ helps you filter and fetch only the data you need from databases

→ a language designed to allow both technical and non-technical users query, manipulate, and transform data from a relational database

→ SQL database โ‰ซ including SQLite, MySQL, Postgres, Oracle and Microsoft SQL Server
(implementation ๋ฉด์—์„œ ์•ฝ๊ฐ„ ์•ฝ๊ฐ„์”ฉ ๋ฐ๋ฒ  ์ข…๋ฅ˜๋ณ„๋กœ ๋‹ค๋ฆ„ + ์ง€์›ํ•˜๋Š” ์ถ”๊ฐ€์ ์ธ ๊ธฐ๋Šฅ & ์ €์žฅ์†Œ ์œ ํ˜•๋„ ์•ฝ๊ฐ„ ๋‹ค๋ฆ„)

 

โ€ป ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ ์ €์žฅ์— ์œ ๋ฆฌํ•œ database๋ฅผ ์•ก์„ธ์Šคํ•˜๊ธฐ์— ํ•„์š”ํ•œ SQL โ€ป

 

์—‘์…€์ด๋‚˜ csv file์€ ํ•œ๊ณ„๊ฐ€ ์žˆ์Œ. ๋Œ€์šฉ๋Ÿ‰ data๋ฅผ ์กด์žฌํ•˜๊ธฐ์— ์šฉ๋Ÿ‰๋„ ์ ๊ณ  ๋‹ค๋ฃจ๊ธฐ ํž˜๋“ค๋‹ค. ํ•˜์ง€๋งŒ ๋ฐ‘์—์„œ ๋‹ค๋ฃฐ relational database๋Š” ๋Œ€์šฉ๋Ÿ‰์˜ data(structured)๋ฅผ ์‰ฝ๊ฒŒ ์ €์žฅํ•  ์ˆ˜ ์žˆ๊ณ  ํ•œ๋ฒˆ์— ์„œ๋กœ ๊ด€๊ณ„๊ฐ€ ๊นŠ์€ ์—ฌ๋Ÿฌ table์„ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— data ๋‹ค๋ฃจ๊ธฐ์—๋„ ๋งค์šฐ ์ข‹๋‹ค. 

 

→ ํฌ๊ฒŒ ๋‹ค์„ฏ๊ฐ€์ง€๋กœ ๋‚˜๋ˆ”

1> DDL(Data Definition Language): ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ table๊ณผ ๊ฐ™์€ object๋ฅผ ์ •์˜ํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ์–ธ์–ด. ex) CREATE, DROP

2> DML(Data Manipulation Language): ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ data๋ฅผ ๋ณ€๊ฒฝํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ์–ธ์–ด. ex) INSERT, DELETE, UPDATE

3> DCL(Data Control Language): ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋Œ€ํ•œ ์ ‘๊ทผ๊ถŒํ•œ๊ณผ ๊ด€๋ จ๋œ ์–ธ์–ด. ๊ถŒํ•œ์„ ์„ค์ •ํ•˜๊ฑฐ๋‚˜ ๊ฐ€์ ธ๊ฐ€๊ฑฐ๋‚˜. ex) GRANT, REVOKE

4> DQL(Data Query Language): ์ •ํ•ด์ง„ schema๋‚ด์—์„œ ์ฟผ๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋Š” ์–ธ์–ด. (ex) SELECT.) DML์˜ ์ผ๋ถ€๋ถ„์œผ๋กœ ๋งํ•˜๊ธฐ๋„ ํ•จ

5> TCL(Transaction Control Language): DML์„ ๊ฑฐ์นœ data์˜ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ์ˆ˜์ •ํ•˜๋Š” ์–ธ์–ด. ex) COMMIT, ROLLBACK - ์ž‘์—…ํ•œ ๋‚ด์šฉ์„ ์ตœ์ข…์ ์œผ๋กœ ๋ฐ˜์˜ํ•˜๊ฑฐ๋‚˜, ๋„๋กœ ์ทจ์†Œํ•˜์—ฌ ๋Œ๋ฆฌ๋Š” ์—ญํ•  ๋“ฑ๋“ฑ.

๐Ÿšถ Relational Databases ๐Ÿšถ

๐Ÿฆ„ represents a collection of related (two-dimensional) tables. Each of the tables are similar to an Excel spreadsheet, with a fixed number of named columns (the attributes or properties of the table) and any number of rows of data.

 

์„ค๋ช…>

→ ๋ชจ๋“  table๋“ค์—๋Š” primary key๋ผ๋Š”, ๋ชจ๋“  row๋ฅผ ๊ตฌ๋ณ„ํ•ด์ฃผ๋Š” ์ผ์ข…์˜ ๊ธฐ๋ณธํ‚ค๊ฐ€ ๋“ค์–ด์žˆ๋Š” column์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์ด๋Ÿฐ table๋“ค๋ผ๋ฆฌ ๊ธฐ๋ณธํ‚ค - ์™ธ๋ž˜ํ‚ค์™€ ์—ฐ๊ฒฐ๋˜์–ด ๊ด€๊ณ„๋ฅผ ํ˜•์„ฑํ•  ๋•Œ, ํ•ด๋‹น table๋“ค์˜ ์ง‘ํ•ฉ์ฒด๋ฅผ Relational Database๋ผ๊ณ  ํ•œ๋‹ค.

→ ๊ฐ table์€ data์˜ ์—ฌ๋Ÿฌ ํŠน์ง•๋“ค์„ ๋‚˜์—ดํ•œ column๋“ค์ด ์žˆ์œผ๋ฉฐ, ํ•ด๋‹น ํŠน์ง•๋“ค์˜ ์—ฌ๋Ÿฌ ์ง‘ํ•ฉ์ฒด์ธ row๋“ค์ด ์กด์žฌํ•œ๋‹ค.

 

-- Relational Database ์˜ˆ์‹œ --

โ‰ชBooksโ‰ซ

book_id(PK) book_page ratings(out of 5)
324 334 4/5
881 154 4.5/5

→ Books๋ผ๋Š” table์— ๊ณ ์œ ํ‚ค column์ธ book_id๊ฐ€ ์กด์žฌํ•œ๋‹ค.

 

โ‰ชAuthorsโ‰ซ

author_id(PK) book_id ratings(out of 10)
381 324 5/10
112 881 8/10

→ Books์˜ book_id์™€ ์—ฐ๊ฒฐ๋˜๋Š” Authors table์˜ foreign key book_id column์ด ๋˜‘๊ฐ™์ด ์กด์žฌํ•œ๋‹ค.

(์ด๋ ‡๊ฒŒ ๊ธฐ๋ณธํ‚ค - ์™ธ๋ž˜ํ‚ค์˜ ์กฐํ•ฉ์ด ์ƒ์„ฑ๋จ์œผ๋กœ์จ ๋‘ ํ…Œ์ด๋ธ”์ด ์„œ๋กœ ์—ฐ๊ฒฐํ•˜๋Š” ๊ด€๊ณ„๋ง์„ ํ˜•์„ฑํ•˜๊ธฐ ๋•Œ๋ฌธ์— '๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค'๋ผ๊ณ  ๋ถ€๋ฆ„!)

 

→ ์–ธ๊ธ‰ํ•œ <๊ธฐ๋ณธํ‚ค - ์™ธ๋ž˜ํ‚ค> ์กฐํ•ฉ์—์„œ ์ง€์ผœ์•ผ ํ•  ๋‘ ๊ฐ€์ง€ ์ œ์•ฝ์‚ฌํ•ญ์ด ์กด์žฌํ•จ. โ‘ 'entity integrity': table์— ์กด์žฌํ•˜๋Š” primary key ๊ธฐ๋ณธํ‚ค๋Š” ๊ณ ์œ ํ•˜๊ณ , ํ•ด๋‹น ๊ธฐ๋ณธํ‚ค๋Š” ๊ฐ row๋งˆ๋‹ค ๋ชจ๋‘ ๋‹ค๋ฅธ, ๊ฐ row๋ฅผ ๊ตฌ๋ณ„ํ•ด ์ฃผ๋Š” ์†์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Œ. ๊ทธ๋ฆฌ๊ณ  null๊ฐ’์ด๋ฉด X. โ‘ก'referential integrity': ํ•œ table์— foreign key column์ด ์žˆ๋‹ค๋ฉด ํ•ด๋‹น FK์™€ ์—ฐ๊ฒฐ๋˜๋Š” ํƒ€ table์˜ PK - ์ด๋ ‡๊ฒŒ ํ•œ ๊ฐœ๋„ ๋น ์ง์—†์ด ๋ชจ๋“  FK๊ฐ€ PK์™€ ํ•จ๊ป˜ ์Œ์œผ๋กœ ์กด์žฌํ•œ๋‹ค. 

 

SQL๊ณผ์˜ ๊ด€๊ณ„>

→ 'The standard user and application program interface (API) of a relational database is the Structured Query Language. SQL code statements are used both for interactive queries for information from a relational database and for gathering data for reports' - relational db์˜ table(relation)๋“ค์— ์ ‘๊ทผํ•ด์„œ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ data๋ฅผ ๋ชจ์œผ๋Š” ๋“ฑ, ๊ด€๊ณ„ ์†์˜ ์—ฌ๋Ÿฌ ์ •๋ณด๋“ค์„ ์กฐํ•ฉํ•˜๊ณ  ๋˜ ๊ฐ€์ ธ์˜ค๋Š” ์ผ์ข…์˜ API ์—ญํ• ์„ ํ•˜๋Š” ๊ฑธ 'SQL'์–ธ์–ด๊ฐ€ ๋‹ด๋‹นํ•œ๋‹ค๊ณ  ๋งํ•  ์ˆ˜ ์žˆ๋‹ค.

 

Q. ํŠน์ง•๊ณผ ์žฅ์ ?

 

-- ์žฅ์  --

โ‘  <ease of use>: 'In the enterprise, relational databases are used to organize data and identify relationships between key data points. They make it easy to sort and find information, which helps organizations make business decisions more efficiently and minimize costs. They work well with structured data.' 

์ •๋ณด๋ฅผ ์ฐพ๊ณ  ๊ด€๋ฆฌํ•˜๊ณ  ๋‹ค๋ฃจ๋Š” ๋ฐ ๋งค์šฐ ํŽธ๋ฆฌ & ์‰ฌ์›€

 

โ‘ก <categorizing data>: 'Database administrators can easily categorize and store data in a relational database that can then be queried and filtered to extract information for reports. Relational databases are also easy to extend and aren't reliant on physical organization. After the original database creation, a new data category can be added without having to modify the existing applications.'

→ logical data independence(RDB๋‚ด์˜ data๊ฐ€ ๋ฐ”๋€Œ์–ด๋„ ๋ฌผ๋ฆฌ์ ์ธ ์™ธ๋ถ€ ์žฅ์น˜์—๋Š” ์˜ํ–ฅ์„ ์•ˆ ์ฃผ๋Š” ์„ฑ์งˆ)๋ผ๋Š” ์„ฑ์งˆ ๋•Œ๋ฌธ์— RDB๋‚ด์—์„œ ์‰ฝ๊ฒŒ data๋ฅผ ์ €์žฅํ•˜๊ณ  ๋˜ ์ข…๋ชฉ๋ณ„๋กœ ๋‚˜๋ˆ„์–ด ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. physicalํ•œ ๋ถ€๋ถ„๊ณผ logicalํ•œ ๋ถ€๋ถ„์ด ์„œ๋กœ ์˜ํ–ฅ์„ ์•ˆ๋ฐ›๋Š”, ๋…๋ฆฝ์ ์ด๋ผ๋Š” ๋ถ€๋ถ„์ด ๋งค์šฐ ํฐ ์žฅ์ !

 

โ‘ข <accuracy>: ''Data is stored just once, eliminating data deduplication in storage procedures.'

→ data๋Š” ํ•œ ๋ฒˆ๋งŒ ์ €์žฅ๋œ๋‹ค. ์ค‘๋ณต ํ—ˆ์šฉ์ด ์•ˆ๋จ

 

โ‘ฃ <collaboration> 'Multiple users can access the same database'

๋‹ค์ˆ˜์˜ ์‚ฌ์šฉ์ž๊ฐ€ ๋˜‘๊ฐ™์€ data์— ์ ‘๊ทผ ๊ฐ€๋Šฅํ•จ!

 

โ‘ค <security>: ์ผ๋ถ€ ์‚ฌ์šฉ์ž์—๊ฒŒ๋งŒ ์ผ๋ถ€ data์— ํ•œํ•ด์„œ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ๋” ์„ค์ • ๊ฐ€๋Šฅ

 

-- ๋‹จ์  --

โ‘  <structures>: ' Relational databases require a lot of structure and a certain level of planning because columns must be defined and data needs to fit correctly into somewhat rigid categories. The structure is good in some situations, but it creates issues related to the other drawbacks, such as maintenance and lack of flexibility and scalability.'

→ table์ด๋ผ๋Š” ์ผ์ •ํ•œ ๊ตฌ์กฐ์— ๋งž์ถ”์–ด data๋ฅผ ์ง‘์–ด๋„ฃ์–ด์•ผ ํ•จ! ์œ ์—ฐ์„ฑ ์ธก๋ฉด์—์„œ ํฐ ๋‹จ์ ์ž„.

 

โ‘ก <maintanence issues>: 'Developers and other personnel responsible for the database must spend time managing and optimizing the database as data gets added to it'

→ ์œ ์ง€๋ณด์ˆ˜ ์ธก๋ฉด์—์„œ ํฐ ๋‹จ์ . database๊ฐ€ ์ˆ˜์ •๋  ๋•Œ๋งˆ๋‹ค ๊ด€๋ฆฌํ•ด์•ผ ํ•จ.

 

โ‘ข <inflexibility>: 'Relational databases are not ideal for handling large quantities of unstructured data. Data that is largely qualitative, not easily defined or dynamic is not optimal for relational databases, because as the data changes or evolves, the schema must evolve with it, which takes time.'

→ ์œ ์—ฐ์„ฑ์ด ๋งŽ์ด ๋–จ์–ด์ง€๋Š” ๋‹จ์ . ๋Œ€๊ทœ๋ชจ์˜ ๋น„์ •ํ˜• data๋ฅผ ์ง‘์–ด๋„ฃ๊ธฐ์—๋Š” ๋„ˆ๋ฌด ํ•ธ๋“ค๋งํ•˜๊ธฐ ์–ด๋ ค์›€. ๊ทธ๋ฆฌ๊ณ  ์ง€์†์ ์œผ๋กœ ๋ณ€ํ™”ํ•˜๋Š” ์†์„ฑ์„ ๊ฐ€์ง„ data๋ผ๋ฉด ๋Œ€๊ทœ๋ชจ ์ˆ˜์ •์ž‘์—…์ด ๊ณ„์† ์ด๋ฃจ์–ด์ ธ์•ผ ํ•˜๋Š”๋ฐ ์‹œ๊ฐ„๋„ ๋งŽ์ด ๊ฑธ๋ฆฌ๊ณ  ๋งค์šฐ ๋น„ํšจ์œจ์ ์ž„.

 

โ‘ฃ <lack of scalability>: ''Relational databases do not horizontally scale well across physical storage structures with multiple servers. It is difficult to handle relational databases across multiple servers because as a data set gets larger and more distributed, the structure is disrupted, and the use of multiple servers has effects on performance -- such as application response times -- and availability.

→ ์—ญ์‹œ ํ™•์žฅ์„ฑ๋„ ๋–จ์–ด์ง. ๋‹ค์–‘ํ•œ ๋Œ€๊ทœ๋ชจ์˜ server์— ์ ํ•ฉํ•˜๊ฒŒ data๊ฐ€ ์ €์žฅ๋˜๋ ค๋ฉด ๊ธฐ์กด data ๊ตฌ์กฐ๋ฅผ ๋ฐ”๊ฟ”์•ผ ํ•˜๊ณ  ์ด๋Š” ๊ณง ์—ฌ๋Ÿฌ server performance์— ์˜ํ–ฅ์„ ์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ์—ญ์‹œ ๋น„ํšจ์œจ์ ์ž„.

 

-- SQL๊ณผ RDB ๊ฐœ๋… ์„ค๋ช… ์™„๋ฃŒ! --


* ์ถœ์ฒ˜1) https://www.youtube.com/watch?v=wr7Qwp0cBzI&t=47s 

* ์ถœ์ฒ˜2) https://sqlbolt.com/

* ์ถœ์ฒ˜3) https://www.youtube.com/watch?v=OqjJjpjDRLc 

* ์ถœ์ฒ˜4) https://www.techtarget.com/searchdatamanagement/definition/relational-database

๋Œ“๊ธ€