Learning MySQL Language Structure
Learning MySQL Language Structure
Session 2
Ramkumar Lakshminarayanan
Copyright HP Education 1
MySQL Vs 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’;
Copyright HP Education 3
Comments and Portability
Copyright HP Education 5
Escape characters
Copyright HP Education 6
Naming limitations and quoting
Copyright HP Education 7
Identifiers can be almost anything. However, identifiers
may not end with one or more spaces:
Copyright HP Education 8
Some common errors we normally miss
Copyright HP Education 9
String parsing error
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)
Copyright HP Education 12
Strict SQL Mode
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
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
Copyright HP Education 16
Character sets and collations
MySQL supports many different character sets and
collations.
Copyright HP Education 17
Colliation
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 | |
+-------------------+------------+
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
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.
Copyright HP Education 27
■ String comparison — By default, strings are
compared in the order determined by the collation
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