How to create database mysql

How to create database mysql

How to create database mysql

Once you know how to enter SQL statements, you are ready to access a database.

Suppose that you have several pets in your home (your menagerie) and you would like to keep track of various types of information about them. You can do so by creating tables to hold your data and loading them with the desired information. Then you can answer different sorts of questions about your animals by retrieving data from the tables. This section shows you how to perform the following operations:

Create a database

Load data into the table

Retrieve data from the table in various ways

Use multiple tables

The menagerie database is simple (deliberately), but it is not difficult to think of real-world situations in which a similar type of database might be used. For example, a database like this could be used by a farmer to keep track of livestock, or by a veterinarian to keep track of patient records. A menagerie distribution containing some of the queries and sample data used in the following sections can be obtained from the MySQL website. It is available in both compressed tar file and Zip formats at https://dev.mysql.com/doc/.

Use the SHOW statement to find out what databases currently exist on the server:

The mysql database describes user access privileges. The test database often is available as a workspace for users to try things out.

The list of databases displayed by the statement may be different on your machine; SHOW DATABASES does not show databases that you have no privileges for if you do not have the SHOW DATABASES privilege. See Section 13.7.7.14, “SHOW DATABASES Statement”.

If the test database exists, try to access it:

where your_mysql_name is the MySQL user name assigned to you and your_client_host is the host from which you connect to the server.

How to create database mysql

Once you know how to enter SQL statements, you are ready to access a database.

Suppose that you have several pets in your home (your menagerie) and you would like to keep track of various types of information about them. You can do so by creating tables to hold your data and loading them with the desired information. Then you can answer different sorts of questions about your animals by retrieving data from the tables. This section shows you how to perform the following operations:

Create a database

Load data into the table

Retrieve data from the table in various ways

Use multiple tables

The menagerie database is simple (deliberately), but it is not difficult to think of real-world situations in which a similar type of database might be used. For example, a database like this could be used by a farmer to keep track of livestock, or by a veterinarian to keep track of patient records. A menagerie distribution containing some of the queries and sample data used in the following sections can be obtained from the MySQL website. It is available in both compressed tar file and Zip formats at https://dev.mysql.com/doc/.

Use the SHOW statement to find out what databases currently exist on the server:

The mysql database describes user access privileges. The test database often is available as a workspace for users to try things out.

The list of databases displayed by the statement may be different on your machine; SHOW DATABASES does not show databases that you have no privileges for if you do not have the SHOW DATABASES privilege. See Section 13.7.5.14, “SHOW DATABASES Statement”.

If the test database exists, try to access it:

where your_mysql_name is the MySQL user name assigned to you and your_client_host is the host from which you connect to the server.

How to create database mysql

CREATE DATABASE is not permitted within a session that has an active LOCK TABLES statement.

Each create_option specifies a database characteristic. Database characteristics are stored in the data dictionary.

The CHARACTER SET option specifies the default database character set. The COLLATE option specifies the default database collation. For information about character set and collation names, see Chapter 10, Character Sets, Collations, Unicode.

The ENCRYPTION option, introduced in MySQL 8.0.16, defines the default database encryption, which is inherited by tables created in the database. The permitted values are ‘Y’ (encryption enabled) and ‘N’ (encryption disabled). If the ENCRYPTION option is not specified, the value of the default_table_encryption system variable defines the default database encryption. If the table_encryption_privilege_check system variable is enabled, the TABLE_ENCRYPTION_ADMIN privilege is required to specify a default encryption setting that differs from the default_table_encryption setting. For more information, see Defining an Encryption Default for Schemas and General Tablespaces.

A database in MySQL is implemented as a directory containing files that correspond to tables in the database. Because there are no tables in a database when it is initially created, the CREATE DATABASE statement creates only a directory under the MySQL data directory. Rules for permissible database names are given in Section 9.2, “Schema Object Names”. If a database name contains special characters, the name for the database directory contains encoded versions of those characters as described in Section 9.2.4, “Mapping of Identifiers to File Names”.

Creating a database directory by manually creating a directory under the data directory (for example, with mkdir ) is unsupported in MySQL 8.0.

When you create a database, let the server manage the directory and the files in it. Manipulating database directories and files directly can cause inconsistencies and unexpected results.

MySQL has no limit on the number of databases. The underlying file system may have a limit on the number of directories.

MySQL CREATE DATABASE

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

Introduction to the MySQL CREATE DATABASE statement

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

Creating a new database using the mysql client tool

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

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

It’ll prompt you for entering a password. To authenticate, you need to type the password for the root user account and press the Enter key.

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

Then, issue the CREATE DATABASE command with a database name e.g., testdb and press Enter:

It’ll return the following:

After that, use the SHOW CREATE DATABASE command to review the created database:

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

Finally, select the newly created database to work with by using the USE statement:

Now, you can start creating tables and other databases objects within the testdb database.

To quit the mysql program, type exit command:

Creating a new database using MySQL Workbench

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

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

How to create database mysql. Смотреть фото How to create database mysql. Смотреть картинку How to create database mysql. Картинка про How to create database mysql. Фото How to create database mysql

Second, type the name for the connection and click the Test Connection button.

How to create database mysql. Смотреть фото How to create database mysql. Смотреть картинку How to create database mysql. Картинка про How to create database mysql. Фото How to create database mysql

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

How to create database mysql. Смотреть фото How to create database mysql. Смотреть картинку How to create database mysql. Картинка про How to create database mysql. Фото How to create database mysql

You need to (1) type the password for the root user, (2) check the Save password in vault, and (3) click OK button.

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

How to create database mysql. Смотреть фото How to create database mysql. Смотреть картинку How to create database mysql. Картинка про How to create database mysql. Фото How to create database mysql

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

How to create database mysql. Смотреть фото How to create database mysql. Смотреть картинку How to create database mysql. Картинка про How to create database mysql. Фото How to create database mysql

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

How to create database mysql. Смотреть фото How to create database mysql. Смотреть картинку How to create database mysql. Картинка про How to create database mysql. Фото How to create database mysql

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

Fifth, the following window is open. You need to (1) enter the schema name, (2) change the character set and collation if necessary, and click the Apply button:

How to create database mysql. Смотреть фото How to create database mysql. Смотреть картинку How to create database mysql. Картинка про How to create database mysql. Фото How to create database mysql

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

How to create database mysql. Смотреть фото How to create database mysql. Смотреть картинку How to create database mysql. Картинка про How to create database mysql. Фото How to create database mysql

If everything is fine, you will see the new database created and showed in the schemas tab of the Navigator section.

How to create database mysql. Смотреть фото How to create database mysql. Смотреть картинку How to create database mysql. Картинка про How to create database mysql. Фото How to create database mysql

Seventh, to select the testdb2 database, (1) right-click the database name and (2) choose Set as Default Schema menu item:

How to create database mysql. Смотреть фото How to create database mysql. Смотреть картинку How to create database mysql. Картинка про How to create database mysql. Фото How to create database mysql

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

How to create database mysql. Смотреть фото How to create database mysql. Смотреть картинку How to create database mysql. Картинка про How to create database mysql. Фото How to create database mysql

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

How to create database mysql

Once you know how to enter SQL statements, you are ready to access a database.

Suppose that you have several pets in your home (your menagerie) and you would like to keep track of various types of information about them. You can do so by creating tables to hold your data and loading them with the desired information. Then you can answer different sorts of questions about your animals by retrieving data from the tables. This section shows you how to perform the following operations:

Create a database

Load data into the table

Retrieve data from the table in various ways

Use multiple tables

The menagerie database is simple (deliberately), but it is not difficult to think of real-world situations in which a similar type of database might be used. For example, a database like this could be used by a farmer to keep track of livestock, or by a veterinarian to keep track of patient records. A menagerie distribution containing some of the queries and sample data used in the following sections can be obtained from the MySQL website. It is available in both compressed tar file and Zip formats at https://dev.mysql.com/doc/.

Use the SHOW statement to find out what databases currently exist on the server:

The mysql database describes user access privileges. The test database often is available as a workspace for users to try things out.

The list of databases displayed by the statement may be different on your machine; SHOW DATABASES does not show databases that you have no privileges for if you do not have the SHOW DATABASES privilege. See Section 13.7.5.15, “SHOW DATABASES Statement”.

If the test database exists, try to access it:

where your_mysql_name is the MySQL user name assigned to you and your_client_host is the host from which you connect to the server.

Источники информации:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *