
SQLite๋ ์ค์น๊ฐ ๊ฐํธํ๊ณ ๊ฐ๋ณ์ง๋ง, ์ค์ต๊ณผ ํ๋ก์ ํธ์ ๊ฐ๋ ฅํ ๋๊ตฌ์ ๋๋ค. ์ด ๊ธ์์๋ SQLite ์คํ๋ถํฐ ํ ์ด๋ธ ์์ฑ, ๋ฐ์ดํฐ ์ฝ์ /์กฐํ/์์ /์ญ์ , JOIN๊น์ง ์ค์ต ์์ ์ ํจ๊ป SQL ํต์ฌ ๋ฌธ๋ฒ ์์ฝ์ ์ ๊ณตํฉ๋๋ค.
cd C:\Users\User\Documents\lsbigdata-gen4
ํด๋น ํด๋๊ฐ ์๋ค๋ฉด ์๋์ฒ๋ผ ์์ฑํฉ๋๋ค:
mkdir C:\Users\User\Documents\lsbigdata-gen4
cd C:\Users\User\Documents\lsbigdata-gen4
SQLite ์คํ ํ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์ฑ:
sqlite3 myschool.db
| ํญ๋ชฉ | ์ค๋ช |
|---|---|
| ๋ฌธ์ฅ ๊ตฌ๋ถ | SQL ๋ฌธ์ฅ์ ์ธ๋ฏธ์ฝ๋ก (;)์ผ๋ก ๋๋จ |
| ๋์๋ฌธ์ | ๊ตฌ๋ถํ์ง ์์ (SELECT vs select) |
| ๋ฌธ์์ด | 'Alice', '10th' ์ฒ๋ผ ์์๋ฐ์ดํ ์ฌ์ฉ |
| ๊ณต๋ฐฑ ํฌํจ ์ด๋ฆ | "table name"์ฒ๋ผ ํฐ๋ฐ์ดํ๋ก ๊ฐ์ธ์ผ ํจ |
| ์ฌ๋ฌ ์ค ์์ฑ | ๊ฐ๋ฅ (์ธ๋ฏธ์ฝ๋ก ์ด ๋ฌธ์ฅ ์ข ๋ฃ ํ์) |
| ์ฃผ์ | -- ํ ์ค, /* ์ฌ๋ฌ ์ค */ |
| ์คํ ์์ | FROM โ WHERE โ SELECT โ ORDER BY |
CREATE TABLE students (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER,
grade TEXT
);
INSERT INTO students (name, age, grade) VALUES ('Alice', 15, '10th');
INSERT INTO students (name, age, grade) VALUES ('Bob', 16, '11th');
INSERT INTO students (name, age, grade) VALUES ('Charlie', 15, '10th');
.headers ON
.mode column
SELECT * FROM students;
๐ฅ๏ธ ์ถ๋ ฅ ๊ฒฐ๊ณผ:
id name age grade
-- ------- --- ------
1 Alice 15 10th
2 Bob 16 11th
3 Charlie 15 10th
SELECT name, grade FROM students WHERE age = 15;
SELECT * FROM students ORDER BY age DESC;
UPDATE students SET grade = '11th' WHERE name = 'Charlie';
SELECT * FROM students;
DELETE FROM students WHERE name = 'Bob';
SELECT * FROM students;
.tables
.schema students
SELECT COUNT(*) FROM students;
SELECT AVG(age) FROM students;
SELECT grade, COUNT(*) FROM students GROUP BY grade;
CREATE TABLE scores (
student_id INTEGER,
subject TEXT,
score INTEGER
);
INSERT INTO scores VALUES (1, 'Math', 85);
INSERT INTO scores VALUES (1, 'English', 90);
INSERT INTO scores VALUES (3, 'Math', 88);
SELECT students.name, scores.subject, scores.score
FROM students
JOIN scores ON students.id = scores.student_id;
๐ฅ๏ธ ์ถ๋ ฅ ๊ฒฐ๊ณผ:
name subject score
------- -------- -----
Alice Math 85
Alice English 90
Charlie Math 88
.exit
| SQL ๋ช ๋ น์ด | ์ค๋ช | ์์ |
|---|---|---|
CREATE TABLE | ํ ์ด๋ธ ์์ฑ | CREATE TABLE students (...); |
INSERT INTO | ๋ฐ์ดํฐ ์ฝ์ | INSERT INTO students VALUES (...); |
SELECT | ๋ฐ์ดํฐ ์กฐํ | SELECT * FROM students; |
WHERE | ์กฐ๊ฑด ๊ฒ์ | SELECT * FROM students WHERE age = 15; |
ORDER BY | ์ ๋ ฌ | SELECT * FROM students ORDER BY age DESC; |
UPDATE + SET | ๋ฐ์ดํฐ ์์ | UPDATE students SET grade = '11th'; |
DELETE FROM | ๋ฐ์ดํฐ ์ญ์ | DELETE FROM students WHERE name = 'Bob'; |
.tables | ํ ์ด๋ธ ๋ชฉ๋ก ํ์ธ | .tables |
.schema | ํ ์ด๋ธ ๊ตฌ์กฐ ํ์ธ | .schema students |
COUNT() | ํ ๊ฐ์ | SELECT COUNT(*) FROM students; |
AVG() | ํ๊ท ๊ณ์ฐ | SELECT AVG(age) FROM students; |
GROUP BY | ๊ทธ๋ฃนํ | SELECT grade, COUNT(*) FROM students GROUP BY grade; |
JOIN + ON | ํ ์ด๋ธ ์ฐ๊ฒฐ | ... JOIN B ON A.id = B.a_id |
.headers ON | ์ด ์ด๋ฆ ํ์ | .headers ON |
.mode column | ์ด ์ ๋ ฌ ์ถ๋ ฅ | .mode column |
.exit | ์ข ๋ฃ | .exit |
| ์์ฑ ์์ | ์คํ ์์ |
|---|---|
SELECT โ FROM โ WHERE โ ORDER BY | FROM โ WHERE โ SELECT โ ORDER BY |
-- ์์ฑ์ ์ด๋ ๊ฒ
SELECT name
FROM students
WHERE age > 15;
ํ์ง๋ง ๋ด๋ถ ์คํ ์์๋:
FROM studentsWHERE age > 15SELECT name์ด ํฌ์คํธ๋ SQLite ์ค์ต์ ํตํด SQL์ ๊ธฐ๋ณธ๊ธฐ๋ฅผ ํํํ ์์ ์ ์๋๋ก ๊ตฌ์ฑ๋์์ต๋๋ค. Python, ๋ฐ์ดํฐ ๋ถ์, ์ฑ ๊ฐ๋ฐ ๋ฑ ๋ค์ํ ๋ถ์ผ์์ SQL์ ํ์ ์ธ์ด์ ๋๋ค. SQLite๋ก ์ฐ์ตํ๋ฉด์ ์์ฐ์ค๋ฝ๊ฒ ์ตํ๋ณด์ธ์!
๐ ํ๊ทธ ์ถ์ฒ:
#SQLite #SQL๊ธฐ์ด #๋ฐ์ดํฐ๋ฒ ์ด์ค #SQLite์ค์น #SQL์ค์ต #DB์ด๋ณด #๋ฐ์ดํฐ๋ถ์