System variables, Stored procedures & functions, triggers & transactions Flashcards

1
Q

There are two kinds of variables in MySQL, name them

A

global variables - affect the server overall

session variables - affect individual client connections

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

How can you see all system variables?

A

SHOW GLOBAL (or) SESSION VARIABLES;

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

When do changes to system variables apply?

A

On sessions started after the changes.

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

How would you set the global variable ‘port’?

A

SET GLOBAL port = 2222;
or
SET @@GLOBAL.port = 2222;

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

What is a user-defined variable

A

a session are session specific.

written as @var_name and are defined with a SET statement

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

How can you select from tables into variables?

A

SELECT Dname, Dnumber

FROM Company.Department LIMIT 1 INTO @x, @y;

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

What is a local variable?

A

its a variable scoped inside BEGIN - END as:

DECLARE var_name type(int) [DEFAULT value]

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

what is a stored procedure/function

A

named blocks encapsulating SQL statements and procedures

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

how are stored procedure and functions stored

A

stored in a memory cache for later calls i.e. it does not need to be reparsed, recompiled and re-optimized every time is is run

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

what is a stored function

A

computes and returns ONE value.

– you pass it something and it returns a result
– should be small and fast; does it on the fly.

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

what is a stored procedure

A

it is a group of possibly complex set of statements

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

What are the main differences between a stored procedure and function?

A
  • Procedures can have input/output parameters; functions have only input parameters
  • Procedures may return one or more values; functions always one mandatory value
  • Procedures allow all DML statement; Functions allow only SELECT statement
  • Exception can be handled in procedures but cannot be used in functions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you create a stored procedure with an output variable?

A
DELIMITER //
CREATE PROCEDURE calc_value (OUT ending_value INT) BEGIN
DECLARE total_value INT;
SET total_value = 50;
label1: WHILE total_value <= 2100 DO
SET total_value = total_value * 2; END WHILE label1;
SET ending_value = total_value; END //
DELIMITER ;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

how can you call stored procedures with output variables and see the variable??

A
CALL calc_value(@value); 
SELECT @value;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a trigger?

A

an action that takes place when certain events occur.

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

When does a trigger trigger?

A

you can set it to activate before or after the trigger event

17
Q

What happens when you create multiple triggers on the same event?

A

by default, triggers with same event and action activate in the order they were created

the order may also be explicitly defined at trigger creation

18
Q

what is the syntax for creating triggers?

A

CREATE TRIGGER trigger_name
{ BEFORE | AFTER } { INSERT | UPDATE | DELETE } ON tbl_name FOR EACH ROW [{ FOLLOWS | PRECEDES } other_trigger_name]
trigger_body

19
Q

Can triggers call stored functions and procedures?

A

yes

20
Q

When would you use triggers?

A

– generating some derived column values automatically
– enforcing referential integrity
– event logging and storing information on table access
– synchronous replication of tables
– imposing security authorizations
– preventing invalid transactions

21
Q

what is DELIMITER //

A

Something you use to create and run blocks of codes in triggers and stored procedures

22
Q

what is the difference between a stored procedure and function?

A

procedures have inputs and outputs

function only has inputs

procedures can return multiple values and functions only one mandatory value

procedures allow DML and functions only SELECT

procedures allow transactions

23
Q

how do you define a stored procedure?

A

DELIMITER //
CREATE PROCEDURE decrementWith (IN start_value INT, IN decrement_amount INT ,OUT output INT)
BEGIN
SET output = start_value - decrement_amount;
END //
DELIMITER ;

24
Q

What is a transaction?

A

A transaction is a logical unit of work that contains one or more SQL statements. A transaction is an atomic unit. The effects of all the SQL statements in a transaction can be either all committed (applied to the database) or all rolled back (undone from the database)