Lesson 21

Creating Tables

In this lesson you learn how to set up detail specifications of MySQL tables. First you learn about the different data types and attributes and then you learn how to use that information to create the tables.

You learn what the different data types are and how to assign them to fields. You learn how to set up a field so that it is automatically assigned a value when a row is added, whether that is a MySQL-calculated value to be used as an arbitrary key or a constant value to be used as a default.

You also learn advanced functions in phpMyAdmin to create and change tables and how to do the same thing in PHP. Finally, you learn how to create and use a text file to perform MySQL commands.

Understanding Data Types

Just as in PHP, MySQL has different data types for the fields. The data types in MySQL are stricter than in PHP and there is not a one-to-one correlation.

Strings

There are two types of strings in MySQL. The first is text strings, which have character sets and collations. This is the type of string that you use most often. Text strings are further defined as follows:

  • CHAR: This is the character data type. You define exactly how many characters are stored. For example, if you want a field to be exactly six characters long, you define it as CHAR(6). If you pass it data that is less than that, it pads with spaces at the end. If you pass it more, the extra characters are truncated. Whether you are truncating blanks or non-blank characters and what ...

Get PHP and MySQL® 24-Hour Trainer now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.