-
Notifications
You must be signed in to change notification settings - Fork 0
/
MySQL_d09_class02
402 lines (309 loc) · 10.6 KB
/
MySQL_d09_class02
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
********* 오후 수업 ********
#################################
# ace로 접속
#################################
C:\Users\CPB06GameN>mysql -u ace -p
#################################
database 확인
#################################
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| acedb |
| information_schema |
+--------------------+
#################################
database 사용체크
#################################
mysql> use acedb;
Database changed
#################################
table생성 및 필드값 입력
#################################
mysql> create table score(
-> name char(20),
-> kor int,
-> mat int,
-> eng int);
Query OK, 0 rows affected (0.03 sec)
#################################
table생성 확인
#################################
mysql> show tables;
+-----------------+
| Tables_in_acedb |
+-----------------+
| score |
+-----------------+
1 row in set (0.01 sec)
#################################
table, 필드값 속성 확인
#################################
mysql> desc score;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name | char(20) | YES | | NULL | |
| kor | int | YES | | NULL | |
| mat | int | YES | | NULL | |
| eng | int | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
4 rows in set (0.01 sec)
#################################
root가 아니기 떄문에 ace는 mysql에
접근권한이 없다.
#################################
mysql> use mysql;
ERROR 1044 (42000): Access denied for user 'ace'@'%' to database 'mysql'
#################################
트랜잭션(tranaction)
#################################
두개 이상의 과정을 하나의 개념으로 보고
중간에 완료가 되지 않을 경우 0으로 처리해
버린다.
commit = 시행
rollback = 완료가 되지 않을경우 0으로 돌림
######################################
데이터 입력(workbench에서 실행)
######################################
insert into score values('이순신',85,87,90);
insert into score values('강감찬',75,80,95);
insert into score values('한석봉',99,98,99);
insert into score values('황진이',35,45,20);
insert into score values('안중근',90,85,90);
insert into score values('박문수',95,98,96);
insert into score values('임꺽정',15,35,45);
insert into score values('김정호',90,95,80);
insert into score values('정몽주',90,90,95);
insert into score values('오우석',50,45,60);
# 전체선택하고 번개
select * from score;
# 한줄 실행은 Ctrl + Enter
이순신 85 87 90
이순신 85 87 90
이순신 85 87 90
강감찬 75 80 95
한석봉 99 98 99
황진이 35 45 20
안중근 90 85 90
박문수 95 98 96
임꺽정 15 35 45
김정호 90 95 80
######################################
cmd 에서 실행
######################################
mysql> select * from score;
ERROR 2013 (HY000): Lost connection to MySQL server during query
No connection. Trying to reconnect...
Connection id: 11
Current database: acedb
+--------+------+------+------+
| name | kor | mat | eng |
+--------+------+------+------+
| 이순신 | 85 | 87 | 90 |
| 이순신 | 85 | 87 | 90 |
| 이순신 | 85 | 87 | 90 |
| 강감찬 | 75 | 80 | 95 |
| 한석봉 | 99 | 98 | 99 |
| 황진이 | 35 | 45 | 20 |
| 안중근 | 90 | 85 | 90 |
| 박문수 | 95 | 98 | 96 |
| 임꺽정 | 15 | 35 | 45 |
| 김정호 | 90 | 95 | 80 |
| 정몽주 | 90 | 90 | 95 |
| 오우석 | 50 | 45 | 60 |
+--------+------+------+------+
12 rows in set (0.01 sec)
mysql> select count(*) from score;
+----------+
| count(*) |
+----------+
| 12 |
+----------+
1 row in set (0.00 sec)
mysql> select * from score limit 5;
+--------+------+------+------+
| name | kor | mat | eng |
+--------+------+------+------+
| 이순신 | 85 | 87 | 90 |
| 이순신 | 85 | 87 | 90 |
| 이순신 | 85 | 87 | 90 |
| 강감찬 | 75 | 80 | 95 |
| 한석봉 | 99 | 98 | 99 |
+--------+------+------+------+
5 rows in set (0.00 sec)
mysql> select * from score limit 3, 3;
+--------+------+------+------+
| name | kor | mat | eng |
+--------+------+------+------+
| 강감찬 | 75 | 80 | 95 |
| 한석봉 | 99 | 98 | 99 |
| 황진이 | 35 | 45 | 20 |
+--------+------+------+------+
3 rows in set (0.00 sec)
mysql> desc score;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name | char(20) | YES | | NULL | |
| kor | int | YES | | NULL | |
| mat | int | YES | | NULL | |
| eng | int | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> select name, kor from score limit 5;
+--------+------+
| name | kor |
+--------+------+
| 이순신 | 85 |
| 이순신 | 85 |
| 이순신 | 85 |
| 강감찬 | 75 |
| 한석봉 | 99 |
+--------+------+
5 rows in set (0.00 sec)
######################################
조금 중요한거
######################################
mysql> select name as '이름', kor as '국어' from score limit 5;
+--------+------+
| 이름 | 국어 |
+--------+------+
| 이순신 | 85 |
| 이순신 | 85 |
| 이순신 | 85 |
| 강감찬 | 75 |
| 한석봉 | 99 |
+--------+------+
5 rows in set (0.00 sec)
######################################
총점 평균 구하기
######################################
select (kor+eng+mat) as '총점' from score;
select ((kor+eng+mat)/3) as '평균' from score;
######################################
이름, 총점, 평균 다 나오게
######################################
#1
select name as '이름', (kor+eng+mat) as '총점',
((kor+eng+mat)/3) as '평균' from score;
#2
select name as '이름', kor as '국어', eng as '영어', mat as '수학',
(kor+eng+mat) as '총점', ((kor+eng+mat)/3) as '평균' from score;
+--------+------+------+------+------+---------+
| 이름 | 국어 | 영어 | 수학 | 총점 | 평균 |
+--------+------+------+------+------+---------+
| 이순신 | 85 | 90 | 87 | 262 | 87.3333 |
| 이순신 | 85 | 90 | 87 | 262 | 87.3333 |
| 이순신 | 85 | 90 | 87 | 262 | 87.3333 |
| 강감찬 | 75 | 95 | 80 | 250 | 83.3333 |
| 한석봉 | 99 | 99 | 98 | 296 | 98.6667 |
| 황진이 | 35 | 20 | 45 | 100 | 33.3333 |
| 안중근 | 90 | 90 | 85 | 265 | 88.3333 |
| 박문수 | 95 | 96 | 98 | 289 | 96.3333 |
| 임꺽정 | 15 | 45 | 35 | 95 | 31.6667 |
| 김정호 | 90 | 80 | 95 | 265 | 88.3333 |
| 정몽주 | 90 | 95 | 90 | 275 | 91.6667 |
| 오우석 | 50 | 60 | 45 | 155 | 51.6667 |
+--------+------+------+------+------+---------+
12 rows in set (0.00 sec)
==============================================
###############################################
데이터 재입력
(다 지우고 INSERT 한번 더 하기)
###############################################
delete from score;
insert into score values('이순신',85,87,90);
insert into score values('강감찬',75,80,95);
insert into score values('한석봉',99,98,99);
insert into score values('황진이',35,45,20);
insert into score values('안중근',90,85,90);
insert into score values('박문수',95,98,96);
insert into score values('임꺽정',15,35,45);
insert into score values('김정호',90,95,80);
insert into score values('정몽주',90,90,95);
insert into score values('오우석',50,45,60);
#결과물
mysql> select * from score;
+--------+------+------+------+
| name | kor | mat | eng |
+--------+------+------+------+
| 이순신 | 85 | 87 | 90 |
| 강감찬 | 75 | 80 | 95 |
| 한석봉 | 99 | 98 | 99 |
| 황진이 | 35 | 45 | 20 |
| 안중근 | 90 | 85 | 90 |
| 박문수 | 95 | 98 | 96 |
| 임꺽정 | 15 | 35 | 45 |
| 김정호 | 90 | 95 | 80 |
| 정몽주 | 90 | 90 | 95 |
| 오우석 | 50 | 45 | 60 |
+--------+------+------+------+
10 rows in set (0.00 sec)
###########################################
정렬하기
###########################################
select * from score order by name asc;
#순서대로(기본값적용)
+--------+------+------+------+
| name | kor | mat | eng |
+--------+------+------+------+
| 강감찬 | 75 | 80 | 95 |
| 김정호 | 90 | 95 | 80 |
| 박문수 | 95 | 98 | 96 |
| 안중근 | 90 | 85 | 90 |
| 오우석 | 50 | 45 | 60 |
| 이순신 | 85 | 87 | 90 |
| 임꺽정 | 15 | 35 | 45 |
| 정몽주 | 90 | 90 | 95 |
| 한석봉 | 99 | 98 | 99 |
| 황진이 | 35 | 45 | 20 |
+--------+------+------+------+
10 rows in set (0.00 sec)
select * from score order by name desc;
#역순(desc)
+--------+------+------+------+
| name | kor | mat | eng |
+--------+------+------+------+
| 황진이 | 35 | 45 | 20 |
| 한석봉 | 99 | 98 | 99 |
| 정몽주 | 90 | 90 | 95 |
| 임꺽정 | 15 | 35 | 45 |
| 이순신 | 85 | 87 | 90 |
| 오우석 | 50 | 45 | 60 |
| 안중근 | 90 | 85 | 90 |
| 박문수 | 95 | 98 | 96 |
| 김정호 | 90 | 95 | 80 |
| 강감찬 | 75 | 80 | 95 |
+--------+------+------+------+
10 rows in set (0.00 sec)
#############################################
오후 수업 마지막 핵심 내용
#############################################
select user();
use mysql;
use acedb;
show tables;
create table s(
id int auto_increment primary key,
name varchar(20),
kor tinyint,
eng tinyint,
mat tinyint);
desc s;
insert into s(name, kor, eng, mat) values('이순신',85,87,90);
insert into s(name, kor, eng, mat) values('강감찬',75,80,90);
insert into s(name, kor, eng, mat) values('한석봉',99,98,99);
insert into s(name, kor, eng, mat) values('황진이',35,45,20);
insert into s(name, kor, eng, mat) values('안중근',90,85,90);
insert into s(name, kor, eng, mat) values('박문수',95,98,96);
insert into s(name, kor, eng, mat) values('임꺽정',15,35,45);
insert into s(name, kor, eng, mat) values('김정호',90,95,80);
insert into s(name, kor, eng, mat) values('정몽주',90,90,95);
insert into s(name, kor, eng, mat) values('양주종',50,45,60);
select * from s limit 5;
select * from s where kor > 80;
select * from s where mat <= 50;
select * from s where mat <= 50 or kor <= 50;
select * from s where mat <= 50 and kor <= 50;