Professional Documents
Culture Documents
My SQL Commands
My SQL Commands
Returns the columns and column information pertaining to the designated table.
mysql> show columns from [table name];
Show certain selected rows with the value "whatever".
mysql> SELECT * FROM [table name] WHERE [field name] = "whatever";
Show all records containing the name "Bob" AND the phone number '3444444'.
mysql> SELECT * FROM [table name] WHERE name = "Bob" AND phone_number =
'3444444';
Show all records not containing the name "Bob" AND the phone number '3444444' order
by the phone_number field.
mysql> SELECT * FROM [table name] WHERE name != "Bob" AND phone_number =
'3444444' order by phone_number;
Show all records starting with the letters 'bob' AND the phone number '3444444'.
mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number =
'3444444';
Show all records starting with the letters 'bob' AND the phone number '3444444' limit to
records 1 through 5.
mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number =
'3444444' limit 1,5;
Use a regular expression to find records. Use "REGEXP BINARY" to force case-sensitivity.
This finds any record beginning with a.
mysql> SELECT * FROM [table name] WHERE rec RLIKE "^a";
Show unique records.
mysql> SELECT DISTINCT [column name] FROM [table name];
Show selected records sorted in an ascending (asc) or descending (desc).
mysql> SELECT [col1],[col2] FROM [table name] ORDER BY [col2] DESC;
Return number of rows.
to create tables on the already created databases. Check with your hosting provider to see what
the proper database names are and what the usernames and passwords are for the database(s).
Example:
CREATE DATABASE myMediaLibrary;
CREATE TABLE
Create a table on the database you are currently logged into.
Example:
CREATE TABLE IF NOT EXISTS `music` (
`id` int(8) unsigned NOT NULL,
`artist` text NOT NULL,
`album` text NOT NULL,
`title` text NOT NULL,
`track` text NOT NULL,
`year` varchar(8) NOT NULL,
PRIMARY KEY (`id`),
FULLTEXT KEY `artist` (`artist`),
FULLTEXT KEY `album` (`album`),
FULLTEXT KEY `title` (`title`),
FULLTEXT KEY `track` (`track`),
FULLTEXT KEY `year` (`year`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
The table set up will be similar to:
id
artist
album
title
tr y
ac e
k ar
NOTE: You may want to use AUTO INCREMENT to have MySQL automatically maintain the
id field when you add a new item to the table. This is up to you on how you would like to
maintain the id., which in this case is the "PRIMARY KEY" which needs to be unique and is
what MySQL needs to keep things orderly:
CREATE TABLE IF NOT EXISTS `music` (
`id` int(8) unsigned NOT NULL AUTO_INCREMENT,
`artist` text NOT NULL,
`album` text NOT NULL,
`title` text NOT NULL,
`track` text NOT NULL,
`year` varchar(8) NOT NULL,
PRIMARY KEY (`id`),
FULLTEXT KEY `artist` (`artist`),
FULLTEXT KEY `album` (`album`),
FULLTEXT KEY `title` (`title`),
FULLTEXT KEY `track` (`track`),
FULLTEXT KEY `year` (`year`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
Refer to the MySQL documentation for more info on defining column data types.
INSERT STATEMENTS
INSERT INTO table_name ( `col_A`, `col_B`, `col_C`) VALUES ( `col_A_data`, `col_B_data`,
`col_C_data`) ;
Inserts a row into the table using the data defined in the VALUES section.
As you can see, the column names are established within the first set of parenthesis, the order of
the data in the second set of parenthesis must match the order of the column names defined int he
first set of parentheses.
Example:
INSERT INTO music ( 'id', `artist`, `album`) VALUES ( '1', `the beatles`, `Abbey Road`);
artist
album
the
beatles
title
tr y
ac e
k ar
Abbey Road
I've color coded each "pair" so you can see that the data "col_B_data' that is going to go into
column name "col_B" must be second. if there is no data available for col_B, use empty single
quotes as:
INSERT INTO table_name ( `col_A`, `col_B`, `col_C`) VALUES ( `col A data `, ``, `col C
data`) ;
Example:
INSERT INTO music ( 'id', `artist`, `album`) VALUES ( '1', ``, `Abbey Road`);
Puts a row into the database tables as:
id
artist
album
title
tr y
ac e
k ar
Abbey Road
REPLACE STATEMENTS
REPLACE INTO table_name ( `col_A`, `col_B`) VALUES ( `col A data`, `col B data`) ;
Replaces items in a row. This action is dependant on the "id" because all tables need to have at
least one "unique" column. Meaning that one of the columns must be used to provide row data
that is unique to each row. In this example, "id" is the unique row.
Example:
REPLACE INTO music ( 'id', `artist`, `album`) VALUES ( '1', `the beatles`, `abbey road`);
Puts a row into the database tables as:
id
artist
album
the
beatles
title
tr y
ac e
k ar
Abbey Road
As you can see, we've just replaced the empty "artist" with the new data of "the beatles"
REPLACE behaves much like INSERT except that if the "unique column" is supplied, the
existing row will be updated, if the "unique column" is not provided, a new row will be added.
Example:
REPLACE INTO music ( `artist`, `album`) VALUES ( `the beatles`, `abbey road`);
Puts a new row into the database tables as:
id
artist
album
the
beatles
Abbey Road
the
beatles
Abbey Road
title
tr y
ac e
k ar
NOTE: You may get errors when trying this kind of REPLACE, because usually the "unique
column" can not be null (or empty). Some people like to set up the database where the "unique
column" is automatically incremented by MySQL. This is kind of confusing and can lead to a
headache. So just remember to include the "unique column" when using the REPLACE
statement, or you'll get duplicate rows... OR use the UPDATE statement.
UPDATE STATEMENTS
UPDATE table_name SET col_B='new_data' WHERE col_A='reference_data' ;
Will update a row's columns with the new values specified in the SET section and the row to
update is specified by the WHERE section
Example:
UPDATE music SET title='Come Together' WHERE id=1;
Notice that quotes are not around the id-1. This is because you shouldn't have to use quotes when
you are entering an integer.
id
artist
the
beatles
album
title
Abbey Road
Come Together
tr y
ac e
k ar
To update two or more columns, separate value pairs in the SET section with a comma
Example:
UPDATE music SET track='1', year='1969' WHERE id=1;
Notice that quotes are not around the id-1. This is because you shouldn't have to use quotes when
you are entering an integer.
id
artist
the
beatles
album
title
Abbey Road
Come Together
tr y
ac e
k ar
1
19
69
It may seem frustrating to see that quotes were used around the "integers' in the SET section, but
not in the WHERE section. Sometimes you may need to use them, but sometimes you do not. A
llot of it depends on how the database was originally set up. If we initially set up the database
where "track" and "year" did not use TEXT, but rather INT(11) we could probably get away with
not using quotes around the data in the SET section. This issue also has to do with how MySQL
is set up on your server.
Another thing to consider is that because we're using single quotes around the data that we are
entering... what if your data contains a single quote? Such as:
UPDATE music SET title='Mike's Song' WHERE id=1;
As you can see, now there are three single quotes, and chances are MySQL will return an error.
There are two things you can try.
1. Escape the single quote in the data as:
UPDATE music SET title='Mike\'s Song' WHERE id=1;
1. Use tricky quotes:
UPDATE music SET title=`Mike's Song` WHERE id=1;
Tricky quotes are just above the "tab" button on your keyboard.
SELECT STATEMENTS
SELECT * FROM table_name WHERE 1 ;
Returns all rows and all columns from table_name.. All rows is specified by the * (asterisk)
symbol between SELECT and FROM.
Example:
SELECT * FROM music WHERE 1;
Returns:
id
artist
album
title
tr y
ac e
k ar
1
the
beatles
the
beatles
the
beatles
the
beatles
the
beatles
the
beatles
the
beatles
the
beatles
the
Abbey Road
Something
Abbey Road
Maxwell's Silver
Hammer
1
9
6
9
1
9
6
9
1
9
6
9
1
9
6
9
1
9
6
9
1
9
6
9
1
9
6
9
Oh! Darling
Abbey Road
Octopus's Garden
Abbey Road
Abbey Road
1
9
6
9
Come Together
Abbey Road
Abbey Road
Abbey Road
Because
Abbey Road
beatles
Money
10
11
12
13
14
15
16
17
the
beatles
the
beatles
the
beatles
the
beatles
the
beatles
the
beatles
the
beatles
the
beatles
9
6
9
Sun King
10 1
9
6
9
11 1
9
6
9
Abbey Road
Polythene Pam
12 1
9
6
9
Abbey Road
13 1
9
6
9
Golden Slumbers
14 1
9
6
9
15 1
9
6
9
Abbey Road
The End
16 1
9
6
9
Abbey Road
Her Majesty
Abbey Road
Abbey Road
Abbey Road
Abbey Road
17 1
9
6
9
18
beatles
Rubber Soul
19
20
21
22
23
24
25
26
1
9
6
9
1
9
6
9
1
9
6
9
1
9
6
9
1
9
6
9
Michelle
beatles
beatles
The Word
beatles
Rubber Soul
1
9
6
9
beatles
Rubber Soul
Nowhere Man
beatles
Rubber Soul
1
9
6
9
beatles
Rubber Soul
2
Norwegian Wood (This
Bird Has Flown)
beatles
Rubber Soul
1
9
6
9
Drive My Car
beatles
Rubber Soul
Rubber Soul
What Goes On
Rubber Soul
Girl
9
6
9
27
28
29
beatles
Rubber Soul
10 1
9
I'm Looking Through You
6
9
Rubber Soul
In My Life
11 1
9
6
9
Wait
12 1
9
6
9
If I Needed Someone
13 1
9
6
9
14 1
9
6
9
beatles
beatles
Rubber Soul
30
beatles
Rubber Soul
31
beatles
Rubber Soul
id
artist
album
title
the
beatles
Abbey Road
Come Together
the
beatles
Abbey Road
Something
18
beatles
Rubber Soul
Drive My Car
19
beatles
Rubber Soul
tr y
ac e
k ar
1
19
69
19
69
19
69
19
69
album
the beatles
Abbey Road
Example:
SELECT title, album FROM music WHERE title='You Won\'t See Me', 'I\'m Looking Through
You';
NOTE: Notice that the single quotes are "escaped" by using a back slash within the data. You
may want to try using tricky quotes instead as:
SELECT album, date FROM music WHERE title=`You Won't See Me`, `I'm Looking Through
You`;
Returns:
album
date
Rubber Soul
1969
Rubber Soul
1969
Example:
SELECT * FROM music WHERE title RLIKE ('you') OR title RLIKE ('mustard') ;
Returns:
id
artist
album
title
tr y
ac e
k ar
the
beatles
Abbey Road
19
69
the
beatles
Abbey Road
19
69
20
beatles
Rubber Soul
19
69
27
beatles
Rubber Soul
10 19
69
11
the
beatles
Abbey Road
11 19
69
Example:
SELECT * FROM music WHERE title RLIKE ('you') AND title RLIKE ('me') ;
Returns:
id
artist
the
beatles
20
beatles
album
title
Abbey Road
Rubber Soul
tr y
ac e
k ar
9
19
69
19
69
NOTE: In this example, we are only searching within the same column, you can search different
columns if you want, and you can also add more OR or AND statements, or you can mix and
match OR and AND statements to refine the search..
Example:
SELECT * FROM music WHERE title RLIKE ('you') OR artist RLIKE ('the') AND track
RLIKE ('1969');
SELECT * FROM table_name WHERE 1 ORDER BY RAND() LIMIT
number_of_rows_to_return ;
Will select random rows and limit the number of rows returned to the number specified in the
LIMIT section.
Example:
SELECT * FROM music WHERE 1 ORDER BY RAND() LIMIT 3;
Returns something similar to :
(Can't be too sure here, since MySQL is picking rows randomly, but you can see that only three
rows are returned :)
id
25
artist
beatles
album
title
tr y
ac e
k ar
Rubber Soul
What Goes On
1
9
6
9
13
the
beatles
the
beatles
Abbey Road
13 1
9
6
9
5
Abbey Road
Octopus's Garden
1
9
6
9
If we do something like:
SELECT DISTINCT artist FROM music WHERE 1;
... because the for the first album, we used "the beatles" whereas the second album, we just used
"beatles" -- so only the first row that MySQL encounters with unique data in the "artist" column
will be returned... because the rest of the rows are not unique.
album
beatles
Rubber Soul
the beatles
Abbey Road
Ray Lamontagne
Trouble
Wheat kings
The Police
Outlandos d'Amour
NOTE: Yes, both "the beatles" and "beatles" albums would be returned because the entire data
contained in the "artist" column is unique for both of these albums.
SELECT MAX(col_A) FROM table_name ;
Will return the highest value for that column. Usually you want to sue this on a column that
references a number. For example, let's say you wanted to find the highest ID on your table so
that you could determine the next ID to use fro a new item that you are inserting into your tables.
You could use the MAX statement to find the largest ID number, then when you INSERT your
new item, you can simply bump up the number by one.
Example:
SELECT MAX(id) FROM music;
Returns:
id
31
... because the highest ID in our table is 31. e.g. there are 31 items in our table as defined by the
ID column.
If we ran something like:
SELECT MAX(track) FROM music ;
... we'd get:
track
17
DELETE STATEMENTS
DELETE FROM table_name WHERE column_name='search_data';
Will remove rows that match the search data defined in the WHERE section
Example:
DELETE FROM music WHERE artist='the beatles';
Will remove all rows that have "the beatles" defined in the "artist" column.
DELETE FROM table_name WHERE column_name IN ('search_data_A', 'search_data_B');
Will remove all rows that match the search data defined in the IN section.
Example:
DELETE FROM music WHERE artist IN ('the beatles', 'beatles');
... will remove all the items from our example table