cd C:\Bitnami\wampstack-8.1.1-0\mariadb\bin
mysql -uroot -p
MariaDB [(none)]> CREATE DATABASE 생성할 데이터베이스명 ;
==> Query OK, 1 row affected (0.002 sec)
MariaDB [(none)]> SHOW DATABASES;
==>
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| study |
| test |
+--------------------+
5 rows in set (0.001 sec)
MariaDB [(none)]> DROP DATABASE 삭제할 데이터베이스명 ;
MariaDB [(none)]> USE 사용할 데이터베이스명 ;
MariaDB [(none)]> USE study;
Database changed
MariaDB [study]>
MariaDB [study]> CREATE TABLE 생성할테이블명 ( 필드명 데이터타입(길이) );
MariaDB [study]> CREATE TABLE topic(
-> id INT(11) NOT NULL AUTO_INCREMENT, #필드명 데이터타입(길이) 공백X 자동으로 증가(중복x)
-> title VARCHAR(100) NOT NULL,
-> description TEXT NULL,
-> created DATETIME NOT NULL,
-> author VARCHAR(30) NULL,
-> profile VARCHAR(100) NULL,
-> PRIMARY KEY(id)
);
==> Query OK, 0 rows affected (0.015 sec)
MariaDB [study]> SHOW TABLES;
==>
+-----------------+
| Tables_in_study |
+-----------------+
| topic |
+-----------------+
1 row in set (0.001 sec)
MariaDB [study]> DESC 테이블명;
MariaDB [study]> DESC topic;
==>
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(100) | NO | | NULL | |
| description | text | YES | | NULL | |
| created | datetime | NO | | NULL | |
| author | varchar(30) | YES | | NULL | |
| profile | varchar(100) | YES | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
6 rows in set (0.014 sec)
MariaDB [study]> INSERT INTO 테이블명 ( 필드명 ) VALUES ( 넣을값 );
MariaDB [study]> INSERT INTO topic
->(title,description,created,author,profile)
->VALUES('MySQL','MySQL is ...',NOW(),'egoing','developer');
==>
Query OK, 1 row affected (0.005 sec)
+
MariaDB [study]> INSERT INTO topic (title,description,created,author,profile) VALUES('SQL Server','SQL Server is ...',NOW(),'duru','data administrator');
Query OK, 1 row affected (0.010 sec)
MariaDB [study]> INSERT INTO topic (title,description,created,author,profile) VALUES('PostgreSQL','~~ Server is ...',NOW(),'taeho','data sientist, developer');
Query OK, 1 row affected (0.002 sec)
MariaDB [study]> INSERT INTO topic (title,description,created,author,profile) VALUES('MongoDB','~~ is ...',NOW(),'egoing','developer');
Query OK, 1 row affected (0.010 sec)
MariaDB [study]> SELECT * FROM 테이블명 ;
MariaDB [study]> SELECT * FROM topic;
==>
+----+------------+-------------------+---------------------+--------+--------------------------+
| id | title | description | created | author | profile |
+----+------------+-------------------+---------------------+--------+--------------------------+
| 1 | MySQL | MySQL is ... | 2022-01-13 20:28:13 | egoing | developer |
| 2 | ORACLE | ORACLE is ... | 2022-01-13 20:40:47 | egoing | developer |
| 3 | SQL Server | SQL Server is ... | 2022-01-13 20:42:29 | duru | data administrator |
| 4 | PostgreSQL | ~~ Server is ... | 2022-01-13 20:44:34 | taeho | data sientist, developer |
| 5 | MongoDB | ~~ is ... | 2022-01-13 20:45:19 | egoing | developer |
+----+------------+-------------------+---------------------+--------+--------------------------+
5 rows in set (0.001 sec)
여기서 SELECT 옆에 붙은 '*' 는 모든/전체를 의미한다.
MariaDB [study]> SELECT 열이름 FROM 테이블명 ;
MariaDB [study]> SELECT id,title FROM topic;
==>
+----+------------+
| id | title |
+----+------------+
| 1 | MySQL |
| 2 | ORACLE |
| 3 | SQL Server |
| 4 | PostgreSQL |
| 5 | MongoDB |
+----+------------+
5 rows in set (0.000 sec)
MariaDB [study]> SELECT 열이름 FROM 테이블명 WHERE 특정열 = ' 원하는 값 ';
MariaDB [study]> SELECT id,title FROM topic WHERE author = 'egoing';
==>
+----+---------+
| id | title |
+----+---------+
| 1 | MySQL |
| 2 | ORACLE |
| 5 | MongoDB |
+----+---------+
3 rows in set (0.001 sec)
MariaDB [study]> SELECT 열이름 FROM 테이블명 WHERE 특정열 IN ( 찾는값1,찾는값2~ );
MariaDB [study]> SELECT 열이름 FROM 테이블명 WHERE 특정열 NOT IN ( 찾는값1,찾는값2~ );
= , <> , < , > , ≤, ≥ 도 가능
MariaDB [study]> SELECT 열이름 FROM 테이블명 WHERE 특정열 LIKE '%특정문자%' ; (값이 들어가는 경우 전체)
MariaDB [study]> SELECT 열이름 FROM 테이블명 WHERE 특정열 LIKE '%특정문자' ; (값으로 시작하는 경우)
MariaDB [study]> SELECT 열이름 FROM 테이블명 WHERE 특정열 LIKE '특정문자%' ; (값으로 끝나는 경우)
MariaDB [study]> SELECT 열이름 FROM 테이블명 WHERE 특정열 LIKE '__특정문자%' ; (앞에 두개가 더 붙고 특정문자로 끝나는 경우)
기본적으로 MySQL 은 대소문자를 구분하지 않지만, 문자열에서는 대소문자를 구분함.
MariaDB [study]> SELECT * FROM topic WHERE author IN ('egoing', 'taeho');
==>
+----+------------+------------------+---------------------+--------+--------------------------+
| id | title | description | created | author | profile |
+----+------------+------------------+---------------------+--------+--------------------------+
| 1 | MySQL | MySQL is ... | 2022-01-13 20:28:13 | egoing | developer |
| 2 | ORACLE | ORACLE is ... | 2022-01-13 20:40:47 | egoing | developer |
| 4 | PostgreSQL | ~~ Server is ... | 2022-01-13 20:44:34 | taeho | data sientist, developer |
| 5 | MongoDB | ~~ is ... | 2022-01-13 20:45:19 | egoing | developer |
+----+------------+------------------+---------------------+--------+--------------------------+
4 rows in set (0.001 sec)
MariaDB [study]> SELECT 열이름 FROM 테이블명 ORDER BY 특정열 ; ( 오름차순 )
MariaDB [study]> SELECT 열이름 FROM 테이블명 ORDER BY 특정열 DESC ; ( 내림차순 )
MariaDB [study]> SELECT id,title,created FROM topic ORDER BY created DESC ;
==>
+----+------------+---------------------+
| id | title | created |
+----+------------+---------------------+
| 5 | MongoDB | 2022-01-13 20:45:19 |
| 4 | PostgreSQL | 2022-01-13 20:44:34 |
| 3 | SQL Server | 2022-01-13 20:42:29 |
| 2 | ORACLE | 2022-01-13 20:40:47 |
| 1 | MySQL | 2022-01-13 20:28:13 |
+----+------------+---------------------+
5 rows in set (0.001 sec)
MariaDB [study]> SELECT 열이름 FROM 테이블명 LIMIT 개수 ;
MariaDB [study]> SELECT * FROM topic LIMIT 2;
==>
+----+--------+---------------+---------------------+--------+-----------+
| id | title | description | created | author | profile |
+----+--------+---------------+---------------------+--------+-----------+
| 1 | MySQL | MySQL is ... | 2022-01-13 20:28:13 | egoing | developer |
| 2 | ORACLE | ORACLE is ... | 2022-01-13 20:40:47 | egoing | developer |
+----+--------+---------------+---------------------+--------+-----------+
2 rows in set (0.001 sec)
MariaDB [study]> SELECT 열 FROM 테이블명 GROUP BY 그룹화할 열 ;
MariaDB [study]> SELECT author FROM topic GROUP BY author;
==>
+--------+
| author |
+--------+
| duru |
| egoing |
| taeho |
+--------+
3 rows in set (0.001 sec)
MariaDB [study]> SELECT 열 FROM 테이블명 GROUP BY 그룹화할 열 HAVING 조건식;
MariaDB [study]>SELECT 열 FROM 테이블명 WHERE 조건식 GROUP BY 그룹화할 열 HAVING 조건식;
WHERE는 그룹화 하기 전, HAVING은 그룹화 후 조건처리
GROUP BY에 대해 쉽게 알려준 블로그 출처
MariaDB [study]> SELECT COUNT( 열이름 ) FROM 테이블명 ;
NULL 값은 제외하고 계산하며, 열이름 대신 ' * ' 을 입력한다면 전체행의 개수를 가져옴.
MariaDB [study]> SELECT COUNT(author) FROM topic;
==>
+---------------+
| COUNT(author) |
+---------------+
| 5 |
+---------------+
1 row in set (0.001 sec)
MariaDB [study]> SELECT COUNT(author) as cnt FROM topic;
# cnt 라는 이름으로 개수를 가져옴
==>
+-----+
| cnt |
+-----+
| 5 |
+-----+
1 row in set (0.001 sec)
MariaDB [study]> SELECT COUNT( DISTINCT 열이름 ) FROM 테이블명 ;
MariaDB [study]> SELECT IF( 조건문,'참일경우 바꿀 데이터 ',' 거짓일경우 바꿀 데이터 ') AS '출력할 컬럼이름 ' FROM ' 테이블명 ' ;
MariaDB [study]> SELECT IF(author IS NULL, 'noname', author) AS dis FROM topic;
==>
+--------+
| dis |
+--------+
| egoing |
| egoing |
| duru |
| taeho |
| egoing |
+--------+
5 rows in set (0.008 sec)
MariaDB [study]>
SELECT
CASE
WHEN 조건식1 THEN '참일 경우의 바꿀 데이터1'
WHEN 조건식2 THEN '참일 경우의 바꿀 데이터2'
...
ELSE '조건에 맞지 않을 경우 바꿀 데이터'
END AS '출력할 컬럼의 별칭'
FROM '테이블명';
SELECT ANIMAL_TYPE,
CASE
WHEN NAME IS NULL THEN 'No name'
ELSE NAME
END AS NAME
,SEX_UPON_INTAKE FROM ANIMAL_INS ;
MariaDB [study]> SELECT DATE_FORMAT( ' DATE형식의 컬럼명 ' , '%Y-%m-%d(원하는 출력형식') AS '날짜' FROM 테이블명 ;
예시 프로그래머스 문제 - DATETIME에서 DATE로 형 변환
SELECT ANIMAL_ID, NAME,DATE_FORMAT(DATETIME , '%Y-%m-%d') AS '날짜'
FROM ANIMAL_INS ;
MariaDB [study]> UPDATE '수정할 테이블명' SET '바꿀 칼럼명' = '바꿀 데이터'~ WHERE '바꿀행의 조건'='그 값';
MariaDB [study]> SELECT * FROM topic;
+----+------------+-------------------+---------------------+--------+--------------------------+
| id | title | description | created | author | profile |
+----+------------+-------------------+---------------------+--------+--------------------------+
| 1 | MySQL | MySQL is ... | 2022-01-13 20:28:13 | egoing | developer |
| 2 | ORACLE | ORACLE is ... | 2022-01-13 20:40:47 | egoing | developer |
| 3 | SQL Server | SQL Server is ... | 2022-01-13 20:42:29 | duru | data administrator |
| 4 | PostgreSQL | ~~ Server is ... | 2022-01-13 20:44:34 | taeho | data sientist, developer |
| 5 | MongoDB | ~~ is ... | 2022-01-13 20:45:19 | egoing | developer |
+----+------------+-------------------+---------------------+--------+--------------------------+
5 rows in set (0.001 sec)
MariaDB [study]> UPDATE topic SET description = 'Oracle is...', title = 'Oracle' WHERE id=2;
Query OK, 1 row affected (0.003 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [study]> SELECT * FROM topic;
+----+------------+-------------------+---------------------+--------+--------------------------+
| id | title | description | created | author | profile |
+----+------------+-------------------+---------------------+--------+--------------------------+
| 1 | MySQL | MySQL is ... | 2022-01-13 20:28:13 | egoing | developer |
| 2 | Oracle | Oracle is... | 2022-01-13 20:40:47 | egoing | developer |
| 3 | SQL Server | SQL Server is ... | 2022-01-13 20:42:29 | duru | data administrator |
| 4 | PostgreSQL | ~~ Server is ... | 2022-01-13 20:44:34 | taeho | data sientist, developer |
| 5 | MongoDB | ~~ is ... | 2022-01-13 20:45:19 | egoing | developer |
+----+------------+-------------------+---------------------+--------+--------------------------+
5 rows in set (0.000 sec)