MySQL Flashcards

1
Q

How do you get a list of users on MYSQL?

A

As root:

SELECT User FROM mysql.user;

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

How do you see all the databases in MySQL?

A

SHOW databases;

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

How do you select a database to use?

A

USE databasename;

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

How do you see the tables in a database?

A

SHOW tables;

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

How do you create a table called admins?

A
CREATE TABLE admins (
  id INT(11) NOT NULL AUTO_INCREMENT,
  username VARCHAR(50) NOT NULL,
  hashed_password VARCHAR(60) NOT NULL,
  PRIMARY KEY (id)
);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you set a primary key?

A
Inside a create statement
PRIMARY KEY (fieldname)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What datatype is a string?

A

VARCHAR

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

What datatype would you normally set an ID to?

A

INT

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

How could you just show the fields from a table called admins?

A

SHOW FIELDS FROM admins;

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

Delete a row with the field menu_name is equal to “Delete Me” from table subjects:

A

DELETE FROM subjects WHERE menu_name=”Delete Me”;

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

How do you update an existing menu_name in table subjects that has an id of 23?

A

UPDATE subjects SET menu_name=’Foobar’ WHERE id=23 LIMIT 1;

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

What command can you use in MySQL to determine which user you are logged in as?

A

SELECT USER(); CURRENT_USER();

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

How do you create a new user on a MySQL database?

A

CREATE USER ‘foobar’@’localhost’ IDENTIFIED BY ‘password’;

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

How do you grant create privileges to a user in MySQL?

A

GRANT CREATE ON sandbox TO foo@’localhost’ IDENTIFIED BY ‘password’;

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

What must you run after setting privileges as root?

A

FLUSH PRIVILEGES;

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

How do you display the structure of a database table in order to verify it’s makeup?

A

DESCRIBE tablename;

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

What function do you use in PHP to change the way PHP saves sessions (i.e. to a database)?

A

session_set_save_handler()

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

How would you had a field ‘admin’ to a table in MySQL?

A

ALTER TABLE users ADD admin tinyint(1) AFTER email;

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

What datatype can you use as BOOLEAN?

A

tinyint(1) - zero is false, any positive number is true (BOOLEAN is an alias to tinyint apparently)

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

How do you see what permissions a user has on a mysql system?

A

SHOW GRANTS FOR ‘username’@’localhost’;

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

How do you make sure a column in mysql is unique?

A

Use the UNIQUE constraint on the field.

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

A table was created, but you missed the fact that one field should be unique, how do you fix this?

A
ALTER TABLE users
ADD UNIQUE (username);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

How would you add a primary key to an existing table (assuming no pre-existing links)?

A

ALTER TABLE my_table ADD id INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;

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

What does ZEROFILL do when creating an INT type column?

A

The zerofill option fills empty slots in the display of the number column.

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

How do you escape a “ ‘ “ character in MySQL?

A

Use two together.. ‘’ will escape the second apostrophe. i.e. (‘Ray’’s shop’);

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

What does the \G option do at the end of an SQL statement (in MySQL)?

A

Provides the results in a vertical grouping style.

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

What does a LEFT JOIN do?

A

Returns all the rows from the left table (table1) with the matching rows in the right table (table 2).

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

If there is no match on the right hand side, what is returned?

A

NULL

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

What is the syntax for a LEFT JOIN (table 1 and table 2)?

A

SELECT columns
FROM table1
LEFT JOIN table2
ON table1.column = table2.column;

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

What is another name for LEFT JOIN?

A

LEFT OUTER JOIN

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

How can you improve efficiency of joins in MySQL?

A

Ensure the columns you are joining on are indexed (keys), and make sure the columns are of the exact same type.

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

When using mysqli_fetch_array, you can pass as a second parameter a predefined variable that indicates the type of array. What are they?

A

MYSQLI_NUM
MYSQLI_ASSOC
MYSQLI_BOTH

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

What does the CONCAT_WS function in mysql do?

A

Concatenates results.

CONCAT_WS(separator, w1, w2)

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

Why might you use the CONCAT_WS function in msyqli?

A

For instance when returning two address fields where either one could be omitted. The behavior of the function is to only add the separator if both fields exist, which prevents the output looking something like…
, address2
when address1 doesn’t exist.

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

When creating a stored function, declarations must be made where?

A

Between a BEGIN and END code block and before any other statements (i.e. straight after the BEGIN keyword)

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

How do you declare a variable in a stored function?

A

DECLARE var1 INT

37
Q

What do the stored function datatypes correspond to?

A

MySQL data types

38
Q

Once a function has been declared, how do you set it?

A

SET var1 = value

39
Q

What is the syntax for creating a stored function in MySQL?

A

CREATE FUNCTION myfunc (myvar1 INT, myvar2 CHAR) RETURNS type code

40
Q

Where in a stored function do statements end with a semicolon?

A

Between the BEGIN and END blocks

41
Q

What is the basic syntax for a full stored function?

A
CREATE FUNCTION name (arguments)
  RETURNS type
BEGIN
  statement1;
  statement2;
END
42
Q

What is the issue with using MySQL stored functions in PHP?

A

The delimiter for the statements is also the delimiter for MySQL functions (i.e. will try to execute half way through the MySQL function).

43
Q

How do you get around the issue with delimiters in PHP?

A

Use the MySQL keyword DELIMITER
Change the delimiter to something else, but make sure you change it back.

DELIMITER $$
.. do stored functions
DELIMITER ;

44
Q

Can you return a list of data from a stored function?

A

No - Return types are scalar in nature.

45
Q

Delimiter has been set to $$ and you have finished your stored function definition. You type END; and nothing happens, what is wrong with this?

A

You have set the delimiter to $$, you must type:

END $$ to finish the stored function. (Remember to set the DELIMITER back to ; if you have finished)

46
Q

How do you query mysql to show you a stored function definition?

A

SHOW CREATE FUNCTION name;

47
Q

How do you query mysql to show you a list of stored functions?

A

SHOW FUNCTION STATUS;

48
Q

What are stored routines associated with?

A

The database - so if you drop a database, you will also drop the routines.

49
Q

How do you create an enumeration in a MySQL table?

A
In the create clause:
userType ENUM ('public', 'author', 'admin'),
50
Q

Rows can also be referred to as…

A

tuples or records

51
Q

What is the name for the ‘layout’ of the database?

A

schema (usually in the following form)

Customers(CustomerID, Name, Address)

52
Q

Primary keys in a schema are usually…

A

underlined

53
Q

Foreign keys in a schema are usually represented by…

A

italics

54
Q

What are the three different relationships between two tables?

A

one-one
one-many
many-many

55
Q

An update anomaly is…

A

When you update a database and end up with inconsistent data - i.e. a persons address is stored in multiple areas, changing it once doesn’t get all of them - causing an anomaly.

56
Q

What are the three kind of anomalies?

A

Deletion
Insertion
Update

57
Q

A deletion anomaly is?

A

Deleting a row removes data that should be stored (for example if customers address was stored against the order, instead of it’s own table).

58
Q

An insertion anomaly is?

A

If you have data that should be stored in one place (i.e. address) and instead store it against a row that changes frequently (i.e. the order) - the user may enter different information that conflicts with previous entries.

59
Q

A well designed database will have columns with what kind of data?

A

Atomic (i.e. not multiple types of data in one field)

60
Q

To avoid a design with many empty attributes, what should you do?

A

Create another table for those attributes (i.e. instead of having an empty “reviews” column against books, make it another table which only gets a new row if a review exists)

61
Q

what is the problem with having null values in a table?

A

It wastes space, and it also creates ambiguity. Was the value forgotten, or does it not exist? Also not good for working out totals.

62
Q

How do you set a password for a user?

A

SET PASSWORD FOR ‘cain’@’locahost’ = PASSWORD(‘cleartextpassword’);

63
Q

How do you load an existing sql document into a mysql database?

A

mysql -h host -u username -D dbname -p < mydb.sql

64
Q

What is the difference between char and varchar in mysql?

A

Char will always use the amount allocated (i.e. you specify 50, it will use 50 and pad out the spaces). Varchar uses only what is passed in plus one byte. But char is faster.

65
Q

How do you get information about the installation of mysql?

A

from command line:

mysqladmin -h hostname -u root -p variables

66
Q

What is DDL?

A

Data Definition Language, used for defining the data in the database.

67
Q

What is DML?

A

Data Manipulation Language - used for querying or modifying the database.

68
Q

General rule of thumb when doing equi joins is that you need one less … for each pair of tables you want to join?

A

One less join condition for each pair of tables that you want to join.

69
Q

Left Joins are useful for…

A

finding rows where there is no match (i.e. left join will keep everything in the left table regardless of whether there is a match - you can then check for NULL to find the rows that perhaps haven’t bought something).

70
Q

An equi join uses the ‘where’ clause to specify join conditions - what does a left join use?

A

on

71
Q

Using a comma to seperate two tables is the same as an…

A

inner join

72
Q

How do you specify an alias?

A

select c.name

from customers as c;

73
Q

When is using an alias mandatory?

A

When joining a table to itself.

74
Q

What is a cartesian product?

A

All combinations of all the rows in all the tables in the join. Used by specifying a comma between table names and not specifying a WHERE clause

75
Q

What is a full join?

A

Same as a cartesian product.

76
Q

What is a cross join?

A

Same as a cartesian product and full join - can be achieved by using “CROSS JOIN” keywords between two tables

77
Q

Inner join is semantically equivalent to?

A

Using the ‘,’.

78
Q

What needs to be specified to make an INNER JOIN a true INNER JOIN?

A

A where clause.

79
Q

LEFT JOIN is the same as…

A

LEFT OUTER JOIN

80
Q

RIGHT JOIN is the same as…

A

RIGHT OUTER JOIN

81
Q

Which table does RIGHT JOIN keep the records for regardless of match?

A

The right table

82
Q

What two values define the sort order in MYSQL?

A

ASC and DESC

83
Q

In ANSI SQL, the only things that can apppear in the select clause when using GROUP BY are?

A

The aggregate functions and the columns named in the group by clause.

84
Q

How do you test the results of an aggregate function?

A
Using the HAVING function
SELECT customerid, avg(amount)
FROM orders
GROUP BY customerid
HAVING avg(amount) > 50;
85
Q

What MYSQL extension (non ANSI) can you use to deal with pagination in an application (i.e. high score tables)?

A

LIMIT 2, 3;

The first number describes which row to start from, the second how many results to return.

86
Q

Using subqueries, we have access to subquery operators. What are the operators?

A

ANY, (Returns true if the comparison is true for any of the rows in the subquery)
SOME, (alias for any)
IN, (Equivalent to =Any)
ALL (Returns true if the comparison is true for ALL of the rows in the subquery)

87
Q

What is a correlated subquery?

A

Where data from the outer query is used in the inner query.

88
Q

What is a row subquery?

A

Where an entire row is returned instead of single values - typically used to find duplicate rows.

89
Q

What is the difference between ANSI SQL and MySQL when it comes to ALTERING tables?

A

You can only update none alteration per ALTER TABLE call in ANSI SQL