MySQL PDF
MySQL PDF
What is MySQL?
MySQL is (as of 2008) the world's most widely used open source relational database management
system (RDBMS) that runs as a server providing multi-user access to a number of databases. It is
named after co-founder Michael Widenius' daughter, My. The SQL phrase stands for Structured Query
Language.
The MySQL development project has made its source code available under the terms of the GNU
General Public License, as well as under a variety of proprietary agreements. MySQL was owned and
sponsored by a single for-profit firm, the Swedish company MySQL AB, now owned by Oracle
Corporation.
MySQL is a popular choice of database for use in web applications, and is a central component of the
widely used LAMP open source web application software stack (and other 'AMP' stacks). LAMP is an
acronym for "Linux, Apache, MySQL, Perl/PHP/Python." Free-software-open source projects that
require a full-featured database management system often use MySQL.
For commercial use, several paid editions are available, and offer additional functionality. Applications
which use MySQL databases include: TYPO3, Joomla, WordPress, phpBB, MyBB, Drupal and other
software. MySQL is also used in many high-profile, large-scale websites, including Wikipedia, Google
(though not for searches), Facebook, Twitter, Flickr, and YouTube.
Text types:
Data type Description
CHAR(size) Holds a fixed length string (can contain letters, numbers, and special characters).
The fixed size is specified in parenthesis. Can store up to 255 characters
VARCHAR(size) Holds a variable length string (can contain letters, numbers, and special
characters). The maximum size is specified in parenthesis. Can store up to 255
characters. Note: If you put a greater value than 255 it will be converted to a
TEXT type
TINYTEXT Holds a string with a maximum length of 255 characters
TEXT Holds a string with a maximum length of 65,535 characters
BLOB For BLOBs (Binary Large OBjects). Holds up to 65,535 bytes of data
MEDIUMTEXT Holds a string with a maximum length of 16,777,215 characters
MEDIUMBLOB For BLOBs (Binary Large OBjects). Holds up to 16,777,215 bytes of data
LONGTEXT Holds a string with a maximum length of 4,294,967,295 characters
LONGBLOB For BLOBs (Binary Large OBjects). Holds up to 4,294,967,295 bytes of data
ENUM(x,y,z,etc.) Let you enter a list of possible values. 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.
Note: The values are sorted in the order you enter them.
You enter the possible values in this format: ENUM('X','Y','Z')
SET Similar to ENUM except that SET may contain up to 64 list items and can store
more than one choice
1
MIS-410, MySQL Basics EWU, Summer -2013
Number types:
*The integer types have an extra option called UNSIGNED. Normally, the integer goes from an
negative to positive value. Adding the UNSIGNED attribute will move that range up so it starts at zero
instead of a negative number.
Date types:
Data type Description
DATE() A date. Format: YYYY-MM-DD
Note: The supported range is from '1000-01-01' to '9999-12-31'
DATETIME() *A date and time combination. Format: YYYY-MM-DD HH:MM:SS
Note: The supported range is from '1000-01-01 00:00:00' to '9999-12-31
23:59:59'
TIMESTAMP() *A timestamp. TIMESTAMP values are stored as the number of seconds since the
Unix epoch ('1970-01-01 00:00:00' UTC). Format: YYYY-MM-DD HH:MM:SS
Note: The supported range is from '1970-01-01 00:00:01' UTC to '2038-01-09
03:14:07' UTC
TIME() A time. Format: HH:MM:SS
Note: The supported range is from '-838:59:59' to '838:59:59'
YEAR() A year in two-digit or four-digit format.
Note: Values allowed in four-digit format: 1901 to 2155. Values allowed in two-
digit format: 70 to 69, representing years from 1970 to 2069
*Even if DATETIME and TIMESTAMP return the same format, they work very differently. In an INSERT
or UPDATE query, the TIMESTAMP automatically set itself to the current date and time. TIMESTAMP
also accepts various formats, like YYYYMMDDHHMMSS, YYMMDDHHMMSS, YYYYMMDD, or YYMMDD.
2
MIS-410, MySQL Basics EWU, Summer -2013
MySQL:
MySQL:
Let's illustrate the foreign key with an example. Look at the following two tables:
3
MIS-410, MySQL Basics EWU, Summer -2013
Note that the "P_Id" column in the "Orders" table points to the "P_Id" column in the "Persons" table.
The "P_Id" column in the "Persons" table is the PRIMARY KEY in the "Persons" table.
The "P_Id" column in the "Orders" table is a FOREIGN KEY in the "Orders" table.
The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables.
The FOREIGN KEY constraint also prevents that invalid data form being inserted into the foreign key
column, because it has to be one of the values contained in the table it points to.
MySQL:
4
MIS-410, MySQL Basics EWU, Summer -2013
MySQL:
If you define a CHECK constraint on a single column it allows only certain values for this column.
If you define a CHECK constraint on a table it can limit the values in certain columns based on values
in other columns in the row.
MySQL:
To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple columns, use
the following SQL syntax:
MySQL:
The default value will be added to all new records, if no other value is specified.
The DEFAULT constraint can also be used to insert system values, by using functions like GETDATE():
6
MIS-410, MySQL Basics EWU, Summer -2013
MySQL:
MySQL:
7
MIS-410, MySQL Basics EWU, Summer -2013
Notice that the new column, "DateOfBirth", is of type date and is going to hold a date. The data type
specifies what type of data the column can hold. For a complete reference of all the data types
available in MS Access, MySQL, and SQL Server, go to our complete Data Types reference.
Notice that the "DateOfBirth" column is now of type year and is going to hold a year in a two-digit or
four-digit format.
8
MIS-410, MySQL Basics EWU, Summer -2013
By default, the starting value for AUTO_INCREMENT is 1, and it will increment by 1 for each new
record.
To let the AUTO_INCREMENT sequence start with another value, use the following SQL statement:
To insert a new record into the "Persons" table, we will not have to specify a value for the "P_Id"
column (a unique value will be added automatically):