0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

MySQL⑦ 使用できる値を制限する

Posted at

使用できる値を制限する

1つだけ選択できる enum

カラムを作成する カラム名 enum(値①, 値②, 値③)

create table users (
id int unsigned primary key auto_increment,
name varchar(20),
score float,
rank enum('gold', 'silver', 'bronze')
);

値を入力

insert into users (name, score, rank) values ('taguchi', 5.8, 'silver');
insert into users (name, score, rank) values ('fkoji', 8.2, 'gold');
insert into users (name, score, rank) values ('dotinstall', 6.1, 'red');  <- これはrankの値は保存されない

内部的に値に1番スタートの連番が振られている

enum(①, ②, ③)

select * from users where rank = 'silver';
select * from users where rank = 2;

上記は同じ意味になる

複数選択できる set

カラムを作成する カラム名 set(値①, 値②, 値③)

create table users (
id int unsigned primary key auto_increment,
name varchar(20),
score float,
rank set('gold', 'silver', 'bronze')
);

値を入力

insert into users (name, score, rank) values ('taguchi', 5.8, 'silver, gold');
insert into users (name, score, rank) values ('fkoji', 8.2, 'gold, bronze');
insert into users (name, score, rank) values ('dotinstall', 6.1, 'red');  <- これはrankの値は保存されない

内部的に値に番号が振られている

enum(2の0条, 2の1条, 2の4条)

select * from users where rank = 'gold,silver';
select * from users where rank = 3;  <- これは、2の0条の1 と 2の1条の2 を足した数

上記は同じ意味になる

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?