35.SQL-1

SOWA·2023년 4월 17일
0

SQL

목록 보기
1/12

🧷 데이터 베이스

여러 사람이 공유하여 사용할 목적으로 체계화해 통합, 관리하는 데이터의 집합체

🖇️ DBMS (Database Management System)

사용자와 데이터베이스 사이에서 사용자의 요구에따라 정보를 생성해주고 데이터베이스를 관리해주는 소프트웨어

🖇️ 관계형 데이터베이스(RDB: Relational Database)

서로간에 관계가 있는 데이터 테이블들을 모아둔 데이터 저장공간

🧷 SQL(Structured Query Language)

데이터 베이스에서 데이터를 정의,조작,제어하기 위해 사용하는 언어
표준이 존재. DBMS마다 추가기능 제공

🖇️ SQL 구성

  • 데이터 정의 언어(DDL: Data Definition Language)
    • CREATE, ALTER, DROP 등의 명령어
  • 데이터 조작 언어(DML: Data Manipulation Language)
    • INSERT, UPDATE, DELETE, SELECT등의 명령어
  • 데이터 제어 언어(DCL: Data Control Language)
    • GRANT, REVOKE, COMMIT, ROLLBACK등의 명령어

ㄴ데이터 사이언티스트가 집중해야할 언어: 데이터 조작 언어


show databases;
: 데이터 베이스들 보여주기

USE testdb;
:testdb로 이동

mysql> use testdb;
Database changed

drop database testdb
:testdb 삭제

Query OK, 0 rows affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sakila             |
| sys                |
| world              |
+--------------------+
6 rows in set (0.00 sec)

🖇️ User 관리

  • useer 조회
    사용자정보는 mysql에서 관리하므로 일단 mysql 데이터베이스로 이동후 조회

mysql> use mysql;
mysql> select host, user from user;

+-----------+------------------+
| host      | user             |
+-----------+------------------+
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
4 rows in set (0.00 sec)

  • user 생성-localhost
    현재 PC에서만 접속 가능한 사용자를 비밀번호와 함께 생성

CREATE USER 'username'@'localhost' identified by 'password';

mysql> select host, user from user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
| localhost | username         |
+-----------+------------------+
5 rows in set (0.00 sec)

  • user 생성-%
    외부에서 접속 가능한 사용자를 비밀번호와 함께 생성
    CREATE USER 'username'@'%' identified by 'password';
mysql> select host, user from user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| %         | username         |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
| localhost | username         |
+-----------+------------------+
6 rows in set (0.00 sec)

  • user 삭제

DROP USER 'username'@'localhost';
DROP USER 'username'@'%';

mysql> select host, user from user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
4 rows in set (0.00 sec)


🖇️ User 권한 관리

  • user 권한 확인
    사용자에게 부여된 모든 권한 목록을 확인

SHOW GRANTS FOR 'me'@'localhost';

+----------------------------------------+
| Grants for me@localhost                |
+----------------------------------------+
| GRANT USAGE ON *.* TO `me`@`localhost` |
+----------------------------------------+
1 row in set (0.00 sec)

  • user 권한 부여
    사용자에게 특정 데이터베이스의 모든 권한을 부여

GRANT ALL ON dbname.* to 'username'@'localhost';


GRANT ALL ON testdb.* to 'me'@'localhost';

 show grants for 'me'@'localhost';
+--------------------------------------------------------+
| Grants for me@localhost                                |
+--------------------------------------------------------+
| GRANT USAGE ON *.* TO `me`@`localhost`                 |
| GRANT ALL PRIVILEGES ON `testdb`.* TO `me`@`localhost` |
+--------------------------------------------------------+
  • 수정내용이 적용이 되지않은 경우 새로고침

FLUSH PRIVILEGES;


  • user 권한 제거
    사용자에게 특정 데이터 베이스의 모든 권한 삭제

REVOKE ALL ON dbname.* from 'username'@'localhost';

REVOKE ALL ON testdb.* from 'me'@'localhost';

+----------------------------------------+
| Grants for me@localhost                |
+----------------------------------------+
| GRANT USAGE ON *.* TO `me`@`localhost` |
+----------------------------------------+
1 row in set (0.00 sec)


from.제로베이스 데이터 취업스쿨 강의

0개의 댓글