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
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
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
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
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
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
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:
7. Query the new database
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
- Instructions for installing MySQL on Windows (and remote access)
2. Delete MySQL database
3. Learn PHP and MySQL
Learning PHP and MySQL will help you create entertainment and business websites.
4. Back up MySQL database
Data backup is recommended, especially with important databases.
5. Change the database structure
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
- 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
- How to Connect to MySQL Using PHP
May be interested
- How to install MySQL on Ubuntu 20.04in this article, tipsmake will show how to install mysql version 8.0 on ubuntu 20.04 server. by completing it, you'll have an active relational database that can be used to build your next website or app.
- How to install and configure MySQL server on Pidatabases like mysql are often the primary component of dynamic web pages and one of the best ways to store data for web applications. mysql is a database management system that allows you to store and maintain large amounts of data with ease.
- How to install MySQL Workbench Community Edition on Windows 10detailed instructions on how to install mysql workbench community edition on windows 10 to manage operations with mysql database management system more easily ..
- Working with Rockmongorockmongo is a mongodb management tool. using it, you can manage your server, database, collection, document, index, .... it provides a very friendly way for users to read, write and create documents. rockmongo is quite similar to phpmyadmin tool for php and mysql.
- Oracle improves MySQL on Windowsoracle has updated the commercial version of mysql database for windows, added a graphical installer and the ability to perform failover clustering, ...
- Guide to creating Virtual Hosting with PureFTPd and MySQLin the following article, tipsmake.com will show you how to install the pureftpd server using virtual user accounts from mysql's database, all inside the real system.
- MySQL vulnerabilities allow malicious servers to steal data from customerssomeone can take advantage of this problem to steal sensitive data from an improperly configured web server, allowing connection to untrusted servers or from database management applications. .
- Compare the performance of MongoDB and SQL Server 2008in the following article, we will point out the difference in performance of the relatively new database management system, which is mongodb and one of the popular systems like sql server (mysql or oracle). most of the data is given here in the form of charts, so we can easily imagine the advantages and disadvantages of mongodb as well as other database management systems.
- Steps to Import data from different tables in MySQL Workbenchinstructions on how to export and import data from different tables in a mysql database through the mysql workbench tool
- How to Send Sql Queries to Mysql from the Command Linea simple text-based program called mysql should have been part of your mysql installation. it lets you send sql queries directly to the mysql server and output the results in text format. it is a quick and easy way to test your mysql...