简体   繁体   English

MySQL SET DEFAULT返回错误:查询为空

[英]MySQL SET DEFAULT returns error: query is empty

I have an existing MySQL (version 5.6.12) table called 'users' and am trying to set the column defaults via php (version 5.4.12), for example: 我有一个名为'users'的现有MySQL(版本5.6.12)表,并尝试通过php(版本5.4.12)设置列默认值,例如:

$query = "ALTER TABLE users ALTER username SET DEFAULT NULL";
$result = mysqli_query($query);

However this generates a SQL error (which results in the message 'query is empty'). 但是,这会产生SQL错误(导致消息“查询为空”)。 'username' is a VARCHAR(200) type column. “用户名”是VARCHAR(200)类型的列。

Using a quoted literal (such as 'John') for the default value results in the same error. 使用带引号的文字(例如“ John”)作为默认值会导致相同的错误。 (Just for kicks, though I am using MySQL I have also tried modifying the query according to SQL/MS Access or Oracle syntax but it still doesn't work). (只是,尽管我正在使用MySQL,但我也尝试过根据SQL / MS Access或Oracle语法修改查询,但仍然无法正常工作)。

What am I doing wrong? 我究竟做错了什么?

EDIT: Problem solved. 编辑:问题解决了。 The above query is fine and nothing is wrong. 上面的查询很好,没有错。

I made an incredibly dumb error and forgot $ sign in front of $query , ie my code was written $result = mysqli_query(query); 我犯了一个非常愚蠢的错误,在$query前面忘了$符号,即我的代码写成$result = mysqli_query(query);

(Just so you know, I am a programmer with decades of experience.) (请注意,我是一位拥有数十年经验的程序员。)

Change query like below 如下更改查询

$query = "ALTER TABLE users MODIFY Column username VARCHAR(200) NULL DEFAULT NULL";

or 要么

 $query = "ALTER TABLE users CHANGE Column username username VARCHAR(255) NULL DEFAULT NULL";

I use this one for my website 我将这个用于我的网站

$query = "ALTER TABLE 'users' CHANGE 'username' 'username' VARCHAR( 100 ) NULL DEFAULT NULL";
$result = mysqli_query($query);

Problem solved. 问题解决了。 The query is actually fine and nothing is wrong. 该查询实际上很好,没有错。

I made an incredibly dumb error and forgot $ sign in front of $query , ie my code was written $result = mysqli_query(query); 我犯了一个非常愚蠢的错误,在$query前面忘了$符号,即我的代码写成$result = mysqli_query(query);

(Just so you know, I am a programmer with decades of experience.) (请注意,我是一位拥有数十年经验的程序员。)

In MySQL you use backticked [`] quoted literals, don't use the quote sign [']. 在MySQL中,您使用带引号的[`]带引号的文字,请勿使用引号[']。 On the other side, every DDBB engine uses its slightly different flavour of SQL. 另一方面,每个DDBB引擎都使用略有不同的SQL风格。 Most of them anyway are able to interpret common subsets as described by the various active standards SQL89, SQL92, SQL99... and so on. 无论如何,它们中的大多数都能够解释各种有效标准SQL89,SQL92,SQL99 ...等所描述的公共子集。

In short, your SQL is wrong, in regards to what MySQL expects it to be. 简而言之,就MySQL的期望而言,您的SQL是错误的。 You'll have to build your SQL skills on top of trial and error by working with different SQL engines, until you can give each of these engines the SQL variant they expect. 您必须通过使用不同的SQL引擎来在反复试验的基础上构建SQL技能,直到可以为每个引擎提供他们期望的SQL变体。 Just don't give up and insist on the query. 只是不要放弃并坚持查询。 Simplify it, running shorter strings until you find one that works, then refine it by adding smaller pieces until you reach your objective. 简化它,运行较短的字符串,直到找到有效的字符串,然后通过添加较小的字符串来优化它,直到达到目标为止。

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

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