Site icon TestingDocs.com

MySQL CREATE TABLE Statement

Overview

In this tutorial, we will learn about MySQL CREATE TABLE Statement. After the database structure
is designed and the database has been created, we can add the individual tables to the database.

CREATE DATABASE statement can be found at:

https://www.testingdocs.com/create-a-new-mysql-database/

Syntax

The general statement syntax is shown below:

CREATE TABLE <table name> (
<column name> <column type> [<column options>],
[<column name> <column type> [<column options>],….,]
[,<primary key definition>]
);

Example

In this example, we will create CountryLanguage database table in the world MySQL database.

CREATE TABLE CountryLanguage (
CountryCode CHAR(3) NOT NULL,
Language CHAR(30) NOT NULL,
IsOfficial ENUM(‘True’, False’) NOT NULL DEFAULT ‘FALSE’,
Percentage FLOAT(3,1) NOT NULL,
PRIMARY KEY(CountryCode, Language)
);

 

Description

Description of the CREATE TABLE statement is as follows:

We create a table called CountryLanguage. ( is the beginning of the table structure definition that ends  with the ).

CountryCode: Column is assigned the data type of CHAR and maximum length of 3 characters that cannot contain NULL . The comma at the end of the line indicates that you will continue defining columns, or set a primary key.

Language:Column of the data type CHAR and a length of 30, also with no NULLs.

IsOffical: Column is assigned the data type of ENUM( with the value of ‘True’ or’ False’), and the addition of NOT NULL means each row must be true or false, not NULL. If the field is not provided it will be set to ‘False’ by the phrase DEFAULT ‘False’.

Percentage: Column with the data type of FLOAT and values assigned at 3 total digits of decimal and 1 digit to the right, cannot have NULL values.

PRIMARY KEYDefines the type of key and to what columns it applies, in this case a combination of both CountryCode and Language columns to define a unique identifier for each row.

 

) is the closing of the table structure definition.

The semi-colon ; means execute this statement (using the MySQL command line client) and create the table CountryLanguage database table.

MySQL Tutorials

MySQL Tutorials on this website:

https://www.testingdocs.com/mysql-tutorials-for-beginners/

For more information on MySQL Database:

https://www.mysql.com/

Exit mobile version