bembry.org
Home / Technology / Web_development / Php

MySQL Commands

Conventions
All commands in MySQL end with a semicolon ;

Starting MySQL
If the MySQL is not started, type:
/etc/rc.d/init.d/mysql start

show databases;
Lists the databases on the system. show may also be used to show "tables".

use dbasename;
Select database to use.

describe table;
Describes all the fields in a table.

Field Types:
DATE -- Date field.
TEXT -- Text of any size. Like "memo" in Access.
VARCHAR (255) -- Character string of variable length up to (maximum length).
CHAR (255) -- Character string of exact size (size).
INT -- Integer

Special Field Attributes:
BINARY -- Makes the field case sensitive. Only works with CHAR adn VARCHAR.
DEFAULT value -- Lets you set a default value if no value is passed to the field.
AUTO-INCREMENT -- Automatically add a number in this field, and increment it by one for each new record. Essentially a counter.
PRIMARY KEY (fieldname) -- Declares one field to be the primary key.

Creating Tables
CREATE TABLE tablename (field1 VARCHAR(255), field2 CHAR (2));
Enter as many fields as you want, using the names you want for the fields. Obviously the fields can be any type, not just CHAR or VARCHAR.

Adding Data
INSERT INTO table VALUES (field1, field2, ... field n);
Creates new record in table with values assigned. Fields are in order determined when you first create the table. Use DESCRIBE tablename to find out order of data fields.

Updating Data
UPDATE table SET field = value WHERE field2 = value2;
Updates information in database. Restricted access