MySQL Data Methods | WebReference

MySQL Data Methods

MySQL Data Methods

Excerpted from Chapter 3: Working with Data in MySQL from the MySQL, 3rd Edition by Paul Dubois. ISBN 0672326736, Copyright © 2005. Used with the permission of Sams Publishing.

VIRTUALLY EVERYTHING YOU DO in MySQL involves data in some way or another because the purpose of a database management system is, by definition, to manage data. Even a simple SELECT 1 statement involves expression evaluation to produce an integer data value.

Every data value in MySQL has a type. For example, 37.4 is a number and 'abc' is a string. Sometimes data types are explicit, such as when you issue a CREATE TABLE statement that specifies the type for each column you define as part of the table:

CREATE TABLE mytbl
(
   int_col INT, # integer-valued column
   str_col CHAR(20), # string-valued column
   date_col DATE # date-valued column
);

Other times data types are implicit, such as when you refer to literal values in an expression, pass values to a function, or use the value returned from a function.The following INSERT statement does all of those things:

INSERT INTO mytbl (int_col,str_col,date_col)
VALUES(14,CONCAT('a','b'),20050115);

The statement performs the following operations, all of which involve data types:

  • It assigns the integer value 14 to the integer column int_col.

  • It passes the string values 'a' and 'b' to the CONCAT() string-concatenation function. CONCAT() returns the string value 'ab', which is assigned to the string column str_col.

  • It assigns the integer value 20050115 to the date column date_col.The assignment involves a type mismatch, but the integer value can reasonably be interpreted as a date value, so MySQL performs an automatic type conversion that converts the integer 20050115 to the date '2005-01-15'.

To use MySQL effectively, it's essential to understand how MySQL handles data.This chapter describes the types of data values that MySQL can handle, and discusses the issues involved in working with those types:

  • The general categories of data values that MySQL can represent, including the NULL value.

  • The specific data types MySQL provides for table columns, and the properties that characterize each data type. Some of MySQL's data types are fairly generic, such as the BLOB string type. Others behave in special ways that you should understand to avoid being surprised.These include the TIMESTAMP data type and integer types that have the AUTO_INCREMENT attribute.

  • MySQL's capabilities for working with different character sets. Note: Support for multiple character sets was introduced beginning with MySQL 4.1, but underwent quite a bit of development during the early 4.1 releases. For best results, avoid early releases and use a recent 4.1 release instead.

  • How to choose data types appropriately for your table columns. It's important to know how to pick the best type for your purposes when you create a table, and when to choose one type over another when several related types might be applicable to the kind of values you want to store.

  • MySQL's rules for expression evaluation.You can use a wide range of operators and functions in expressions to retrieve, display, and manipulate data. Expression evaluation includes rules governing type conversion that come into play when a value of one type is used in a context requiring a value of another type. It's important to understand when type conversion happens and how it works; some conversions don't make sense and result in meaningless values. Assigning the string '13' to an integer column results in the value 13, but assigning the string 'abc' to that column results in the value 0 because 'abc' doesn't look like a number. Worse, if you perform a comparison without knowing the conversion rules, you can do considerable damage, such as updating or deleting every row in a table when you intend to affect only a few specific rows. MySQL 5.0 introduces "strict" datahandling mode, which enables you to cause bad data values to be rejected.

Two appendixes provide additional information that supplements the discussion in this chapter about MySQL's data types, operators, and functions.These are Appendix B,"Data Type Reference," and Appendix C,"Operator and Function Reference."

The examples shown throughout this chapter use the CREATE TABLE and ALTER TABLE statements extensively to create and alter tables.These statements should be reasonably familiar to you because we have used them in Chapter 1, "Getting Started with MySQL and SQL," and Chapter 2,"MySQL SQL Syntax and Use." See also Appendix E,"SQL Syntax Reference."

MySQL supports several table types, each of which is managed by a different storage engine, and which differ in their properties. In some cases, a column with a given data type behaves differently for different storage engines, so the way you intend to use a column might determine or influence which storage engine to choose when you create a table.This chapter refers to storage engines on occasion, but a more detailed description of the available engines and their characteristics can be found in Chapter 2.

Data handling also depends in some cases on how default values are defined and on the current SQL mode. For general background on setting the SQL mode, see "The Server SQL Mode," in Chapter 2. In the current chapter, default value handing is covered in "Specifying Column Default Values." Strict mode and the rules for treatment of bad data are covered in "How MySQL Handles Invalid Data Values."

Created: March 27, 2003
Revised: April 24, 2006

URL: http://webreference.com/programming/mysql_data/1