LoginSignup
0
1

More than 3 years have passed since last update.

[MySQL] 基本操作

Last updated at Posted at 2020-12-16

概要

MySQLの基本操作のメモ(コマンド一覧).

目次

  1. MySQLの起動方法
  2. データベースの操作
  3. テーブルの操作
  4. sqlFileの中身を実行する
  5. テーブルをコピーする

起動方法

ターミナル
mysql -u root -p

→Enter password: [パスワード]

データベースの操作

↓ データベース一覧を表示するコマンド

mysql>
show databases;

↓ データベースを作成するコマンド

mysql>
create database データベース名;

↓ データベースを削除するコマンド

mysql>
drop database データベース名;

↓ データベースを選択するコマンド

mysql>
use データベース名;

テーブルを操作するにはこのUSEコマンドで一度データベースを選択しなければならない.

テーブルの操作

↓ テーブル一覧を表示するコマンド

mysql>
show tables;

↓ テーブルを作成するコマンド(例)

mysql>
CREATE table non_index_table (id INT AUTO_INCREMENT, num INT, PRIMARY KEY (id));
INSERT INTO non_index_table (num) VALUES ('7084');
INSERT INTO non_index_table (num) VALUES ('1348');

テーブルの作成はCREATE文を使う
データを追加するにはINSERT文を使う

CREATE table テーブル名 (カラム名 型, カラム名 型, ...);
INSERT INTO テーブル名 (カラム名, カラム名) VALUES (値, 値);

AUTO_INCREMENT は insert するたびに自動で+1して設定してくれる.
PRIMARY KEY (カラム名) で主キーを設定する.

↓ テーブルの中身を確認するコマンド

mysql>
SELECT * FROM テーブル名;

↓ テーブルを削除するコマンド

mysql>
drop table テーブル名;

↓ データを更新するコマンド(例)

mysql>
update non_index_table set num=2411 where id=1668608; 

update テーブル名 set カラム名1=値1 where カラム名2=値2;
(テーブル名)の(カラム名1=値1)の場所の(カラム名2)を(値2)に更新する.

sqlfileの中身を実行する

mysql>
source (sqlFile.sqlのアドレス);

テーブルをコピーする

mysql>
CREATE TABLE 新しいテーブル名 SELECT * FROM 古いテーブル名;
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1