-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
[Content] Postgresql #97
Comments
hoiekim
pushed a commit
to hoiekim/databases
that referenced
this issue
Jul 24, 2020
hoiekim
pushed a commit
to hoiekim/databases
that referenced
this issue
Jul 26, 2020
hoiekim
pushed a commit
to hoiekim/databases
that referenced
this issue
Jul 26, 2020
AliceHuang1027
pushed a commit
to AliceHuang1027/databases
that referenced
this issue
Jul 28, 2020
…d add unit test
AliceHuang1027
pushed a commit
to AliceHuang1027/databases
that referenced
this issue
Jul 28, 2020
AliceHuang1027
pushed a commit
to AliceHuang1027/databases
that referenced
this issue
Jul 28, 2020
AliceHuang1027
pushed a commit
to AliceHuang1027/databases
that referenced
this issue
Jul 28, 2020
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
0 Credentials
or
Create PG Credentials - Button
1 Intro
Postgres is an application. More specifically, a database application for you to organize your data. It can store data for you and you can have multiple apps connect to your Postgres:
2 Basics
To quickly help you get started, we will go over a few basic commands that you can follow along. Explanations and best practices will be covered in the section below.
2.1 Connect
To connect to your app to Postgres, you need a module called
pg
. You need to specify where the postgres application is located (host and port), the name of your database, and your username / password to login to the database.2.2 Simple Commands
After connecting to Postgres, you can now send commands that Postgres can understand. These commands are called Sequel Query Language (SQL). Here are a few:
CREATE TABLE lesson( id serial PRIMARY KEY, title VARCHAR (256) );
lesson
that has 2 columns:id
andtitle
id
column is a primary key and theserial
property means that it will automatically increase as you add rows into the table.VARCHAR (256)
means that title is a string of up to 256 characters.INSERT INTO lesson (title) VALUES ('postgres tutorial')
lesson
table with a title of 'postgres tutorial'.select * from lesson where title = 'postgres tutorial'
"postgres tutorial'
.UPDATE lesson SET title = 'postgres demo' WHERE id=1;
id =1
, and then changes the title to'postgres demo'
DELETE FROM lesson WHERE id=1;
id = 1
To run a Query, you run
client.query(' -- YOUR SQL HERE --')
Make sure you await to resolve! All SQL commands involves sending a the SQL to your postgres application and then waiting for it to finish executing, so the commands will be asynchronous. Therefore any client actions will be asynchronous and returns a promise. To wait for a command to finish, you need an
await
or.then
to wait for the query to resolve before continuing.2.3 Usage in your website
How would you execute these commands in your website? Here are a few sample code to build an API:
2.3.1 Security
Notice how we don't create a string from user input and directly execute that query like this:
If you create an SQL query directly from user input, your database will have a security issue called (Injection vulnerability). A user could simply pass in the following as id:
1; UPDATE lesson SET title="my awesome title" WHERE id=1
. The user could essentially do anything they want to your database, including deleting all your data.To prevent this awful attack, always make sure to pass user input as the second argument into
client.query
. Theclient.query
function will help you clean the user input to prevent sql injection attacks.3 Database Design
When it comes to Postgres and other SQL databases, you must be very intentional about your data and make sure it is clear and organized. If you are building a website like c0d3 and you have a
user
table to store user information and alesson
table to store lesson information (like title and description), how do you store the user's progress for each lesson?Option1 Could you add a column to the
user
table?user
table.Option2 Could you add a column to the
lesson
table?lesson
table.Creating a new table. The best solution is to create a new table called
userlesson
that has 3 columns:userId
to retrieve user's information from the correspondinguser
table when needed.lessonId
to retrieve lesson's information from the correspondinglesson
table when needed.status
that is a string, to store the user's status for the each lesson.In the above example, since
userId
andlessonId
are used to retrieve the row from the user and lesson tables respectively, they are called foreign keys.userId
is a foreign key to theuser
table.The
id
column in theuser
andlesson
table that other tables reference to, is called the primary key.id
column is a primary key for the lesson (or user) table.Let's say you want to see what the status is for lesson with id 5 for every user in your database. How would you do that?
Option 1 : Get all the users from your database and then get their lesson status for each user:
Option 2: Run 1 query using join:
3.1 Joins
Join helps you combine these tables together and get data from all the tables in 1 query. If you forget how to use Join, remember that you can always use option 1 to get the data you need by running multiple queries. To understand why you need join, let's analyze the two options above. Let's say:
userlesson
table has 7000 rows.Option1: 1000*7000 lookups. 7 Million lookups!
userlesson
table to find the correctuserid
andlessonid
Option2: 7000 lookup + magic time
Postgres's magic time is really fast. Option 2 will be significantly faster than option 1 and with a big database it could mean the difference between waiting months and waiting minutes. If you are a data analyst, part of your job is to figure out how to join tables efficiently to get the data you need quickly.
Sometimes, you could unknowingly join tables in a way to make your query ALOT slower than option 1. The most painful part about this mistake is that when data is small, you don't notice the query taking a long time. However, as the company grows over the years and more data is accumulated, the code is forgotten and the application becomes really, really slow. This problem is very difficult to fix and to avoid writing joins directly, most companies use an Object Relational Mapping (ORM) library to generate the SQL queries for you. This not only avoids the problem of engineers joining the tables incorrectly but also allows developers to use the database without knowing SQL.
4 ORM
In the previous section, you learned how to directly connect to your postgres database, execute simple queries, and execute more complex queries using join. These SQL queries are hard to remember, manage, and could be written incorrectly to make the queries really slow. To solve these problems, most production systems use an Object Relational Mapping (ORM) library. We will use the most common one called sequelize in this section. Sequelize converts your JavaScript functions into SQL commands for you.
4.1 Connection and models
To use sequelize, you must first connect to your database and then create the necessary tables.
5 Scaling
The text was updated successfully, but these errors were encountered: