Upgrade to Pro — share decks privately, control downloads, hide ads and more …

データベース基礎

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

 データベース基礎

Avatar for taramanji

taramanji

July 05, 2024
Tweet

More Decks by taramanji

Other Decks in Technology

Transcript

  1. こんなテーブルで考えます User 整数型 id (主キー) ⽂字列型 name (⾮null) ⽂字列型 email

    (⾮null) 主キーとは 重複禁⽌ Null禁⽌の制約があるカラム
  2. テーブルを作る CREATE TABLE IF NOT EXISTS User ( id INT

    AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL ) User 整数型 id (主キー) ⽂字列型 name (⾮null) ⽂字列型 email (⾮null) さっき説明したUserTBを作成する VARCHER(255)とは255 バイトまでの⽂字列を可変⻑フィールドに保管 AUTO_INCREMENTとはidの値を⾃動で1ずつ増やして格納するようにすること
  3. データを取り出す SELECT id FROM User; User 整数型 id (主キー) ⽂字列型

    name (⾮null) ⽂字列型 email (⾮null) UserTBからidカラムを取り出す SELECT * FROM User; UserTBから全カラムを取り出す
  4. データを絞ってとる User 整数型 id (主キー) ⽂字列型 name (⾮null) ⽂字列型 email

    (⾮null) SELECT * FROM User WHERE id = 1; UserTBからid==1のデータの全カラムを取り出す SELECT * FROM User WHERE name = “John” AND email = “*@gmail.com” ; UserTBからname=“John”かつemailが@gmail.comで終わるデータの全カラムを取り出す
  5. データを格納する User 整数型 id (主キー) ⽂字列型 name (⾮null) ⽂字列型 email

    (⾮null) INSERT INTO User (name, email) VALUES("John Doe", "[email protected]"); UserTBにデータを⼊れる