简体   繁体   English

我的CREATE TABLE MySQL语法有什么问题?

[英]What is wrong with my CREATE TABLE MySQL syntax?

I am having some trouble with the CREATE IF NOT EXISTS clause. 我在CREATE IF NOT EXISTS子句上遇到了麻烦。

I am using a C# application to create a MySQL table, the connection to DB has been established so it's not a problem. 我正在使用C#应用程序创建MySQL表,已经建立了与数据库的连接,所以这不是问题。

The error I am getting is an exception when I try to execute the query, I get the message: 当我尝试执行查询时,我得到的错误是一个异常,我得到以下消息:

MySql.Data.MySqlClient.MySqlException (0x80004005): You have an error in your SQL syntax; MySql.Data.MySqlClient.MySqlException(0x80004005):您的SQL语法有错误。 check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF NOT EXISTS(price VARCHAR, time VARCHAR)' at line 1 在第1行的'IF NOT EXISTS(price VARCHAR,time VARCHAR)'附近检查与您的MySQL服务器版本相对应的手册以使用正确的语法

In debug mode, the immediate window shows my command string as: 在调试模式下,立即窗口将我的命令字符串显示为:

CREATE TABLE ticks_14_11_2016 IF NOT EXISTS(price VARCHAR, time VARCHAR) 创建表ticks_14_11_2016如果不存在(价格VARCHAR,时间VARCHAR)

From the examples I have seen, this should be the proper syntax. 从我看到的示例中,这应该是正确的语法。 I am not worried about constraints and keys for the time being, I just need the query to execute... 我暂时不担心约束和键,只需要执行查询即可。

Also, here is the C# code which I use to build the string and execute query: 另外,这是我用来构建字符串和执行查询的C#代码:

string tableName = "ticks_" + getTodayString();
            if (databaseClient.IsConnect()) {
                string tableString = "CREATE TABLE " + tableName +
                " IF NOT EXISTS" +
                "(price VARCHAR, " +
                "time VARCHAR)";

                try
                {
                    var command = databaseClient.Connection.CreateCommand();
                    command.CommandText = tableString;
                    command.ExecuteNonQuery();
                } catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }

The variable databaseClient has a member that is the MySQLConnection object 变量databaseClient具有一个成员,该成员是MySQLConnection对象

Also, my server version is: 5.6.28-76.1 另外,我的服务器版本是: 5.6.28-76.1

You have the if not exists in the wrong place, and also, the varchar type needs a mandatory length argument. if not exists错误位置中if not exists ,并且varchar类型需要一个必填的length参数。

A corrected version should be: 更正的版本应为:

CREATE TABLE IF NOT EXISTS ticks_XXXXX (price VARCHAR(10), time VARCHAR(10));

Change the length to whatever is appropriate for you. 将长度更改为适合您的长度。

For more information see the reference manual . 有关更多信息,请参见参考手册

You have other ways also to check whether table exists in database or not. 您还可以通过其他方法检查数据库中是否存在表。

IF OBJECT_ID(N'dbo.ticks_14_11_2016', N'U') IS NOT NULL
BEGIN
    ------Exists
END

IF EXISTS(SELECT 1 FROM sys.Objects WHERE  Object_id = 

OBJECT_ID(N'dbo.ticks_14_11_2016') AND Type = N'U')
BEGIN
   ------Exists
END

    IF EXISTS(SELECT 1 FROM sys.Tables WHERE  Name = N'ticks_14_11_2016 ' AND Type = N'U')
 BEGIN
      ----Exists
 END

IF OBJECT_ID('ticks_14_11_2016') IS NOT NULL
BEGIN
     -----Exists
END

Use your logic accordingly 相应地使用您的逻辑

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

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