Database/MySQL 실습

[DBeaver] MySQL실습 - 문자열 처리 실습

juunghee 2025. 1. 12. 23:44
문자열 처리 실습

 

 

 

● test6 데이터베이스에 books 테이블 생성하기

Table Name: books

- id : 제품 고유 ID (프라이머리 키, 자동 증가)
- title : 책 제목 (문자열, 최대 100자)
- author_fname : 저자의 퍼스트네임 (문자열, 최대 100자)
- author_lname : 저자의 라스트네임 (문자열, 최대 100자)
- released_year : 출판 연도 (정수형)
- stock_quantity : 재고 수량 (정수형)
- pages : 페이지 수 (정수형)

 

 

 

● books 테이블에 데이터 추가하기

sql script를 새로 열어서 insert into 해서 데이터 입력

INSERT INTO books (title, author_fname, author_lname, released_year, stock_quantity, pages) VALUES ('The Namesake', 'Jhumpa', 'Lahiri', 2003, 32, 291), ('Norse Mythology', 'Neil', 'Gaiman',2016, 43, 304), ('American Gods', 'Neil', 'Gaiman', 2001, 12, 465), ('Interpreter of Maladies', 'Jhumpa', 'Lahiri', 1996, 97, 198), ('A Hologram for the King: A Novel', 'Dave', 'Eggers', 2012, 154, 352), ('The Circle', 'Dave', 'Eggers', 2013, 26, 504), ('The Amazing Adventures of Kavalier & Clay', 'Michael', 'Chabon', 2000, 68, 634), ('Just Kids', 'Patti', 'Smith', 2010, 55, 304), ('A Heartbreaking Work of Staggering Genius', 'Dave', 'Eggers', 2001, 104, 437), ('Coraline', 'Neil', 'Gaiman', 2003, 100, 208), ('What We Talk About When We Talk About Love: Stories', 'Raymond', 'Carver', 1981, 23, 176), ("Where I'm Calling From: Selected Stories", 'Raymond', 'Carver', 1989, 12, 526), ('White Noise', 'Don', 'DeLillo', 1985, 49, 320), ('Cannery Row', 'John', 'Steinbeck', 1945, 95, 181), ('Oblivion: Stories', 'David', 'Foster Wallace', 2004, 172, 329), ('Consider the Lobster', 'David', 'Foster Wallace', 2005, 92, 343);

 

 

 

● 실습문제

 

  1. 타이틀의 공백을 -> 로 바꿔서 나오도록 조회.

select replace(title, ' ', '->') as title
from books;

 

  2. 다음 컬럼처럼 나오게 조회.

select author_lname as forwards, reverse(author_lname) as backwards
from books;

 

 

  3. 다음처럼 이름을 합치되 대문자로 합쳐서 조회.

select upper(concat(author_fname,' ', author_lname)) as 'full name in caps'
from books;

 

 

 

 

  4. 다음처럼 타이틀컬럼과 년도컬럼을 합치되, was released in이 들어가도록 합쳐서 조회.

select concat(title, ' was released in ', released_year) as blurd
from books;

 

  5. 다음처럼 타이틀과, 타이틀에 적힌 글자의 갯수가 나오도록 조회.

select title, char_length(title) as 'character count'
from books;

 

 

  6. 다음처럼 조회

     단, 숏타이틀은 앞에서 10글자까지만 나오고, 뒤에 ...이 나오도록 만들고,

     author는 이름 두개 컬럼을 합치고. quantity는 원래 숫자에 in stock이 붙도록 조회

select concat(substr(title, 1, 10), '...'), concat(author_lname, ',', author_fname), concat(stock_quantity, ' in stock')
from books;

컬럼 이름 정리
->
select concat(substr(title, 1, 10), '...') as 'short title',
concat(author_lname, ',', author_fname) as author, concat(stock_quantity, ' in stock') as quantity
from books;



home