简体   繁体   English

如何在不重命名表属性的情况下解决SQL名称冲突?

[英]How to resolve SQL name conflict without renaming table attributes?

I am trying to create a table (shown below) for my final year project, but I encountered SQL name conflict error. 我正在尝试为我的最后一年的项目创建一个表(如下所示),但是遇到了SQL名称冲突错误。 It seems the words 'User' and 'role' are reserved by the SQL. SQL似乎保留了“用户”和“角色”两个词。 Is there any way I can resolve the issue, without renaming the words. 有什么办法可以解决这个问题,而无需重命名。

CREATE TABLE User
(
    ID INT generated by default as identity PRIMARY KEY,
    username varchar(45) NOT NULL,
    password varchar(255) NOT NULL,
    role varchar(255) NOT NULL
);

Throw square brackets around their name (or any reserved word that you want as your column names). 将方括号(或要用作列名称的任何保留字)放在方括号中。

[User] [用户]

or 要么

[Role] [角色]

Put double quotes around User. 用双引号引起来。 In general it never hurts to quote all your identifiers. 通常,引用所有标识符从来没有什么坏处。

Try this is you are using SQL Server(in MySql it can be done using backticks ``): 尝试这是您正在使用SQL Server(在MySql中可以使用backticks ``)完成:

CREATE TABLE [User]
(
    ID INT generated by default as identity PRIMARY KEY,
    username varchar(45) NOT NULL,
    password varchar(255) NOT NULL,
    [role] varchar(255) NOT NULL
);

On a side note:- It is not recommended to use reserved keywords for tabe name and column names. 附带说明:- 不建议为Tabe名称和列名称使用保留关键字。

Use some other names like Users and Roles which are not reserved keyword and which have a similar namings. 使用其他名称,例如“用户”和“角色”,它们不是保留关键字,并且具有相似的命名。

EDIT: 编辑:

After looking at your comments it looks like you are using Derby. 查看您的评论后,您似乎正在使用Derby。 So you need to use double quotes. 因此,您需要使用双引号。

Try this: 尝试这个:

CREATE TABLE "User"
(
    ID INT generated by default as identity PRIMARY KEY,
    username varchar(45) NOT NULL,
    password varchar(255) NOT NULL,
    "role" varchar(255) NOT NULL
);

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

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