Stored procedures Flashcards

1
Q

Cite a main problem with mixing sql code with say c#, java, or python production code?

A

If any of the tables or rows are changed that code will also have to be refactored. Use stored procedures instead

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

What is considered the ‘body’ of this stored procedure?

CREATE PROCEDURE get_clients()
BEGIN
SELECT * FROM clients
END

A

SELECT * FROM clients

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

What is the syntax for creating a stored procedure?

A

CREATE PROCEDURE named_procedure()

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

True or False
a single statement in a store procedure requires termination

A

True
DELIMITER $$
CREATE PROCEDURE get_clients()
BEGIN
select * from clients;
END$$

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

What is the purpose of the DELIMITER clause in sql?
DELIMITER $$
CREATE PROCEDURE get_clients()
BEGIN
select * from clients;
END$$

A

treats the entire stored procedure as a single unit

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

True or False
You can use anything as a DEMILITER (&&, **, $$, //)

A

True
but $$, // seems conventional

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

True or False
When executing a stored procedure, a resulting table is generated?

A

False
not in mysql

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

In myql what must be done to see any changes after creating a stored procedure?

A

must refresh the left workbench panel

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

What is the syntax for calling a stored procedure?

A

CALL named_procedure()

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

– Create a stored procedure called
– get_invoices_with_balance
– to return all the invoices with a balance > 0

A

DELIMITER $$
CREATE PROCEDURE get_invoices_with_balance()
BEGIN
select *
from invoices_with_balance
where balance > 0;
END$$

DELIMITER ;

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

True or False
When creating a view, stored procedure, trigger … it’s best practice to make sure the underlining query works as anticipated.

A

True, always do this!

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

True or False
Whenever you change the DELIMITER, you must change it back to it’s default value of?

A

True
DELIMITER ;

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

To create a stored procedure in mysql workbench all you have to do is?

A

Right-click stored procedure, create stored procedure.

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

When creating a store procedure in mysql through the workbench, you always have to adjust the delimiter.

A

No this is performed automatically in mysql workbench

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

Why does mysql use back ticks when naming a stored procedure? ie get_payments

A

To prevent naming clashes with sql keywords

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

What is the syntax to drop a stored procedure?

A

DROP PROCEDURE ‘get_payments’

17
Q

What is the syntax for dropping a store procedure if it exists?

A

DROP PROCEDURE IF EXISTS ‘get_payment’

18
Q

True or False
An error will be thrown if you attempt to drop a view or stored procedure if it no longer exists or was previously dropped.

A

True
Which is why you should use IF EXITS condition when dropping views or stored procedures

19
Q

Write a stored procedure that returns the state when called.

CALL get_clients_by_state(‘CA’)

A

DELIMITER $$
CREAT STORED PROCEDURE get_cleints_by_state
(
state CHAR(2) – ‘FL’
)
BEGIN
SELECT * FROM clients c
WHERE c.state = state;
END $$

20
Q

True or False
All parameters are required in mysql?

A

True

21
Q

– Write a stored procedure to return invoices
– for a given client
– call get_invoices_by_client

A

create procedure ‘invoices_by_client’
(
client_id INT
)
begin
select *
from invoices i
where i.client_id = client_id;
end

22
Q

True or False
When creating a stored procedure via workbench console you have to ctrl+enter just like executing any sql query.

A

False
You have to click the apply button on the bottom right corner (apply | revert)

23
Q

What syntax is required when setting a stored procedure default value with an IF clause?

A

END IF;

24
Q

Break down this stored procedure. By name? Was this create in the workbench? What is the delimiter $$ and end $$ mean? What is happening with the IF clause statement?

A

name: get_clients_by_state,
not created in workbench can see delimiter being set manually, the delimiter allows the SP to be seen as a whole, the IF clause is a condition all null values will default to “CA”

25
Q

What is the query to get this default (NULL) result from a stored procedure ‘get_clients_by_state’

A

CALL get_clients_by_state(NULL)

26
Q

True or False
The NULL parameter is required in the following query?

CALL get_clients_by_state(NULL)

A

True

27
Q

Refactor this into a single SELECT query

A

SELECT * FROM clients c
WHERE c.state = IFNULL(state, c.state)
– ifnull c.state = state is true, state will be returned
– ifnull false then c.state = c.state which is always true

28
Q

– Write a stored procedure called get_payments
– with two parameters

– client_id => INT
– payment_method_id => TINYINT

A

CREATE PROCEDURE ‘get_payments’(
cleint_id INT,
payment_method_id TINYINT
)
BEGIN
SELECT *
FROM payments p
WHERE
p.client_id = IFNULL(cleint_id, p.client_id) AND
p.payment_method = (payment_method_id, p.payment_method);
END