๐Ÿ“˜ Programming/DB

[MySQL] ๊ธฐ๋ณธ ๋ฌธ๋ฒ• (SELECT, DROP, UPDATE, DELETE, ALTER)

ํ•œ์ฝ”๋”ฉ 2020. 7. 14. 11:07
728x90
728x90
MySQL ๊ตฌ๋ฌธ

 

MySQL์—์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋Œ€ํ•œ ์ž‘์—… ๋ช…๋ น์€ SQL ๊ตฌ๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค. 

SELECT * FROM Reservation;

์ผ๋ฐ˜์ ์ธ ๊ตฌ๋ฌธ ๋’ค์—๋Š” ์„ธ๋ฏธ์ฝœ๋ก (;)์„ ๋ถ™์ž…๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ ์„ธ๋ฏธ์ฝœ๋ก ์€ SQL ๊ตฌ๋ฌธ์„ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ธฐ์ค€์ด ๋ฉ๋‹ˆ๋‹ค.

 

๋˜ํ•œ, MySQL์€ ํ‚ค์›Œ๋“œ์™€ ๊ตฌ๋ฌธ์—์„œ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋˜๋„๋ก์ด๋ฉด 1๋ฒˆ์ด๋‚˜ 2๋ฒˆ์ฒ˜๋Ÿผ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ์ผ๊ด€๋˜๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ…Œ์ด๋ธ” ๋ช…๊ณผ ํ•„๋“œ์˜ ์ด๋ฆ„์€ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜๋ฏ€๋กœ, ์ฃผ์˜ํ•ด์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

SELECT * FROM Reservation;
select * from Reservation;
SeLeCt * FrOm Reservation;

MySQL ์ฃผ์„

 

์ฃผ์„์ด๋ž€ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์ดํ•ด๋ฅผ ๋•๋Š” ์„ค๋ช…์„ ์ ๊ฑฐ๋‚˜ ๋””๋ฒ„๊น…์„ ์œ„ํ•ด ์ž‘์„ฑํ•˜๋Š” ์ผ์ข…์˜ ๋ฉ”๋ชจ์ž…๋‹ˆ๋‹ค.

MySQL ์„œ๋ฒ„๋Š” ์ฃผ์„์„ ๋ฌด์‹œํ•˜๋ฏ€๋กœ, ์‹ค์ œ ์‹คํ–‰ ๊ฒฐ๊ณผ์—๋Š” ์•„๋ฌด๋Ÿฐ ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

MySQL์—์„œ ์ฃผ์„์„ ์ž‘์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

# ์ฃผ์„ ๋‚ด์šฉ
-- ์ฃผ์„ ๋‚ด์šฉ
/*
	์—ฌ๋Ÿฌ ์ฃผ์„ 
    ๋‚ด์šฉ๋“ค์ด 
    ์žˆ์Šต๋‹ˆ๋‹ค.
*/

--์„ ์‚ฌ์šฉํ•œ ์ฃผ์„๋ฌธ์—์„œ ๋‘ ๊ฐœ ์ด์ƒ์˜ ํ•˜์ดํ”ˆ(-) ๋’ค์—๋Š” ๋ฐ˜๋“œ์‹œ ํ•œ ์นธ์˜ ๊ณต๋ฐฑ์ด ์กด์žฌํ•ด์•ผ๋งŒ ์ฃผ์„์œผ๋กœ ์ •์ƒ ์ธ์‹๋ฉ๋‹ˆ๋‹ค.


MySQL ์ฃผ์š” ๊ตฌ๋ฌธ

 

MySQL์—์„œ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ์ฃผ์š” ๊ตฌ๋ฌธ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•ด ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ…Œ์ด๋ธ”์„ ์ž‘์„ฑํ•ด ๋ณผ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

ID Name Date RoomNum
1 ํ™๊ธธ๋™ 2016-01-05 2014
2 ์ž„๊บฝ์ • 2016-02-12 918
3 ์žฅ๊ธธ์‚ฐ 2016-01-16 1208
4 ํ™๊ธธ๋™ 2016-03-17 504

 

์ด๋ฅผ ํ†ตํ•ด ์‚ดํŽด๋ณผ MySQL ์ฃผ์š” ๊ตฌ๋ฌธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

1. CREATE DATABASE (DB ์ƒ์„ฑ)

CREATE MyDB;

2. ALTER DATABASE (DB ํŠน์„ฑ ๋ณ€๊ฒฝ)

ALTER MyDB2;

3. CREATE TABLE (DB ๋‚ด ํ…Œ์ด๋ธ” ์ƒ์„ฑ)

CREATE TABLE Reservation(ID INT, NAME VARCHAR(3), Date DATE, RoomNum INT);

4. ALTER TABLE (ํ…Œ์ด๋ธ” ํŠน์„ฑ ๋ฐ ์š”์†Œ ๋ณ€๊ฒฝ)

ALTER TABLE PLAYER 
ADD (ADDRESS VARCHAR2(80));					// ์ปฌ๋Ÿผ ์ถ”๊ฐ€

ALTER TABLE PLAYER 
DROP COLUMN ADDRESS; 						// ์ปฌ๋Ÿผ ์‚ญ์ œ

ALTER TABLE TEAM_TEMP 
MODIFY (ORIG_YYYY VARCHAR2(8) DEFAULT '20020129' NOT NULL);	// ์ปฌ๋Ÿผ ์ˆ˜์ •

ALTER TABLE PLAYER 
RENAME COLUMN PLAYER_ID TO TEAM_ID;				// ์ปฌ๋Ÿผ๋ช… ๋ณ€๊ฒฝ

5. DROP TABLE (ํ…Œ์ด๋ธ” ์‚ญ์ œ)

DROP TABLE PLAYER;

6. INSERT INTO (ํ…Œ์ด๋ธ” ๋‚ด ์ปฌ๋Ÿผ ์ถ”๊ฐ€)

INSERT INTO Reservation(ID, Name, ReserveDate, RoomNum) 
VALUES(1, 'ํ™๊ธธ๋™', '2016-01-05', 2014);

7. UPDATE (ํ…Œ์ด๋ธ” ๋˜๋Š” ๋ทฐ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐฑ์‹ )

UPDATE EMPLOYEE
  SET WORKDEPT = 'D11',
      PHONENO = '7213',
      JOB = 'DESIGNER'
  WHERE EMPNO = '000270'

8. DELETE (ํ…Œ์ด๋ธ”์—์„œ ํ–‰์„ ์ œ๊ฑฐ)

 DELETE FROM CORPDATA.EMPLOYEE
 WHERE WORKDEPT = 'D11'

9. SELECT (ํ…Œ์ด๋ธ”์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒ)

SELECT Nm_Kor,Age FROM My_Table WHERE Age=25 

10. CREATE INDEX (์ธ๋ฑ์Šค ์ƒ์„ฑ)

CREATE INDEX grade_index_score
ON student.grade (score ASC)

11. DROP INDEX (์ธ๋ฑ์Šค ์‚ญ์ œ)

ALTER TABLE Reservation
DROP INDEX NameIdx;

 

728x90
๋ฐ˜์‘ํ˜•