Friday 13 November 2020

SQL: Specify the database you want to work

Using the command ‘use {DATABASE_NAME}’, you can specify the database that you want to work with.

Step 1: Connect to mysql server by executing the command 'mysql -u root -p'. 

bash-3.2$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 43
Server version: 8.0.21 Homebrew

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

 

Step 2: Execute the command ‘show databases’ to get all the databases.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| demo               |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql>

Step 3: Use demo database by executing the command ‘use demo’.

mysql> use demo;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql>

 

Step 4: Create a table customer by executing below command.

 

CREATE TABLE customer (id INT, name VARCHAR(20));

mysql> CREATE TABLE customer (id INT, name VARCHAR(20));
Query OK, 0 rows affected (0.02 sec)

mysql> 
mysql> describe employee;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int         | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

 

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment