공부 이야기

MySQL 공부하기(Feat. Workbench)

창이 2023. 11. 19.
728x90
반응형

안녕하세요. 업무 상 필요해서 데이터베이스 관련 공부를 해보고 있습니다. 

 

DBMS란, 데이터베이스를 관리하는 시스템으로써, 여러 응용 시스템들의 통합된 정보를 저장하여 운영할 수 있는 공용 데이터들의 묶음입니다. 

 

그 중 RDBMS란, 관계형 데이터베이스 관리 시스템으로 불리는데 가장 많이 사용되는 종류입니다. 역사가 오래되며 가장 신뢰성이 높으며 탐색 속도가 빠릅니다. 이 RDBMS는 테이블 형태로 되어 있으며 속성과 데이터 값으로 구조화되어 있습니다. 

 

Primary Key(기본키) : 한 테이블의 각 행을 유일하게 식별해주는 컬럼으로, 각 테이블마다 Primary Key가 존재해야 하며, NULL 값을 허용하지 않고, 각 행마다 유일한 값이어야 함. 

Foreign Key : 한 테이블의 필드 중 다른테이블의 행을 식별할 수 있는 키 

 

Schema > 데이터 베이스의 테이블 구조 및 형식, 관계 등의 정보를 형식 언어로 기술한 것 

1) 관계형 데이터베이스를 사용하여 데이터를 저장할 때 가장 먼저 할 일은 데이터의 공통 속성을 식별하여 컬럼으로 정의하고 테이블을 만드는 것 

2) 통상적으로 하나의 테이블이 아닌 여러 개의 테이블로 만들고 각 테이블 구조, 형식, 관계를 정의함

3) 이를 스키마라고 하고 일종의 데이터베이스 설계도로 이해하면 됨. 

4) 데이터베이스마다 스키마를 만드는 언어가 존재하며, 해당 스키마만 있으면 동일한 구조의 데이터베이스를 만들 수 있다. 

 

각 데이터베이스 안에는 여래 개의 데이터베이스 이름이 존재. 그리고 각 데이터베이스 안에는 여러 개의 테이블이 존재함. 

create database mydata;
use mydata;

 

'mydata' 데이터베이스를 생성하고 mytable 테이블을 생성한다.

그리고 mytable 테이블 안에 id, name, modelnumber, series 속성을 넣어놓는다

id 값은 자동으로 증가하는 auto_increment 값을, 

NOT NULL을 넣어 데이터가 비어 있으면 0으로 처리한다. 

create table mytable (
id INT UNSIGNED NOT NULL auto_increment, 
name varchar(50) NOT NULL,
modelnumber varchar(15) NOT NULL,
series varchar(30) NOT NULL,
PRIMARY KEY(id)
);
alter table mytable add lowest_price INT UNSIGNED NOT NULL;

테이블에 있는 데이터를 변경하거나 추가할 때는 alter 문법을 이용한다. 

desc mytable;

 

insert into mytable values (1, 'i7', '7700', 'Kaby Lake');
select * from mytable; -- id는 auto_increment라서 굳이 table 넣어줄 필요 없음 
insert into mytable (name, modelnumber, series) values('i7', '7700', 'Kaby Lake');
insert into mytable (name, modelnumber, series) values('i5', '9600', 'Coffee Lake');
insert into mytable (name, modelnumber, series) values('i5', '9400', 'Coffee Lake');
insert into mytable (name, modelnumber, series) values('i7', '9700', 'Coffee Lake');
insert into mytable (name, modelnumber, series) values('i7', '8700', 'Coffee Lake');

mytable 테이블 안에 구체적인 값을 넣는다. 

alter table mytable modify column modelnumber varchar(30) NOT NULL;

 

select * from mytable;

 

select name from mytable;
select name, modelnumber from mytable;
select id from mytable order by id desc; -- 내림차순
select id from mytable order by id asc; -- 오름차순
select id from mytable where id < 3 ;



select id, name from mytable where id > 3 and id < 7;
-- 찾으려는 데이터 중 일부분만 검색하고 싶을 때 like 
select id, name from mytable where name LIKE '%7%'; -- 7이 들어간 값 모두 
select * from mytable;
select series from mytable where series LIKE 'Kaby%'; -- Kaby로 시작하는 값 모두 
select * from mytable where series LIKE 'Kaby%';
select id, name from mytable limit 5;
select id, name from mytable limit 2, 2; -- 처음 두 개 빼고 그 다음 검색 되는 두개



select * from mytable;
select * from mytable where modelnumber like '7700%';
select * from mytable where name = 'i7';
select * from mytable where name like '%i7%';
select * from mytable where series like '%Kaby Lake%' LIMIT 1;



update mytable set name = 'i3' where id = 3;
update mytable set name = 'i3', modelnumber = '5500K' where id = 3;
select * from mytable;
delete from mytable where id = 1;
select * from mytable;



desc mytable;
update mytable set lowest_price = 176660 where id = 2;
update mytable set lowest_price = 468090 where id = 3;
update mytable set lowest_price = 357520 where id = 4;
update mytable set lowest_price = 252130 where id = 5;
update mytable set lowest_price = 369800 where id = 6;



select * from mytable;
select name, modelnumber from mytable where lowest_price <= 300000;
select * from mytable where lowest_price >= 400000;




create database ecommerce;
use ecommerce;
show databases;
create table product (
product_code varchar(20) not null,
title varchar(200) not null,
ori_price int, 
discount_price int,
discount_percent int,
delevery varchar(2),
primary key(product_code)
);



DESC product;
create table ranking (
id INT UNSIGNED AUTO_INCREMENT,
category varchar(50),
sub_category varchar(50), 
ranking int not null,
product_code varchar(20),
primary key(id)
);



insert into product values('215673140', '스위트바니 여름신상', 23000, 6900, 70, 'F');
SELECT * FROM product;



use sakila;
show tables;
select * from country;

select * from film limit 10;
select count(*) from film; -- 영화 천개 있구나 

select distinct rating from film; -- 범주형 데이터 ( 도메인 지식이 있으면 있을 수록 좋음. 데이터 분석의 깊이도 달라질 수 있음 )
select distinct release_year from film; 
select * from rental limit 10;
select * from rental where inventory_id = 367;

select count(*) from customer;

select * from payment limit 5;
select 
sum(amount), avg(amount), 
    max(amount), min(amount) 
from payment;

SELECT * FROM rental WHERE inventory_id = 367 and staff_id = 1;

 

RDBMS의 경우, inventroy_id, customer_id와 같이 다른 테이블에 상세 정보가 있고 각 정보를 구별할 수 있는 ID 값으로 연결되어 있는 경우가 많다. 특히, 개발자들과 소통할 때 세세하게 정보를 모두 알아야 한다는 말에 너무 공감이 되면서도 지금 우리 개발자들한테 미안한 감정도 많이 생겼던 것 같다. 이번 강의를 들으면서 현업에서 겹치는 게 많기도 해서 즐겁게 들었던 것 같다. CRUD에 대해서는 완벽하게 숙지하고 갈 수 있도록 할 것이다.

728x90
반응형

댓글

추천 글