0
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 5 years have passed since last update.

応用カリキュラム mysql 01

0
Posted at

仕事でこれでもか、というほどDB2触ってて良かった。復習楽チン。
但し、検索しかしてこなかった為、登録・更新・削除をしっかり覚える。

SQL

Structured Query Language はリレーショナルデータベースの操作を行うための言語。
SQLはISO(国際標準化機構)で規格化が行われている為、どんなリレーショナルデータベースに対しても、ほぼ同じ文法で操作ができる。

DDL

Data Definition Language データを定義するSQLの命令。
CREATE,ALTER,DROP データベースやテーブルの作成,更新,削除の3つ。

DML

Data Manipulation Language データを操作するSQLの命令。
INSERT,UPDATE,DELETE,SELECT データの登録,更新,削除,検索の4つ。

DMLのみ確認する。

-- データの登録
insert into テーブル名 values(1, 2);
-- 特定のカラムのみ
insert into テーブル名(カラム名1, カラム名2) values(1, 2);
-- テーブルにあるデータ全て表示
select * from テーブル名

デフォルト値を設定していないカラムの値は、データ入力時に何も入れなかった場合、NULLが入る。

-- データの更新
update テーブル名 set 変更内容 where 条件;
-- カラム1でレコードを指定してカラム2を更新
update テーブル名 set カラム1="aaa" where カラム2="bbb";
-- データの削除
delete from テーブル名 where 条件;
-- カラム1でレコードを指定してレコードを削除
delete from テーブル名 where カラム1="aaa";
-- データの検索 基本形(テーブルにあるデータ全て表示)
select * from テーブル名 where 条件
-- (式分解) select カラム
-- (式分解) from テーブル
-- * ワイルドカード 文字の代わりにできる。全て、の意味。
-- (式分解) where 条件
-- =,<=などの比較演算子が使える。文字列はダブルコーテーションで囲む。
where a=1
where a<=1
-- AND,OR,NOTなどの論理演算子が使える。
where a=1 and b=1
where a=1 or b=1
where not a=1
-- 範囲検索between (下限、上限を含む)
where カラム名 between 下限 and 上限
-- 1つのカラムに2つ以上の検索条件指定 in()
where カラム名 in (1, 2)
-- 文字列連結 concat()
concat(文字列1, 文字列2)
-- 別名付与 as 名前 (as を省略して名前のみを記述する書き方もできる)
select データ as 名前
-- 上記の場合、名前がカラム名として表示される
-- 重複レコードの除外表示 distinct カラム名
select distinct カラム名
-- レコードのグループ化 指定したカラムが同じ値を持つデータを1つのグループとしてまとめる
-- 挙動がdistinctに似ているが違うもの
-- クエリの末尾に書く
group by
-- レコードの数を数える count(カラム名)
select count(カラム名) -- NULLは除外して数える
select count(*) -- NULLも含めて数える
-- テーブルの結合 join
from テーブル名1 join テーブル名2 on テーブル名1.カラム名1 = テーブル2.カラム名2
from テーブル名1 a join テーブル名2 b on a.カラム名1 = b.カラム名2
-- サブクエリ
-- 検索結果を検索条件として検索結果を取得する。クエリのネスト。
-- select句でも、from句でも、where句でも使える
select * from table1 where column1 in (
  select column1 from table2 where column2 = "aaa"
)
0
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
0
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?