# zerobase DB사용
use zerobase;
Database changed
# person table 생성
mysql> CREATE TABLE person
-> ( id int,
-> name varchar(16),
-> age int,
-> sex char
-> );
Query OK, 0 rows affected (0.05 sec)
# person table 정보확인
mysql> desc person
-> ;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | varchar(16) | YES | | NULL | |
| age | int | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
DB의 테이블에 있는 데이터를 변형[입력(INSERT), 수정(UPDATE), 삭제(DELETE)] 하거나 조회(SELECT)하기 위해 사용한다.
🧷 데이터입력
INSERT INTO table_name (column1, column2 ...) VALUES (value1, value2..) # column명은 모든컬럼의 데이터 입력 시 생략가능하고, # 단, 순서에 맞게 value를 넣어줄것
#이효리 데이터 입력
mysql> INSERT INTO person (id, name, age, sex)
-> VALUES (1,'이효리',43,'F');
Query OK, 1 row affected (0.02 sec)
# 이상순 데이터 입력
mysql> INSERT INTO person
-> VALUES (2, '이상순', 48, 'M');
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO person
-> VALUES (3, '유재석', 52, 'M');
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO person
-> VALUES (4, '이미주', 30, 'F');
Query OK, 1 row affected (0.01 sec)
🧷 데이터 조회
SELECT column1, column2, ... # 전체컬럼 확인 시 * FROM table_name; WHERE condition
# 전체 데이터 확인
mysql> select * from person;
+------+-----------+------+------+
| id | name | age | sex |
+------+-----------+------+------+
| 1 | 이효리 | 43 | F |
| 2 | 이상순 | 48 | M |
+------+-----------+------+------+
2 rows in set (0.00 sec)
# ------부분조회 + where 조건------
mysql> select * from person where name='이효리';
+------+-----------+------+------+
| id | name | age | sex |
+------+-----------+------+------+
| 1 | 이효리 | 43 | F |
+------+-----------+------+------+
1 row in set (0.01 sec)
mysql> select * from person where sex = 'F';
+------+-----------+------+------+
| id | name | age | sex |
+------+-----------+------+------+
| 1 | 이효리 | 43 | F |
+------+-----------+------+------+
1 row in set (0.00 sec)
mysql> select age from person where name = '이상순';
+------+
| age |
+------+
| 48 |
+------+
1 row in set (0.00 sec)
select * from person where age > 50;
+------+-----------+------+------+
| id | name | age | sex |
+------+-----------+------+------+
| 3 | 유재석 | 52 | M |
+------+-----------+------+------+
1 row in set (0.00 sec)
🧷 데이터 수정
UPDATE table_name SET column1 = value1, column2 = values, .. WHERE condition #where문이 없을경우 전체 데이터 변경
UPDATE person SET age = 23 WHERE name = '이효리';
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from person;
+------+-----------+------+------+
| id | name | age | sex |
+------+-----------+------+------+
| 1 | 이효리 | 23 | F |
| 2 | 이상순 | 48 | M |
| 3 | 유재석 | 52 | M |
| 4 | 이미주 | 30 | F |
+------+-----------+------+------+
4 rows in set (0.00 sec)
🧷 데이터 삭제
DELETE FROM table_name WHERE condition
DELETE FROM person where sex = 'F';
Query OK, 2 rows affected (0.02 sec)
mysql> select * from person;
+------+-----------+------+------+
| id | name | age | sex |
+------+-----------+------+------+
| 2 | 이미주 | 48 | M |
| 3 | 유재석 | 52 | M |
+------+-----------+------+------+
2 rows in set (0.00 sec)
select * from person;
+------+-----------+------+------+
| id | name | age | sex |
+------+-----------+------+------+
| 1 | 이효리 | 22 | F |
| 2 | 이상순 | 50 | M |
+------+-----------+------+------+
2 rows in set (0.00 sec)
mysql> delete from person;
Query OK, 2 rows affected (0.02 sec)
mysql> select * from person;
Empty set (0.00 sec)
🚩
DROP TABLE table_name
vsDELETE FROM table_name
- drop table : 테이블 구조와 데이터 모두 삭제
- delete from : 테이블 데이터 삭제 (용량초기화 X)