MySql CreateDB Tables
MySql CreateDB Tables
Limit to 64 characters
Use underscore (_) to separate words. Do Not use space
End primary keys and foreign keys with id
List primary key first in the table, then foreign keys
Field names must be descriptive
Field names must be unique across every table, except for keys
Remain consistent with your naming conventions
Create tables
To create a table, we have to define the name of the table, the columns and the constraints
(primary key, foreign key, and indexes), the data type, type of table (MyISAM, BDB, InnoDB,
Memory).
If you do not specify table type, MySQL will automatically create the table in default
type: MyISAM. Later we will learn about types of tables in MySQL.
TEXT TYPES
BLOB A string /Binary data with a maximum length of 65535 characters (64
KB).
MEDIUMBLOB A string / Binary Data with a maximum length of 16777215 characters (16
MB).
The ( ) brackets allow you to enter a maximum number of characters will be used in the column.
E.g. VARCHAR(20)
CHAR and VARCHAR are the most widely used types. CHAR is a fixed length string and is
mainly used when the data is not going to vary much in it's length. VARCHAR is a variable
length string and is mainly used when the data may vary in length.
Page 4
CHAR may be faster for the database to process considering the fields stay the same length down
the column. VARCHAR may be a bit slower as it calculates each field down the column, but it
saves on memory space. Which one to ultimately use is up to you.
Using both a CHAR and VARCHAR option in the same table, MySQL will automatically
change the CHAR into VARCHAR for compatibility reasons.
BLOB stands for Binary Large OBject. Both TEXT and BLOB are variable length types that
store large amounts of data. They are similar to a larger version of VARCHAR. These types can
store a large piece of data information (images and sound files), but they are also processed
much slower.
e.g. Photo BLOB, (you assume that all photos stored in the database will be less than 64 KB)
NUMBER TYPES
FLOAT A small number with a floating decimal point. Uses 4 bytes of storage.
DOUBLE( , ) A large number with a floating decimal point. Uses 8 bytes of storage.
Supports greater precision or more decimal places than Float.
Double and float are approximate data types – values are rounded
according to the length of the column and decimal specifications.
DECIMAL( , ) A DOUBLE stored as a string , allowing for a fixed decimal point with
(length) and (Decimals) specifications.
Only used when you need storage accurate information – Financial info.
Price Decimal (7,2) – Price Decimal (length =7, Decimals = 2).
Weight Float (8,4) – Weight Float (length = 8, decimals = 4)
Number types can be UNSIGNED – limiting the column to positive numbers or zero, or be
defined as ZEROFILL – any extra room will be padded with zeros. ZEROFILLS are also
automatically UNSIGNED.
Page 5
The integer types have an extra option called UNSIGNED. Normally, the integer goes from a
negative to positive value. Using an UNSIGNED command will move that range up so it starts at
zero instead of a negative number.
DATE TYPES
DATE - YYYY-MM-DD.
TIME - HH:MM:SS.
MISC TYPES
ENUM ( ) Short for ENUMERATION. This column can only store one of the values
that are declared in the specified list contained in the ( ) brackets.
ENUM('y','n') You can list up to 65535 values in an ENUM list. If a value is
inserted that is not in the list, a blank value will be inserted.
SET similar to ENUM except each column may have more than one of the specified
possible values.
SET may contain up to 64 list items and can store more than one choice.
NOT NULL: NULL value means that the field has no value or it is unknown. NOT
NULL enforces that a value has to be added to the column.
DEFAULT: When a large portion of your database will have the same value, preset a
default value. Example: gender ENUM (‘M’, ‘F’) DEFAULT ‘F’
AUTO_INCREMENT: sets the value to the next logical value in the series
Page 6
The DVD rentals database keeps a track of inventory of DVDs, provides information about
DVDs, records rental transactions and stores the names customers and employees
Exercise 2:
See create_tables_dvdrentals1.txt
Insert data
With this method you will populate only columns that matter. For any column not
given a value, the value will be treated as NULL or the established default value). This method
is preferable for populating the database.
Method 2: Insert into tablename Values (‘value 1’, ‘value 2’, NULL, ‘value 4’, ……);
With this method, you must specify a value for every column, even if any column
has a null or default value. You will cause an error if the values do not match the number of
columns.
String values must be entered with quotation marks and numbers must be entered without
any quotation marks.
See insertdata_dvdrentals