How to create a database in MySQL
Follow the instructions below to create a database of US state populations. Invite you to consult!
Method 1: Create and manipulate on a database
1. Create the database
How to create a database in MySQL Picture 1
From the MySQL command line, enter the CREATE DATABASE ;
command data_name CREATE DATABASE ;
. Replace data_name by your database name. This name cannot contain spaces.
- For example: To create a database for all US states, you can use the command:
CREATE DATABASE us_states;
- Note: Do not capitalize commands. All MySQL commands must be terminated with a " ; " sign. If you forget to put the semicolon in the command, you simply add it to the next line to execute the previous command.
2. Display the list of existing databases
How to create a database in MySQL Picture 2
Enter the SHOW DATABASES;
command SHOW DATABASES;
to list all the databases you have. Besides the database you just created, you will see the mysql database and test database. At this point, you can "ignore" them.
3. Select your database
How to create a database in MySQL Picture 3
Once you have created the database, you need to select it to start the editing process. Enter USE us_states;
command USE us_states;
. The notice Database changed
( Database changed
has been changed) will appear, indicating that the database has been activated.
4. Create a data table
How to create a database in MySQL Picture 4
Table is the place to store database information. To create the table, you need to enter the entire format of the table into the original command. To create the data table, enter the following command: CREATE TABLE states (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, state CHAR(25), population INT(9));
. This command will help create a table named "states" consisting of three fields: id
(code), state
(state) and population
.
- The
INT
command ensures that the code field contains only numbers (integers). - The
NOT NULL
command ensures that the code field is not left blank. - The
PRIMARY KEY
command specifies the code field as the key field of the table. Key fields should be fields without any duplication. - The
AUTO_INCREMENT
command will automatically assign the increased values to the code, basically automatically numbering each data entry. - The
CHAR
(character) andINT
(integer) instructions specify the type of data allowed to enter these fields. The next number represents the maximum number of characters or integers that the school can receive.
5. Enter data into the table
How to create a database in MySQL Picture 5
When creating the table successfully, start entering information. Use the command below to enter the first data: INSERT INTO states (id, state, population) VALUES (NULL, 'Alabama', '4822023');
.
Basically, this command requires the database to enter the information given into the three corresponding fields on the data table. Since the code section containing the NOT NULL
statement, thanks to the AUTO_INCREMENT
command, entering NULL
as the parameter value will force it to automatically get a value of 1.
6. Enter more data
How to create a database in MySQL Picture 6
You can import multiple streams of data at the same time with one command. To enter the next three states, use the command: INSERT INTO states (id, state, population) VALUES (NULL, 'Alaska', '731449'), (NULL, 'Arizona', '6553255'), (NULL, 'Arkansas', '2949131');
.
This command will create a data table as shown below:
How to create a database in MySQL Picture 7
7. Query the new database
How to create a database in MySQL Picture 8
When the basic database is set up, you can enter query commands to retrieve specific results. First, enter the command: SELECT * FROM states;
. This command will return the entire database, as shown by the " * " command - meaning "whole".
- For advanced queries, enter the command:
SELECT state, population FROM states ORDER BY population;
. This command will return the data table with the states sorted by population field value instead of alphabetical order. The code field will not be displayed because you only retrieve datastate
andpopulation
. - To list the state's population in reverse order, enter the command:
(SELECT state, population FROM states ORDER BY population DESC);
. TheDESC
command will list them by descending value, from high to low instead of low.
Method 2: Learn more about MySQL
1. Install MySQL on Windows-based computers
How to create a database in MySQL Picture 9
- Instructions for installing MySQL on Windows (and remote access)
2. Delete MySQL database
How to create a database in MySQL Picture 10
3. Learn PHP and MySQL
How to create a database in MySQL Picture 11
Learning PHP and MySQL will help you create entertainment and business websites.
4. Back up MySQL database
How to create a database in MySQL Picture 12
Data backup is recommended, especially with important databases.
5. Change the database structure
How to create a database in MySQL Picture 13
If the need to use the database changes, you may have to adjust the structure to handle other information.
Having fun!
You should read it
- Instructions for installing MySQL on Windows and remote access
- How to Check Database Size in MySQL
- A serious vulnerability on phpMyAdmin allows an attacker to destroy the database
- Instructions on how to connect to MySQL Database in Eclipse
- How to Create a Secure Session Management System in PHP and MySQL
- Backup and restore MySQL with mysql-zrm on Debian Sarge
- How to Learn PHP and MySQL
- Save your database with Recovery Toolbox for MySQL
- How to Create a Table in MySQL
- Best tools for designing and administering Mysql databases
- How to Connect to MySQL Using PHP
- How to install MySQL on Ubuntu 20.04
May be interested
Write and run Java code on the computer for the first time
75 great user interface design ideas you need to know - Part 2
Which operating system should I choose to use when programming?
5 interesting features about Java 9 you may not know yet
Learn Ruby programming from 0
6 useful tips from 11-year experienced programmers