Добавил:
ИВТ (советую зайти в "Несортированное")rnПИН МАГА Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Database 2024 / Books / Искусство PostgreSQL.pdf
Скачиваний:
8
Добавлен:
20.11.2024
Размер:
1.62 Mб
Скачать

3

Getting Ready to read this Book

Be sure to use the documentation for the version of PostgreSQL you are using, and if you’re not too sure about that just query for it:

1show server_version;

server_version

════════════════

9.6.5 (1 row)

Ideally, you will have a database server to play along with.

If you’re using MacOSX, check out Postgres App to install a PostgreSQL server and the psql tool.

For Windows check https://www.postgresql.org/download/windows/.

If you’re mainly running Linux mainly you know what you’re doing already right? My experience is with Debian, so have a look at https://apt. postgresql.org and install the most recent version of PostgreSQL on your station so that you have something to play with locally. For Red Hat packaging based systems, check out https://yum.postgresql.org.

In this book, we will be using psql a lot and we will see how to con gure it in a friendly way.

You might prefer a more visual tool such as pgAdmin or OmniDB; the key here is to be able to easily edit SQL queries, run them, edit them in order to x them, see the explain plan for the query, etc.

Chapter 3 Getting Ready to read this Book j 24

If you have opted for either the Full Edition or the Enterprise Edition of the book, both include the SQL les. Check out the toc.txt le at the top of the les tree, it contains a detailed table of contents and the list of les found in each section, such as in the following example:

2Introduction

2 Structured Query Language

2.1Some of the Code is Written in SQL

2.2A First Use Case

2.3Loading the Data Set 02-intro/02-usecase/03_01_factbook.sql

2.4Application Code and SQL 02-intro/02-usecase/04_01.sql 02-intro/02-usecase/04_02_factbook-month.py

2.5A Word about SQL Injection

2.6PostgreSQL protocol: server-side prepared statements 02-intro/02-usecase/06_01.sql 02-intro/02-usecase/06_02.sql

2.7Back to Discovering SQL 02-intro/02-usecase/07_01.sql

2.8Computing Weekly Changes 02-intro/02-usecase/08_01.sql

3 Software Architecture

3.1Why PostgreSQL? 02-intro/03-postgresql/01_01.sql

3.2The PostgreSQL Documentation

4 Getting Ready to read this Book

02-intro/04-postgresql/01.sql

To run the queries you also need the datasets, and the Full Edition includes instructions to fetch the data and load it into your local PostgreSQL instance. The Enterprise Edition comes with a PostgreSQL instance containing all the data already loaded for you, and visual tools already setup so that you can click and run the queries.

Part III

Writing Sql Queries

j 26

In this chapter, we are going to learn about how to write SQL queries. There are several ways to accomplish this this, both from the SQL syntax and semantics point of view, and that is going to be covered later. Here, we want to address how to write SQL queries as part of your application code.

Maybe you are currently using an ORM to write your queries and then have never cared about learning how to format, indent and maintain SQL queries. SQL is code, so you need to apply the same rules as when you maintain code written in other languages: indentation, comments, version control, unit testing, etc.

Also to be able to debug what happens in production you need to be able to easily spot where the query comes from, be able to replay it, edit it, and update your code with the new xed version of the query.

Before we go into details about the speci cs of those concerns, it might be a good idea to review how SQL actually helps you write sof ware, what parts of the code you are writing in the database layer and how much you can or should be writing. The question is this: is SQL a good place to implement business logic?

Next, to get a more concrete example around The Right Way™ to implement SQL queries in your code, we are going to have a detailed look at a very simple application, so as to work with a speci c code base.

Af er that, we will be able to have a look at those tools and habits that will help you in using SQL in your daily life as an application developer. In particular, this chapter introduces the notion of indexing strategy and explains why this is one of the tasks that the application developer should be doing.

To conclude this part of the book, Yohann Gabory shares his Django expertise with us and covers why SQL is code, which you read earlier in this chapter.

Соседние файлы в папке Books