1
1

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基礎知識(データベース、テーブル、ユーザー作成)

Last updated at Posted at 2020-12-05

MYSQLデータベース作成

SQLにログインする 

実行コマンド mysql -u root(rootユーザーでログインする場合)

xxxxxx@MacBook-Pro bin % mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.22 Homebrew

Copyright (c) 2000, 2020, 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.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

データベースについて

データベースの確認

mysql> SHOW DATABASES;

データベースの作成

mysql> create database データーベース名;
Query OK, 1 row affected (0.01 sec)

データベースの選択

上記のコマンドで作成したデータベースを選択する。

mysql> USE 使用するデーターベース名;
Database changed

データベース使用許可追加

mysql> GRANT ALL ON データベース名.* TO 'your_mysql_name'@'your_client_host';

データベースの削除

 drop database 削除するデータベース名;

テーブルについて

テーブルの確認

mysql> SHOW TABLES FROM データベース名;

テーブルの作成

()内には値が入る。

mysql> CREATE TABLE テーブルを作成するデータベース名.作成するテーブル名 (id int,name VARCHAR(20));

※VARCHAR =可変型の文字列を意味するデータ型 
※int   =整数のデータ型

テーブルの削除

drop table データベース名.削除するテーブル名;

テーブルの確認

DESCRIBE データベース名.テーブル名;

mysql>  DESCRIBE travel_apps.test;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int         | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

データの追加

INSERT INTO データベース名.テーブル名(xx,, yy)VALUES(aa, "bb");

追加した値の確認

ISERTで追加した値は以下の通り、確認ができる

mysql> SELECT * FROM データベース名.テーブル名;
+------+------+
| id   | name |
+------+------+
|    1 | test |
+------+------+

カラムについて

カラムの追加

mysql> ALTER TABLE データベース名.テーブル名 ADD カラム名 データ型;

以下の通りcolumnの追加を確認

mysql> DESCRIBE データベース名.テーブル名;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int         | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
| age   | int         | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

更新に関してはUPDATE, 削除に関してはdropを使う。

ユーザーについて

ユーザーの作成

create user 'ユーザー名'@'localhost又はIP' identified by 'パスワード';

ユーザー一覧の確認

mysql> SELECT Host, User FROM mysql.user;

ユーザーの削除

mysql> drop User ユーザー名;
1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?