Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 13

Full Stack

Web Programming
Seven Advanced Academy

RDBMS - MySQL

Lesson 78
Connect to MySQL from the command line

● To connect to MySQL from the command line, follow these steps:


● At the command line, type the following command, replacing USERNAME
with your username:

mysql -u USERNAME -p
Connect to MySQL from the command line/Cont.

● At the Enter Password prompt, type your password. When you type the
correct password, the mysql> prompt appears.

● To display a list of databases, type the following command at the mysql>


prompt:

show databases;
Connect to MySQL from the command line/Cont.

● To access a specific database, type the following command at the mysql>


prompt, replacing DBNAME with the database that you want to access:

use databases;
Connect to MySQL from the command line/Cont.

● After you access a database, you can run SQL queries, list tables, and so on.
Additionally:
● To view a list of MySQL commands, type help at the mysql> prompt.
● To exit the mysql program, type \q at the mysql> prompt.
Connect to MySQL from the command line/Cont.

● When you run a command at the mysql> prompt, you may receive a
warning message if MySQL encounters a problem. For example, you may
run a query and receive a message that resembles the following:

Query OK, 0 rows affected, 1 warning (0.04 sec).


Connect to MySQL from the command line/Cont.

● To view the complete warning message, type the following command:

SHOW WARNINGS;
The SQL CREATE DATABASE Statement

● The CREATE DATABASE statement is used to create a new SQL database.

CREATE DATABASE databasename;

//Example

CREATE DATABASE testDB;


The SQL DROP DATABASE Statement.

● The DROP DATABASE statement is used to drop an existing SQL


database.
DROP DATABASE databasename;

//Example

DROP DATABASE testDB;

● Note: Be careful before dropping a database. Deleting a database will result


in loss of complete information stored in the database!
The SQL BACKUP DATABASE Statement

● The BACKUP DATABASE statement is used in SQL Server to create a full


backup of an existing SQL database. (Note this command should be run
outside of mysql)
#Syntax

mysqldump -u user_name -p your_password database_name > File_name.sql

#Example
mysqldump -u root -p secret testDB > desktop/backfile.sql
The SQL BACKUP DATABASE Statement/Cont

● Tip: Always backup the database to a different drive than the actual
database. If you get a disk crash, you will not lose your backup file along
with the database.
BACKUP WITH DIFFERENTIAL Example

● The following SQL statement creates a differential back up of the database


"testDB":
BACKUP DATABASE testDB
TO DISK = 'D:\backups\testDB.bak'
WITH DIFFERENTIAL;

● Tip: A differential backup reduces the backup time (since only the changes
are backed up).
Congratulation!

You might also like