I am getting this error when attempting to insert into mybb's mybb_settinggroups SQL table:
SQL Error: 1364 - Field 'description' doesn't have a default value
Query: INSERT INTO mybb_settinggroups (name,title,disporder,isdefault)
VALUES ('lock','Lock Settings',1,0)
How can I resolve this?
The field description would seem to be declared NOT NULL with no default value. So, you need to include it in the INSERT:
INSERT INTO mybb_settinggroups (name, title, disporder, isdefault, description)
VALUES ('lock', 'Lock Settings', 1, 0, 'no description available');
You may have a more reasonable description in mind.
You can either drop not null constraint or alter the table on description column
as below
alter table name alter column
description
set
default 'no description'
Related
I can't set default value for for column in my table. I see lot of many example, but i can't resolve my issue. I have table with two columns: id, name. I'm need, if id == null, then set default value.
ALTER TABLE my_table ALTER id SET DEFAULT nextval('val_seq')
INSERT INTO my_table (id, name) VALUES (null, 'test_name')
I receive error: null value in column "id" violates not-null constant. How fixed this?
Just try not to include the column that you want to use the default value for in your insert statement.
INSERT INTO my_table (name) VALUES ('test_name')
The default constraint will provide a default value for a column IF no other value is specified.
I've a table titled 'user' in MySQL database.
It has a field titled 'full_name'(varchar(255)). I want to set 'NOT NULL' attribute to this column and update the column with value 'Dummy User' where 'full_name' column contains blank value.
So my question is how should I achieve both these things in one single update query?
The query I tried is as below but it gave me error:
ALTER TABLE `user` MODIFY COLUMN full_name NOT NULL DEFAULT 'Dummy User' WHERE full_name = '';
Please somebody help me.
As the noble #jens pointed out in his comment, there is no way to alter the type of the full_name column to NOT NULL and also replace the empty strings with a default value in a single query.
The first query below changes the full_name column type to NOT NULL and sets the default column value to Dummy User. It also will replace all already-existing NULL entries with your desired default value. We are halfway done. To cover the records which have empty string, I use a simple UPDATE statement.
ALTER TABLE users
MODIFY COLUMN full_name VARCHAR(255) NOT NULL DEFAULT 'Dummy User';
UPDATE users
SET full_name = 'Dummy User'
WHERE full_name = ''
My problem is the following:
I have a table foo_table with id column ID as bigint (primary key), name column NAME as varchar(80) and code COLUMN code as varchar(20). I have a unique constraint on the CODE column.
If I fill this table with only "number" strings in the code column, and then one day decide to add a row with a, surprise, "string" string in that code column, say putting "My Code", it works fine ! The error comes when I tried updating one of my previous records, one that has a CODE value being a number; in this case I get the famous error saying:
ERROR 1292 (22007): Truncated incorrect DOUBLE value: 'My Code'.
I'm not even touching that record ! I presume MySQL is trying to check the unique constraint and try to convert every value in the CODE column to double, but that's insane, and very unprofessional.
Anyone has a solution ? Like turning off string<->number conversion in MySQL ?
Thanks in advance.
Edit:
Ok I have the sample code, but while creating it I saw how to fix the problem, which is still a problem in MySQL as far as I'm concerned:
CREATE TABLE foo_table (
ID bigint(20) NOT NULL DEFAULT '0',
NAME varchar(80) DEFAULT NULL,
CODE varchar(20) NOT NULL DEFAULT '',
UNIQUE KEY foo_table_uk (CODE)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC;
insert into foo_table (id, name, code) values (1, 'one', '1');
insert into foo_table (id, name, code) values (2, 'two', '2');
insert into foo_table (id, name, code) values (3, 'three', 'three');
update foo_table set name='a-one', code='1' where code=1; <-- This update throws the error
update foo_table set name='a-one', code='1' where code='1'; <-- this update works fine
as you see in the title, even if i removed "not null" feature from the related field, it still doesn't let me to insert null value for that field although the field is nullable!
Any help would be appreciated.
EDITED
Create:
CREATE TABLE `review` (
..
`RATING` int(11) DEFAULT NULL,
..
(`CATALOG_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=31625 DEFAULT CHARSET=latin5 ROW_FORMAT=DYNAMIC
Query:
INSERT INTO review (RATING,..) VALUES (null,..);
Error message:
Error: Column 'RATING' cannot be null
SQLState: 23000
ErrorCode: 1048
I also try to insert without RATING in the insert query, even if it is default null and nullable field, it gives the same error message and never inserts the field.
Bohemian, thanks for your attention. You are right, I figured out that there is a trigger for insert action which effects the related field. I disabled the trigger and the error is fixed. Thanks.
First of all look your "datetime / created_at / updated_at" field, do not assign the default value, select current time. after that you can can update the fields name
SQL query:
INSERT INTO `website_categorization`.`category_keyword` (
`ID` ,
`ID_Category` ,
`Keyword` ,
`Score`)
VALUES (
NULL , '18', 'free mail', ''
), (
NULL , '18', 'web email free', ''
)
MySQL said:
#1062 - Duplicate entry '18-free mail' for key 'ID_Category'
It shows this duplicate entry error even though there is no entry at row no 1062. ( ID is primary key, and unique(ID_Category,Keyword) ).
Can u help me in this?...
You already have a row in your database with the values '18' and 'free mail'. You can't have two such rows because of the unique constraint. You have some choices:
Remove the original row and try your insert again: DELETE FROM yourtable WHERE ID_Category = '18' AND Keyword = 'free mail'.
Remove the unique constraint to allow both rows to exist.
Use INSERT IGNORE to ignore the error.
Use REPLACE instead of INSERT to replace the old row with the new row.
Attempt the INSERT knowing that the client-side will be alerted of the error.
Well, it means that the data you are inserting breaks the unique constraints. From the error messasge I'd say some data already exists with the pair (18, 'free mail') - you say that is constrained to be unique.
The row number is not an indication, because it doesn't correspond to the key.
Your ID_category key is declared as unique and thus you cannot have two entries with the same value.
If your ID field is truly a primary key, it is mostly likely (or should be) auto-incremented. So leave that field out of the INSERT query.
That is MySQL Error number 1062, not row number. The error means duplicate entry. You are inserting NULL and '18' twice in ID and ID_Category respectively, so it will throw this error the 2nd time you do it. ID_Category is very likely the name of your index. You can do a
show index from website_categorization.category_keyword
to see the index name.