-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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' | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
## Syntax | ||||||||||||||||||||||||||
Comment on lines
+14
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||||||||||||||||||||||||||
Comment on lines
+17
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
## Example | ||||||||||||||||||||||||||
Here's an example of positional parameters in PostgreSQL: | ||||||||||||||||||||||||||
Comment on lines
+20
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
First, let's create a simple table: | ||||||||||||||||||||||||||
```sql | ||||||||||||||||||||||||||
Comment on lines
+25
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||
CREATE TABLE employees ( | ||||||||||||||||||||||||||
id SERIAL PRIMARY KEY, | ||||||||||||||||||||||||||
name TEXT, | ||||||||||||||||||||||||||
age INTEGER | ||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
INSERT INTO employees (name, age) VALUES | ||||||||||||||||||||||||||
('John', 30), | ||||||||||||||||||||||||||
('Alice', 25), | ||||||||||||||||||||||||||
('Bob', 35); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||
```sql | ||||||||||||||||||||||||||
PREPARE get_older_employees (INTEGER) AS | ||||||||||||||||||||||||||
SELECT * FROM employees WHERE age > $1; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
```sql | ||||||||||||||||||||||||||
EXECUTE get_older_employees(25); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.