Mysql sql: Difference between revisions

From 탱이의 잡동사니
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
== Overview ==
== Overview ==
Mysql 사용법 정리.
Mysql 사용법 정리.
== Installation ==
Debian 8(Jessie) 에서 Mysql-server-5.6 이상 버전을 설치하기 위해서는 다음의 스크립트를 사용하면 된다.<ref>http://blog.programster.org/debian-8-install-mysql-5-6/</ref>
<source lang=bash>
#!/bin/bash
cd /tmp
# Confirm the version at http://dev.mysql.com/downloads/repo/apt/
wget -O mysql-apt-config.deb https://dev.mysql.com/get/mysql-apt-config_0.3.7-1debian8_all.deb
sudo dpkg -i mysql-apt-config.deb
# At the popup-page, select "Apply"
# Install mysql-server package
sudo apt-get update
sudo apt-get install mysql-server-5.6 -y
</source>


== Data types ==
== Data types ==

Revision as of 23:47, 9 November 2015

Overview

Mysql 사용법 정리.

Installation

Debian 8(Jessie) 에서 Mysql-server-5.6 이상 버전을 설치하기 위해서는 다음의 스크립트를 사용하면 된다.<ref>http://blog.programster.org/debian-8-install-mysql-5-6/</ref> <source lang=bash>

  1. !/bin/bash

cd /tmp

  1. Confirm the version at http://dev.mysql.com/downloads/repo/apt/

wget -O mysql-apt-config.deb https://dev.mysql.com/get/mysql-apt-config_0.3.7-1debian8_all.deb

sudo dpkg -i mysql-apt-config.deb

  1. At the popup-page, select "Apply"
  1. Install mysql-server package

sudo apt-get update sudo apt-get install mysql-server-5.6 -y </source>

Data types

datetime

Mysql-5.6 버전 이후부터 사용가능.

foreign key

Mysql-3.23.43 버전 이후부터는 외래키 지정이 가능하다.

Options

  • on delete cascade

부모의 해당키가 삭제되면 자동으로 삭제된다.

  • on delete set null

부모의 해당키가 삭제되면 자동으로 null 로 만든다.

  • on update cascade

부모의 해당키가 삭제되면 자동으로 갱신된다.

Example

agent id 를 외래키로 지정을 한 agent_group 테이블이다. agent_group 에 속한 agent 삭제시 같이 삭제되고, 업데이트 시, 같이 업데이트된다.

<source lang=sql> create table agent (

   id          int not null unique,
   name        varchar(255),
   primary key(id)

);

create table agent_group(

   group_id    int not null unique,
   agent_id    int not null,
   foreign key(agent_id)       references agent(id) on delete cascade on update cascade,
   primary key(group_id, agent_id)

); </source>

ETC

Check table exist

특정 테이블이 있는지, 아닌지를 알고 싶을 때가 있다. 이런 경우, 다음의 쿼리를 입력하면, 테이블이 있으면 정상, 없으면 오류를 리턴한다.<ref>http://stackoverflow.com/questions/8829102/mysql-check-if-table-exists-without-using-select-from</ref> <source lang=sql> SELECT 1 FROM testtable LIMIT 1; </source>

Execute sql script

SQL 스크립트를 실행하고자 할 때는 다음과 같이 하면 된다.<ref>https://dev.mysql.com/doc/refman/5.7/en/mysql-batch-commands.html</ref>

-- shell
shell> mysql db_name
shell> mysql db_name < text_file
shell> mysql < text_file

-- mysql
mysql> source file_name
mysql> \. file_name

References

<references />