如何在 MySQL CREATE TABLE 查询中使用 CHAR_LENGTH()?
CHAR_LENGTH(yourColumnName)在创建表时使用。让我们先看一个例子并创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Title varchar(200), `Number_of_characters` int as (char_length(Title)) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(Title) values('Introduction To MySQL'); mysql> insert into DemoTable(Title) values('Introduction To Java'); mysql> insert into DemoTable(Title) values('Introduction To MongoDB');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+----+-------------------------+----------------------+ | 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)