SQL Error: 1364 - Field 'buys' doesn't have a default value - mysql

I am getting this error in mybb SQL table:
SQL Error: 1364 - Field 'buys' doesn't have a default value
Query: INSERT INTO mybb_bank_post ('pid','cost') VALUES ('1680','10000')
How can I resolve this?

This suggests that buys is declared NOT NULL. So, you need to assign it a value when you insert into the table:
INSERT INTO mybb_bank_post (pid, cost, buys)
VALUES (1680, 10000, 0);
Or, declare the table so buys is not NOT NULL -- that is NULL values are allowed. Or, provide a default value in the table definition.
Note: Do not use single quotes for column names or for numeric constants. Only use single quotes for strings and date constants.

Related

MySql INSERT...ON DUPLICATE UPDATE with partial VALUES

I have a list of possibly-incomplete set of values that will be used to append to or update a MySql table using the INSERT...ON DUPLICATE KEY UPDATE construct. The requirements are as follows:
If an operation resolves to an INSERT and the field value IS supplied, use the value supplied;
If an operation resolves to an INSERT and the field value IS NOT supplied, use the field's table DEFAULT value;
If an operation resolves to an UPDATE and the field value IS supplied, use the value supplied;
If an operation resolves to an UPDATE and the field value IS NOT supplied, retain the current (table) field value.
I've come up with the following statement, but the clauses wrapped in ** are erroneous and I'm having difficulty expressing them:
INSERT INTO `test`
(`id`, `num`, `text`)
VALUES
('1', 100, 'aaa'),
('2', 200, DEFAULT),
('3', DEFAULT, 'ccc')
ON DUPLICATE KEY UPDATE
`num` = IF (**VALUES(`num`) = DEFAULT**, `num`, VALUES(`num`)),
`text` = IF (**VALUES(`text`) = DEFAULT**, `text`, VALUES(`text`));
Notes: id is the unique key. Both num and text have default (NOT NULL) values set.
Things I've tried, but aren't satisfactory:
Replacing DEFAULT in VALUES with NULL, and then test for, e.g., IF (VALUES (num) = NULL .... This works, but will insert NULL on INSERT (and generate a warning - e.g., "Column 'text' cannot be null"), which is not acceptable - I need to have the default value applied to the missing fields;
Using something like 'xxx' instead of DEFAULT for missing values, and testing for 'xxx' (STRCMP), but this will insert 'xxx' in case of INSERT;
I've not tried this as I can't find the command/proper syntax, but the idea is to test (in the IF clause) whether num and text in VALUES are literals (num or string) or a MySql keyword (i.e., DEFAULT) - possibly using regex? - and then act accordingly.
Of course, an alternative to the above might entail obtaining existing values from the database and/or hardcoding into the query the default values for the missing fields, but I trust the same result can be achieved more elegantly using a single MySql statement.
Thanks in advance for your feedback.

MySQL INSERT INTO results in Unknown Column in Field List

I am using the SQL feature phpMyAdmin to add 1 single record into my table. For simplicity, the record will be blank except for the 'symbol' field.
Table structure:
token_id = auto-increment, primary key
symbol = varchar(255)
every thing else is set to allow null entires, so should be irrelevant
I have tried the following queries, but all result in the same error:
unknown column 'symbol' in 'field list'
What I have tried:
INSERT INTO tokens (symbol) VALUES ('XYZ');
INSERT INTO tokens (symbol) VALUES ("XYZ");
INSERT INTO tokens (symbol) VALUES (XYZ);
INSERT INTO tokens.symbol VALUES ('XYZ');
INSERT INTO `tokens`.`symbol` VALUES ('XYZ');
Any suggestions?
Just for reference, trying the INSERT and using all columns and setting them to null results in the same exact error.
The correct format with backticks is
INSERT INTO tokens (symbol) VALUES ('XYZ');
FYI, I tried your first query on my server and worked fine so might be a problem with table structure.

MySQL Insert Cast

I have tried to Insert a value into a table in MySQL but I can't make it work. I am using the following queries:
INSERT into articulo values (32,'Sala',CAST('$10,000.45999' AS DECIMAL(10,5)),40.2399,200.2399,3,'kid 3');
MySQL shows the following error:
1 row(s) affected, 1 warning(s): 1292 Truncated incorrect DECIMAL value: '$10,000.45999'
And it shows the following into the table:
Of course I created the table 'articulo' before:
CREATE Table articulo
(
id_art int NOT NULL,
nom_art varchar (25) DEFAULT 'XXXXXXXXXXXXX',
prec_art decimal (10,5) DEFAULT 0.000,
peso_art decimal (10,5),
existencia float,
color_art int, CONSTRAINT chk_color1 CHECK (color_art between 0 and 20),
um_art varchar (10) DEFAULT 'DEF_PZA',
primary key (id_art)
);
I have seen many examples for Casting but all of them use the cast function under a select
statement.
Any idea how I can do in order to perform what I want?
I want to store $10,000.45999 into the table as a decimal value.
This would be 10000.45999
Thanks for your support!
You can insert the value by fixing up the number. For your case, this should work:
INSERT into articulo
SELECT 32, 'Sala',
CAST(REPLACE(REPLACE('$10,000.45999', ',', ''), '$', '') AS DECIMAL(10,5)),
40.2399, 200.2399, 3, 'kid 3';
Strictly speaking, the cast() is not necessary, but I like to avoid implicit conversions -- these can lead to hard-to-detect problems.
As a note: it is a good idea to include the column list in the insert statement.
You can't use commas or the dollar symbol in your value in that query.
You could rewrite your query as:
INSERT into articulo values (32,'Sala',CAST('10000.45999' AS DECIMAL(10,5)),40.2399,200.2399,3,'kid 3');
However you don't need to cast your value as a decimal if your column is already well defined as DECIMAL(10,5).
Simply write:
INSERT into articulo values (32,'Sala',10000.45999,40.2399,200.2399,3,'kid 3');

MySql insert statement to binary datatype?

I am using MySQL database.
I have one table having column with datatype binary(16).
I need help with the insert statement for this table.
Example:
CREATE TABLE `assignedresource` (
`distid` binary(16) NOT NULL
)
insert into assignedresource values ('9fad5e9e-efdf-b449');
Error : Lookup Error - MySQL Database Error: Data too long for column 'distid' at row 1
How to resolve this issue?
You should remove the hyphens to make the value match the length of the field...
Example:
CREATE TABLE `assignedresource` (
`distid` binary(16) NOT NULL
)
insert into assignedresource values ('9fad5e9eefdfb449');
Also, MySQL standard is to use this notation to denote the string as binary... X'9fad5e9eefdfb449', i.e.
insert into assignedresource values (X'9fad5e9eefdfb449');
Well, assuming that you want to strictly insert a hexadecimal string, first you need to remove the dashes and then "unhex" your string before inserting it into a binary(16) data type column, the code would go like this:
INSERT INTO `assignedresource` VALUES(UNHEX(REPLACE('9fad5e9e-efdf-b449','-','')));
Also... the "usable" data you are inserting is actually 8 bytes after undashing it, so binary(8) would do fine if you plan on not storing the dashes.
You can strip the hyphens and perpend 0x to the value unquoted, like this:
insert into assignedresource values (0x9fad5e9eefdfb449);
As well as, as this (mentioned in other answers):
insert into assignedresource values (X'9fad5e9eefdfb449');
Both are valid notation for a hexadecimal literal.
Your string is 18 char long, change the database
CREATE TABLE `assignedresource` (
`distid` binary(18) NOT NULL
)

MS Access INSERT INTO statement

I need to insert form data from my VB.NET application to a Microsoft Access database.
I am getting the error "Syntax error in INSERT INTO statement" when using the following syntax:
INSERT INTO bs1 (teacher, subject, date, period)
VALUES ('test', 'test', 'test', 'test')
I'll admit I'm used to the MySQL type syntax, any help on this matter would be greatly appreciated, thanks.
I believe date is a reserved word. You need to encapsulate the reserved field names in square brackets:
INSERT INTO bs1 (teacher, subject, [date], period) VALUES ('test', 'test', 'test', 'test')
EDIT: See the following article for a complete list of reserved words in Access 2002 and greater:
http://support.microsoft.com/kb/286335
~md5sum~
In Access the delimiter for literal values inserted into date fields is #, for text fields is ' or " and numeric field values do not have a delimiter, which suggests:
INSERT INTO bs1 (teacher, subject, [date], period)
VALUES ('test', 'test', #2009-12-31#, 0)
In Access Database Engine SQL code, when you need to specify that a literal value is of type DATETIME, you can either explicitly cast the value to DATETIME or use # characters to delimit the value.
Using an explicit cast using the CDATE() function:
INSERT INTO bs1 (teacher, subject, [date], period)
VALUES ('test', 'test', CDATE('2009-12-31 00:00:00'), 0);
Using a DATETIME literal value:
INSERT INTO bs1 (teacher, subject, [date], period)
VALUES ('test', 'test', #2009-12-31 00:00:00#, 0);
When INSERTing a value into a column of type DATETIME, if you do not specify an explicit DATETIME value, the engine will implicitly attempt to coerce a value to DATETIME. The literal value 'test' cannot be coerced to type DATETIME and this would appear to be the source of your syntax error.
Note: none of the above applies to the NULL value. In Access Database Engine SQL there is no way to cast the NULL value to an explicit type e.g.
SELECT CDATE(NULL)
generates an error, "Invalid use of NULL". Therefore, to specify a NULL DATETIME literal, simply use the NULL keyword.
It pays to remember that the Access Database Engine has but one temporal data type, being DATETIME (its synonyms are DATE, TIME, DATETIME, and TIMESTAMP). Even if you don't explicitly specify a time element, the resulting value will still have a time element, albeit an implicit one. Therefore, it is best to always be explicit and always include the time element when using DATETIME literal values.