You are on page 1of 3

Lab 2

Learning objectives:
 Learn how to create empty database
 Learn how to show available databases
 Learn how to use a database
 Learn how to delete a database
 Learn how to create a database from script file
 Learn how to show tables in a database
 Learn how to list columns in a table

2.1 Create an empty database


CREATE DATABASE statement is used to create a new database.
Syntax
CREATE DATABASE databasename;

Notice that you need a semi-colon to end the command.

Create a database called “salesdata” type the following:


mysql> CREATE DATABASE salesdata;

Create another database called “Lab1” type the following:


mysql> CREATE DATABASE Lab1;

2.2 Show currently available databases


SHOW DATABASES command lists the databases on the MySQL server host.
Show the databases that you currently have access to:
mysql> SHOW DATABASES;

2.3 Use one of the available databases


USE statement
Selects a particular database to work with from available databases.
Execute USE statement to begin using the “salesdata” database:
mysql> USE SALECO;
2.4 Delete a database
DROP DATABASE Statement
Drops (deletes) a particular database from server.
Dropping a database will result in loss of complete information stored in the database.
Syntax
DROP DATABASE databasename;

Delete the “Lab1” database:


mysql> DROP DATABASE Lab1;

Show the databases that you currently have access to and see that “Lab1” database is no longer
available:
mysql> SHOW DATABASES;

2.5 Create a database from script file


SOURCE command
Script files contain any MySQL client-readable commands that can be directly
executed from the file.
Syntax
SOURCE filename.sql;

Create the “Northwind” database from a script file:


mysql> SOURCE C:\Northwind.sql

Copy the script file into the directory C:\. If your files are located in a different directory then use
the full path accordingly. Also notice that no semicolon is used at the end of statement.

Show the databases that you currently have access to and see that “Northwind” database is
available:
mysql> SHOW DATABASES;

2.6 Show the tables in a database


SHOW TABLES command
Lists the tables in a given database.
Syntax
SHOW TABLES;

Begin using the “Northwind” database:


mysql> USE northwind;

Show the tables in the “Northwind” database:


mysql> SHOW TABLES;

2.7 Show the columns in a table


SHOW COLUMNS command
Displays the information about the columns in a given table.
Syntax
SHOW COLUMNS FROM tablename;

Show the columns in the “Products” table:


mysql> SHOW COLUMNS FROM Products;

You might also like