2
2

【データベース勉強中】SQL初心者チートシート

Posted at

はじめに

SQL勉強中に使ったSQLチートシート。

データベース系

データベース新規作成

create database データベース名;

データベース確認

show databases;

データベース削除

drop database テーブル名;

テーブル系

対象のデータベースにあるすべてのテーブルを確認

show tables from 対象のデータベース;

テーブル作成 (CREATE)

create table [テーブル名](
    [カラム] [型] AUTO_INCREMENT PRIMARY KEY,
    [カラム] [型] DEFAULT [デフォルト値] comment [コメント],
    [カラム] [型],
    ...
);

テーブルの項目一覧

①すべて
show full columns from [テーブル名];
②FIELDS
show fields from [テーブル名];
③DESC
desc テーブル名;

テーブル名を変更する

alter table [現在のテーブル名] rename to [新しいテーブル名]

テーブルの項目を変更する

主キーを設定する

alter table [テーブル名] add primary key([カラム名]);

オートインクリメントを設定する

alter table [テーブル名] change [カラム名] [変更するカラム名] [データ型] AUTO_INCREMENT;

カラム名を変更する

alter table [テーブル名] rename [カラム名] to [変更するカラム名];

デフォルト値の設定を追加する

alter table [テーブル名] alter [カラム名] [変更するカラム名] set default [デフォルト値];
デフォルト値の設定を削除する
alter table [テーブル名] alter [カラム名] [変更するカラム名] drop default;

コメント設定を追加する

alter table  [テーブル名] modify [カラム名] comment [コメント];
コメント設定を削除する
alter table  [テーブル名] drop comment [カラム名] ;

テーブルの中身をすべて確認

select * from テーブル名;

テーブル削除 (DROP)

drop table テーブル名;

データ系

データ追加

insert into テーブル名 (列名1, 列名2,...) values (値1, 値2,...);

データ削除

delete from テーブル名 where 指定;

最後に

このページは新しいSQLを使ったら随時更新する。

2
2
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
2
2