To create a database
Expt. No.21
Page No.42
Create a database
Aim
To create a database named school in MySQL, check that it exists, and open it.
Concepts used
Queries
CREATE DATABASE school;SHOW DATABASES;USE school;
A marked line continues the line above it. In your file, write it on the same line.
Output
mysql> CREATE DATABASE school;
Query OK, 1 row affected
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
| test |
+--------------------+
6 rows in set
mysql> USE school;
Database changed
✓ Printed by running this query set, not typed by hand.
Result
CREATE DATABASE made the school database, SHOW DATABASES listed it among the server's databases, and USE made it the current database.
Viva voce: what the examiner may ask
01What is a database?
An organised collection of related data, here stored as tables.
02What does USE do?
It selects the database that later commands will work on.
03What happens if you create a database that already exists?
MySQL gives an error; CREATE DATABASE IF NOT EXISTS school; avoids it.
Variations for the lab test
- Create a database named library.
- Use
CREATE DATABASE IF NOT EXISTStwice and see that no error appears. - Drop the library database.
! Common mistake Forgetting the semicolon at the end: the MySQL prompt shows
-> and waits for the rest of the command.