Mysql commands: Difference between revisions

From KlavoWiki
Jump to navigationJump to search
Line 88: Line 88:
Looking at the Data in a Particular Table<br>
Looking at the Data in a Particular Table<br>
<pre>
<pre>
select * from [tablename];
select * from tablename;
</pre>
</pre>


Line 95: Line 95:
<pre>
<pre>
SELECT * FROM (
SELECT * FROM (
     SELECT * FROM tablename ORDER BY date DESC LIMIT 50
     SELECT * FROM tablename ORDER BY date DESC LIMIT 10
) sub
) sub
ORDER BY date ASC
ORDER BY date ASC;
</pre>
</pre>



Revision as of 00:41, 18 November 2014

Handy Cheat-Sheet of MySQL Commands

From your login shell

Securing MySQL

mysql_secure_installation

Creating a Database

mysqladmin create mydatabase

Dropping (Removing) a Database

mysqladmin drop mydatabase

Import Database

Populating an Existing Database from a *.sql File

mysql databasename < filename.sql

Export Database

To a Local file

Dumping Database Structure and Data to a *.sql file

mysqldump databasename > filename.sql
mysqldump databasename table1 > table1.sql

To a Remote Server

mysqldump <-uUserName> <-pMyPassword> [DatabaseName] | ssh root@myremoteserver.mydomain.com "cat > /tmp/DatabaseName.sql"

User Management

List Users

mysql -B -N -pMyPassword -e "SELECT user, host FROM user" mysql

List User Permissions

mysql -B -N -pMyPassword -e "SHOW GRANTS FOR 'MyUserName'"

From within the MySQL interface

MySQL CLI

Starting MySQL from the Command Line

mysql -uusername -pMyPassword

You will be welcomed with the following message:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is ## to server version: #.##.##

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

The prompt changes to "mysql>", which will be shown in each example below.

Show Databases

Seeing What Databases are Available

show databases;

(be sure to use the semi-colon to terminate the command)

Select Database

Telling MySQL to Use a Specific Database

use mydatabase;


Show Tables

Seeing What Tables are Available Within a Database

show tables;

List Data from a Table

Looking at the Data in a Particular Table

select * from tablename;


Select the last 10 entries by column date

SELECT * FROM (
    SELECT * FROM tablename ORDER BY date DESC LIMIT 10
) sub
ORDER BY date ASC;

Export Data to CSV

SELECT * FROM databasename WHERE calldate >= DATE(20141028) AND calldate <= DATE(20141105) into outfile 'calls.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';

Note: outfile must be before the FIELDS, ENCLOSED and LINES parameters.

Rename Table

Rename an existing Table

RENAME TABLE tbl_name TO new_tbl_name

Delete Table

use databasename;
drop tablename;

User Management

Add User

Adding a Database User with Password

grant all privileges on [databasename].* to [dbusername]@localhost identified by '[dbpassword]';
grant all privileges on mydatabase.* to joeuser@localhost identified by 'supersecretpasswd';
flush privileges;

Delete User

Removing a Database User

delete from mysql.user where user='techgeek' and host='localhost';
flush privileges;

Data Manipulation

Add Data

Add data to a table

insert into cid  (name, number) values 
('My Name', '0731234321'),
('Mr Name', '0412344321');


Delete Data

delete from cid where name like '0%';

Search & Replace

Performing Search-and-Replace Actions on a Table

update DatabaseName set TableName = REPLACE(TableName,"[String to Search For]","[String Replacement]");
update roesublist set Grades = REPLACE(Grades,"Grades: Any","K,1,2,3,4,5,6,7,8,9,10,11,12");