Compare commits

..
2 Commits
Author SHA1 Message Date
maonanhai 63464bcb52 lesson2 added 2026-09-17 01:37:16 +03:00
maonanhai 096c134bb8 refactoring 2026-09-17 01:08:23 +03:00
7 changed files with 268 additions and 91 deletions
+1
View File
@@ -3,5 +3,6 @@
* **Курс:** base-sql
## Список работ
* [Лабораторная работа №0](./works/lab-00)
* [Лабораторная работа №1](./works/lab-01)
* [Лабораторная работа №2](./works/lab-02)
+4
View File
@@ -0,0 +1,4 @@
# Лабораторная работа №0
* **Студент:** Закиров Алим Альбертович
* **Вариант / Карточка:** Первичные и внешние ключи, ограничения целостности базы данных «Pokémon TCG»
+78
View File
@@ -0,0 +1,78 @@
```csharp
Table card {
card_id integer [primary key]
card_name text
card_stage text
card_hp integer
card_num_in_pokedex integer
card_type text
card_height integer
card_weight integer
card_retreat_cost integer
card_release_year date
card_set_id integer [not null]
attack_id integer [not null]
element_type_id integer [not null]
illustrator_id integer [not null]
}
Table card_set {
card_set_id integer [primary key]
card_set_name text
card_set_total_quantity integer
card_set_release_year date
}
Table attack {
attack_id integer [primary key]
attack_name text
attack_base_damage integer
attack_effect text
}
Table element_type {
element_type_id integer [primary key]
element_type_name text
}
Table illustrator {
illustrator_id integer [primary key]
illustrator_name text
}
Ref: card_set.card_set_id < card.card_set_id
Ref: attack.attack_id < card.attack_id
Ref: element_type.element_type_id < card.element_type_id
Ref: illustrator.illustrator_id < card.illustrator_id
// Записи(значения сделаны с помощью ИИ):
Records card_set(card_set_id, card_set_name, card_set_total_quantity, card_set_release_year) {
0, 'Base Set', 102, '1999-01-09'
1, 'Jungle', 64, '1999-06-16'
2, 'Fossil', 62, '1999-10-10'
}
Records attack(attack_id, attack_name, attack_base_damage, attack_effect) {
0, 'Thunder Shock', 10, 'Flip a coin. If tails, this attack does nothing.'
1, 'Vine Whip', 35, null
2, 'Ember', 30, 'Discard 1 Energy card attached to this Pokemon.'
}
Records element_type(element_type_id, element_type_name) {
0, 'Lightning'
1, 'Grass'
2, 'Fire'
}
Records illustrator(illustrator_id, illustrator_name) {
0, 'Mitsuhiro Arita'
1, 'Ken Sugimori'
2, 'Keiji Kinebuchi'
}
Records card(card_id, card_name, card_stage, card_hp, card_num_in_pokedex, card_type, card_height, card_weight, card_retreat_cost, card_release_year, card_set_id, attack_id, element_type_id, illustrator_id) {
0, 'Pikachu', 'Basic', 40, 25, 'Lightning', 4, 60, 1, '1999-01-09', 0, 0, 0, 0
1, 'Bulbasaur', 'Basic', 40, 1, 'Grass', 7, 69, 1, '1999-06-16', 1, 1, 1, 1
2, 'Charmander', 'Basic', 50, 4, 'Fire', 6, 85, 1, '1999-10-10', 2, 2, 2, 2
}
+1 -83
View File
@@ -1,86 +1,4 @@
# Лабораторная работа №1
* **Студент:** Закиров Алим Альбертович
* **Вариант / Карточка:** Первичные и внешние ключи, ограничения целостности базы данных «Pokémon TCG»
---
## Ключевой код
```csharp
Table card {
card_id integer [primary key]
card_name text
card_stage text
card_hp integer
card_num_in_pokedex integer
card_type text
card_height integer
card_weight integer
card_retreat_cost integer
card_release_year date
card_set_id integer [not null]
attack_id integer [not null]
element_type_id integer [not null]
illustrator_id integer [not null]
}
Table card_set {
card_set_id integer [primary key]
card_set_name text
card_set_total_quantity integer
card_set_release_year date
}
Table attack {
attack_id integer [primary key]
attack_name text
attack_base_damage integer
attack_effect text
}
Table element_type {
element_type_id integer [primary key]
element_type_name text
}
Table illustrator {
illustrator_id integer [primary key]
illustrator_name text
}
Ref: card_set.card_set_id < card.card_set_id
Ref: attack.attack_id < card.attack_id
Ref: element_type.element_type_id < card.element_type_id
Ref: illustrator.illustrator_id < card.illustrator_id
// Записи(значения сделаны с помощью ИИ):
Records card_set(card_set_id, card_set_name, card_set_total_quantity, card_set_release_year) {
0, 'Base Set', 102, '1999-01-09'
1, 'Jungle', 64, '1999-06-16'
2, 'Fossil', 62, '1999-10-10'
}
Records attack(attack_id, attack_name, attack_base_damage, attack_effect) {
0, 'Thunder Shock', 10, 'Flip a coin. If tails, this attack does nothing.'
1, 'Vine Whip', 35, null
2, 'Ember', 30, 'Discard 1 Energy card attached to this Pokemon.'
}
Records element_type(element_type_id, element_type_name) {
0, 'Lightning'
1, 'Grass'
2, 'Fire'
}
Records illustrator(illustrator_id, illustrator_name) {
0, 'Mitsuhiro Arita'
1, 'Ken Sugimori'
2, 'Keiji Kinebuchi'
}
Records card(card_id, card_name, card_stage, card_hp, card_num_in_pokedex, card_type, card_height, card_weight, card_retreat_cost, card_release_year, card_set_id, attack_id, element_type_id, illustrator_id) {
0, 'Pikachu', 'Basic', 40, 25, 'Lightning', 4, 60, 1, '1999-01-09', 0, 0, 0, 0
1, 'Bulbasaur', 'Basic', 40, 1, 'Grass', 7, 69, 1, '1999-06-16', 1, 1, 1, 1
2, 'Charmander', 'Basic', 50, 4, 'Fire', 6, 85, 1, '1999-10-10', 2, 2, 2, 2
}
* **Вариант / Карточка:** Проектирование ER-диаграммы базы данных «Pokémon TCG» в dbdiagram.io
+1 -6
View File
@@ -1,9 +1,4 @@
# Лабораторная работа №2
* **Студент:** Закиров Алим Альбертович
* **Вариант / Карточка:** Проектирование связей "Многие-ко-Многим"
---
## Ключевой код
```csharp
* **Вариант / Карточка:** Реализация базы данных «Pokémon TCG» в SQL DDL и рефакторинг под типы карт
+181
View File
@@ -0,0 +1,181 @@
-- 1. СПРАВОЧНИКИ
CREATE TABLE "card_set" (
"card_set_id" INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
"set_name" TEXT NOT NULL,
"set_code" TEXT UNIQUE NOT NULL,
"set_series" TEXT NOT NULL,
"set_total_cards" INT NOT NULL CHECK (set_total_cards > 0),
"set_release_date" DATE NOT NULL,
"set_logo_url" TEXT,
"set_symbol_url" TEXT
);
CREATE TABLE "energy_type" (
"energy_type_id" INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
"energy_type_name" TEXT UNIQUE NOT NULL,
"energy_type_icon_url" TEXT
);
CREATE TABLE "pokemon_sub_type" (
"pokemon_sub_type_id" INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
"sub_type_name" TEXT UNIQUE NOT NULL,
"sub_type_default_prize_count" INT NOT NULL CHECK (sub_type_default_prize_count >= 1),
"sub_type_global_rule_text" TEXT NOT NULL
);
CREATE TABLE "ability_type" (
"ability_type_id" INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
"ability_type_name" TEXT UNIQUE NOT NULL
);
CREATE TABLE "attack_name" (
"attack_name_id" INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
"attack_name_title" TEXT UNIQUE NOT NULL
);
CREATE TABLE "trainer_printed_type" (
"trainer_printed_type_id" INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
"printed_type_name" TEXT UNIQUE NOT NULL
);
CREATE TABLE "game_mechanic" (
"game_mechanic_id" INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
"mechanic_name" TEXT UNIQUE NOT NULL
);
-- 2. ГЛОБАЛЬНЫЙ ПОКЕДЕКС
CREATE TABLE "pokemon_species" (
"species_pokedex_number" INT PRIMARY KEY,
"species_eng_name" TEXT UNIQUE,
"species_generation" INT NOT NULL,
"species_height_m" NUMERIC(5,2) NOT NULL CHECK (species_height_m > 0),
"species_weight_kg" NUMERIC(5,2) NOT NULL CHECK (species_weight_kg > 0)
);
CREATE TABLE "pokedex_entry" (
"entry_id" INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
"entry_pokedex_number" INT NOT NULL,
"entry_source_game" TEXT NOT NULL,
"entry_description" TEXT NOT NULL
);
-- 3. РОДИТЕЛЬСКАЯ ТАБЛИЦА КАРТ
CREATE TABLE "card" (
"card_id" INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
"card_number" TEXT UNIQUE NOT NULL,
"card_set_id" INT NOT NULL,
"card_rarity" TEXT NOT NULL,
"card_type" TEXT NOT NULL CHECK (card_type IN ('pokemon', 'energy', 'trainer')),
"card_image_url" TEXT
);
-- 4. ДОЧЕРНИЕ ТАБЛИЦЫ ПО ТИПАМ КАРТ
CREATE TABLE "pokemon_card" (
"pokemon_card_id" INT PRIMARY KEY,
"pokemon_pokedex_number" INT NOT NULL,
"pokemon_pokedex_entry_id" INT,
"pokemon_hp" INT NOT NULL CHECK (pokemon_hp > 0),
"pokemon_stage" TEXT NOT NULL,
"pokemon_evolves_from" TEXT,
"pokemon_retreat_cost" INT NOT NULL,
"pokemon_sub_type_id" INT NOT NULL,
"pokemon_category" TEXT NOT NULL,
"pokemon_printed_height" TEXT NOT NULL,
"pokemon_printed_weight" TEXT NOT NULL,
"energy_type_id" INT NOT NULL
);
CREATE TABLE "trainer_card" (
"trainer_card_id" INT PRIMARY KEY,
"trainer_printed_type_id" INT NOT NULL,
"trainer_effect_text" TEXT NOT NULL,
"trainer_is_ace_spec" BOOL NOT NULL
);
CREATE TABLE "energy_card" (
"energy_card_id" INT PRIMARY KEY,
"energy_is_special" BOOL NOT NULL,
"energy_main_type_id" INT,
"energy_effect_text" TEXT
);
-- 5. СВЯЗИ ХАРАКТЕРИСТИК ПОКЕМОНОВ И ТРЕНЕРОВ
CREATE TABLE "pokemon_ability" (
"ability_pokemon_card_id" INT PRIMARY KEY,
"ability_type_id" INT NOT NULL,
"ability_name" TEXT NOT NULL,
"ability_effect_text" TEXT NOT NULL
);
CREATE TABLE "pokemon_attack_link" (
"link_id" INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
"link_pokemon_card_id" INT NOT NULL,
"link_attack_name_id" INT NOT NULL,
"link_damage_value" TEXT NOT NULL,
"link_effect_text" TEXT
);
CREATE TABLE "attack_energy_cost" (
"cost_id" INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
"cost_link_id" INT NOT NULL,
"cost_energy_type_id" INT NOT NULL,
"cost_quantity" INT NOT NULL DEFAULT 1
);
CREATE TABLE "pokemon_attribute" (
"attribute_id" INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
"attribute_pokemon_card_id" INT NOT NULL,
"attribute_energy_type_id" INT NOT NULL,
"attribute_kind" TEXT NOT NULL,
"attribute_modifier_value" TEXT NOT NULL
);
CREATE TABLE "trainer_mechanic_link" (
"mechanic_link_trainer_card_id" INT NOT NULL,
"mechanic_link_game_mechanic_id" INT NOT NULL,
PRIMARY KEY ("mechanic_link_trainer_card_id", "mechanic_link_game_mechanic_id")
);
-- 6. УНИКАЛЬНЫЕ ИНДЕКСЫ
CREATE UNIQUE INDEX ON "pokemon_attack_link" ("link_pokemon_card_id", "link_attack_name_id");
CREATE UNIQUE INDEX ON "pokemon_attribute" ("attribute_pokemon_card_id", "attribute_energy_type_id", "attribute_kind");
-- 7. ВНЕШНИЕ КЛЮЧИ
ALTER TABLE "card" ADD FOREIGN KEY ("card_set_id") REFERENCES "card_set" ("card_set_id");
ALTER TABLE "pokedex_entry" ADD FOREIGN KEY ("entry_pokedex_number") REFERENCES "pokemon_species" ("species_pokedex_number");
ALTER TABLE "pokemon_card" ADD FOREIGN KEY ("pokemon_card_id") REFERENCES "card" ("card_id");
ALTER TABLE "trainer_card" ADD FOREIGN KEY ("trainer_card_id") REFERENCES "card" ("card_id");
ALTER TABLE "energy_card" ADD FOREIGN KEY ("energy_card_id") REFERENCES "card" ("card_id");
ALTER TABLE "pokemon_card" ADD FOREIGN KEY ("pokemon_pokedex_number") REFERENCES "pokemon_species" ("species_pokedex_number");
ALTER TABLE "pokemon_card" ADD FOREIGN KEY ("pokemon_pokedex_entry_id") REFERENCES "pokedex_entry" ("entry_id");
ALTER TABLE "pokemon_card" ADD FOREIGN KEY ("pokemon_sub_type_id") REFERENCES "pokemon_sub_type" ("pokemon_sub_type_id");
ALTER TABLE "pokemon_card" ADD FOREIGN KEY ("energy_type_id") REFERENCES "energy_type" ("energy_type_id");
ALTER TABLE "pokemon_ability" ADD FOREIGN KEY ("ability_type_id") REFERENCES "ability_type" ("ability_type_id");
ALTER TABLE "pokemon_ability" ADD FOREIGN KEY ("ability_pokemon_card_id") REFERENCES "pokemon_card" ("pokemon_card_id");
ALTER TABLE "pokemon_attack_link" ADD FOREIGN KEY ("link_pokemon_card_id") REFERENCES "pokemon_card" ("pokemon_card_id");
ALTER TABLE "pokemon_attack_link" ADD FOREIGN KEY ("link_attack_name_id") REFERENCES "attack_name" ("attack_name_id");
ALTER TABLE "attack_energy_cost" ADD FOREIGN KEY ("cost_link_id") REFERENCES "pokemon_attack_link" ("link_id");
ALTER TABLE "attack_energy_cost" ADD FOREIGN KEY ("cost_energy_type_id") REFERENCES "energy_type" ("energy_type_id");
ALTER TABLE "pokemon_attribute" ADD FOREIGN KEY ("attribute_pokemon_card_id") REFERENCES "pokemon_card" ("pokemon_card_id");
ALTER TABLE "pokemon_attribute" ADD FOREIGN KEY ("attribute_energy_type_id") REFERENCES "energy_type" ("energy_type_id");
ALTER TABLE "trainer_card" ADD FOREIGN KEY ("trainer_printed_type_id") REFERENCES "trainer_printed_type" ("trainer_printed_type_id");
ALTER TABLE "trainer_mechanic_link" ADD FOREIGN KEY ("mechanic_link_trainer_card_id") REFERENCES "trainer_card" ("trainer_card_id");
ALTER TABLE "trainer_mechanic_link" ADD FOREIGN KEY ("mechanic_link_game_mechanic_id") REFERENCES "game_mechanic" ("game_mechanic_id");
ALTER TABLE "energy_card" ADD FOREIGN KEY ("energy_main_type_id") REFERENCES "energy_type" ("energy_type_id");
+2 -2
View File
@@ -91,7 +91,7 @@ Table pokemon_card {
pokemon_card_id int [pk, unique, not null]
pokemon_pokedex_number int [not null]
pokemon_pokedex_entry_id int
pokemon_hp int [not null]
pokemon_hp int [not null, check: `pokemon_hp > 0`]
pokemon_stage text [not null]
pokemon_evolves_from text
pokemon_retreat_cost int [not null]
@@ -122,7 +122,7 @@ Table pokemon_ability {
ability_pokemon_card_id int [pk, not null, unique]
ability_type_id int [not null]
ability_name text [not null]
anility_effect_text text [not null]
ability_effect_text text [not null]
}
Table pokemon_attack_link {