λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
Data Base/MySQL

MySQL 24λ²ˆμ§Έμˆ˜μ—…

by μ½”λ”©ν•˜λŠ” λΆ•μ–΄ 2021. 2. 9.
λ°˜μ‘ν˜•

<μ—°μŠ΅λ¬Έμ œ> (데이터 λ³€κ²½λ¬Έμ œλΌμ„œ κ²°κ³ΌλŠ” μΊ‘μ³μ•ˆν•¨)

​

16-1) ν˜„μž¬ 학년이 3학년이면 2019λ…„ 1학기에 등둝 ν…Œμ΄λΈ”μ˜ μˆ˜μ—…λ£Œ(fee_price)에 100000원, λ“±λ‘κΈˆμ΄μ•‘(fee_total), 납뢀총앑(fee_pay)에 각각 100000원을 κ°€μ‚°ν•˜μ—¬ 데이터 값을 λ³€κ²½ν•˜λΌ.

A)

mysql> update fee

-> set fee_price = fee_price + 100000,

-> fee_total = fee_total + 100000,

-> fee_pay = fee_pay + 100000

-> where fee_year = 2019 and fee_term = 1 and stu_no in

-> (select stu_no from student where grade = 3);

​

​

16-2) 등둝 ν…Œμ΄λΈ”μ—μ„œ μž…ν•™κΈˆ(fee_enter), μˆ˜μ—…λ£Œ(fee_price)λ₯Ό 5% μ¦κ°€μ‹œν‚€κ³  λ“±λ‘κΈˆμ΄μ•‘(fee_total), λ‚©λΆ€ 총앑(fee_pay)도 μ¦κ°€μ‹œμΌœ λ³€κ²½ν•˜λΌ.

A)

mysql> update fee

-> set fee_enter = fee_enter * 1.05,

-> fee_price = fee_price * 1.05,

-> fee_total = ifnull(fee_enter, 0) + fee_price,

-> fee_pay = fee_total - ifnull(jang_total , 0);

​

​

16-3) ν•™λ²ˆ 20161001번이 '2016'년도에 μˆ˜κ°• μ‹ μ²­ν•œ λͺ¨λ“  데이터λ₯Ό μ‚­μ œν•˜λΌ.

A)

mysql> delete from attend

-> where stu_no = '20161001'

-> and att_year = '2016';

​

​

16-4) 동아리에 κ°€μž…ν•œ ν•™μƒμ˜ μˆ˜κ°•μ‹ μ²­ 데이터λ₯Ό μ‚­μ œν•˜λΌ.

A)

mysql> delete from attend

-> where stu_no in

-> (select stu_no from circle);

​

​

16-5) "μœ ν•˜λ‚˜" 학생과 같은 λ„μ‹œμ— μ‚΄κ³  μžˆλŠ” λͺ¨λ“  학생을 μ‚­μ œν•˜λΌ. κ·ΈλŸ¬λ‚˜ "μœ ν•˜λ‚˜" 학생은 μ œμ™Έμ‹œν‚¨λ‹€.

A)

mysql> delete from student

-> where substring(address, 1, 2) =

-> (select * from (select substring(address, 1, 2) from student where stu_name = 'μœ ν•˜λ‚˜') as a)

-> and stu_name <> 'μœ ν•˜λ‚˜';

​

​

​​

β–Ά 인덱슀의 μ‚¬μš©

​

create index μΈλ±μŠ€μ΄λ¦„ on ν…Œμ΄λΈ”μ΄λ¦„ (μ—΄, ...);

​

drop index μΈλ±μŠ€μ΄λ¦„ on ν…Œμ΄λΈ”μ΄λ¦„

​​

​

​

<μ—°μŠ΅λ¬Έμ œ>

​

17-2) 학적 ν…Œμ΄λΈ”(STUDENT)μ—μ„œ 생년월일을 μœ μΌν•˜μ§€ μ•Šμ€ 인덱슀λ₯Ό μƒμ„±ν•˜λΌ.​

A)

mysql> create index stu_idx_birth on student(birthday);

​

​

17-3) 학적 ν…Œμ΄λΈ”(STUDENT)μ—μ„œ 이름과 생년월일을 μ‘°ν•©ν•˜μ—¬ μœ μΌν•œ 인덱슀λ₯Ό μƒμ„±ν•˜λΌ.

A)

mysql> create unique index sidx_name_birth on student(stu_name, birthday);

​

​

17-4) 17-2와 17-3μ—μ„œ μƒμ„±ν•œ 인덱슀λ₯Ό μ‚­μ œν•˜λΌ.

A)

mysql> drop index stu_idx_birth on student;

mysql> drop index sidx_name_birth on student;

​

​

​

β–Ά λ·°(VIEW)

​

*뷰의 생성

​

예제 18-1) STUDENT ν…Œμ΄λΈ”λ‘œλΆ€ν„° λͺ¨λ“  ν•™μƒμ˜ ν•™λ²ˆκ³Ό ν•™λ…„, λ°˜μ„ 가지고 μžˆλŠ” λ·° ν…Œμ΄λΈ”(V_CLASS)을 μƒμ„±ν•˜λΌ.

A)

mysql> create view v_class as

-> select stu_no, grade, class

-> from student;

​

​

예제 18-2) λ“±λ‘ν•œ ν•™μƒμ˜ ν•™λ²ˆκ³Ό 등둝년도에 λŒ€ν•œ λ·° ν…Œμ΄λΈ”μ„ μƒμ„±ν•˜λΌ.

A)

mysql> create view v_feeyear as

-> select stu_no, fee_year

-> from fee

-> where fee_year is not null;

​

​

예제 18-3) μž¬ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름, 성별, μž…ν•™λ…„λ„, 생년, μ›”, 일, λ‚˜μ΄μ— λŒ€ν•œ λ·° ν…Œμ΄λΈ”(V_AGES)을 μƒμ„±ν•˜λΌ.

A)

mysql> create view V_AGES as

-> select stu_no, stu_name, gender,

-> substring(stu_no, 1, 4) 'IBHAK_YEAR',

-> substring(birthday, 1, 4) 'B_YEAR',

-> substring(birthday, 5, 2) 'B_MONTH',

-> substring(birthday, 7, 2) 'B_DATE',

-> year(now()) - substring(birthday, 1, 4) + 1 'AGE'

-> from student;

​

​

예제 18-4) μž¬ν•™μƒ 쀑 20μ„Έ 이상인 μ—¬ν•™μƒμ˜ ν•™λ²ˆ, 이름, 성별, μΆœμƒλ…„λ„, λ‚˜μ΄λ₯Ό 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, stu_name, gender, b_year, age

-> from V_AGES

-> where age > 19 and gender in (2, 4, 6);

​

​

예제 18-5) μž¬ν•™μƒ 쀑 21세에 ν•΄λ‹Ήν•˜λŠ” ν•™μƒμ˜ 성년식 행사λ₯Ό μœ„ν•œ λͺ…단을 좜λ ₯ν•˜λΌ. 단, 좜λ ₯ν˜•μ‹μ€ ν•™κ³Ό, ν•™λ…„, ν•™λ²ˆ, 이름, 생년, μ›”, 일, λ‚˜μ΄λ₯Ό 좜λ ₯ν•˜λΌ.

A)

mysql> select dept_code "ν•™κ³Όμ½”λ“œ", grade "ν•™λ…„", s.stu_no "ν•™λ²ˆ", s.stu_name "이름",

-> b_year "λ…„", b_month "μ›”", b_date "일", age "λ‚˜μ΄"

-> from student s, V_AGES v

-> where s.stu_no = v.stu_no and age = 21

-> order by 1, 2, 3;

 

λ°˜μ‘ν˜•

'Data Base > MySQL' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

MySQL 문제  (0) 2021.02.09
MySQL 25λ²ˆμ§Έμˆ˜μ—…  (0) 2021.02.09
MySQL 23λ²ˆμ§Έμˆ˜μ—…  (0) 2021.02.09
MySQL 22λ²ˆμ§Έμˆ˜μ—…  (0) 2021.02.09
MySQL 21λ²ˆμ§Έμˆ˜μ—…  (0) 2021.02.09

λŒ“κΈ€