简体   繁体   English

SQL列名称与数据类型的名称相同-导致错误

[英]SQL Column Names same as names for data types - causing errors

I am very new to SQL and having problems with creating a table structure. 我对SQL非常陌生,在创建表结构时遇到问题。

I want to create a table with four columns - id , text , date and replace . 我想创建一个包含四列的表idtextdatereplace Problem is this is giving me an error in MySQL, I think because the words TEXT and DATE are also names for data types and REPLACE is another term used in SQL so MySQL is getting confused about what my column names should be or doesn't realise the names I've given are actual names. 问题是这给我一个MySQL错误,我想是因为TEXTDATE也是数据类型的名称,而REPLACE是SQL中使用的另一个术语,所以MySQL对我的列名应该或不知道是什么感到困惑我给的名字是真实的名字。 Is there any way I can get around this to get the column names I want, without having to call the columns something else and then change them back manually once created? 有什么办法可以解决这个问题,而不需要调用其他列,然后在创建后手动将其更改回我想要的列名称?

Here's my SQL: 这是我的SQL:

CREATE TABLE message (
    id INT UNIQUE AUTO_INCREMENT NOT NULL,
    text TEXT,
    date INT,
    replace INT DEFAULT 0
);

And the error I'm getting: 我得到的错误是:

#1064 - You have an error in your SQL syntax; #1064-您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'replace INT DEFAULT 0 )' at line 5 检查与您的MySQL服务器版本相对应的手册,以在第5行的'replace INT DEFAULT 0)'附近使用正确的语法

CREATE TABLE message(
    id INT UNIQUE AUTO_INCREMENT NOT NULL ,
    TEXT TEXT,
    DATE INT,
    REPLACE INT DEFAULT 0
);
CREATE TABLE message (
    `id` INT UNIQUE AUTO_INCREMENT NOT NULL,
    `text` TEXT,
    `date` INT,
    `replace` INT DEFAULT 0
);

General rule is don't use these keywords, come up with something different than 'text' for your field name. 一般规则是不要使用这些关键字,为字段名提供不同于“文本”的名称。 If you must, use ` (back-tick) around the column name to tell SQL it's a column name and not what the keyword means. 如果必须的话,请在列名周围使用`(反引号),以告诉SQL它是列名,而不是关键字的含义。 I recommend against calling your columns 'from' 'select' and 'where' as well ;) 我建议不要将列也称为“从”,“选择”和“何处”;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM