ウィルキンソンの炭酸が抜けていく。

webクリエイターのイロハ

mysql メモ3 *テーブルを操作する。*

作業ユーザーの作り方。

決まり文句の以下をうつ。

mysql> grant all on blog_app.* to dbuser@localhost identified by 'パスワード'

 

 grant all on

は許可を与えなさいという意味

 

 

blog_app.*

このデータベース全部のテーブルに対して。

 

to dbuser@localhost

ローカルホストのdbuserに

 

identified by 'ぱすわーど'

このパスワードで、

 

という意味。

 

 

 そうすると、

Query OK, 0 rows affected (0.00 sec)

mysql>

と出ます。

 

 

一回出て

mysql> exit

Bye

[vagrant@localhost ~]$

 

今度は作業ユーザーでmysqlにログインします。

 

vagrant@localhost ~]$ mysql -u dbuser -p blog_app

 

dbuser(作業用ユーザー)でblog_appにはいる。

 

Enter password:

 

パスワード求められる

 

 

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 338

Server version: 5.5.46 MySQL Community Server (GPL) by Remi

 

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

 

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

 

 

これからはこのdbuser(作業用ユーザー)でblog_appの中をいじっていきます。

 

 

 

*テーブルを操作する。*

 

エディタで書いてからやったほうがいい。

 

create table user(
id int,
name varchar(255),
email varchar(255),
password char(32)
);

 

 

idというフィールド名を書いて、その中に入るデータの型を指定する。

intはintegerの略で整数

 

nameは varchar(255) 255文字くらいの文字列という意味。

 

パスワードはあらかじめ決まっている場合は char(32)っていう書き方になる。

それぞれのフィールドはこのようにフィールド名、データの型を1業で区切る。

 

次に続く場合は , (カンマ) を入れる。

 

エディタをコピペして mysqlにはると

 

mysql> create table user(

    -> id int,

    -> name varchar(255),

    -> email varchar(255),

    -> password char(32)

    -> );

Query OK, 0 rows affected (0.05 sec)

 

mysql>

 

Query OKと出るのでうまくいっています。

 

 

データベースと同じように一覧を見たいときは、

 

show tables; 

 

 

mysql> show tables;

+--------------------+

| Tables_in_blog_app |

+--------------------+

| user               |

+--------------------+

1 row in set (0.00 sec)

 

mysql>

 

 

このようにでます。

 

削除するときも一緒で

drop table users;

 

で削除できる。