외로운 Nova의 작업실

mysql 프로그래밍 - 명령문, 함수 정리 본문

Programming/SQL

mysql 프로그래밍 - 명령문, 함수 정리

Nova_ 2023. 1. 11. 19:17

- 명령문

데이터베이스 만들기
create database users;

데이터베이스 사용하기
use users;

테이블 만들기
create table users(
userid VARCHAR(50) not null,
userpassword VARCHAR(50) not null)


테이블에 값 추가하기
insert into users (userid, userpassword) values ("admin", "strawberry")
insert into users (userid, userpassword) values ("guest", "guest");


테이블에서 유저아이디와 패스워드 맞는 것 고르기
select * from users where userid = " " and userpassword = " "

컴퓨터 파일에 쓰기
SELECT 1 INTO OUTFILE  'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/test1.txt'

파일 권한 위치 보기
select @@secure_file_priv

like 명령문 upw가 pass?o로 시작하는 uid 반환
select uid from user where upw like "pass_o%"

- 함수

length함수() : 인자값의 길이를 반환합니다.
select length(userpassword) from users where userid = "admin"

substr함수() : 인자값의 1번째 글자부터 1개를 가져옵니다.
select substr(userpassword, 1, 1) from users where userid = "admin"

char_length() : 인자값의 길이를 반환합니다.
select char_length(userpassword) from users where userid = "admin"

ord() : 인자값의 아스키코드, urf-8값을 10진수로 반환합니다.
select ord("가") from users

bin() : 10진수 인자값을 2진수로 변환하여 반환합니다.
select bin(128) from users

sleep() : 매개변수를 초로 인식하고 잠시 프로그램을 멈춥니다.
sleep(2)

CONVERT(bobl USIG utf8) : bobl 타입을 string으로 변환시켜줍니다.
CONVERT(load_file('C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/test.txt') USING utf8)

load_file("경로") : 경로에 있는 파일에 있는 값을 로드합니다.
select CONVERT(load_file('C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/test.txt') USING utf8)

 

- 서브쿼리, 유니온

테이블에서 admin password의 길이 비교를 서브쿼리로 진행하여
그 테이블 안에서 모든 값들 불러오기 
select * from users  where userid = "" or ((select length(userpassword) 
	where userid = "admin") > 3 ) -- userpassword = "hi"

테이블에서 서브쿼리를 이용해 userpassword의 첫번째 값이 아스키코드 72의 문자보다 
작은경우를 가지고 서브테이블을 만들고 그 서브테이블에서 모든 결과 얻어오기
괄호에 있는 서브 쿼리먼저 실행후 그 테이블 안에서 다시 select 구문 실행함
select * from users  where userid = "" or ((SELECT SUBSTR(userpassword,1,1) 
	WHERE userid="admin") < CHAR(72))
    
유니온: 두개의 테이블 값을 합치는 것
SELECT ID, NAME FROM TABLE1 UNION SELECT ID, NAME FROM TABLE2

- 스키마 조회

schema란 개요를 의미합니다.

스키마 정보 조회
select TABLE_SCHEMA from information_schema.tables group by TABLE_SCHEMA

테이블 정보 조회
select TABLE_SCHEMA, TABLE_NAME from information_schema.TABLES

칼럼 정보 조회
select TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME from information_schema.COLUMNS

실시간 쿼리 실행 조회
select * from information_schema.PROCESSLIST

계정에 따른 실시간 쿼리 실행 조회
select user,current_statement from sys.session

권한 및 계정 정보 조회
select GRANTEE,PRIVILEGE_TYPE,IS_GRANTABLE from information_schema.USER_PRIVILEGES

계정 정보 조회
select User, authentication_string from mysql.user

- 주석

주석처리 내의 명령문 실행하기
select 1 /*!union*/ select 2

 

- 연산자 우선순위

1 INTERVAL
2 BINARY, COLLATE
3 !
4 - (단항 연산자), ~ (비트 연산자)
5 ^
6 *, /, DIV, %, MOD
7 - (이항 연산자), +
8 <<, >>
9 &
10 |
11 = (관계 연산자), <=>, >=, >, <=, <, <>, !=, IS, LIKE, REGEXP, IN
12 BETWEEN, CASE, WHEN, THEN, ELSE
13 NOT
14 AND, &&
15 XOR
16 OR, ||
17 = (대입 연산자), :=

 

- 백쿼트 사용법

1. backtick(` `)과
2. single quotes(' ')와
3. double quotes(" ")의 차이점

1은 띄어쓰기를 못할때 table 및 column 명을 감싸는데 쓰이고
2는 문자열 표기에 쓰이고 (string literals)
3은 기본모드일 경우 2와 같고, ANSI_QUOTES 모드일 경우는 1과 같이 기능한다

Comments