Database Reference
In-Depth Information
info
=
get_enumorset_info
(
dbh
,
db_name
,
tbl_name
,
col_name
)
puts
"Information for
#{
db_name
}
.
#{
tbl_name
}
.
#{
col_name
}
:"
if
info
.
nil?
puts
"No information available (not an ENUM or SET column?)"
else
puts
"Name: "
+
info
[
"name"
]
puts
"Type: "
+
info
[
"type"
]
puts
"Legal values: "
+
info
[
"values"
].
join
(
","
)
puts
"Nullable: "
+
(
info
[
"nullable"
]
?
"yes"
:
"no"
)
puts
"Default value: "
+
(
info
[
"default"
].
nil?
?
"NULL"
:
info
[
"default"
]
)
end
That code produces the following output for the
item
table
colors
column:
Information for cookbook.item.colors:
Name: colors
Type: enum
Legal values: chartreuse,mauve,lime green,puce
Nullable: yes
Default value: puce
Equivalent routines for other APIs are similar. You can find implementations in the
lib
directory of the
recipes
distribution. Such routines are useful for validation of input
values (see
Recipe 12.8
), and are especially handy for generating list elements in web
forms (see Recipes
20.2
and
20.3
).
10.8. Getting Server Metadata
Problem
You want the MySQL server to tell you about itself.
Solution
Several SQL functions and
SHOW
statements return information about the server.
Discussion
MySQL has several SQL functions and statements that provide you with information
about the server itself and about your current client session. The following table shows
a few that you may find useful. Both
SHOW
statements permit a
GLOBAL
or
SESSION
keyword to select global server values or values specific to your session, and a
LIKE
'
pattern
'
clause for limiting the results to variable names matching the pattern:
Statement
Information produced by statement
Server version string
SELECT VERSION()
Default database name (
NULL
if none)
SELECT DATABASE()