如何在 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))
   );
Query OK, 0 rows affected (0.18 sec)

使用 insert 命令在表中插入一些记录 -

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)

使用 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)

更新于: 30-Jul-2019

82 次浏览

开启你的 职业

完成课程获得认证

开始吧
广告
© . All rights reserved.