
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to use CHAR_LENGTH() in MySQL CREATE TABLE query?
Use CHAR_LENGTH(yourColumnName) at the time of table creation. Let us first see an example and create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Title varchar(200), `Number_of_characters` int as (char_length(Title)) ); Query OK, 0 rows affected (0.18 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Title) values('Introduction To MySQL'); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable(Title) values('Introduction To Java'); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable(Title) values('Introduction To MongoDB'); Query OK, 1 row affected (0.04 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+-------------------------+----------------------+ | Id | Title | Number_of_characters | +----+-------------------------+----------------------+ | 1 | Introduction To MySQL | 21 | | 2 | Introduction To Java | 20 | | 3 | Introduction To MongoDB | 23 | +----+-------------------------+----------------------+ 3 rows in set (0.00 sec)
- Related Articles
- MySQL query to create table dynamically?
- Create MySQL query to create a table from an existing table?
- Query MySQL with unicode char code?
- Create table query with manual AUTO_INCREMENT start value in MySQL?
- How to use alias in MySQL select query?
- MySQL: What is 'AUTO_INCREMENT=5' in a create table query?
- What is the use of MySQL CHAR() function?
- How to create a MySQL table with InnoDB engine table?
- How to create a MySQL table with MyISAM engine table?
- How to change the length of the Name column from CHAR(20) to CHAR(50)?
- Will “create table table” work in MySQL since we cannot use reserved words as table name?
- How to create a table from view in MySQL?
- MySQL - How to count all rows per table in one query?
- Create table SQL query in SAP HANA
- How to create a MySQL table with indexes?

Advertisements