본문 바로가기
DB/SQL 예제

CRUD, 함수 예제 1

by ssury94 2024. 11. 28.

 

세팅

 

데이터베이스 test6
테이블 employees
컬럼
더보기

INSERT INTO employees (name, position, email, phone_number, salary) VALUES

('김철수', 'Manager', 'chulsoo.kim@example.com', '010-1234-5678', 5000000),

('이영희', 'Developer', 'younghee.lee@example.com', '010-2345-6789', 4000000),

('박준혁', 'Designer', 'junhyuk.park@example.com', '010-3456-7890', 3500000),

('최수진', 'Tester', 'sujin.choi@example.com', '010-4567-8901', 3200000),

('정우진', 'HR', 'woojin.jung@example.com', '010-5678-9012', 3000000);

 

-- 신규직원 추가

INSERT INTO employees (name,position,email,phone_number,salary)
values('한지수','developer','jisu.han@example.com','010-6789-0123',3800000);

 

 

★-- 모든 직원 이름 이메일조회, 전화번호 가운데 숫자는 **** 마스킹 처리

SELECT name ,email ,
concat('010-****-',substr(phone_number,-4))phone_number
from employees e ;

 

요즘은 010만 쓰지않나 하고 뒷자리만 쓰는걸로 함수작성

 

SELECT name, email,
concat(substr(phone_number,1,4),'****-',substr(phone_number,10,4))
from employees e ;

 

 

그래도 016쓰시는 분이 아직도 있으면 어떻게해 싶어서 수정해봄

SELECT name, email,
REPLACE (phone_number,SUBSTR(phone_number,5,4),'****')
from employees e ;

★ 선생님의 쿼리 

Replace쓰니 복잡하게 앞 010-과 뒷 네자리를 합칠필요없이 깔끔하게 변경됨...

효율적으로 생각합시다...

 

-- 연봉이 350만원 이상인 직원의 이름과 직위 조회

SELECT name, position
FROM employees e
WHERE salary >=3500000;

 

-- 이영희의 직위를 senior developer로 변경 연봉을 450만원으로 업데이트

UPDATE employees
SET position="Senior Developer", salary =4500000
WHERE name='이영희';

 

-- 연봉이 300만원 미만인 직원을 삭제하세요.

DELETE FROM employees
WHERE salary<3000000;

 

-- 직원의 이름과 직위를 하나의 문자열로 결합하여 출력 (김철수-Manager)

SELECT concat(name,'-', position) name_position
FROM employees e ;

 

-- 직원 이름 중 두번째 글자부터 세번째 글자까지 출력하세요

SELECT substring(name,2,2)as name
FROM employees e ;

 

-- 각 직원의 이메일 길이를 계산하여 출력하세요

SELECT name ,email ,CHAR_LENGTH(email)
FROM employees e ;

효율적으로 생각합시다...