Nothing Special   »   [go: up one dir, main page]

Adding Database Tables and Data To A Mysql Database: Their Respective Owners

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

academy.oracle.

com

Adding database tables and data to a MySQL


database.

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of
their respective owners.
2

Using the MySQL Community Edition database.


In this tutorial you will learn how to carry out basic SQL commands in the Community Edition
of the MySQL server so that it can be used as the data source for a Java application using
JDBC.
Accessing the MySQL database.
1. Open the MySQL directory on the windows application list and select MySQL 8.0 Command
Line Client to access the database through the command line.

2. Enter root as the password.


3. To view the available databases on the MySQL server, enter the following command (the
semi colon completes the command and must be included):
show databases;

4. You are going to create a database that will hold some HR information about its employees.
Enter the following command:
create database hr;

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of
their respective owners.
3

5. Set the new hr database to be the one in use by entering the following command:
use hr;

6. You have been provided with a SQL script that will populate the hr database with tables
and data. Copy the MySQL_Schema_HR.sql file into a sql directory within the
JavaProgramming directory on the C Drive.
Run the script by entering the following command:
source c:/JavaProgramming/sql/MySQL_Schema_HR.sql;

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of
their respective owners.
4

7. To view the created tables, enter the following command:


show tables;

8. To view the contents of each table you can run the following select command:
SELECT *
FROM departments;

Substitute the departments table for employees, job_history and jobs to view the content
of the remaining tables.
Accessing the MySQL database properties.
Now that the required data is in the database the following commands will be useful for when
you want to connect to the database via JDBC.
9. To view the current username and database location enter the following command:
select user();

In this example you are logged in as root to the localhost database.

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of
their respective owners.
5

10. To view the port number (required to access the database from Eclipse) enter the following
command:

SHOW variables
WHERE variable_name = 'port';

This shows the default port address of 3306 for a MySQL database. Take a note of the
settings that you have returned from your system as you will need them later.

11. Type exit to close the command line interface (not the database that is running in the
background as a service).

exit;

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of
their respective owners.

You might also like