Creating a table
Operation confirmation environment
- MySQL
-
- MySQL 8.4
- MySQL Workbench
-
- 8.0
- Windows
-
- Windows Server 2022
Required environment
- MySQL
-
- MySQL 8.4 or later
- Previous versions are also acceptable
- MySQL Workbench
-
- 8.0 or later
- Windows
-
- Windows 11
- Windows 10
- Windows Server
-
- Windows Server 2022
- Windows Server 2019
- Windows Server 2016
precondition
- Make sure you can connect to your MySQL database
- If you want to use Workbench, Workbench must be installed
- You must have created a schema
- Connect to the database with permissions to create tables
About the sample schema
test_schema
In this case, we will operate on a schema named .
Creating a table in Workbench
Start MySQL Workbench and connect to the database.
Select the Schema tab in the Navigator pane to expand the schema tree of interest. Right-click on Tables and select Create Table.
The Create Table screen will appear, so you can enter the required items.
Table
Project Name | Content |
---|---|
Table Name | Enter the table name. In the case of English characters, it will be lowercase, so it is better to enter in lowercase letters from the beginning. |
Charaset/Collation | If there is no reason to change it, you can leave it as Default. |
Engine | If there is no reason to change it, InnoDB is fine. |
Comments | This is a comment from the table. You don't have to put it in, but it will be useful later. |
Column
Enter as many columns as you need in the table.
Project Name | Content |
---|---|
Column Name | It is the column name. In the case of English characters, it will be lowercase, so it is better to enter in lowercase letters from the beginning. |
Datatype | Set the type of data to be stored in the column. |
PK, NN, etc. | Primary key, Not Null, etc. The explanation will be long, so I will omit it. |
Default/Expression | Include the value that you want to set by default if the record was null when you created it. |
Comments | Column comments. You don't have to put it in, but it will be useful later. |
You can also create indexes, etc., but I will omit it because it would be a long explanation for creating a table. It is possible to change it later.
Once you've entered your table settings, click the "Apply" button in the bottom right corner.
You will see the SQL you want to run, so click the "Apply" button.
Once it runs without errors, click the "Finish" button.
I think you can confirm that the table is created from the schema tree. You can close the tab you used to create a new one.
Creating a table with a command
Select MySQL > MySQL 8.4 Command Line Client from the Start menu. If you can run the command, you can run it with another tool.
Enter your administrator password to log in.
Enter SQL like this: Replace the schema name, table name, column information, etc. with the content you want to create. You can use the Enter key to type across multiple lines. At the end of the SQL, enter a semicolon that indicates the end of the SQL.
CREATE TABLE `test_schema`.`test_table2` (
`id` INT NOT NULL COMMENT 'ユニークなIDが入ります。',
`name` VARCHAR(45) NULL COMMENT '名前です。',
PRIMARY KEY (`id`),
UNIQUE INDEX `id_UNIQUE` (`id` ASC) VISIBLE)
COMMENT = 'テストコメント';
Once typed, run it with the Enter key. If there are no errors, you are OK.
You can see a list of the tables you created by running the following SQL: Please match the schema name.
SHOW TABLES FROM test_schema;