MySQL Notes

Basic Functions

Start MySQL: (as root) /etc/init.d/mysql start (or status/stop/restart)

Open Database: (as carl) mysql -u carl -p databasename

Create new database: create database databasename;

Show list of existing databases: show databases;

Use a specific database: use databasename;

Create a new table: create table tablename (fieldname1 datatype, fieldname2 datatype);

Add 1 record: insert into tablename (fieldname1, fieldname2) values ('Bob', 'Jones');

Add 3 records: insert into tablename (fieldname1, fieldname2) values ('Bob', 'Jones'), ('Sam', 'Adams'), ('Bill', 'Schmatz');

Select all records unsorted: select * from tablename;

Select specific records and sort: select fieldname1, fieldname2 from tablename order by fieldname2;
Add desc at the end to reverse sort order

Quit MySQL: \q;

Data Types

Data TypeDescription
CHAR[length]Fixed length field from 0 to 255 characters long
VARCHAR[length]Variable length field from 0 to 65,535 characters long
TINYTEXTA string with a maximum of 255 characters
TEXTA string with a maximum of 65,535 characters
MEDIUMTEXTA string with a maximum of 16,777,215 characters
LONGTEXTA string with a maximum of 4,294,967,295 characters
TINYINT[Length]Range of -128 to 127 or 0 to 255 unsigned
SMALLINT[Length]Range of -32,768 to 32,767 or 0 to 65,535 unsigned
MEDIUMINT[Length]Range of -8,388,608 to 8,388,607 or 0 to 16,777,215 unsigned
INT[Length]Range of -2,147,483,648 to 2,147,483,647
or 0 to 4,294,967,295 unsigned
BIGINT[Length]Range of -9,233,372,036,854,775,808 to 9,233,372,036,854,775,807
or 0 to 18,446,744,073,709,551,615 unsigned
FLOAT[Length, Decimals]Small number with a floating point decimal
DOUBLE[Length, Decimals]Large number with a floating point decimal
DECIMAL[Length, Decimals]DOUBLE stored as a string, allowing for a fixed decimal point
DATEIn the format of YYYY-MM-DD
DATETIMEIn the format of YYYY-MM-DD HH:MM:SS
TIMESTAMPIn the format of YYYYMMDDHHMMSS; acceptable range
ends in the year 2037
TIMEIn the format HH:MM:SS
ENUMShort for enumeration, which means that each column
can have one of several possible values
SETLine ENUM except that each column can have more then one of
several possible values