Learning MySQL Language Structure
Session 2
Ramkumar Lakshminarayanan
Copyright HP Education 1
MySQL Vs SQL
MySQL was originally designed with three basic ideas in
mind: to be fast, reliable, and easy to use. Like every
database system, MySQL does not completely follow
the SQL standard and has its own extensions to SQL.
Copyright HP Education 2
For example, Using MySQL extensions the SQL
standard way of finding out which tables arethe sakila
database is:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA=’sakila’;
The easiest way to find out this information in MySQL
is:
SHOW TABLES FROM sakila;
Copyright HP Education 3
Comments and Portability
The -- is the standard SQL simple comment introducer.
Everything on a line after this is considered a
comment.
The SQL standard bracketed comment introducer and
terminator /* */ allow partial line and multi-line
commenting. Putting ! after the bracketed comment
introducer indicates that this is MySQL specific code,
and the mysqld server will parse it:
/*! SHOW DATABASES */;
The mysqld server will parse the SHOW DATABASES
statement
Copyright HP Education 4
Case-sensitivity
Traditionally, SQL reserved words are written in
uppercase, such as SELECT, FROM, NULL, and AS.
These words are case-insensitive, meaning that
SELECT, select, and SeLeCt are all parsed by
mysqld as the same reserved word.
Copyright HP Education 5
Escape characters
The escape sequences for strings in mysqld are:
■ \\ to print the \ character.
■ \’ to print the ’ character, even if the string is
quoted with ’.
■ \" to print the " character, even if the string is
quoted with ".
■ \_ prints the _ character. This can be used to
search for the actual value when using LIKE.
If _ is not escaped, it is used as the wildcard
character for one character.
Copyright HP Education 6
Naming limitations and quoting
Identifiers are names of: databases, tables, views,
fields, indexes, tablespaces, stored routines,triggers,
events, servers, log file groups, and aliases (specified
with the AS keyword).
Identifiers are all limited to 64 characters except aliases,
which are limited to 255 characters.
Copyright HP Education 7
Identifiers can be almost anything. However, identifiers
may not end with one or more spaces:
mysql> CREATE TABLE `space ` (id INT);
ERROR 1103 (42000): Incorrect table name
’space ’
mysql> CREATE TABLE space (`id ` INT);
ERROR 1166 (42000): Incorrect column name
’id ’
Copyright HP Education 8
Some common errors we normally miss
% and _ are wildcard characters as specified in the
SQL standard. To find a string that contains the
actual character % using the LIKE operator, escape it
with \:
mysql> USE sakila;
Database changed
mysql> SELECT first_name FROM staff WHERE first_name LIKE ’M%’;
+------------+
| first_name |
+------------+
| Mike |
+------------+
1 row in set (0.00 sec)
mysql> SELECT first_name FROM staff WHERE first_name LIKE ’M\%’;
Copyright HP Education 9
String parsing error
The following example shows that, when the string
Mike is not quoted, mysqld parses it as the name of a
field. However, when the string is quoted, mysqld
parses it as a string:
mysql> SELECT last_name FROM staff WHERE first_name=Mike;
ERROR 1054 (42S22): Unknown column ’Mike’ in ’where clause’
mysql> SELECT last_name FROM staff WHERE first_name=’Mike’;
+-----------+
| last_name |
+-----------+
| Hillyer |
+-----------+
1 row in set (0.00 sec)
Copyright HP Education 10
The following example shows that when the number 1 is
not quoted, mysqld parses it as a number. However,
when the number is quoted, mysqld parses it as a
field name:
mysql> SELECT first_name, last_name FROM staff WHERE active=1;
+------------+-----------+
| first_name | last_name |
+------------+-----------+
| Mike | Hillyer |
| Jon | Stephens |
+------------+-----------+
2 rows in set (0.00 sec)
mysql> SELECT first_name, last_name FROM staff WHERE active=`1`;
ERROR 1054 (42S22): Unknown column ’1’ in ’where clause’
Copyright HP Education 11
mysql> select 1 + '10q4';
+------------+
| 1 + '10q4' |
+------------+
| 11 |
+------------+
1 row in set, 1 warning (0.00 sec)
mysql> show warnings;
+---------+------+------------------------------------------+
| Level | Code | Message |
+---------+------+------------------------------------------+
| Warning | 1292 | Truncated incorrect DOUBLE value: '10q4' |
+---------+------+------------------------------------------+
1 row in set (0.00 sec
Copyright HP Education 12
Strict SQL Mode
if such a truncation occurs when storing a value in a
table, by default mysqld issues a similar warning and
stores the truncated value. For mysqld to issue an
error and refuse to store such values, a strict SQL
mode must be used.
-- We will about this later in administration in detail ...
Copyright HP Education 13
Dot notation
MySQL has a special dot notation that can be used to
specify a database when referring to a table. Simply
place a dot character (.) between the database and
table name.
Mysql> use sakila;
mysql> select table_name from tables limit 1 ;
ERROR 1146 (42S02): Table 'sakila.tables' doesn't exist
mysql>select table_name from information_schema.tables limit
1;
+----------------+
| table_name |
+----------------+
| CHARACTER_SETS |
+----------------+
1 row in set (0.00 sec)
Copyright HP Education 14
Time zones
When mysqld starts, it determines the time zone of the
operating system and sets the system_time_zone
system variable accordingly.
By default, mysqld sets the value of time_zone to
SYSTEM, which means that it operates using the
time zone in system_time_zone.
Copyright HP Education 15
A small thought on time zone
Web servers and database servers may have different
time zones, and thus have different times. An order
coming from an application on a web server whose
time is in PST (UTC-8) and stored on a database
server whose time is in EST (UTC-5) has a problem
Here a time zone synchronization has to be done
between the web server and the database server.
Copyright HP Education 16
Character sets and collations
MySQL supports many different character sets and
collations.
A character set, or charset, is the set of available
characters that can be used — similar to an alphabet.
Different languages have different alphabets, and the
most often used character sets contain the letters of
many alphabets (for example, the default latin1
character set includes all of the characters in Latin
languages, including accented characters and
characters using the cedilla.
Copyright HP Education 17
Colliation
A collation specifies the lexical sort order; in English the
lexical sort order begins with a, b, c, d;
in Spanish the lexical sort order begins with a, b, c, ch,
d;
in Greek the lexical sort order begins with α , β , χ , δ . A
collation can also specify if a sort order is case-
sensitive or not;
a binary collation is a collation that is case-sensitive.
Copyright HP Education 18
Each character set has a default collation and may have
other collations associated with it as well.
The default character set for MySQL, latin1, has a
default collation of latin1_swedish_ci and seven
additional collations
Copyright HP Education 19
mysql> SELECT COLLATION_NAME, IS_DEFAULT
-> FROM INFORMATION_SCHEMA.COLLATIONS
-> WHERE CHARACTER_SET_NAME=’latin1’;
+-------------------+------------+
| COLLATION_NAME | IS_DEFAULT |
+-------------------+------------+
| latin1_german1_ci | |
| latin1_swedish_ci | Yes |
| latin1_danish_ci | |
| latin1_german2_ci | |
| latin1_bin | |
| latin1_general_ci | |
| latin1_general_cs | |
| latin1_spanish_ci | |
+-------------------+------------+
8 rows in set (0.00 sec)
Copyright HP Education 20
The different levels to which character set and collation
can be set are:
Server — The system variables character_set_server
and collation_server specify the default character set
and collation for a database when a CREATE
DATABASE statement does not have any
CHARACTER SET or COLLATE clauses.
Database — The system variables
character_set_database and collation_database
specify the default character set and collation for the
current database.
Copyright HP Education 21
mysql> show variables like 'charac%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
Copyright HP Education 22
mysql> set character_set_database='latin1';
mysql> show variables like 'charac%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
Copyright HP Education 23
Database options such as the default character set
and collation are stored in plain text in the db.opt file
in each database.
Table — A CREATE TABLE or ALTER TABLE tblname
ADD COLUMN statement can use a CHARACTER
SET or COLLATE clause. This will set the default
character set and collation for a field added with no
character set or collation specified.
Field — Each field can have its own character set and
collation
Copyright HP Education 24
[root@localhost /]# cd /var/lib/mysql/
[root@localhost mysql]# ls
bank ib_logfile0 my_database mysql.sock std wiproadmin
gov ib_logfile1 myexample one three
ibdata1 localhost.localdomain.err mysql sakila two
[root@localhost mysql]# cd sakila
[root@localhost sakila]# ls
actor.frm film_actor.frm payment_date.TRN
actor_info.frm film_category.frm payment.frm
address.frm film.frm payment.TRG
category.frm film_list.frm rental_date.TRN
city.frm film_text.frm rental.frm
country.frm film_text.MYD rental.TRG
customer_create_date.TRN film_text.MYI sales_by_film_category.frm
customer.frm film.TRG sales_by_store.frm
customer_list.frm ins_film.TRN staff.frm
customer.TRG inventory.frm staff_list.frm
db.opt language.frm store.frm
del_film.TRN nicer_but_slower_film_list.frm upd_film.TRN
[root@localhost sakila]# gedit db.opt
Copyright HP Education 25
MySQL deviates from how most database administrator
expect it to behave in the following ways:
Storage engines — Each table is an instantiation of a
storage engine. Different tables can have different
storage engines. Different storage engines function
differently with regard to performance, ACID
(atomicity, consistency, isolation, durability)
compliance.
Copyright HP Education 26
Errors — MySQL makes attempts to make sense of
what should throw an error. By default, mysqld
automatically allows inserting invalid data,
automatically truncates data that is too large for a
data type, implicitly converts data and more.
The sql_mode servervariable can be set to change most
of this type of behavior.
Copyright HP Education 27
■ String comparison — By default, strings are
compared in the order determined by the collation
■ SCHEMA — is an alias to DATABASE in the
CREATE SCHEMA, ALTER SCHEMA and DROP
SCHEMA statements. MySQL does not support the
AUTHORIZATION clause.
■ MySQL does not support the concept of catalogs. The
INFORMATION_SCHEMA database has many
system views with fields relating to catalogs where
the value is NULL.
Copyright HP Education 28
Foreign key constraints — MySQL accepts foreign key
constraints in table definitions, but only tables using
transactional storage engines (such as InnoDB and
Falcon) actually implement foreign key checking. All
other storage engine types disregard foreign key
constraint definitions without producing an error.
Privileges and permissions —MySQL uses the GRANT
and REVOKE syntax as specified in the SQL
standard, with some changes already mentioned
(such as lack of domains and thus a lack of DOMAIN
privileges)
Copyright HP Education 29
Transaction management — Transaction management
is partially supported in MySQL. Transactions are
only supported when using tables defined with
transactional storage engines, such as InnoDB and
Falcon.
Copyright HP Education 30