Skip to content · ⁨コンテンツへスキップ⁩
Subjects · ⁨科目⁩
  • 1 Querying basics · ⁨クエリの基礎⁩
    1.1

    SELECTおよびFROM

    English

    A database 数据库 keeps data in tables 表. A query 查询 reads data with SELECT. SELECT * returns every column; FROM names the table.

    • Each row 行 is one record 记录. SQL keywords are written in UPPERCASE by habit.
    • A statement ends with a semicolon ;.
    日本語

    データベースはデータをテーブルに格納します。クエリはSELECTを使用してデータを読み取ります。SELECT *はすべてのカラムを返します;FROMはテーブル名を指定します。

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1, 'Mei', 88), (2, 'Sam', 71);
    SELECT * FROM student;
    
    テーブルはレコードを格納:各行は1つのレコード、各カラムは1つのフィールド
    テーブルはレコードを格納:各行は1つのレコード、各カラムは1つのフィールド
    • 各行は1つのレコードです。SQLキーワードは慣例として大文字で書きます。
    • ステートメントは半角ピリオド ; で終わります。
    1.2

    カラムの選択

    English

    List the columns 列 you want, separated by commas. Use AS to rename a column in the result (an alias 别名).

    A selected column can also be a calculation — pair it with AS to name the new column:

    • SELECT DISTINCT col removes duplicate values from the result.
    日本語

    希望するカラムをコンマで区切ってリストアップします。結果内でカラム名を変更するには AS を使用します(エイリアス)。

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1, 'Mei', 88), (2, 'Sam', 71);
    SELECT name, score AS mark FROM student;
    

    選択されたカラムは計算式也可以是; 新しいカラム名を付けるために AS とペアにします:

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1, 'Mei', 88), (2, 'Sam', 71);
    SELECT name, score + 5 AS bonus FROM student;
    
    • SELECT DISTINCT col は結果から重複値を削除します。
    1.3

    WHEREによるフィルタリング

    English

    WHERE keeps only the rows that match a condition 条件. Compare with =, <> (not equal), <, >, <=, >=. Put text in single quotes.

    • SQL runs the parts in this order: FROM → WHERE → SELECT.

    Common mistakes

    • Put text values in single quotes: WHERE name = 'Ann'; column names have no quotes.
    • SELECT * returns every column — name only the columns you need.
    • WHERE filters rows; it comes after FROM.
    日本語

    WHERE は条件に一致する行のみを保持します。=, <> (不等), <, >, <=, >= と比較してください。テキストはシングルクォートで囲みます。

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1, 'Mei', 88), (2, 'Sam', 71), (3, 'Ana', 95);
    SELECT name, score FROM student WHERE score >= 80;
    
    • SQLはこの順序で部分を実行します:FROM → WHERE → SELECT。

    ** 一般的なミス **

    • テキスト値はシングルクォートで囲みます:WHERE name = 'Ann'; カラム名には引用符なし。
    • SELECT * はすべてのカラムを返すので——必要なカラムのみ名前を指定してください。
    • WHERE は行をフィルタリングし、FROM の後に配置される。
  • 2 Filtering & logic · ⁨フィルタリングと論理⁩
    2.1

    AND, OR, NOT

    English

    Combine conditions with AND, OR, NOT. AND needs all sides true; OR needs any side true; NOT reverses one. Use brackets to set the precedence 优先级.

    日本語

    条件を AND, OR, NOT で組み合わせます。AND はすべての条件が真である必要があります; OR はいずれかの条件が真であれば十分です; NOT は1つの条件を反転させます。優先順位を設定するために括弧を使用してください。

    CREATE TABLE student (id INTEGER, name TEXT, form TEXT, score INTEGER);
    INSERT INTO student VALUES (1,'Mei','11A',88),(2,'Sam','11B',71),(3,'Ana','11A',95);
    SELECT name FROM student WHERE form = '11A' AND score >= 90;
    
    ANDは全て真が必要;ORはいずれか真;NOTは1つ反転
    ANDは全て真が必要;ORはいずれか真;NOTは1つ反転
    2.2

    LIKE, IN および BETWEEN

    English

    LIKE matches a text pattern 模式: % stands for any text and _ for one character — these are wildcards 通配符.

    Pattern Matches
    'M%' starts with M
    '%a' ends with a
    '%an%' contains "an"
    'M_i' M, then exactly one character, then i

    IN matches a set 集合 of values; BETWEEN matches a range 范围 (both ends included).

    日本語

    LIKE はテキストパターンに一致します:% は任意のテキストを表し、_ は1文字を表します——これらはワイルドカードです。

    CREATE TABLE student (id INTEGER, name TEXT, form TEXT, score INTEGER);
    INSERT INTO student VALUES (1,'Mei','11A',88),(2,'Sam','11B',71),(3,'Ana','11A',95);
    SELECT name FROM student WHERE name LIKE 'M%';        -- starts with M
    
    パターン 一致する内容
    'M%' Mで始まる
    '%a' aで終わる
    '%an%' "an" を含む
    'M_i' M、次にちょうど1文字、そしてi

    IN は値のセットに一致し; BETWEEN は範囲に一致します(両端を含みます)。

    CREATE TABLE student (id INTEGER, name TEXT, form TEXT, score INTEGER);
    INSERT INTO student VALUES (1,'Mei','11A',88),(2,'Sam','11B',71),(3,'Ana','11A',95);
    SELECT name, score FROM student
    WHERE form IN ('11A', '11C') AND score BETWEEN 80 AND 100;
    
    2.3

    NULL

    English

    NULL marks a missing value 缺失值 — nothing was stored in that cell. NULL is not 0 and not an empty string. A comparison with = or <> never matches it; test with IS NULL or IS NOT NULL.

    • WHERE score = NULL returns no rows at all — not even Sam's row.
    • A NULL in arithmetic gives NULL: score + 5 stays NULL for Sam.

    Common mistakes

    • Test for an empty value with IS NULL, never = NULL.
    • In LIKE, % matches any text and _ matches one character: 'A%' means "starts with A".
    • IN (1, 2, 3) is shorter than many ORs; BETWEEN a AND b includes both ends.
    日本語

    NULL は欠落している値を示します——そのセルには何も格納されていません。NULL は 0 でもなく、空白文字列でもありません。= や <> との比較は絶対に一致せず、IS NULL または IS NOT NULL でテストする必要があります。

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1, 'Mei', 88), (2, 'Sam', NULL), (3, 'Ana', 95);
    SELECT name FROM student WHERE score IS NULL;
    
    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1, 'Mei', 88), (2, 'Sam', NULL), (3, 'Ana', 95);
    SELECT name, score FROM student WHERE score IS NOT NULL;
    
    • WHERE score = NULL は結果に一行も返しません——サムさんの行さえも返しません。
    • 演算におけるNULLはNULLになります:score + 5 はサムさんに対してNULLのままです。

    ** 一般的なミス **

    • 空の値のテストには IS NULL を使用し、= NULL を絶対に使用しないでください。
    • LIKE において、% は任意のテキストに一致し、_ は1文字に一致します:'A%' は「Aで始まる」を意味します。
    • IN (1, 2, 3) は多くの OR より短い;BETWEEN a AND b は両端を含める。
  • 3 Sorting & limiting · ⁨ソートと制限⁩
    3.1

    ORDER BY および LIMIT

    English

    ORDER BY sorts 排序 the result rows. Add DESC for descending 降序 (high to low); the default is ascending 升序 (low to high).

    Sort by more than one column

    List several columns. A tie 平局 in the first column is broken by the next.

    LIMIT (top-N)

    LIMIT n keeps only the first n rows — pair it with ORDER BY for a top-N 前 N 名 list.

    • ORDER BY can also sort by an alias or a calculation: ORDER BY avg_score DESC.
    • Text sorts alphabetically: ORDER BY name runs A → Z.

    Common mistakes

    • ORDER BY sorts ascending by default; add DESC for descending.
    • ORDER BY comes near the end, after WHERE and GROUP BY.
    • LIMIT caps how many rows come back, but only after sorting.
    日本語

    ORDER BY は結果行をソートします。降順(高い順から低い順)にするには DESC を追加します;デフォルトは昇順(低い順から高い順)です。

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1,'Mei',88),(2,'Sam',71),(3,'Ana',95);
    SELECT name, score FROM student ORDER BY score DESC;
    

    複数のカラムでソート

    複数のカラムをリストアップします。最初のカラムでの同点の場合、次のカラムで破ります。

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1,'Mei',88),(2,'Sam',88),(3,'Ana',95);
    SELECT name, score FROM student ORDER BY score DESC, name ASC;
    

    LIMIT (トップN)

    LIMIT n は最初の n 行のみを保持します——トップNリストにするには ORDER BY とペアにします。

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1,'Mei',88),(2,'Sam',71),(3,'Ana',95);
    SELECT name, score FROM student ORDER BY score DESC LIMIT 2;
    
    • ORDER BY はエイリアスまたは計算式に基づいてソートすることもできます:ORDER BY avg_score DESC。
    • テキストはアルファベット順にソートされます:ORDER BY name は A → Z で実行します。

    ** 一般的なミス **

    • ORDER BY はデフォルトで昇順にソートします;降順にするには DESC を追加します。
    • ORDER BY は末尾近くに来るが、WHERE と GROUP BY の後である。
    • LIMIT は返される行の数を制限しますが、ソートした後に行われます。
    ORDER BYはソート;LIMITはソート後の最初のnを保持
    ORDER BYはソート;LIMITはソート後の最初のnを保持
  • 4 Aggregates & grouping · ⁨集計とグループ化⁩
    4.1

    集約関数

    English

    An aggregate function 聚合函数 turns many rows into one summary 汇总 value. Wrap an average in ROUND(x, 2) to tidy it.

    Function Gives
    COUNT(*) how many rows
    SUM(col) the total
    AVG(col) the mean average
    MIN(col) / MAX(col) the smallest / largest value
    日本語

    集約関数は多数の行を1つの要約値に変換します。平均値を ROUND(x, 2) で包むと整頓されます。

    関数 提供される値
    COUNT(*) 行数
    SUM(col) 合計
    AVG(col) 平均値
    MIN(col) / MAX(col) 最小値 / 最大値
    CREATE TABLE student (id INTEGER, name TEXT, form TEXT, score INTEGER);
    INSERT INTO student VALUES (1,'Mei','11A',88),(2,'Sam','11B',71),(3,'Ana','11A',95);
    SELECT COUNT(*), ROUND(AVG(score), 2), MAX(score) FROM student;
    
    4.2

    GROUP BY および HAVING

    English

    GROUP BY makes one summary row per group 组. HAVING filters those groups — it is like WHERE, but it runs after grouping.

    Whatever order you write them in, SQL always runs the clauses of a query in the same fixed order:

    Step Clause
    1 FROM (and any JOIN)
    2 WHERE — filter rows
    3 GROUP BY — form groups
    4 HAVING — filter groups
    5 SELECT — compute the output columns
    6 ORDER BY, then LIMIT

    Common mistakes

    • You cannot select a plain column beside an aggregate unless it is in GROUP BY.
    • Filter rows with WHERE (before grouping) and filter groups with HAVING (after).
    • COUNT(*) counts rows; COUNT(col) skips NULLs in that column.
    日本語

    GROUP BY はグループごとに1つの要約行を作成します。HAVING はこれらのグループをフィルタリングします——WHERE に似ていますが、グループ化後に実行されます。

    CREATE TABLE student (id INTEGER, name TEXT, form TEXT, score INTEGER);
    INSERT INTO student VALUES (1,'Mei','11A',88),(2,'Sam','11B',71),(3,'Ana','11A',95);
    SELECT form, COUNT(*) AS n, ROUND(AVG(score), 2) AS avg_score
    FROM student
    GROUP BY form
    HAVING COUNT(*) > 1;
    
    GROUP BYは値ごとに1つのバケットに行を集約;各バケットが1つの要約行になる
    GROUP BYは、各行を値ごとに1つのバケツにまとめます。各バケツが1つの集計行になります

    SQLクエリの節をどのように記述しても、SQLは常に同じ固定された順序で実行されます:

    ステップ 節
    1 FROM (および JOIN)
    2 WHERE — 行をフィルタリング
    3 GROUP BY — グループ形成
    4 HAVING — グループをフィルタリング
    5 SELECT — 出力列の計算
    6 ORDER BY、次に LIMIT

    ** 一般的なミス **

    • 集計関数と同じ行には、 GROUP BY に含まれていない plain(単純な)列を選択することはできません。
    • WHERE でグループ化前に行をフィルタリングし、 HAVING でグループ化後にグループをフィルタリングします。
    • COUNT(*) は行数をカウントします; COUNT(col) はその列のNULLをスキップします。
  • 5 Joins · ⁨ジョイン⁩
    5.1

    キーとリレーションシップ

    English

    A primary key 主键 uniquely names each row in a table. A foreign key 外键 in one table points to the primary key of another — that builds a relationship 关系 between them.

    日本語

    主キーはテーブル内の各行を一意に識別します。あるテーブルの外部キーは別のテーブルの主キーを指し示し、これにより両者の間にリレーションシップが構築されます。

    CREATE TABLE class (id INTEGER, name TEXT);
    CREATE TABLE student (id INTEGER, name TEXT, class_id INTEGER);
    INSERT INTO class VALUES (1, 'Maths'), (2, 'Art');
    INSERT INTO student VALUES (1, 'Mei', 1), (2, 'Sam', 2);
    SELECT * FROM student;
    
    5.2

    INNER JOIN

    English

    A join 连接 combines rows from two tables. INNER JOIN ... ON ... keeps rows where the keys match. A short alias 别名 (s, c) keeps the query readable.

    日本語

    JOINは2つのテーブルからの行を組み合わせます。 INNER JOIN ... ON ... はキーが一致する行のみを保持します。短いエイリアス( s, c )を使用すると、クエリが読みやすくなります。

    CREATE TABLE class (id INTEGER, name TEXT);
    CREATE TABLE student (id INTEGER, name TEXT, class_id INTEGER);
    INSERT INTO class VALUES (1, 'Maths'), (2, 'Art');
    INSERT INTO student VALUES (1, 'Mei', 1), (2, 'Sam', 2);
    SELECT s.name, c.name AS class
    FROM student s INNER JOIN class c ON s.class_id = c.id;
    
    INNER JOINは各外部キーを主キーにマッチさせ、一致した行を組み合わせます
    INNER JOINは各外部キーを主キーにマッチさせ、一致した行を組み合わせます
    5.3

    グループ化とのJOIN

    English

    Join first, then GROUP BY to summarise across the joined rows.

    日本語

    まずJOINを行い、その後 GROUP BY を使用して結合された行全体を集計します。

    CREATE TABLE class (id INTEGER, name TEXT);
    CREATE TABLE student (id INTEGER, name TEXT, class_id INTEGER);
    INSERT INTO class VALUES (1, 'Maths'), (2, 'Art');
    INSERT INTO student VALUES (1, 'Mei', 1), (2, 'Sam', 2), (3, 'Ana', 1);
    SELECT c.name AS class, COUNT(*) AS n
    FROM student s INNER JOIN class c ON s.class_id = c.id
    GROUP BY c.name;
    
    5.4

    LEFT JOIN

    English

    An INNER JOIN keeps only matched rows. A LEFT JOIN 左连接 keeps every row of the left (first) table; where there is no match, the right table's columns come back as NULL.

    • Sam has no class, but the row still appears — with class as NULL.
    • To find only the unmatched rows, add WHERE c.id IS NULL.

    Common mistakes

    • A join with no ON condition pairs every row with every row (a cross join).
    • Match the foreign key to the primary key: ON orders.customer_id = customers.id.
    • An INNER JOIN drops rows that have no match on the other side; use a LEFT JOIN to keep them.
    日本語

    INNER JOINは一致する行のみを保持します。 LEFT JOIN は左側(最初の)テーブルのすべての行を保持し、一致しない場合は右側のテーブルの列は NULL として返されます。

    CREATE TABLE class (id INTEGER, name TEXT);
    CREATE TABLE student (id INTEGER, name TEXT, class_id INTEGER);
    INSERT INTO class VALUES (1, 'Maths'), (2, 'Art');
    INSERT INTO student VALUES (1, 'Mei', 1), (2, 'Sam', NULL);
    SELECT s.name, c.name AS class
    FROM student s LEFT JOIN class c ON s.class_id = c.id;
    
    • Samはクラスを持っていませんが、行は依然として表示され、 class の値は NULL となります。
    • 一致しない行のみを見つけるには、 WHERE c.id IS NULL を追加します。

    ** 一般的なミス **

    • ON の条件がないJOINでは、すべての行がすべての行とペアになります(クロスジョイン)。
    • 外部キーを主キーにマッチさせる: ON orders.customer_id = customers.id 。
    • 他方側に一致する行がない行はINNER JOINで除外されます。それらを残すにはLEFT JOINを使用します。
  • 6 Modifying data · ⁨データの修正⁩
    6.1

    INSERT

    English

    INSERT INTO ... VALUES ... adds new rows 行. Name the columns, then give the values in the same order. (Each block below ends with a SELECT so you can see the result.)

    • INSERT INTO t VALUES (...), (...); adds several rows in one statement.
    日本語

    INSERT INTO ... VALUES ... adds new rows 行. Name the columns, then give the values in the same order. (Each block below ends with a SELECT so you can see the result.)

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student (id, name, score) VALUES (1, 'Mei', 88);
    INSERT INTO student VALUES (2, 'Sam', 71);
    SELECT * FROM student;
    
    • INSERT INTO t VALUES (...), (...); adds several rows in one statement.
    INSERT adds a new row to a table
    INSERT adds a new row to a table
    6.2

    UPDATE

    UPDATE ... SET ... WHERE ... changes existing rows. Always add WHERE, or every row changes.

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1, 'Mei', 88), (2, 'Sam', 71);
    UPDATE student SET score = 75 WHERE name = 'Sam';
    SELECT * FROM student;
    
    6.3

    DELETE

    English

    DELETE FROM ... WHERE ... removes rows. Without WHERE it empties the whole table 表.

    Common mistakes

    • UPDATE and DELETE without a WHERE change every row — always add the WHERE.
    • In INSERT, the values must line up with the column list in order and type.
    • Test a risky DELETE first as a SELECT with the same WHERE.
    日本語

    DELETE FROM ... WHERE ... removes rows. Without WHERE it empties the whole table 表.

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1, 'Mei', 88), (2, 'Sam', 71);
    DELETE FROM student WHERE score < 80;
    SELECT * FROM student;
    

    Common mistakes

    • UPDATE and DELETE without a WHERE change every row — always add the WHERE.
    • In INSERT, the values must line up with the column list in order and type.
    • Test a risky DELETE first as a SELECT with the same WHERE.
  • 7 Defining tables · ⁨テーブルの定義⁩
    7.1

    CREATE TABLE & データ型

    English

    CREATE TABLE defines a table's schema 表结构: the column names and their data types 数据类型. The main SQLite types are INTEGER, TEXT, and REAL (a decimal number).

    日本語

    CREATE TABLE はテーブルのスキーマ(列名とそのデータ型)を定義します。主要なSQLiteのデータ型は INTEGER 、 TEXT 、および REAL (小数点付き数字)です。

    CREATE TABLE student (id INTEGER, name TEXT, score REAL);
    INSERT INTO student VALUES (1, 'Mei', 88.5);
    SELECT * FROM student;
    
    CREATE TABLEは列とその型を指定します
    CREATE TABLEは列とその型を指定します
    7.2

    キーと制約

    English

    A constraint 约束 is a rule on a column: PRIMARY KEY (a unique id), NOT NULL (must have a value), UNIQUE, and DEFAULT (a fallback value).

    Declare a foreign key with REFERENCES — it records that the column points at another table's primary key:

    • The long form is FOREIGN KEY (class_id) REFERENCES class(id) on its own line.
    日本語

    制約とは列に対するルールです: PRIMARY KEY (一意ID)、 NOT NULL (値必須)、 UNIQUE 、 DEFAULT (デフォルト値)。

    CREATE TABLE student (
        id INTEGER PRIMARY KEY,
        name TEXT NOT NULL,
        score INTEGER DEFAULT 0
    );
    INSERT INTO student (id, name) VALUES (1, 'Mei');
    SELECT * FROM student;
    

    外部キーを REFERENCES で宣言します。これはその列が他のテーブルの主キーを指していることを記録します:

    CREATE TABLE class (id INTEGER PRIMARY KEY, name TEXT);
    CREATE TABLE student (
        id INTEGER PRIMARY KEY,
        name TEXT NOT NULL,
        class_id INTEGER REFERENCES class(id)
    );
    INSERT INTO class VALUES (1, 'Maths');
    INSERT INTO student VALUES (1, 'Mei', 1);
    SELECT s.name, c.name AS class
    FROM student s INNER JOIN class c ON s.class_id = c.id;
    
    • 長い形式は単独の行に FOREIGN KEY (class_id) REFERENCES class(id) を書きます。
    7.3

    ALTER TABLE

    English

    ALTER TABLE ... ADD COLUMN ... changes the schema of a table that already exists. A DEFAULT fills the new column in the old rows.

    • ALTER TABLE student RENAME TO pupil; renames the whole table.
    • DROP TABLE student; deletes the table completely — structure and data.

    Common mistakes

    • Every column needs a data type (e.g. INTEGER, TEXT).
    • A PRIMARY KEY must be unique and cannot be NULL.
    • A foreign key value must exist in the table it points to.
    日本語

    ALTER TABLE ... ADD COLUMN ... は既に存在するテーブルのスキーマを変更します。 DEFAULT は古い行の新しい列を埋めます。

    CREATE TABLE student (id INTEGER, name TEXT);
    INSERT INTO student VALUES (1, 'Mei');
    ALTER TABLE student ADD COLUMN score INTEGER DEFAULT 0;
    SELECT * FROM student;
    
    • ALTER TABLE student RENAME TO pupil; はテーブル全体の名前を変更します。
    • DROP TABLE student; はテーブルを完全に削除します——構造とデータです。

    ** 一般的なミス **

    • 各カラムにはデータ型が必要です(例:INTEGER, TEXT)。
    • 主キーは一意であり、NULLであることはできません。
    • 外部キーの値は、それが指すテーブル内に存在する必要があります。
  • 8 Database design · ⁨データベース設計⁩
    8.1

    リレーションシップとER図

    English

    Tables connect through relationships 关系. One-to-many 一对多 is the most common: one class has many students. Many-to-many 多对多 needs a join table 连接表 in the middle. An entity-relationship diagram 实体关系图 (ER diagram) draws each entity 实体 as a box and each relationship as a line.

    Relationship Example
    one-to-one a person and their passport
    one-to-many a class and its students
    many-to-many students and clubs

    The join table holds one row per link. Here Mei is in two clubs, and Chess has two members:

    日本語

    テーブルはリレーションシップを通じて接続されます。One-to-many(1対多)が最も一般的です:1つのクラスに多くの学生がいる場合です。Many-to-many(多対多)には中間にジョイントテーブルが必要です。エンティティ・リレーションシップ図(ER図)は、各エンティティをボックスで、各リレーションシップを線で描きます。

    リレーションシップ 例
    one-to-one(1対1) 人とそのパスポート
    one-to-many(1対多) クラスとその学生
    many-to-many(多対多) 学生とクラブ
    「many」側が外部キーを持ち、many-to-manyリンクにはジョイントテーブルが必要です
    「many」側が外部キーを持ち、many-to-manyリンクにはジョイントテーブルが必要です

    ジョイントテーブルはリンクごとに1行を持ちます。ここではMeiは2つのクラブに参加しており、Chessには2人のメンバーがいます:

    CREATE TABLE student (id INTEGER PRIMARY KEY, name TEXT);
    CREATE TABLE club (id INTEGER PRIMARY KEY, name TEXT);
    CREATE TABLE membership (student_id INTEGER, club_id INTEGER);
    INSERT INTO student VALUES (1, 'Mei'), (2, 'Sam');
    INSERT INTO club VALUES (1, 'Chess'), (2, 'Art');
    INSERT INTO membership VALUES (1, 1), (1, 2), (2, 1);
    SELECT s.name, c.name AS club
    FROM membership m
    INNER JOIN student s ON m.student_id = s.id
    INNER JOIN club c ON m.club_id = c.id;
    
    8.2

    正規化

    English

    Normalisation 范式化 organises tables to avoid redundancy 冗余 (the same data repeated) and the update mistakes it causes. The first three normal forms 范式:

    • 1NF: every cell holds one atomic 原子 value — no lists inside a cell.
    • 2NF: no column depends on only part of a composite key 复合主键.
    • 3NF: no column depends on another non-key column.
    Unnormalised (bad) Normalised (better)
    student(name, club1, club2) student(name) + membership(student, club)

    Common mistakes

    • Split repeating groups into their own table (normalisation) instead of many similar columns.
    • Each table should describe ONE kind of thing.
    • Link tables with a foreign key that points to another table's primary key.
    日本語

    正規化(Normalisation)は、重複(同じデータの繰り返し)とそれに伴う更新ミスを避けるためにテーブルを整理するもの。最初の3つの正規形:

    • 1NF: 各セルには1つの原子値が含まれる——セル内にリストは含めない。
    • 2NF: どの列も複合キーの一部のみに依存してはいけない。
    • 3NF: どの列も非キー列に依存してはいけない。
    未正規化(悪い) 正規化済み(良い)
    student(name, club1, club2) student(name) + membership(student, club)

    ** 一般的なミス **

    • 繰り返しのグループを多数の類似した列にするのではなく、独自のテーブルに分割する(正規化)。
    • 各テーブルはONE(1つ) kind of thing(種類のもの)だけを説明すべきです。
    • 他方のテーブルの主キーを指す外部キーを使ってテーブルを連結します。

Log in or create account · ⁨ログインまたはアカウント作成⁩

IGCSE, A-Level & AP