MySQLのテーブル作成(CREATE TABLE)のサンプル

MySQLのテーブル作成(CREATE TABLE)のサンプル。


シンプルなパターン。

CREATE TABLE sample (
  id INT
);

様々な属性を指定したパターン。

CREATE TABLE IF NOT EXISTS `sample` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'コメント',
  `c1` VARCHAR(255) NOT NULL DEFAULT 'hoge' UNIQUE COMMENT 'コメント'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT 'コメント';

様々な型を指定したパターン。※また、PRIMARY KEYとUNIQUEを別に指定している。

CREATE TABLE IF NOT EXISTS `sample` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'コメント',
  `c1` VARCHAR(255) NOT NULL DEFAULT 'hoge' COMMENT 'コメント',
  `c2` TEXT,
  `c3` TINYINT,
  `c4` SMALLINT,
  `c5` INT,
  `c6` BIGINT,
  `c7` DATETIME,
  `c8` TIMESTAMP,
  PRIMARY KEY(`id`),
  UNIQUE(`c1`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT 'コメント';

テーブルを作成し、追加すると、

insert into sample (c2, c3, c4, c5, c6, c7) values ('2', 3, 4, 5, 6, '0007-7-7');

下記のような感じで追加される。

select * FROM sample;
+----+------+------+------+------+------+------+---------------------+---------------------+
| id | c1   | c2   | c3   | c4   | c5   | c6   | c7                  | c8                  |
+----+------+------+------+------+------+------+---------------------+---------------------+
|  1 | hoge | 2    |    3 |    4 |    5 |    6 | 0007-07-07 00:00:00 | 2015-03-12 19:53:19 |
+----+------+------+------+------+------+------+---------------------+---------------------+

小文字版

create table sample (
  id int
);
create table if not exists `sample` (
  `id` int unsigned not null auto_increment primary key comment 'コメント',
  `c1` varchar(255) not null default 'hoge' unique comment 'コメント'
) engine=innodb default charset=utf8 comment 'コメント';
create table if not exists `sample` (
  `id` int unsigned not null auto_increment comment 'コメント',
  `c1` varchar(255) not null default 'hoge' comment 'コメント',
  `c2` text,
  `c3` tinyint,
  `c4` smallint,
  `c5` int,
  `c6` bigint,
  `c7` datetime,
  `c8` timestamp,
  primary key(`id`),
  unique(`c1`)
) engine=innodb default charset=utf8 comment 'コメント';

テーブルの確認

作成したテーブルは下記のような感じで確認できる

desc sample;
+-------+------------------+------+-----+-------------------+-----------------------------+
| Field | Type             | Null | Key | Default           | Extra                       |
+-------+------------------+------+-----+-------------------+-----------------------------+
| id    | int(10) unsigned | NO   | PRI | NULL              | auto_increment              |
| c1    | varchar(255)     | NO   | UNI | hoge              |                             |
| c2    | text             | YES  |     | NULL              |                             |
| c3    | tinyint(4)       | YES  |     | NULL              |                             |
| c4    | smallint(6)      | YES  |     | NULL              |                             |
| c5    | int(11)          | YES  |     | NULL              |                             |
| c6    | bigint(20)       | YES  |     | NULL              |                             |
| c7    | datetime         | YES  |     | NULL              |                             |
| c8    | timestamp        | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------+------------------+------+-----+-------------------+-----------------------------+
show full columns from sample;
+-------+------------------+-----------------+------+-----+-------------------+-----------------------------+---------------------------------+--------------+
| Field | Type             | Collation       | Null | Key | Default           | Extra                       | Privileges                      | Comment      |
+-------+------------------+-----------------+------+-----+-------------------+-----------------------------+---------------------------------+--------------+
| id    | int(10) unsigned | NULL            | NO   | PRI | NULL              | auto_increment              | select,insert,update,references | コメント     |
| c1    | varchar(255)     | utf8_general_ci | NO   | UNI | hoge              |                             | select,insert,update,references | コメント     |
| c2    | text             | utf8_general_ci | YES  |     | NULL              |                             | select,insert,update,references |              |
| c3    | tinyint(4)       | NULL            | YES  |     | NULL              |                             | select,insert,update,references |              |
| c4    | smallint(6)      | NULL            | YES  |     | NULL              |                             | select,insert,update,references |              |
| c5    | int(11)          | NULL            | YES  |     | NULL              |                             | select,insert,update,references |              |
| c6    | bigint(20)       | NULL            | YES  |     | NULL              |                             | select,insert,update,references |              |
| c7    | datetime         | NULL            | YES  |     | NULL              |                             | select,insert,update,references |              |
| c8    | timestamp        | NULL            | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | select,insert,update,references |              |
+-------+------------------+-----------------+------+-----+-------------------+-----------------------------+---------------------------------+--------------+
show create table sample\G
*************************** 1. row ***************************
       Table: sample
Create Table: CREATE TABLE `sample` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'コメント',
  `c1` varchar(255) NOT NULL DEFAULT 'hoge' COMMENT 'コメント',
  `c2` text,
  `c3` tinyint(4) DEFAULT NULL,
  `c4` smallint(6) DEFAULT NULL,
  `c5` int(11) DEFAULT NULL,
  `c6` bigint(20) DEFAULT NULL,
  `c7` datetime DEFAULT NULL,
  `c8` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `c1` (`c1`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='コメント'