1
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 1 year has passed since last update.

MySQLでデータベースを作ってテーブルにデータを入れて取得してみた

Posted at

概要

今日もデータベースの勉強ということで、MySQLで遊んでみました。
インストールして、データベースを作って、テーブルを作って、データを入れて、取得するところまでやってみました。
以下のページを参考にしました。
https://www.javadrive.jp/mysql/

環境構築

以下のコマンドを実行しました。

$ sudo apt install mysql-server

rootでログイン

以下のコマンドを実行しました。

$ sudo mysql

ユーザーを作りました。create userを実行しました。

mysql> CREATE USER 'user1'@'localhost' IDENTIFIED BY '********';
Query OK, 0 rows affected (0.10 sec)

データベースを作りました。create databaseを実行しました。

mysql> CREATE DATABASE mydb;
Query OK, 1 row affected (0.09 sec)

user1に権限を与えました。grantを実行しました。

mysql> GRANT ALL ON mydb.* TO 'user1'@'localhost';
Query OK, 0 rows affected (0.11 sec)

user1でログイン

以下のコマンドを実行しました。

$ mysql -u user1 -p

データベースを切り替えました。useを実行しました。

mysql> use mydb;
Database changed

テーブルを作りました。create tableを実行しました。

mysql> create table animal (id int, name varchar(10));
Query OK, 0 rows affected (0.52 sec)

データを入れました。insertを実行しました。

mysql> insert into animal (id, name) values (11, 'monkey');
Query OK, 1 row affected (0.09 sec)

データを取得してみました。selectを実行しました。

mysql> select * from animal;
+------+--------+
| id   | name   |
+------+--------+
|   11 | monkey |
+------+--------+
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?