해리의 데브로그

The PHP Practitioner 04 - Database(MySQL)

|

Laracasts - The PHP Practitioner 강의를 듣고 정리한 포스팅 입니다.

1. MySQL 기본 설정

설치 및 기본 셋팅 관련 정보는 링크 참조

#MySQl 설치
brew install mysql

#MySQL 기본 환경 설정
mysql_secure_installation 

#서버 실행
brew servcies start mysql
mysql.server start #background에서 실행시킬 필요가 없는 경우

#MySQL 접속
mysql -u root -p

#서버 종료
brew servcies stop mysql
mysql.server stop 

2. MySQL 관련 기초 코드

mysql> show databases;  #database list
mysql> create database mytodo; #create database
Query OK, 1 row affected (0.00 sec)

mysql> use mytodo # move to specific database
Database changed

# create schema
mysql> create table todos (
    -> description text,
    -> completed boolean
    -> );

# schema list
mysql> show tables;
+------------------+
| Tables_in_mytodo |
+------------------+
| todos            |
+------------------+
1 row in set (0.00 sec)

# schema detail
mysql> describe todos;
+-------------+------------+------+-----+---------+-------+
| Field       | Type       | Null | Key | Default | Extra |
+-------------+------------+------+-----+---------+-------+
| description | text       | YES  |     | NULL    |       |
| completed   | tinyint(1) | YES  |     | NULL    |       |
+-------------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

# delete schema
mysql> drop table todos;
Query OK, 0 rows affected (0.01 sec)

# Add NOT NULL options
mysql> create table todos (
    -> description text NOT NULL,
    -> completed boolean NOT NULL
    -> );
    
# Add id - PK, Auto_increment
mysql> create table todos (
    -> id integer PRIMARY KEY AUTO_INCREMENT,
    -> description text NOT NULL,
    -> completed boolean NOT NULL
    -> );

mysql> describe todos;
+-------------+------------+------+-----+---------+----------------+
| Field       | Type       | Null | Key | Default | Extra          |
+-------------+------------+------+-----+---------+----------------+
| id          | int(11)    | NO   | PRI | NULL    | auto_increment |
| description | text       | NO   |     | NULL    |                |
| completed   | tinyint(1) | NO   |     | NULL    |                |
+-------------+------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

# Table CRUD...
mysql> insert into todos (description, completed) values ('Go to the store', false);
mysql> select * from todos

Comments