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
|
use mydb;
-- 한줄 주석문
/*
범위 주석문
SQL - Structured Query Language
구조적 질의어
Data를 다루는 방법으로써,
CRUD
insert, select, update, delete
select가 전체 90%를 차지할 만큼 비중이 크다.
employees: 사원
departments: 부서
job: 업무
*/
select * from employees; -- employees 테이블의 모든 데이터 검색
select employee_id, first_name, salary -- 원하는 column만 가져옴
from employees; -- employees 테이블에서
desc employees; -- employees 테이블의 column에 대한 정보
/*
자료형
<java MySQL>
int int, decimal 예를들면, decimal(5)
double double, decimal 예를들면, decimal(5,1) 소수 첫째자리까지 표현
String varchar
Date date
*/
-- table 생성
/*
create table 테이블명(
컬럼명1 자료형,
컬럼명2 자료형
);
*/
-- 테이블정보 조회
select *
from information_schema.tables
where table_schema = 'mydb';
-- varchar (== String)
create table tb_varchar(
col1 varchar(10), -- 10byte라는 뜻 (영문자: 1byte, 한글은 한글자에 3byte)
col2 varchar(20)
);
select * from tb_varchar;
insert into tb_varchar(col1, col2)
values('abc', 'ABC'); -- 문자열 집어넣을 때, 작은 따옴표! col1에 abc, col2에 ABC 들어감
-- int, double
create table tb_decimal(
col1 decimal, -- 소수점 아래는 반올림해서 다 없앰
col2 decimal(5), -- 총 다섯자리만 남고 소수점 아래 버림
col3 decimal(5,2) -- 소수 둘째짜리까지 남기고, 그 아래는 버림. 총 다섯
);
insert into tb_decimal(col1, col2, col3)
values(1234.5678, 12345.12, 123.456);
select * from tb_decimal;
-- 날짜
create table tb_date(
col1 date,
col2 date
);
-- 현재 날짜
insert into tb_date(col1, col2)
values(now(), '2022-12-25'); -- now()는 현재날짜
select * from tb_date;
-- 현재 날짜, 시간
create table board(
col1 timestamp,
col2 timestamp default now()
);
insert into board(col1, col2)
values(now(), default);
insert into board(col1, col2)
values(now(), now());
select * from board;
drop table tb_varchar;
drop table tb_decimal;
drop table tb_date;
drop table board;
|
cs |
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
|
use mydb;
/*
형식: select절 -> 검색
select (값, 컬럼명, 함수, sub query)
from (테이블명, sub query)
*/
select 1 from dual; -- dual: 임시테이블
select '한글' from dual;
-- 특정 테이블에 대해서 모든 데이터를 취득
select *
from employees;
select *
from departments;
select employee_id, last_name, hire_date
from employees;
select '이름', employee_id, last_name, hire_date
from employees;
-- 컬럼의 별명(alias) 큰 따옴표 붙여야 함
select employee_id AS "사원번호", last_name as "성", salary "월급"
from employees;
select employee_id AS 사원번호, last_name as 성, salary 월급
from employees;
/* 띄어쓰기 할때는 큰 따옴표 반드시 붙여야 함 -> "사원 번호" 이렇게!
select employee_id AS "사원 번호", last_name as 성, salary 월급
from employees;도 가능하다.
*/
-- 산술연산자( +, -, *, /)
select first_name, last_name, salary * 12 as 연봉
from employees;
-- 문자열 합치기
select concat('이름: ', last_name, ' ', first_name) as 이름
from employees;
|
cs |
'DB > MySQL' 카테고리의 다른 글
MySQL - JOIN(2) (0) | 2023.01.05 |
---|---|
MySQL - JOIN(1) (0) | 2023.01.05 |
MySQL - 문법정리(2) - where, order, group, having... (0) | 2023.01.03 |
MySQL 기초 정리 (0) | 2023.01.03 |