-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql_01
433 lines (388 loc) · 12.3 KB
/
mysql_01
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
Enter password: ****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 8.0.25 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2021-05-13 15:50:40 |
+---------------------+
1 row in set (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
6 rows in set (0.00 sec)
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
mysql> create database bdb;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| bdb |
| information_schema |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
7 rows in set (0.00 sec)
mysql> use bdb;
Database changed
mysql> show tables;
Empty set (0.01 sec)
mysql> create table score(
-> name char(20),
-> kor int,
-> eng int,
-> mat int,
-> );
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 6
mysql> create table score(
-> name char(20),
-> kor int,
-> eng int,
-> mat int);
Query OK, 0 rows affected (0.04 sec)
mysql> select 2 * 10;
+--------+
| 2 * 10 |
+--------+
| 20 |
+--------+
1 row in set (0.00 sec)
mysql> show tables;
+---------------+
| Tables_in_bdb |
+---------------+
| score |
+---------------+
1 row in set (0.01 sec)
mysql> create table Man(
-> name char(10),
-> age int);
Query OK, 0 rows affected (0.03 sec)
mysql> show tables;
+---------------+
| Tables_in_bdb |
+---------------+
| man |
| score |
+---------------+
2 rows in set (0.00 sec)
========= table에 데이터 입력 ==========
mysql> insert into man values("손흥민", 30);
Query OK, 1 row affected (0.02 sec)
mysql> insert into man values("이강인", 21);
Query OK, 1 row affected (0.01 sec)
mysql> select * from man;
+--------+------+
| name | age |
+--------+------+
| 손흥민 | 30 |
| 이강인 | 21 |
+--------+------+
2 rows in set (0.01 sec)
=======================================
mysql> desc score;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name | char(20) | YES | | NULL | |
| kor | int | YES | | NULL | |
| eng | int | YES | | NULL | |
| mat | int | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
4 rows in set (0.02 sec)
mysql> desc man;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name | char(10) | YES | | NULL | |
| age | int | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
======================================
mysql> insert into man values('류현진', 26);
Query OK, 1 row affected (0.01 sec)
mysql> select * from man;
+--------+------+
| name | age |
+--------+------+
| 손흥민 | 30 |
| 이강인 | 21 |
| 류현진 | 26 |
+--------+------+
3 rows in set (0.01 sec)
mysql> update man set age=40 where name='류현진';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
============================================================
mysql> select name as '이름', age as '나이' from man;
+--------+------+
| 이름 | 나이 |
+--------+------+
| 손흥민 | 30 |
| 이강인 | 21 |
| 류현진 | 40 |
+--------+------+
3 rows in set (0.00 sec)
========================================================
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| bdb |
| information_schema |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
7 rows in set (0.01 sec)
======================================================
mysql> use mysql;
Database changed
mysql> show tables;
+------------------------------------------------------+
| Tables_in_mysql |
+------------------------------------------------------+
| columns_priv |
| component |
| db |
| default_roles |
| engine_cost |
| func |
| general_log |
| global_grants |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| password_history |
| plugin |
| procs_priv |
| proxies_priv |
| replication_asynchronous_connection_failover |
| replication_asynchronous_connection_failover_managed |
| role_edges |
| server_cost |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+------------------------------------------------------+
35 rows in set (0.01 sec)
=====================================================
mysql> use world;
Database changed
mysql> show tables;
+-----------------+
| Tables_in_world |
+-----------------+
| city |
| country |
| countrylanguage |
+-----------------+
3 rows in set (0.01 sec)
=========================================================
##desc(schema 같은거 = 구조 보기)
mysql> desc city;
+-------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------+------+-----+---------+----------------+
| ID | int | NO | PRI | NULL | auto_increment |
| Name | char(35) | NO | | | |
| CountryCode | char(3) | NO | MUL | | |
| District | char(20) | NO | | | |
| Population | int | NO | | 0 | |
+-------------+----------+------+-----+---------+----------------+
5 rows in set (0.01 sec)
###key = primary key(절대중복되지 않는 고유한 숫자, ISBN, 주민번호 같은거),
=========================================================
mysql> select count(*) from city;
+----------+
| count(*) |
+----------+
| 4079 |
+----------+
1 row in set (0.02 sec)
=========================================
mysql> select name, countrycode from city limit 10;
+----------------+-------------+
| name | countrycode |
+----------------+-------------+
| Kabul | AFG |
| Qandahar | AFG |
| Herat | AFG |
| Mazar-e-Sharif | AFG |
| Amsterdam | NLD |
| Rotterdam | NLD |
| Haag | NLD |
| Utrecht | NLD |
| Eindhoven | NLD |
| Tilburg | NLD |
+----------------+-------------+
10 rows in set (0.00 sec)
=================================================
한국의 도시만 검색해보기
mysql> select name, countrycode from city where countrycode = 'KOR';
+------------+-------------+
| name | countrycode |
+------------+-------------+
| Seoul | KOR |
| Pusan | KOR |
| Inchon | KOR |
| Taegu | KOR |
| Taejon | KOR |
| Kwangju | KOR |
| Ulsan | KOR |
| Songnam | KOR |
| Puchon | KOR |
| Suwon | KOR |
| Anyang | KOR |
| Chonju | KOR |
| Chongju | KOR |
| Koyang | KOR |
| Ansan | KOR |
| Pohang | KOR |
| Chang-won | KOR |
| Masan | KOR |
| Kwangmyong | KOR |
| Chonan | KOR |
| Chinju | KOR |
| Iksan | KOR |
| Pyongtaek | KOR |
| Kumi | KOR |
| Uijongbu | KOR |
| Kyongju | KOR |
| Kunsan | KOR |
| Cheju | KOR |
| Kimhae | KOR |
| Sunchon | KOR |
| Mokpo | KOR |
| Yong-in | KOR |
| Wonju | KOR |
| Kunpo | KOR |
| Chunchon | KOR |
| Namyangju | KOR |
| Kangnung | KOR |
| Chungju | KOR |
| Andong | KOR |
| Yosu | KOR |
| Kyongsan | KOR |
| Paju | KOR |
| Yangsan | KOR |
| Ichon | KOR |
| Asan | KOR |
| Koje | KOR |
| Kimchon | KOR |
| Nonsan | KOR |
| Kuri | KOR |
| Chong-up | KOR |
| Chechon | KOR |
| Sosan | KOR |
| Shihung | KOR |
| Tong-yong | KOR |
| Kongju | KOR |
| Yongju | KOR |
| Chinhae | KOR |
| Sangju | KOR |
| Poryong | KOR |
| Kwang-yang | KOR |
| Miryang | KOR |
| Hanam | KOR |
| Kimje | KOR |
| Yongchon | KOR |
| Sachon | KOR |
| Uiwang | KOR |
| Naju | KOR |
| Namwon | KOR |
| Tonghae | KOR |
| Mun-gyong | KOR |
+------------+-------------+
70 rows in set (0.01 sec)
====================================
### 자료 보는 방법 ###
#DB LIST 보기
show databases;
#sys라는 DB에 접근
use sys;
#테이블 분류
show tables;
#세부정보
desc session;
===================================
원하는 자료만 제한적으로 보기
mysql> use world;
Database changed
mysql> show tables;
+-----------------+
| Tables_in_world |
+-----------------+
| city |
| country |
| countrylanguage |
+-----------------+
3 rows in set (0.00 sec)
mysql> select code, name from country order by code limit 10;
+------+----------------------+
| code | name |
+------+----------------------+
| ABW | Aruba |
| AFG | Afghanistan |
| AGO | Angola |
| AIA | Anguilla |
| ALB | Albania |
| AND | Andorra |
| ANT | Netherlands Antilles |
| ARE | United Arab Emirates |
| ARG | Argentina |
| ARM | Armenia |
+------+----------------------+
10 rows in set (0.01 sec)
====================================
mysql> select * from lunch; => 전체항목 다보기 [*]
mysql> select menu, price from lunch;
+--------+-------+
| menu | price |
+--------+-------+
| 짜장면 | 5000 |
| 비빔밥 | 5000 |
| 돈가스 | 5000 |
+--------+-------+
3 rows in set (0.00 sec)
===================================