메뉴 건너뛰기

조회 수 163 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부

 

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
CREATE DATABASE haksa;
 
SHOW DATABASES;
 
USE haksa;
 
 
 
 
 
-- make insa table
 
CREATE TABLE insa(
 
bunho INT(1AUTO_INCREMENT,
 
name CHAR(8NOT NULL,
 
e_name CHAR(4NOT NULL,
 
town CHAR(6NOT NULL,
 
PRIMARY KEY(bunho)
 
);
 
 
 
INSERT INTO insa VALUES('1','hong','kevi','seoul');
 
INSERT INTO insa VALUES('2','kang','devi','seoul');
 
INSERT INTO insa VALUES('3','kim','jack','seoul');
 
INSERT INTO insa VALUES('4','su','jes','seoul');
 
INSERT INTO insa VALUES('5','go','hoo','seoul');
 
 
 
-- what is  rollback , commit ? 
 
 
 
SELECT * FROM insa;
 
 
 
SET autocommit = 0;
 
 
 
UPDATE insa SET town ='degu' WHERE bunho = 4;
 
 
 
ROLLBACK;
 
 
 
COMMIT;
 
 
 
 
 
-- savepoint /truncate 
 
 
 
UPDATE insa SET town ='jine' WHERE bunho = 2;
 
 
 
 
 
SAVEPOINT aa;
 
 
 
DELETE FROM insa WHERE bunho = 3;
 
 
 
SELECT * FROM insa;
 
 
 
ROLLBACK TO aa;
 
 
 
 
 
-- TRUNCATE can't rollback
 
 
 
 
 
 
 
--  create index  if some sql can't primary key , use index
 
--  create index index's name on table(calum)
 
 
 
index stu_prim on
 
student (stu_no);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-- ----------------------------------------------------------------------------------
 
 
 
 
 
 
 
-- select exam
 
-- 1.성별이 남자인 각 학생의 학번, 이름, 영문이름, 학년, 주민등록번호를 영문이름 순서로 출력하
 
-- 1 or 2
 
-- SUBSTRING(str,pos,len) : 첫번째 인자의 문자열에서 두번째 인자의 위치부터 세번째 인자의 길이만큼 반환한다.
 
-- 예 : select SUBSTRING('Quadratically',5,6);
 
select substring(id_num,8,1from student;
 
select id_num from student where substring(id_num,8,1)=1;
 
select stu_no,stu_name,stu_ename,grade,id_num
 
from student
 
where 1=substring(id_num,8,1)
 
ORDER BY stu_ename ;
 
 
 
 
 
 
 
-- 2.학년이 1학년이고 성별이 남자인 각 학생의 학번과 이름을 출력하는데, 출력 순서는 학번 내림차순이다. 
 
 
 
select * from student;
 
select stu_no, stu_name
 
from student
 
where substring(id_num,8,1)=1
 
and grade=1
 
order by stu_no desc;
 
 
 
 
 
-- 3.학생 테이블의 학번, 이름 , 출생년도 , 나이를 뷰테이블로 만들어 출력하라 
 
 
 
select stu_no, stu_name, id_num, year(now())-birth_year+1 as 'age' from student;
 
 
 
create view ages(stu_no,stu_name,id_num,age) as
 
select stu_no, stu_name, id_num, year(now())-birth_year+1 as 'age' from student;
 
select * from ages;
 
 
 
-- 언제 뷰를 사용하나?
 
-- 
 
-- - 반복되는 명령문이나 루틴을 간단히 사용하고자 할때 
 
-- - 테이블의 출력 방법을 재구성하고자 할때
 
-- - 여러 단계에서 조회 명령문이 사용될 때
 
-- - 데이터를 보호하고자 할때
 
 
 
 
 
-- security 
 
 
 
use mysql;
 
update user set password=password('1234'where user = 'root';
 
flush privileges;
 
 
 
 
 
-- create user
 
 
 
create user lee@localhost identified by '1234';
 
create user choi identified by '1234';
 
 
 
-- grant 
 
 
 
grant selectinsertupdate on haksa.* to lee@localhost;
 
grant all PRIVILEGES on haksa.* to choi;
 
grant all PRIVILEGES on *.* to lee@localhost;
 
 
 
-- revoke
 
 
 
revoke select on haksa.* from choi@'%';
 
revoke selectupdate on haksa.* from lee@'localhost';
 
flush PRIVILEGES;
 
 
 
-- CHECK 
 
select host, db, user, select_priv,update_priv from db;
 
 
 
-- delect
 
 
 
drop user 'user's name'
 
 
 
delete from user where user= 'user's name'
 
 
 
delete from db where user = 'users'name'
 
 
 
 
cs

 

CREATE DATABASE haksa;

SHOW DATABASES;

USE haksa;

 

 

-- make insa table

CREATE TABLE insa(

bunho INT(1) AUTO_INCREMENT,

name CHAR(8) NOT NULL,

e_name CHAR(4) NOT NULL,

town CHAR(6) NOT NULL,

PRIMARY KEY(bunho)

);

 

INSERT INTO insa VALUES('1','hong','kevi','seoul');

INSERT INTO insa VALUES('2','kang','devi','seoul');

INSERT INTO insa VALUES('3','kim','jack','seoul');

INSERT INTO insa VALUES('4','su','jes','seoul');

INSERT INTO insa VALUES('5','go','hoo','seoul');

 

-- what is  rollback , commit ? 

 

SELECT * FROM insa;

 

SET autocommit = 0;

 

UPDATE insa SET town ='degu' WHERE bunho = 4;

 

ROLLBACK;

 

COMMIT;

 

 

-- savepoint /truncate 

 

UPDATE insa SET town ='jine' WHERE bunho = 2;

 

 

SAVEPOINT aa;

 

DELETE FROM insa WHERE bunho = 3;

 

SELECT * FROM insa;

 

ROLLBACK TO aa;

 

 

-- TRUNCATE can't rollback

 

 

 

--  create index  if some sql can't primary key , use index

--  create index index's name on table(calum)

 

index stu_prim on

student (stu_no);

 

 

 

 

 

 

 

 

-- ----------------------------------------------------------------------------------

 

 

 

-- select exam

-- 1.성별이 남자인 각 학생의 학번, 이름, 영문이름, 학년, 주민등록번호를 영문이름 순서로 출력하

-- 1 or 2

-- SUBSTRING(str,pos,len) : 첫번째 인자의 문자열에서 두번째 인자의 위치부터 세번째 인자의 길이만큼 반환한다.

-- 예 : select SUBSTRING('Quadratically',5,6);

select substring(id_num,8,1) from student;

select id_num from student where substring(id_num,8,1)=1;

select stu_no,stu_name,stu_ename,grade,id_num

from student

where 1=substring(id_num,8,1)

ORDER BY stu_ename ;

 

 

 

-- 2.학년이 1학년이고 성별이 남자인 각 학생의 학번과 이름을 출력하는데, 출력 순서는 학번 내림차순이다. 

 

select * from student;

select stu_no, stu_name

from student

where substring(id_num,8,1)=1

and grade=1

order by stu_no desc;

 

 

-- 3.학생 테이블의 학번, 이름 , 출생년도 , 나이를 뷰테이블로 만들어 출력하라 

 

select stu_no, stu_name, id_num, year(now())-birth_year+1 as 'age' from student;

 

create view ages(stu_no,stu_name,id_num,age) as

select stu_no, stu_name, id_num, year(now())-birth_year+1 as 'age' from student;

select * from ages;

 

-- 언제 뷰를 사용하나?

-- 

-- - 반복되는 명령문이나 루틴을 간단히 사용하고자 할때 

-- - 테이블의 출력 방법을 재구성하고자 할때

-- - 여러 단계에서 조회 명령문이 사용될 때

-- - 데이터를 보호하고자 할때

 

 

-- security 

 

use mysql;

update user set password=password('1234') where user = 'root';

flush privileges;

 

 

-- create user

 

create user lee@localhost identified by '1234';

create user choi identified by '1234';

 

-- grant 

 

grant select, insert, update on haksa.* to lee@localhost;

grant all PRIVILEGES on haksa.* to choi;

grant all PRIVILEGES on *.* to lee@localhost;

 

-- revoke

 

revoke select on haksa.* from choi@'%';

revoke select, update on haksa.* from lee@'localhost';

flush PRIVILEGES;

 

-- CHECK 

select host, db, user, select_priv,update_priv from db;

 

-- delect

 

drop user 'user's name'

 

delete from user where user= 'user's name'

 

delete from db where user = 'users'name'

 

desc db;


  1. No Image 12Apr
    by
    2017/04/12 Views 5017 

    MySQL 계정생성하기

  2. No Image 27Feb
    by
    2014/02/27 Views 5693 

    Mysql 기본 명령어

  3. 27Mar
    by 조쉬
    2021/03/27 Views 163 

    MYSQL 기초문법&예제&문제

  4. MYSQL 기초문법&예제&문제 2

  5. No Image 23Dec
    by
    2016/12/23 Views 5500 

    mysql 날짜 관련 date_add, date_format

  6. MySQL 대용량 DBMS 개선 사례

  7. No Image 18Jul
    by
    2018/07/18 Views 1973 

    MySQL 마스터/마스터 replication 에서 AUTO_INCREMENT 문제 해결 방법

  8. No Image 27Feb
    by
    2014/02/27 Views 6254 

    MySql 문자열 합치기

  9. MYSQL 부속질의어 예제&문제 9

  10. No Image 30Aug
    by
    2016/08/30 Views 5842 

    MySQL 서버 데몬이 죽었을때 다시 실행하는 방법

  11. No Image 12Apr
    by
    2017/04/12 Views 5330 

    MySQL 손상된 테이블 복구

  12. No Image 25Nov
    by
    2020/11/25 Views 362 

    MySQL 암호화 방법

  13. MySQL 에서 NULL 값 처리

  14. No Image 12Apr
    by
    2017/04/12 Views 5365 

    mySQL 에서 날자표현 방법

  15. No Image 12Apr
    by
    2017/04/12 Views 5727 

    MySQL 에서 랜덤(random)으로 레코드 읽어오기

  16. MySql 에서 외래키(FK) 설정하는 방법과 Toad 에서 확인하기

  17. MySQL 에서 테이블에 이미 존재하는 값으로 UPDATE 하는 경우

  18. No Image 28Dec
    by
    2017/12/28 Views 3917 

    MySQL 연결 속도

  19. MySql 윈도우에서 DB dump 백업과 복구하기

  20. MySql 의 Trigger(트리거) 로 정보 업데이트

Board Pagination Prev 1 2 3 4 5 6 7 Next
/ 7

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved