Skip to content

Instantly share code, notes, and snippets.

@RomanDG
Created June 26, 2019 16:02
Show Gist options
  • Save RomanDG/b5c19dd4bd84d9c48b91ecc9fba6f7f8 to your computer and use it in GitHub Desktop.
Save RomanDG/b5c19dd4bd84d9c48b91ecc9fba6f7f8 to your computer and use it in GitHub Desktop.
Задание по основам баз данных и SQL
1. ======================================
create database test_guru;
create table categories (
id serial primary key,
title varchar(25) not null
);
create table tests (
id serial primary key,
title varchar(25) not null,
level varchar(50) not null,
category_id int not null
);
create table questions (
id serial primary key,
body text not null,
tests_id int not null
);
2. ======================================
insert into categories (title) values ('Frontend'), ('Backend'), ('Mobile Development');
insert into tests (title, level, category_id) values
('Ruby', 'Advanced', 2), ('HTML', 'Beginner', 1),
('Kotlin', 'Middle', 3), ('Elixir', 'Advanced', 2),
('JavaScript', 'Middle', 1);
insert into questions (body, tests_id) values
('How to parse an XML file using Ruby and keep specific formatting?', 1),
('What is the equivalent of Java static methods in Kotlin?', 3),
('Are Elixir variables really immutable?', 4),
('How to display the contents of an html text field in JavaScript?', 2),
('Is there a javascript function that I can make to give radiobuttons a value?', 5);
select * from tests where category_id between 2 and 3;
select body from questions where tests_id = 1;
update tests set title='php', level='Middle' where id = 4;
delete from tests where id=4;
select title from tests join categories title on tests.category_id = categories.id;
select t.title, c.title as title from tests t inner join categories c on t.category_id = c.id;
select q.body as body, t.title as title from questions q inner join tests t on q.tests_id = t.id;
@psylone
Copy link

psylone commented Jun 26, 2019

https://gist.github.com/RomanDG/b5c19dd4bd84d9c48b91ecc9fba6f7f8#file-gistfile1-txt-L20
Лучше в единственном числе, ведь вопрос принадлежит к конкретному тесту.

https://gist.github.com/RomanDG/b5c19dd4bd84d9c48b91ecc9fba6f7f8#file-gistfile1-txt-L39
Здесь лучше через OR, поскольку фактически есть тольк 2 значения.

https://gist.github.com/RomanDG/b5c19dd4bd84d9c48b91ecc9fba6f7f8#file-gistfile1-txt-L47
Проверял этот запрос? Смущает title после categories, этот алиас на отношение не используется.

https://gist.github.com/RomanDG/b5c19dd4bd84d9c48b91ecc9fba6f7f8#file-gistfile1-txt-L49
Здесь стоит указать алиасы для атрибутов названий чтобы было ясно что из какой таблицы получается.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment