Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added Postgresql positional-parameters #4544

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
Title: 'Postional parameters in sql'
Description: 'positional parameters are placeholders used in SQL statements for passing values dynamically to queries or functions. '
Subjects:
- 'Web Development'
- 'Computer Science'
- 'postgresql'
Tags:
- 'positional-parameters'
- 'Documentation'
CatalogContent:
- 'getting-started-off-platform-for-data-science'
- 'paths/design-databases-with-postgresql'
- 'paths/computer-science'
Comment on lines +1 to +13
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Title: 'Postional parameters in sql'
Description: 'positional parameters are placeholders used in SQL statements for passing values dynamically to queries or functions. '
Subjects:
- 'Web Development'
- 'Computer Science'
- 'postgresql'
Tags:
- 'positional-parameters'
- 'Documentation'
CatalogContent:
- 'getting-started-off-platform-for-data-science'
- 'paths/design-databases-with-postgresql'
- 'paths/computer-science'
---
Title: 'Postional Parameters'
Description: 'Positional Parameters in PostgreSQL are used within prepared statements and dynamic SQL queries to represent placeholders for values that will be supplied later during execution.'
Subjects:
- 'Web Development'
- 'Computer Science'
Tags:
- 'PostgreSQL'
- 'Database'
- 'Data'
CatalogContent:
- 'getting-started-off-platform-for-data-science'
- 'paths/design-databases-with-postgresql'
---


## Syntax
Comment on lines +14 to +15
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Syntax


**Positional parameters** are often utilized in prepared statements or in dynamic SQL queries.They must always appear first in a parameter set. In access method services, positional parameters are never optional.They serve as placeholders for dynamic values that will be supplied later when executing the query or prepared statement.
When a query or prepared statement is executed, the positional parameters are replaced with actual values based on the order in which they appear and the values provided during execution.
Comment on lines +17 to +18
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
**Positional parameters** are often utilized in prepared statements or in dynamic SQL queries.They must always appear first in a parameter set. In access method services, positional parameters are never optional.They serve as placeholders for dynamic values that will be supplied later when executing the query or prepared statement.
When a query or prepared statement is executed, the positional parameters are replaced with actual values based on the order in which they appear and the values provided during execution.
Positional parameters in PostgreSQL are used within prepared statements and dynamic SQL queries to represent placeholders for values that will be supplied later during execution.These placeholders are denoted by `$1`, `$2`, etc., indicating their position in the parameter list. When the query or prepared statement is executed, the positional parameters are replaced with actual values based on their order and the supplied values.


## Example
Here's an example of positional parameters in PostgreSQL:
Comment on lines +20 to +21
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Example
Here's an example of positional parameters in PostgreSQL:
## Example
Here's an example of using positional parameters in PostgreSQL:


Suppose we have a table called employees with columns id, name, and age. We want to retrieve employees whose age is greater than a specified value, and we want to make this value dynamic using positional parameters.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Suppose we have a table called employees with columns id, name, and age. We want to retrieve employees whose age is greater than a specified value, and we want to make this value dynamic using positional parameters.
Consider a table called `employees` with columns `id`, `name`, and `age`. To retrieve employees whose age is greater than a specified value using positional parameters, the following SQL statements can be used:


First, let's create a simple table:
```sql
Comment on lines +25 to +26
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
First, let's create a simple table:
```sql
First, create a simple table:
```sql

CREATE TABLE employees (
id SERIAL PRIMARY KEY,
name TEXT,
age INTEGER
);

INSERT INTO employees (name, age) VALUES
('John', 30),
('Alice', 25),
('Bob', 35);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

```
Now, let's write a query using positional parameters to retrieve employees older than a specified age:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Now, let's write a query using positional parameters to retrieve employees older than a specified age:
Next, write a query using positional parameters to retrieve employees older than a specified age:

```sql
PREPARE get_older_employees (INTEGER) AS
SELECT * FROM employees WHERE age > $1;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

```
In this query:

PREPARE creates a prepared statement named get_older_employees.
(INTEGER) specifies the data type of the positional parameter.
$1 is the positional parameter reference.
Comment on lines +45 to +49
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In this query:
PREPARE creates a prepared statement named get_older_employees.
(INTEGER) specifies the data type of the positional parameter.
$1 is the positional parameter reference.
In the above query:
- `PREPARE` creates a prepared statement named `get_older_employees`.
- `(INTEGER)` specifies the data type of the positional parameter.
- `$1` is the positional parameter reference.

Now, we can execute the prepared statement with a specific value for the age parameter:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Now, we can execute the prepared statement with a specific value for the age parameter:
Execute the prepared statement with a specific value for the age parameter:


```sql
EXECUTE get_older_employees(25);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

```
This will return all employees whose age is greater than 25.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This will return all employees whose age is greater than 25.
This will return all employees whose age is greater than 25.


The benefit of using positional parameters is that you can reuse the prepared statement with different parameter values without needing to rewrite the entire query. It also helps prevent SQL injection attacks by separating the query logic from the input values.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The benefit of using positional parameters is that you can reuse the prepared statement with different parameter values without needing to rewrite the entire query. It also helps prevent SQL injection attacks by separating the query logic from the input values.
## Benefits of Using Positional Parameters:
- Reusability: The prepared statement can be reused with different parameter values without needing to rewrite the entire query.
- Security: Positional parameters help prevent SQL injection attacks by separating the query logic from the input values.