Commands Description Samples: Joshua B. Posawen IT 223
Commands Description Samples: Joshua B. Posawen IT 223
POSAWEN
IT 223
Commands Description Samples
-h hostname -u root -p To login (from unix shell) use - [Mysql dir]/bin/mysql-h
h only if needed. hostname -u root -p
create database Create a database on the sql create database 2ADB;
[databasename]; server.
show databases; List all databases on the sql show databases;
server.
use [db name]; Switch to a database. use 2ADB;
show tables; To see all the tables in the db. show tables;
describe [table name]; To see database's field describe 2ADB;
formats.
drop database [database To delete a db. drop database 2ADB;
name];
drop table [table name]; To delete a table. drop table Students;
SELECT * FROM [table name]; Show all data in a table. SELECT * FROM Students;
show columns from [table Returns the columns and show columns from Students;
name]; column information pertaining
to the designated table.
SELECT * FROM [table name] Show certain selected rows SELECT * FROM Students
WHERE [field name] = with the value "whatever". WHERE name = "Bob";
"whatever";
SELECT * FROM [table name] Show all records containing SELECT * FROM Students
WHERE name = "Bob" AND the name "Bob" AND the WHERE name = "Bob" AND
phone_number = '3444444'; phone number '3444444'. phone_number = '3444444';
SELECT * FROM [table name] Show all records not SELECT * FROM Students
WHERE name != "Bob" AND containing the name "Bob" WHERE name != "Bob" AND
phone_number = '3444444' AND the phone number phone_number = '3444444'
order by phone_number; '3444444' order by the order by phone_number;
phone_number field.
SELECT * FROM [table name] Show all records starting with SELECT * FROM Students
WHERE name like "Bob%" the letters 'bob' AND the WHERE name like "Bob%"
AND phone_number = phone number '3444444'. AND phone_number =
'3444444'; '3444444';
JOSHUA B. POSAWEN
IT 223
Commands Description Samples
SELECT * FROM [table name] Use a regular expression to find SELECT * FROM Students
WHERE rec RLIKE "^a$"; records. Use "REGEXP BINARY" WHERE rec RLIKE "^a$";
to force case-sensitivity. This
finds any record beginning with
a.
SELECT DISTINCT [column Show unique records. SELECT DISTINCT IDnum
name] FROM [table name]; FROM [table name];
SELECT [col1],[col2] FROM Show selected records sorted in SELECT [col1],[col2] FROM
[table name] ORDER BY an ascending (asc) or descending [table name] ORDER BY [col2]
[col2] DESC; (desc). DESC;
INSERT INTO [table name] Switch to mysql db.Give user INSERT INTO [table name]
(Host,Db,User,Select_priv,In privilages for a db. (Host,Db,User,Select_priv,Ins
sert_priv,Update_priv,Delet ert_priv,Update_priv,Delete_
e_priv,Create_priv,Drop_pri priv,Create_priv,Drop_priv)
v) VALUES VALUES
('%','db','user','Y','Y','Y','Y','Y', ('%','db','user','Y','Y','Y','Y','Y','
'N'); N');
JOSHUA B. POSAWEN
IT 223
UPDATE [table name] SET To update info already in a table. UPDATE [table name] SET
Select_priv = 'Y',Insert_priv = Select_priv = 'Y',Insert_priv =
'Y',Update_priv = 'Y' where 'Y',Update_priv = 'Y' where
[field name] = 'user'; [field name] = 'user';