LoginSignup
22
23

More than 5 years have passed since last update.

みんなの為のMySQLまとめ(1)

Last updated at Posted at 2015-02-05

参考
http://homepage2.nifty.com/sak/w_sak3/doc/sysbrd/mysql_06.htm

データベースに関する操作

現在存在しているデータベース一覧表示。

mysql> SHOW DATABASES;

データベースを作成する。

mysql> CREATE DATABASE(データベース名);

データベースを破棄する。

mysql> DROP DATABASE(データベース);

データベース内に入る。

mysql>USE(データベース名)

テーブルの一覧を表示。

mysql> SHOW TABLES;

テーブル作成

CREATE TABLE(テーブル名);

create table (テーブル名)(
key1    char(008)  primary key,
data1  int8,
data2  int8,
data3  int8
);

テーブルを破棄する。

mysql> DROP TABLE(テーブル名);
 
 
■項目タイプ(フィールドタイプ)

・MySQL 項目型

int / integer    4 バイト整数
smailint         2 バイト整数
bigint / int8    8 バイト整数
float            浮動小数点
double / real    倍精度浮動小数点
date             日付
time             時間
timestamp        日付時間
char(文字数)     固定長文字列  (最大 256 文字)
varchar(文字数)  可変長文字列  (最大 256 文字)
text             ラージ文字列  (最大 65535 文字)
mediumtext       ラージ文字列  (最大 16777215 文字)
largetext        ラージ文字列  (最大 4294967295 文字)
blob             ラージバイナリ(最大 65535 bytes)
mediumblob       ラージバイナリ(最大 16777215 bytes)
largeblob        ラージバイナリ(最大 4294967295 bytes)

■キー作成

・単項目のキー設定は、テーブル作成時の create 文で行うと良い。
項目定義に primary key を指定するだけである。

key1 char(008) primary key,

・複合キーは、テーブル作成後に別途 alter 文でも追加できる。

alter table testm add primary key (
    key1,
    key2
);

■順序作成(オートナンバー)

・順序は、auto_increment と定義することで利用できる。
昔の MySQL と仕様が変わった? (それとも私の古い資料が間違っている?)
構文も違うし、キー指定しないとエラーでダメであった。

create table testm (
  key1           smallint unsigned not null auto_increment primary key,
  data1          int8,
  data2          int8,
  data3          int8
) type=InnoDB;

insert into testm (data1) values (100);
insert into testm (data1) values (200);

+------+-------+-------+-------+
| key1 | data1 | data2 | data3 |
+------+-------+-------+-------+
|    1 |   100 |  NULL |  NULL |
|    2 |   200 |  NULL |  NULL |
+------+-------+-------+-------+

・Oracle と同じ順序のような create sequence はないようである。

22
23
4

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
22
23