MySQL에 데이터베이스 생성하기

Jinny·2023년 2월 26일
0

MySQL

목록 보기
2/2

💾 데이터베이스 생성

✔️ 1. MySQL 접속

  • 다음 명령어로 비밀번호 입력 후 MySQL에 접속한다.
mysql -u root -p
  • 다음과 같은 긴 문장이 나오면 정상적으로 접속된 것이다.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 27
Server version: 5.7.41 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input state

✔️ 2. 데이터베이스 리스트 확인

  • 다음 명령어를 입력하여 데이터베이스 리스트를 확인한다.
  • 4개의 데이터베이스는 자동 생성된 것이다.
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

✔️ 3. 데이터베이스 생성

  • 다음 명령어를 사용하여 데이터베이스를 한다.
  • 예시: "UserLog" 데이터베이스 생성
mysql> CREATE DATABASE UserLog;
Query OK, 1 row affected (0.02 sec)
  • 데이터베이스 리스트를 다시 확인해 보면 새로 만든 데이터베이스가 정상적으로 생성된 것을 확인할 수 있다.
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| UserLog            |  // 새로 생성된 데이터베이스 
| mysql              |
| performance_schema |
| sys                |
+--------------------+

✔️ 4. table & column 만들기

  • 다음 명령어를 사용하여 DB를 선택한 후 table을 확인할 수 있다.

  • DB 선택

mysql> use UserLog;
Database changed
  • 테이블 확인
mysql> SHOW tables;
Empty set (0.00 sec) // 테이블을 방금 만들어서 당연히 비어있다.
  • 쿼리문으로 테이블 생성
mysql> CREATE table user_log(
    -> ID INT NOT NULL AUTO_INCREMENT,
    -> nickname VARCHAR(64),
    -> money DEC(10, 2),
    -> last_visit DATETIME,
    -> PRIMARY KEY(ID)
    -> );
Query OK, 0 rows affected (0.07 sec)
  • 생성된 테이블 확인
mysql> SHOW tables;
+-------------------+
| Tables_in_UserLog |
+-------------------+
| user_log          |
+-------------------+
1 row in set (0.00 sec)
  • 테이블 구조 확인
mysql> desc user_log;
+------------+---------------+------+-----+---------+----------------+
| Field      | Type          | Null | Key | Default | Extra          |
+------------+---------------+------+-----+---------+----------------+
| ID         | int(11)       | NO   | PRI | NULL    | auto_increment |
| nickname   | varchar(64)   | YES  |     | NULL    |                |
| money      | decimal(10,2) | YES  |     | NULL    |                |
| last_visit | datetime      | YES  |     | NULL    |                |
+------------+---------------+------+-----+---------+----------------+
4 rows in set (0.05 sec)

profile
공부는 마라톤이다. 한꺼번에 많은 것을 하다 지치지 말고 조금씩, 꾸준히, 자주하자.

0개의 댓글