I practiced using common database and table operations. The database operations team for my organization had configured a relational database instance and asked me to practice creating and dropping (deleting) databases and tables.
After completion, I learned how to:
By the end, I completed some common database and table operations. I created a database and tables, and used SHOW, ALTER, and DROP statements.
Sample data in this is taken from Statistics Finland, general regional statistics, February 4, 2022.
In this task, I connected to an EC2 instance configured with a database client. This client is used to run structured query language (SQL) queries against a relational database. This instance is referred to as the Command Host.
sudo su
cd /home/ec2-user/
Tips:
mysql -u root --password='re:St@rt!9'
The MySQL command-line client is an SQL shell that you can use to interact with database engines.
| Switch | Description |
|---|---|
| -u or --user | The MySQL user name used to connect to a database instance |
| -p or --password | The MySQL password used to connect to a database instance |
I ran the following cumulative command:
In this task, I created a database named world and a table named country. I then altered the country table.
SHOW DATABASES;
To determine the available database and to ensure that I was working with the correct database instance, I used the SHOW DATABASES; command.
CREATE DATABASE world;
SHOW DATABASES;
CREATE TABLE world.country (
`Code` CHAR(3) NOT NULL DEFAULT '',
`Name` CHAR(52) NOT NULL DEFAULT '',
`Conitinent` enum('Asia','Europe','North
America','Africa','Oceania','Antarctica','South America') NOT NULL
DEFAULT 'Asia',
`Region` CHAR(26) NOT NULL DEFAULT '',
`SurfaceArea` FLOAT(10,2) NOT NULL DEFAULT '0.00',
`IndepYear` SMALLINT(6) DEFAULT NULL,
`Population` INT(11) NOT NULL DEFAULT '0',
`LifeExpectancy` FLOAT(3,1) DEFAULT NULL,
`GNP` FLOAT(10,2) DEFAULT NULL,
`GNPOld` FLOAT(10,2) DEFAULT NULL,
`LocalName` CHAR(45) NOT NULL DEFAULT '',
`GovernmentForm` CHAR(45) NOT NULL DEFAULT '',
`HeadOfState` CHAR(60) DEFAULT NULL,
`Capital` INT(11) DEFAULT NULL,
`Code2` CHAR(2) NOT NULL DEFAULT '',
PRIMARY KEY (`Code`)
);
USE world;
SHOW TABLES;
SHOW COLUMNS FROM world.country;
Notice that the Continent column is spelled incorrectly as Conitinent.
ALTER TABLE world.country RENAME COLUMN Conitinent TO Continent;
SHOW COLUMNS FROM world.country;
I created a table named city and added two columns named Name and Region. Both columns used the CHAR data type:
CREATE TABLE world.city (`Name` CHAR(52), `Region` CHAR(26));
In this task, I deleted the world database and country table.
DROP TABLE world.city;
I wrote a query to drop the country table:
DROP TABLE world.country;
SHOW TABLES;
DROP DATABASE world;
SHOW DATABASES;
I have now successfully: