Lesson1 Flashcards

(35 cards)

1
Q

What theorem outlines the basics of distributed systems?

A

CAP Theorem

The CAP Theorem states that it is impossible for a distributed system to simultaneously provide all three of the following guarantees: Consistency, Availability, and Partition Tolerance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the components of SQL?

A
  • DDL (Data Definition Language)
  • DQL (Data Query Language)
  • DML (Data Manipulation Language)
  • TCL (Transaction Control Language)
  • DCL (Data Control Language)

These components are essential for managing and manipulating relational databases.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What security issue can occur due to user input in SQL queries?

A

SQL Injection

SQL Injection occurs when an attacker is able to manipulate SQL queries by injecting malicious input.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the common causes of SQL injection?

A
  • User input is not checked
  • Complete SQL statements are sent to the server
  • Permission problems

These causes highlight the importance of input validation and proper permission management.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can SQL injections be avoided?

A
  • Always validate your input
  • Don’t send complete SQL statements to the server
  • Use roles with correct permissions

These practices help secure SQL queries against injection attacks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a Prepared Statement?

A

A pre-defined SQL statement with placeholders that is validated or escaped before execution.

Prepared Statements are used to enhance security and performance by preventing SQL injection.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the advantages of using Prepared Statements?

A
  • Avoids SQL injection
  • Improves performance by allowing multiple executions

Prepared statements are compiled once and can be reused, reducing overhead.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What SQL command is used to create a Prepared Statement in PostgreSQL?

A

PREPARE

The PREPARE command allows the creation of a prepared statement with specified parameters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the purpose of Stored Procedures?

A

To execute independent programs that extend SQL’s functionality and improve performance.

Stored Procedures allow for complex operations and reduce communication overhead between client and server.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the difference between Stored Procedures and Stored Functions?

A
  • Stored Procedures: Independent programs without a return type.
  • Stored Functions: Can be used within SQL queries and return a value.

Both are stored in the database but serve different purposes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is PL/pgSQL?

A

PostgreSQL’s default procedural language for writing stored procedures and functions.

PL/pgSQL allows for control structures, variables, and loops, enhancing SQL capabilities.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you declare a variable in PL/pgSQL?

A

Using the DECLARE section with syntax: variable_name type [DEFAULT value];

Variables must be declared before being used in PL/pgSQL.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the purpose of the RETURN statement in functions?

A

To define a function’s result or to terminate the function without returning a value.

RETURN can also be used to yield results in PL/pgSQL.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What type of integrity does custom integrity refer to?

A

Application-dependent, complex constraints based on multiple conditions.

Examples include constraints on exam attempts or salary adjustments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the syntax to create a Stored Procedure in PL/pgSQL?

A

CREATE [ OR REPLACE ] PROCEDURE <functionname>(<parameters>) AS \$\$ <functionbody> \$\$ LANGUAGE plpgsql;</functionbody></parameters></functionname>

The procedure can be executed manually using the CALL keyword.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the basic loop structure in PL/pgSQL?

A

LOOP … END LOOP;

This creates an endless loop that must be exited using a condition.

17
Q

What is a trigger in the context of databases?

A

A database object that automatically executes a specified function in response to certain events.

Triggers can enforce complex integrity rules and automate tasks.

18
Q

What is custom integrity in database management?

A

Custom integrity refers to specific rules that ensure data validity and consistency based on defined conditions.

Examples include enforcing maximum exam attempts or ensuring prerequisites for degree programs.

19
Q

What is the role of application code in ensuring custom integrity?

A

Application code is used to enforce custom integrity rules directly within the application logic.

It is important to not trust client-side validation.

20
Q

What are stored procedures and functions used for in custom integrity?

A

Stored procedures/functions are custom code used to ensure data integrity by executing complex logic on the database side.

An example is verifying bank transfer conditions.

21
Q

What is a trigger in SQL?

A

A trigger is a stored function that automatically executes in response to certain events on a table.

Supported events include INSERT, UPDATE, and DELETE.

22
Q

What types of events can trigger an action in SQL?

A

Triggers can respond to the following events:
* INSERT
* UPDATE
* DELETE
* TRUNCATE

TRUNCATE is specific to PostgreSQL and is a fast way to delete all rows.

23
Q

What does the BEFORE trigger event do?

A

The BEFORE trigger is executed before the event operation to check conditions and potentially abort the operation.

Example: Checking if bank balance is sufficient for a transfer.

24
Q

What does the AFTER trigger event do?

A

The AFTER trigger is executed after the event operation to perform additional actions, such as updating statistics.

This can improve performance by avoiding complex aggregations.

25
What does the INSTEAD OF trigger do?
The INSTEAD OF trigger replaces the operation logic of the triggering event. ## Footnote Example: Using UPDATE to flag rows as deleted instead of actually deleting them.
26
What SQL command is used to create a trigger in PostgreSQL?
CREATE TRIGGER is used to create a trigger in PostgreSQL. ## Footnote The syntax includes specifying the trigger name, event, table, and the function to execute.
27
What is the significance of the RETURN type in functions used for triggers?
The return type must be TRIGGER, allowing the function to control the outcome of the triggering event. ## Footnote The function can either return null or a row matching the table schema.
28
What are the special variables accessible within trigger functions?
Trigger functions can access: * NEW: new values for inserted/updated columns * OLD: old values before the update * TG_*: meta information about the trigger ## Footnote TG_ARGV4 can be used to access additional parameters.
29
What is an example of a trigger function that enforces upper case values?
A trigger function can convert first and last names to upper case before insertion or update. ## Footnote Example code demonstrates setting NEW.first_name and NEW.last_name to upper case.
30
What happens if a trigger function returns null?
Returning null from a trigger function aborts the current operation (INSERT, DELETE, UPDATE). ## Footnote This allows for custom INSTEAD OF logic.
31
How can exceptions be used in trigger functions?
Exceptions can be raised to abort operations based on specific conditions, such as preventing salary reductions. ## Footnote An example function raises an exception if the new salary is less than the old salary.
32
What is the difference between Query Shipping and Data Shipping?
Query Shipping executes computations on the DBMS, while Data Shipping pulls raw data to the client for processing. ## Footnote This distinction affects performance and resource usage.
33
What impact do triggers have on database performance?
Triggers can impact performance as they add additional code execution for every event. ## Footnote The choice between FOR ROW and FOR STATEMENT affects how often the trigger executes.
34
What is the effect of using the WHEN block in triggers?
The WHEN block activates the trigger only under certain conditions, potentially enhancing performance by skipping unnecessary executions. ## Footnote This can prevent the execution of the function body if the condition is not met.
35
What is a key disadvantage of using stored procedures and triggers?
A key disadvantage is the maintenance of multiple code bases, including client application and in-database logic. ## Footnote This can complicate development and updates.