TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

MySQL

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 KEY:  Defines 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/

Related Posts

DataGrip Desktop Shortcut

MySQL /

Launch DataGrip on Windows

DataGrip Download

MySQL /

Install DataGrip IDE on Windows 11

MySQL Workbench Windows 11

MySQL /

New MySQL Connection in Workbench

MySQL Command-line Client

MySQL /

Command-Line MySQL Client

Start MySQL Client

MySQL /

Start MySQL Client on Windows 11

‹ MySQL Constraints› MySQL SHOW CREATE TABLE Statement

Recent Posts

  • Update draw.io on Windows
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com