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

Working with databases #2

Working with databases #2

Here we introduce basic data manipulation statements: SELECT, INSERT, UPDATE, DELETE and how to restrict results using WHERE and LIMIT. Also how to order results using custom fields.

Yanko Simeonoff

May 24, 2017
Tweet

More Decks by Yanko Simeonoff

Other Decks in Programming

Transcript

  1. СТРУКТУРА rel_book_author n 1 public. author author_id serial « nn

    » name text « nn » total_books smallint « nn » public. book book_id serial « nn » author_id smallint « fk » name text « nn »
  2. ПЪРВО МАЛКО ПО-СЛОЖЕН ПРИМЕР Цел: да пишем такива заявки без

    въобще да се замисляме SELECT book.id, book.name, author.name AS "author" FROM book INNER JOIN author USING (author_id)
  3. КОГАТО ИМАМЕ ПОЛЕ С ЕДНАКВО ИМЕ В ПОВЕЧЕ ОТ ЕДНА

    ТАБЛИЦА, ОТ КОЯТО ЧЕТЕМ name author Lord of the rings J. R. R. Tolkien It Stephen King Firestarter Stephen King Cell Stephen King Going postal Terry Pratchet Running man Stephen King SELECT book.name, author.name AS "author" FROM book INNER JOIN author USING (author_id);
  4. Без именуване name COUNT(book.book_id) Stephen King 4 Terry Pratchet 1

    J. R. R. Tolkien 1 Paulo Coehlo 0 SELECT author.name, COUNT(book.book_id) FROM author LEFT JOIN book USING (author_id) GROUP BY author.author_id;
  5. С именуване name books Stephen King 4 Terry Pratchet 1

    J. R. R. Tolkien 1 Paulo Coehlo 0 SELECT author.name, COUNT(book.book_id) AS "books" FROM author LEFT JOIN book USING (author_id) GROUP BY author.author_id;
  6. book_id name author_id genre 2 It 1 horror 3 Firestarter

    1 horror 4 Cell 1 horror SELECT * FROM book WHERE genre = 'horror';
  7. ИЗБРОЯВАНЕ НА СТОЙНОСТИ book_id name author_id genre 2 It 1

    horror 3 Firestarter 1 horror 4 Cell 1 horror 6 Foundation NULL Sci Fi 7 Running man 1 Sci Fi SELECT * FROM book WHERE genre IN ('horror', 'sci fi');
  8. УСЛОВИЯТА МОГАТ ДА БЪДАТ ГРУПИРАНИ book_id name author_id genre 7

    Running man 1 Sci Fi SELECT * FROM book WHERE genre = 'Sci Fi' AND author_id = 1;
  9. ПО ПОДРАЗБИРАНЕ резултатите са сортирани по първичния ключ book_id name

    author_id genre 1 Lord of the rings 3 fantasy 2 It 1 horror 3 Firestarter 1 horror 4 Cell 1 horror 5 Going postal 2 fantasy 6 Foundation NULL Sci Fi SELECT * FROM book;
  10. МОЖЕМ ДА СОРТИРАМЕ ПО ПРОИЗВОЛНО ПОЛЕ book_id name author_id genre

    1 Lord of the rings 3 fantasy 5 Going postal 2 fantasy 2 It 1 horror 3 Firestarter 1 horror 4 Cell 1 horror 6 Foundation NULL Sci Fi 7 Running man 1 Sci Fi SELECT * FROM book ORDER BY genre;
  11. МОЖЕМ ДА СОРТИРАМЕ ПО ПОВЕЧЕ ОТ ЕДНО ПОЛЕ book_id name

    author_id genre 5 Going postal 2 fantasy 1 Lord of the rings 3 fantasy 4 Cell 1 horror 3 Firestarter 1 horror 2 It 1 horror 6 Foundation NULL Sci Fi 7 Running man 1 Sci Fi SELECT * FROM book ORDER BY genre, name;
  12. БЕЗ ОГРАНИЧЕНИЕ book_id name author_id genre 1 Lord of the

    rings 3 fantasy 2 It 1 horror 3 Firestarter 1 horror 4 Cell 1 horror 5 Going postal 2 fantasy 6 Foundation NULL Sci Fi 7 Running man 1 Sci Fi SELECT * FROM book;
  13. САМО ПЪРВИТЕ 3 РЕДА book_id name author_id genre 1 Lord

    of the rings 3 fantasy 2 It 1 horror 3 Firestarter 1 horror SELECT * FROM book LIMIT 3;