LoginSignup
1
0

More than 3 years have passed since last update.

知識0でMySQLをはじめたので、つまづいたところを含めた備忘録。
昨日と実際に実行して動いた構文をただ連ねるだけです。
オタクなので、テンション上げるためにヒプノシスマイクのキャラ名が出てきます。

・まずデータベースを作る
create datebase データベース名;

mysql> create database test1907;

・使いたいデータベースを指定する
use データベース名;

mysql> use test1907;

・テーブルの作成
create table テーブル名(
列名 文字型1,
列名 文字型1,
・・・
);

mysql> create table table1(
    -> no int,
    -> name varchar(20)
    -> );

・値の追加
inset into テーブル名(列名,列名) values(値,値)
※自分の入れたい値だけ追加することも可能
※値が入ってないセルがでた場合は「NULL」になる

mysql> insert into table1(no,name) values(1,'伊奘冉一二三');
mysql> insert into table1(no) values(2);

・データベース中のデータの取り出し
select * from データベース名

mysql> select * from table1;
+------+--------------------+
| no   | name               |
+------+--------------------+
|    1 | 伊奘冉一二三       |
|    2 | NULL               |
+------+--------------------+

.テーブル構造の参照
desc テーブル名;

mysql> desc table1;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| no    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+

特定の列を指定して取り出す
select 列名 from テーブル名
※複数列指定する場合はカンマで繋げる

mysql> select no from table1;
+------+
| no   |
+------+
|    1 |
|    2 |
+------+

・行を絞り込む
select 列名 from テーブル名 where 条件式 
※「*」は、テーブル全体を指定する書き方
※= 等しい、<> 等しくない

mysql> select * from table1 where no=3;
+------+-----------------+
| no   | name            |
+------+-----------------+
|    3 | 観音坂独歩      |
+------+-----------------+
1 row in set (0.00 sec)

mysql> select * from table1 where no<>2;
+------+--------------------+
| no   | name               |
+------+--------------------+
|    1 | 伊奘冉一二三       |
|    3 | 観音坂独歩         |
+------+--------------------+
2 rows in set (0.00 sec)

・NULL値の検索
select 列名 from テーブル名 wehere 列名 is null

mysql> select * from table1 where name is null;
+------+------+
| no   | name |
+------+------+
|    2 | NULL |
+------+------+
1 row in set (0.00 sec)
1
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
1
0