MySQL Commands
MySQL Account Permissions:
|
ALL |
All permissions |
|
ALTER |
Can alter the structure of
tables. |
|
CREATE |
Can create new databases, tables
or indexes. |
|
DELETE |
Can delete rows in tables. |
|
DROP |
Can drop databases or
tables. |
|
FILE |
Can read and write files
on the server. (File access on server
host.) |
|
GRANT |
Can change the permissions
on a MySQL account. |
|
INSERT |
Can insert new rows into
tables. |
|
REVOKE |
Can remove permissions on
a MySQL account. |
|
SELECT |
Can read data from tables. |
|
SHUTDOWN |
Can shut down the MySQL server. |
|
UPDATE |
Can change data in a
table. |
|
USAGE |
No permissions. |
Database and table queries:
ALTER TABLE tableName (followed by specified changes)
ALTER
TABLE tblMembers MODIFY lastName
VARCHAR(50)
ALTER TABLE tblMembers
CHANGE fName firstName VARCHAR(20)
|
ADD columnName
definition |
Adds a column; definition includes the data type and
optional definitions. |
|
ALTER columnName SET DEFAULT value |
Changes the default value
for a column. |
|
ALTER columnName DROP DEFAULT |
Removes the default value
fro a column. |
|
CHANGE columnName newColumnName definition |
Changes the definition of
a column and renames the column; definition
includes the data type and optional definitions. |
|
DROP columnName |
Deletes a column,
including all the data in the column. The
data can’t be recovered. |
|
MODIFY columnName
definition |
Changes the definition of
a column; definition includes the
data type and optional definitions. |
|
RENAME newTableName |
Renames a table. |
CREATE DATABASE databaseName
CREATE TABLE
tableName (
columnName
definition,
columnName definition,
PRIMARY KEY(columnName) )
CREATE TABLE tableName (
loginName VARCHAR(20) NOT
NULL,
password VARCHAR(20) NOT
NULL,
createDate DATE NOT NULL,
lastName VARCHAR(25),
fnameName VARCHAR(20),
Email VARCHAR(50),
addr VARCHAR(50),
city VARCHAR(30),
state CHAR(2),
zip VARCHAR(10),
hPhone CHAR(12),
wPhone CHAR(12),
cPhone CHAR(12),
PRIMARY
KEY(loginName) )
|
CHAR (length) |
Fixed-length character
string. |
|
VARCHAR (length) |
Variable-length character string
with length being the longest
string that can be stored. Length must be between 1 and 255. |
|
TEXT |
Variable-length character
string with a maximum length of 64K. |
|
INT (length) |
Integer range: -2147483648
to +2147483647. If length is 4, only numbers from -999 to
9999 can be displayed , even though higher numbers
are stored. |
|
BIGINT |
A larger integer. The signed range is -9223372036854775808 to
9223372036854775808. |
|
DECIMAL (length, dec) |
Decimal number: length is number of characters,
dec is
the maximum number of decimal places. Ex: 12.34 has a length of
5 and dec
value of 2. |
|
DATE |
Date value of year, month
and day; displayed as YYYY-MM-DD. |
|
TIME |
Time value of hours,
minutes and seconds; displayed as HH:MM:SS. |
|
DATETIME |
Date and time stored
together; displayed as YYYY-MM-DD HH:MM:SS. |
|
ENUM (“val1”, “val2”, …) |
Only the values listed can
be stored. A maximum of 65,535 values
can be listed. Ex: ENUM (“Yes”, “No”) |
|
SERIAL |
A shortcut name for: BIGINT
UNSIGNED NOT NULL AUTO_INCREMENT. |
|
NOT NULL |
This column must have a
value; it can’t be empty. |
|
DEFAULT value |
The value is stored in the column when the row is created if no other
value is given for this column. |
|
AUTO_INCREMENT |
This definition will
create a sequence number. As each row
is added, the value of this column increases by one integer. You can override the auto number by
assigning a specific value. |
|
UNSIGNED |
Used for numeric values
that will never be negative numbers. Ex: An unsigned integer
can have the range of 0 to 4294967295. |
DESCRIBE tableName
DROP DATABASE databaseName
DROP TABLE tableName
SHOW DATABASES
SHOW TABLES
Adding data into databases:
INSERT INTO tableName (columnNmae, columnName, … , columnName)
VALUES
(value, value, … ,
value)
·
Example:
INSERT INTO tblMembers (loginName, password, createDate, lastName, fristName, fax)
VALUES (“nester”, “password”,
“2007-Dec-2”, “Smith”, “Bill”, “”)
1. Values must be listed in the same order in which the
column names are listed.
2. A partial column list is allowed. Columns not listed are given their default
value or left blank.
3. A column list is not required. If no columns are listed, MySQL
will look for values for all the columns, in the order which they appear in the
table.
4. The column list and value list must be the same
length. If the list of columns is longer
or shorter than the list of values, you get an error message.
5. If the NOT NULL definition is assigned to a column,
and no value is entered for that column, you will receive an error since MySQL will not allow this.
LOAD DATA INFILE “path/fileName”
INTO TABLE tableName
LOAD DATA LOCAL INFILE “path/fileName” INTO TABLE tableName
1. The path/filename must include a path to the
file. Ex: “c:/data/datafile1.txt”
2. If an error message is received, ensure LOCAL is
enabled.
|
FIELDS TERMINATED BY ‘character’ |
|
FIELDS ENCLOSED BY ‘character’ |
|
LINES TERMINATED BY ‘character’ |
|
Ex: LOAD DATA INFILE
“path/fileName” INTO TABLE tableName FIELDS TERMINATED BY ‘,’ |
Retrieving data in databases:
SELECT * FROM tableName
SELECT
columnName, columnName, … FROM tableName
|
SELECT lastName,firstName
FROM tblMembers
|
|
SELECT price,price*1.08
FROM tblSales
SELECT price,price*1.08
AS priceWithTax FROM tblSales
|
|
AVG (columnName) |
Returns the average of all
the values in columnName. |
|
COUNT (columnName) |
Returns the number of rows
in which columnName
is not blank. |
|
MAX (columnName) |
Returns the largest value
in ColumnName. |
|
MIN (columnName) |
Returns the smallest value
in ColumnName. |
|
SUM (columnName) |
Returns the sum of all the
values in ColumnName. |
|
SQRT (columnName) |
Returns the square root of
each value in the ColumnName. |
|
DAYNAME (columnName) |
Returns the week day name
for each value in ColumnName. |
SELECT MAX(price)
FROM tblSales
SELECT * FROM tableName WHERE expression AND|OR expression AND|OR expression …
SELECT lastName,firstName FROM tblMembers
WHERE lastName LIKE “B%”
AND
city = “
AND
(phone LIKE “%8%” OR fax LIKE “%8%”
LIMIT startNumber,numberOfRows
1. If the startNumber is not specified, 1 becomes the default value.
2. Example:
SELECT
* FROM tblMembers WHERE state=”TX” LIMIT 3
(Used
to select only the first 3 members who live is TX.)
Example:
SELECT DISTINCT petType
FROM tblPet ORDER BY petType
Combining information from tables:
SELECT query UNION ALL SELECT query
SELECT lastName,firstName FROM tblMembers
UNION ALL
SELECT lastName,firstName FROM tblOldMembers
1. Note: The inclusion of ALL can create duplicate
records if contained on both tables. To
eliminate duplicate records, don’t include ALL in your query.
(SELECT lastName,firstName FROM tblMembers
UNION ALL
SELECT lastName,firstName FROM tblOldMembers)
ORDER BY lastName
1. Note: Using the parentheses as above, the ORDER BY
query will apply to the entire result set.
Example:
SELECT
columnNameList FROM table1, table2
WHERE
table1.col2 = table2.col2
Example: If table1 contains a row for Joe and a row
for Sally, and table2 contains only a row for Sally, an inner join would
contain only one row: the row for Sally.
However, an outer join would contain two rows – a row for Joe and a row
for Sally – even though the row for Joe would have a blank field for weight.
Example:
SELECT columnNameList FROM table1 LEFT JOIN table2
ON
table1.col2 = table2.col2
SELECT columnNameList FROM table1 RIGHT JOIN table2
ON
table1.col2 = table2.col2
SELECT
loginName FROM tblMembers
LEFT JOIN tblLogin
ON
tblMembers.loginName=tblLogin.loginName
WHERE
tblLogin.loginName IS NULL
Updating information:
UPDATE tableName SET columnName=value, columnName=value, …
WHERE clause
UPDATE tblMembers
SET addr=”
WHERE loginName=”jsmith”
Removing information:
DELETE FROM tableName WHERE clause