Help us understand the problem. What is going on with this article?

MySQLコマンド(テーブル作成関連)

はじめに

前回MySQLコマンド(基本)について紹介しました。
今回は実際にテーブル作成に必要なコマンドを紹介していきます。

テーブル作成関連のMySQLコマンド

・テーブルの作成

mysql > CREATE TABLE [テーブル名] (
  [カラム名] [データ型] [オプション]
); 

//例
mysql > CREATE TABLE `users` (
          `id` int NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT "ID",
          `name` VARCHAR(50) NOT NULL COMMENT "名前",
          `mail` VARCHAR(100) NOT NULL COMMENT "メールアドレス",
          `created_at` datetime DEFAULT NULL COMMENT "登録日",
); 

・テーブル一覧の表示

mysql > show tables;

・テーブル名の変更

mysql > ALTER TABLE [旧テーブル名] RENAME [新テーブル名];

・テーブルの削除

mysql > DROP TABLE [テーブル名];

・テーブルにカラムの追加作成

mysql > ALTER TABLE [テーブル名] ADD [カラム名] [型] [必要であればオプション];

・テーブル設計の確認

mysql > show columns from [テーブル名];

//もしくは
mysql > desc [テーブル名];

・カラムの変更

mysql > ALTER TABLE テーブル名 ALTER COLUMN [カラム名] [型];

・カラムの削除

mysql > ALTER TABLE テーブル名 DROP COLUMN [カラム名];

・レコードの追加作成

mysql > INSERT INTO [テーブル名] [カラム名] VALUES [値];

//例
mysql > INSERT INTO users (name, mail, created_at,)
          VALUES ("Yamada Takumi", "takumi@hoge.com", now());

・レコードの更新

mysql > UPDATE [テーブル名] SET [カラム名]=[値] [条件式];

・レコードの削除

//全レコード削除

mysql > DELETE FROM [テーブル名];

//一部レコード削除
mysql > DELETE FROM [テーブル名] WHERE [条件式];

おわりに

ここまで使えれば一通りのMySQLコマンドは困らないと思います。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした