16人参与 • 2026-04-30 • Mysql
以下是项目中实际使用的文件盖章配置表完整建表 sql
create table `justice_file_seal_config` ( `file_seal_config_id` bigint not null comment '文件盖章配置id', `file_category` json not null comment '文件类别编码集合', `seal_rule` varchar(64) not null comment '盖章规则', `seal_type` varchar(32) not null comment '盖章类型', `seal_pages` varchar(100) default null comment '盖章页数,指定页盖章时必填', `keyword` varchar(200) default null comment '关键字,关键字盖章时必填', `del_flag` char(1) default '0' comment '删除标识:0存在,1删除', `remark` varchar(500) default null comment '备注', `create_user_id` bigint default null comment '创建用户id', `create_dept_id` bigint default null comment '创建部门id', `create_org_id` bigint default null comment '创建组织id', `create_time` datetime default null comment '创建时间', `update_user_id` bigint default null comment '更新用户id', `update_time` datetime default null comment '更新时间', primary key (`file_seal_config_id`) ) comment='文件盖章配置表';
语句中存在两种极易混淆的符号:
二者语法作用完全独立,绝对不能混用、省略规则也完全不同。
反引号是mysql 特有语法符号,专门用于包裹数据库名、表名、字段名、索引名等数据库标识符。核心作用:
在开发中,以下场景强制必须添加反引号,否则直接语法报错:
order、group、user、desc、limit等高频关键字;绝大多数企业级开发的规范命名场景,完全可以去掉反引号,不影响 sql 执行:
file_seal_config_id、seal_rule、del_flag等。基于本文业务表,去除多余反引号后的精简 sql,可直接生产使用:
create table justice_file_seal_config ( file_seal_config_id bigint not null comment '文件盖章配置id', file_category json not null comment '文件类别编码集合', seal_rule varchar(64) not null comment '盖章规则', seal_type varchar(32) not null comment '盖章类型', seal_pages varchar(100) default null comment '盖章页数,指定页盖章时必填', keyword varchar(200) default null comment '关键字,关键字盖章时必填', del_flag char(1) default '0' comment '删除标识:0存在,1删除', remark varchar(500) default null comment '备注', create_user_id bigint default null comment '创建用户id', create_dept_id bigint default null comment '创建部门id', create_org_id bigint default null comment '创建组织id', create_time datetime default null comment '创建时间', update_user_id bigint default null comment '更新用户id', update_time datetime default null comment '更新时间', primary key (file_seal_config_id) ) comment='文件盖章配置表';
开发建议:常规业务开发统一使用下划线命名法,默认不添加反引号,代码更简洁;若存在关键字冲突,局部单独加反引号即可。
单引号是标准 sql 通用符号,作用是标识字符串、文本、字符常量。在 mysql 建表语句中,主要使用场景:
comment '备注内容';char、varchar、text类型;以下场景绝对不能省略单引号,省略直接语法异常、字段类型不匹配:
comment后描述文本必须用单引号包裹;del_flag char(1) default '0',字符类型必须用文本包裹;纯数值类型字段(int、bigint、decimal)的默认值,不需要添加单引号:
-- 正确:数字类型无需单引号 sort int default 0 -- 错误:数字类型加单引号无语法报错,但会隐式类型转换,影响性能 sort int default '0'
-- 错误写法:注释无单引号,sql直接报错 del_flag char(1) default 0 comment 删除标识 -- 错误写法:字符类型赋值纯数字,类型不匹配 del_flag char(1) default 0
在 mysql 的建表语句(create table)以及后续的数据操作中,反引号(`)和单引号(')扮演着不同的角色,混用会导致语法错误。下面将详细解释它们的用途、区别及最佳实践。
作用:包裹数据库、表、列、索引、视图等对象的名称(即标识符)。
使用场景:
标识符是 mysql 保留关键字例如:select、order、group 等不能直接作为列名,除非用反引号包裹。
create table `order` (
`select` int,
`group` varchar(20)
);标识符包含特殊字符或空格例如:列名 user name(含空格)、my-column(含连字符)。
create table `my table` (
`first-name` varchar(20),
`last name` varchar(20)
);标识符与数字开头或全部是数字
create table `123abc` (
`123` int
);标识符大小写敏感(在特定字符集/排序规则下)用反引号可明确区分大小写。
注意:
反引号不是必须的。如果标识符符合以下所有条件,可以省略:
_)或美元符号($)但在实际开发中,强烈建议对表名和列名始终使用反引号,以增强可读性并避免未来新版本引入新的关键字。
作用:包裹文本字符串、日期时间值、二进制数据等字面量。
使用场景:
插入或查询字符串值
insert into user (name) values ('张三');
select * from user where name = '李四';日期和时间值
insert into event (event_date) values ('2025-03-15');
select * from log where create_time > '2025-01-01 00:00:00';使用 like 模式匹配中的%和_时
select * from product where code like 'a%'; -- 匹配以a开头的字符串
转义规则:若字符串本身包含单引号,需使用两个单引号转义。
insert into message (content) values ('it''s a nice day.'); -- 实际存入:it's a nice day.| 项目 | 反引号(`) | 单引号(') |
|---|---|---|
| 用途 | 限定数据库对象名称(表、列等) | 限定字符串、日期等字面量 |
| 能否用于对象名 | ✅ 可以且推荐 | ❌ 不能,会被当成字符串常量 |
| 能否用于字符串值 | ❌ 不能,会被解析为列名 | ✅ 可以 |
| 常见错误示例 | select * from 'order' ❌ | select "name" from user ❌(取决于模式) |
错误1:使用单引号包裹表名
create table 'user' (id int); -- 报错:语法错误
正确:
sql
create table `user` (id int);
错误2:使用反引号包裹字符串值
insert into user (name) values (`张三`); -- 报错:列名未找到
正确:
insert into user (name) values ('张三');错误3:列名与字符串混淆
select 'name' from user; -- 返回字符串字面量 'name',而非列 name 的值
正确(假设想获取列 name 的值):
select `name` from user;
在 mysql 中,双引号的行为受 sql 模式影响:
ansi_quotes 模式:双引号等同于反引号,用于标识符限定。为了避免混淆,建议始终使用反引号包裹标识符,使用单引号包裹字符串,不依赖双引号。
查看当前 sql 模式:
select @@sql_mode;
建表语句中,对所有标识符(库、表、列、索引等)使用反引号,即使它们不是关键字也不含特殊字符。
create table `employees` (
`id` int primary key,
`full_name` varchar(100),
`hire_date` date
);在增删改查的值部分,始终使用单引号包裹字符串、日期、枚举值等。
insert into `employees` (`full_name`, `hire_date`) values ('张三', '2025-04-30');避免使用双引号,除非你明确控制 ansi_quotes 模式。
保持统一风格,同一团队应约定相同的标识符引用规范(通常全部用反引号)。
转义处理:在动态生成 sql 时,必须正确处理特殊字符,防止 sql 注入。推荐使用参数化查询(如 preparedstatement)代替手动转义。
-- 建表
create table `product` (
`product_id` int auto_increment primary key,
`product_name` varchar(100) not null,
`price` decimal(10,2),
`description` text
);
-- 插入数据
insert into `product` (`product_name`, `price`, `description`)
values ('笔记本电脑', 5999.00, '15.6英寸,高性能');
-- 查询
select `product_id`, `product_name`
from `product`
where `price` > 5000.00;通过严格遵守上述规则,可以避免大多数因引号误用导致的语法错误,让 sql 语句更清晰、可维护。
到此这篇关于mysql建表语句中反引号``和单引号''的正确使用全解的文章就介绍到这了,更多相关mysql反引号和单引号使用内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论