2020/<programing> <coding>

step 2-2. database 언어 chap 1.

Rosik 2020. 3. 15. 02:51

3.2 mysql 설치(mac os)

https://youtu.be/_A7yOpPJi8g

 

database 수업중 나오는 terminal 명령어들 정리

 

 

cd /usr/local/mysql/bin/

./mysql -uroot -p

./mysql  : mysql 사용 명령어

-uroot   : -u(user)root(user name, 관리자)

-p          :  password

 

 

4 mysql 구조

https://youtu.be/IWEa4DN_1Yk

 

data를 기록하는 최종적인곳 : 표 (table)

표가 많아졌을 때 정리정돈 할 곳 : 데이터베이스 / 스키마 (database / schema : 관련있는 파일들을 모아서 정리하는것, grouping)

스키마들을 저장하는곳 : database server 기능을 하는 프로그램 ->mysql

 

 

5. mysql 서버접속

https://youtu.be/bzhFcbin8_g

 

database는 보안을 중시여김 (권한부여 등)

root 사용자로 프로그램을 사용하는것을 권장하지 않음

 

 

6. 스키마의 사용

https://youtu.be/9stHa3mRbNI

 

database 생성

CREATE DATABASE /name/; (database_first)

 

database 삭제

DROP DATABASE /name/;

 

database 생성확인

SHOW DATABASES;

 

database 선택

USE /name/; 

 

정리 : database(schema)를 만들고 확인할 수 있는 단계까지 도달.

 

 

7. SQL과 테이블의 구조

https://youtu.be/f_m-RcdISxk

 

 

 

Structured Query Language 굳이 직역하자면 구조화된 질의 언어

쉽고, 중요한 언어 

 

맨날 헷갈리는데

행, row, record, 가로

열, column, 세로

 

 

8.1 테이블의 생성

https://youtu.be/d3ye07XRexs

 

MySQL 기본 명령어 정리

 

검색어 cheat sheet

sql 명령어는 소문자도 가능한듯

 

엑셀과 sql의 차이점 : column 데이터 자료형을 설정할 수 있음

 

use database_first;

create table topic(

  id int(11) not null auto_increment,

 

 

8.2 테이블의 생성

https://youtu.be/fPULu-Q-OlQ

 

varchar : variable charictor

 

use database_first;

create table topic(

  id int(11) not null auto_increment,

  title varchar(100) not null,

  description text null,

  created datetime not null,

  author varchar(30) not null,

  profile varchar(100) null,

  primary key(id));  -> 중복방지를 위한 메인 식별값

 

결과 : Query OK, 0 rows affected, 1 warning (0.01 sec)

database table 생성완료.

 

 

9. CRUD

https://youtu.be/0pU6_5BQ2Dk  

 

Create, Read > Update, Delete

 

 

10. INSERT

https://youtu.be/75LHpeOQiOs

 

테이블을 확인

show tables;

 

(topic)table의 구조 확인

desc topic;

 

id 값은 자동증가하기 때문에 굳이 선언하거나, 설정하지 않아도 됨

 

테이블에 데이터 삽입 (내용과 제목의 순서서가 바뀌지 않도록)

INSERT INTO topic (title, description, created, author, profile) VALUES('MySQL', 'MySQL is database languange', NOW(), 'sin', 'technical engineer');

>> Query OK, 1 row affected (0.01 sec) //data create 

 

데이터 불러오기

SELECT * FROM topic; (topic 에서 데이터 불러오기)

>>

+----+-------+----------------------------+---------------------+--------+--------------------+
| id    | title     | description                             | created                      | author   | profile                      |
+----+-------+----------------------------+---------------------+--------+--------------------+
|  1    | MySQL | MySQL is database language | 2020-03-15 15:41:50 | sin         | technical engineer   |
+----+-------+----------------------------+---------------------+--------+--------------------+
1 row in set (0.00 sec)

 

 

테이블에 두 번째 데이터 삽입

INSERT INTO topic (title, description, created, author, profile) VALUES('Oracle', 'Oracle is other database languange', NOW(), 'arbez', 'engineer');

SELECT * FROM topic;

>>

+----+--------+------------------------------------+---------------------+--------+--------------------+
| id    | title       | description                                         | created                       | author  | profile                       |
+----+--------+------------------------------------+---------------------+--------+--------------------+
|  1     | MySQL  | MySQL is database language             | 2020-03-15 15:41:50 | sin         | technical engineer    |
|  2    | Oracle   | Oracle is other database languange   | 2020-03-15 15:54:25 | arbez     | engineer                   |
+----+--------+------------------------------------+---------------------+--------+--------------------+

 

 

마지막 결과

>>

 

  //NOW() 함수형식 (년,월,일)이 어떤것들이 있는지 확인필요

 

 

 

 

11. SELECT

https://youtu.be/FCnJH6fLc64

 

데이터 읽기에 관련된 내용

 

 SELECT id,title,created,author  FROM topic;

>>

원하는 colomn만 볼 수있게, 엑셀의 열 감추기 기능과 같음

https://dev.mysql.com/doc/refman/8.0/en/select.html

 

여기 문법에서 , 대괄호로 된 것들은 선택가능하며, 생략이 가능함.

from, where, limit 등의명령어들도 순서가 중요함, syntax error 유발

 

 

작성자가 'sin' 인 데이터 목록 확인 / WHERE 사용

SELECT id, title, created, author From topic where author='sin';

>>

 

오름 / 내림차순 정렬 / ORDER 사용

mysql> SELECT id, title, created, author From topic where author='sin' ORDER BY id DESC;

>>

 

데이터가 너무 많을 때 모든 데이터를 불러오면 렉걸림 / LIMIT 사용

mysql> SELECT id, title, created, author From topic where author='sin' ORDER BY id DESC LIMIT 2;

>>

 

SELECT 를 필요에 따라 잘 사용하는것이 database의 핵심

 

 

 

12. UPDATE

https://youtu.be/pNINXzXaWWM

 

desc topic;

select * from topic; 이 두개는 전혀다름, 

 

desc는 각 필드의 정보를 ,

select는 그 데이터를 보여줌 ( 엑셀화면 )

 

id값 3의 데이터를 수정,

mysql> UPDATE topic SET description='MS DB Language', title='sql SERVER' WHERE id=3;

>>

id 3번을 보면 내용이 바뀌어있음.

 

*WHERE 를 빠뜨리면 모든 title & description이 바뀌어버리는 재앙이 발생하므로 잊지않도록 하고, 늘 백업본을 두고 수정하도록 하자

 

 

13. DELETE

https://youtu.be/GDY2_t-9l-s

 

id값을 가진 행을 지우고 싶을 때

DELETE FROM topic WHERE id=2;

>>

2, 5번 행 삭제, 

사라진 아이디값이 메꿔지지 않는 것을 볼 수 있다.

처음에는 메꾸어야 한다고 생각했으나, 굳이 그럴필요가 없음.

 

 

 

 

end of first chapter.

 

어떤 기술을 처음 만났을 때 그 기술의 본질이 무엇이고, 혁신이 무엇인지 생각을 해 보는것이 굉장한 도움이 된다.

 

 

그럼 DB의 본질은? 

말 그대로인 데이터베이스 관리, 조금 더 들어가면  DB를 생성, 수정, 읽기, 삭제 기능을 포함하고 있음.

 

 

next chapter.

관계형 DB는 왜 그냥 DB와 구분되는가, 무엇이 다른가.