banner



How To Create Database In Mysql Using Php Code

Summary: in this tutorial, you will acquire how to utilize the MySQL CREATE DATABASE statement to create a new database on a MySQL database server.

Introduction to the MySQL CREATE DATABASE argument

To create a new database in MySQL, yous use the CREATE DATABASE argument with the following syntax:

            

CREATE DATABASE [IF NOT EXISTS] database_name [Grapheme SET charset_name] [COLLATE collation_name]

Code linguistic communication: SQL (Structured Query Language) ( sql )

In this syntax:

  • First, specify name of the database after the the CREATE DATABASE keywords. The database name must be unique within a MySQL server instance. If you try to create a database with a name that already exists, MySQL will event an error.
  • Second, use the IF Non EXISTS choice to conditionally create a database if information technology doesn't exist.
  • Third, specify the character set and collation for the new database. If you skip the Graphic symbol SET and COLLATE clauses, MySQL will the default character prepare and collation for the new database.

Creating a new database using the mysql client tool

To create a new database via the mysql client tool, you lot follow these steps:

Offset, log in to the MySQL Server using a user account that has the CREATE DATABASE privilege:

            

mysql -u root -p

Code language: SQL (Structured Query Language) ( sql )

It'll prompt yous for entering a countersign. To cosign, you need to type the password for the root user account and printing the Enter key.

Side by side, display the current databases available on the server using the SHOW DATABASES statement. This step is optional.

            

Testify DATABASES;

Output:

            

+--------------------+ | Database | +--------------------+ | classicmodels | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ v rows in ready (0.00 sec)

Lawmaking linguistic communication: JavaScript ( javascript )

So, upshot the CREATE DATABASE command with a database name e.chiliad., testdb and press Enter:

            

CREATE DATABASE testdb;

It'll return the following:

            

Query OK, 1 row affected (0.02 sec)

Code language: CSS ( css )

After that, employ the Testify CREATE DATABASE command to review the created database:

            

Evidence CREATE DATABASE testdb;

Code language: SQL (Structured Query Language) ( sql )

MySQL returns the database name and the character ready and collation of the database:

            

+----------+----------------------------------------------------------------------------------------------------------------------------------+ | Database | Create Database | +----------+----------------------------------------------------------------------------------------------------------------------------------+ | testdb | CREATE DATABASE `testdb` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='North' */ | +----------+----------------------------------------------------------------------------------------------------------------------------------+ one row in set (0.01 sec)

Lawmaking language: JavaScript ( javascript )

Finally, select the newly created database to piece of work with by using the Apply argument:

            

USE testdb;

Code language: SQL (Structured Query Language) ( sql )

Output:

            

Database inverse

Now, you can showtime creating tables and other databases objects inside thetestdb database.

To quit the mysql programme, type exit control:

            

exit

Code language: PHP ( php )

Output:

            

Bye

Creating a new database using MySQL Workbench

To create a new database using the MySQL Workbench, yous follow these steps:

First, launch the MySQL Workbench and click the setup new connection button as shown in the following screenshot:

2nd, type the name for the connection and click the Test Connectedness push button.

MySQL Workbench displays a dialog asking for the password of the root user:

Y'all need to (1) type the password for the root user, (2) check the Salve countersign in vault, and (iii) click OK push.

Third, double-click the connection name Local to connect to the MySQL Server.

MySQL Workbench opens the following window which consists of four parts: Navigator, Query, Data, and Output.

Fourth, click the create a new schema in the continued server button from the toolbar:

In MySQL, the schema is the synonym for the database. Creating a new schema as well means creating a new database.

Fifth, the following window is open up. You need to (i) enter the schema proper noun, (two) change the graphic symbol set and collation if necessary, and click theApply button:

6th, MySQL Workbench opens the following window that displays the SQL script which will exist executed. Note that the CREATE SCHEMA statement command has the same effect as the CREATE DATABASE statement.

If everything is fine, y'all will run across the new database created and showed in the schemas tab of the Navigator section.

Seventh, to select the testdb2 database, (one) right-click the database name and (2) choose Set equally Default Schema carte du jour item:

The testdb2 node is open up as shown in the following screenshot.

Now, you can piece of work with testdb2 from the MySQL Workbench.

Summary

  • Use the CREATE DATABASE statement to create a new database.
  • In MySQL, schemas are synonyms of databases.

Was this tutorial helpful?

How To Create Database In Mysql Using Php Code,

Source: https://www.mysqltutorial.org/mysql-create-database/

Posted by: motenbobyth.blogspot.com

0 Response to "How To Create Database In Mysql Using Php Code"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel