Error in SQL Statement using INSERT and AUTO-INCREMENT column - mysql

INSERT INTO `configuration` VALUES ('', 'News Box Character Count', 'NEWS_BOX_CHAR_COUNT', '200', 'Set the number of characters (bytes) that you want to display in the news preview box.', 19, 99, NULL, '2004-09-07 12:00:00', NULL, NULL);
I run this command in phpMyAdmin, it shows
#1366 - Incorrect integer value: '' for column 'configuration_id' at row 1
configuration_id is an auto increment field beginnning of 1

Instead of this INSERT INTO configuration VALUES ('', 'News Box Character Count',
Pass the value as NULL for auto_increment or integer column or you can simply not to include that column in sql query.
INSERT INTO `configuration` VALUES (NULL, 'News Box Character Count', ...
This is because, mysql is running in the strict mode.
You can either use NULL for all the integer columns when there is nothing to enter them or turn off the MySql Strict mode.

For an autoincrement in MySQL, either insert NULL or insert nothing at all:
Easiest & Cleanest: using NULL
INSERT INTO `configuration` VALUES (NULL, 'News Box Character Count', 'NEWS_BOX_CHAR_COUNT', '200', 'Set the number of characters (bytes) that you want to display in the news preview box.', 19, 99, NULL, '2004-09-07 12:00:00', NULL, NULL);
More work: name every column except the autoincrement one
INSERT INTO `configuration` (every,column,except,the,first) VALUES ('News Box Charac`ter Count', 'NEWS_BOX_CHAR_COUNT', '200', 'Set the number of characters (bytes) that you want to display in the news preview box.', 19, 99, NULL, '2004-09-07 12:00:00', NULL, NULL);`

It seems pretty obvious to me? You gave an integer column and you are explicitly inserting a string (although it's empty). If the column is set to auto increment, remove the first value ('') from your values array and you should be fine. Also, maybe you wanto to specify the columns you are inserting values for, like:
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)

For the AUTO_INCREMENT field you can set NULL; it will generate new value automatically.
For example -
INSERT INTO table_name VALUES(NULL, 'value', ...)...

If the column is auto-increment then you can simple omit it from the INSERT statement.

Replace the '' value with NULL ('' isn't a valid integer). See here.

You should always use column names when inserting. So just don't have to insert into the column configuration_id and your sql statement doesn't break when changing the columns.
Use something like this: INSERT INTO table (column1, column2) VALUES (value1, value2);

Related

How to enter 'blank' values in MySQL

I am using VBA to write some values to MySQL table as rows. However, for certain columns, I want to leave them blank so that the default value of NULL can be assumed by the database.
I need to specify all column values in my SQL query. Can I write something like this (two consecutive commas represent a blank column):
SQLStr = "insert into table tablename values (12,13,19,,,,,1,,2,,5)"
You use NULL:
insert into table tablename
values (12, 13, 19, NULL, NULL, NULL, NULL, 1, NULL, 2, NULL, 5);
You can also use '' if the column is a string and by "blank" you mean "empty string".
I should add . . . you should always list the columns you are inserting, so it is better to write this as:
insert into table tablename(col1, col2, col3, col8, col10, col12)
values (12, 13, 19, 1, 2, 5);
(You don't specify what the column names are.)
By default, SQL will put NULL values in for the missing columns -- this can be prevents (using NOT NULL) or another value used (using DEFAULT).

Default values not working in phpmyadmin/mysql database

I can't get a table to accept "" or '' and use the default value. It is inserting NULL instead.
I am trying these commands in the direct input sql window.
INSERT INTO test01 VALUES ("", now(), "");
INSERT INTO test01 VALUES ('', now(), '');
But both just give NULL in the 3rd column. The structure is set to non-null with a default value of "yes". (Without quotation marks).
Here is a screenshot of the structure. You can see NULL is not checked.
http://garryjones.se/extras/so3.png
Default values only work if no value is inserted/updated. If you explicitly set it to an empty string (which is NOT the same as a NULL value) then it will end up with an empty string in the column. Instead of the code above you should eliminate the column from the INSERT statement at all:
INSERT INTO test01 (t1, t2) VALUES ('', now())
Other is already explain the reason here I am adding one more point you are also using current time stamp on update so do not need to use this column as well.
INSERT INTO test01 (t1) VALUES ('')
You could use the DEFAULT keyword: INSERT INTO test01 VALUES ("", now(), DEFAULT);

Storing numbers in a MySQL SET column

How should I insert numbers in a SET column? I know that for ENUM it's recommended to not store numbers in ENUM column, and numbers should be insert with ''.
Is it the same for a SET column? Should be numbers inserted with ''? Like '1','2' or is it fine to insert it like: 1, 2 ?
You insert using SET values names and enclose them in single quotes and separate them with commas.
Minimalistic example from here:
CREATE TABLE myset (col SET('a', 'b', 'c', 'd'));
INSERT INTO myset (col) VALUES ('a,d,d,s');
First of all, don't use MySQL's SET and ENUM column types.
Second, your set definition really shouldn't contain numbers. 'One' is OK, '1' will be extremely confusing.
Third, if you insert numerals like 1, 2, etc then it will actually insert the n-th member of the set instead of that number, whereas if you insert '1' it will insert that member. The difference comes up when you have a set with the following members in that order: 'foo', 'bar', '1', '2' - if you insert '1' it would be the same as inserting a 3, but if you insert a 1 it'd be the same as 'foo'.
Bottom line: don't use the SET and ENUM column types.
Number should insert like 1 ,2 ,3.
This consider as character if you put into single quote like '1','2'.

INSERT values to MySQL

If i have my SQL create statement as follows;
CREATE TABLE TABLENAME12
(
TAB_ID INT NOT NULL AUTO_INCREMENT,
NAME_FIRST NVARCHAR(200),
TYPE NVARCHAR(200),
PRIMARY KEY( TAB_ID )
);
and if i want to Insert values to it, should i enter TAB_ID too ? Since it's auto increment.
When i INSERT INTO SWM_SALES_FEEDBACK VALUES (1,'Jerry','ty'); It gets inserted if i don't specify the primary key INSERT INTO SWM_SALES_FEEDBACK VALUES ('Jerry','ty'); i get the following error :
ERROR 1136 (21S01): Column count doesn't match value count at row 1
1.) What is the point of having AUTO_INCREMENT if it doesn't get auto incremented.
2.) If i have a field called:
BIRTH_TIME DATE,
How should i INSERT value to this field since it's DATE type
1) You have to respect an order of columns in your table. You can do either:
INSERT INTO SWM_SALES_FEEDBACK VALUES (null, 'Jerry','ty');
or
INSERT INTO SWM_SALES_FEEDBACK (NAME_FIRST, TYPE) VALUES ('Jerry','ty');
2) You can use '2012-07-24' format for date column.
With an auto increment, you need to specify your columns
INSERT INTO tablename12 ('name_first','type') values ('Jerry','ty')
If you aren't inserting into every column, you need to tell the query which columns you want to insert into. try INSERT INTO SWM_SALES_FEEDBACK (NAME_FIRST,TYPE) VALUES ('Jerry','ty');
Not sure if mysql has a to_date() function in the new releases:
But, you can use this : Just enclose the date time field in single quotes. You can specify the date time formats
INSERT INTO table_name (birth_date) VALUES ('2008-07-04')
For other date format questions refer this.
For your auto increment, specify the columns
INSERT INTO SWM_SALES_FEEDBACK (NAME_FIRST,TYPE) VALUES ('Jerry','ty');
Point 1
If you don't want to store all fields in DB then you have to specify those column names before VALUES clause as shown below.
INSERT INTO myTable (field2,field3) VALUES ('field2','field3')
In your case, statement should be
INSERT INTO SWM_SALES_FEEDBACK (name_first, type) VALUES ('Jerry','ty')
^^^^^^^^^^^^^^^^^^
Point 2
If you have field as DATETIME, TIMESTAMP as datatype, you can enter date as
INSERT INTO myTable (myDate) VALUES ('2012-12-28 12:12:12')
Format is yyyy-mm-dd hh:mm:ss
If you want to store the current time of the system in DB, you could use NOW() as shown below.
INSERT INTO myTable (myDate) VALUES (NOW())
Try the following code
INSERT INTO SWM_SALES_FEEDBACK (NAME_FIRST,TYPE) VALUES ('Jerry','ty');

MySQL, given default value is not taking to the desired column

I have created a table (RJY) with default value to one of its column. When I enter the command 'Describe rjy;' its shows me default column with the value I gave,
when I insert data into my table using insert command, the column with default value is left blank and it is not taking the value which I gave. Below is my query please solve it.
mysql> insert into rjy (compy_id, reqrmnt, veh_type, go_type, weigh, l_start,l_end)
values ('123456', '111', '12 ty', 'white cemt', '1 tons', '', 'mumbai');
Here I gave default value to "l_start" field
Thank you.
Well, you give your field a value in the insert query (even it's it an empty value).
To have default value, you should change your insert as
insert into rjy (compy_id, reqrmnt, veh_type, go_type, weigh, ,l_end)
values ('123456', '111', '12 ty', 'white cemt', '1 tons', 'mumbai');
Don't include the column in your insert query. Do this:
insert into rjy (compy_id, reqrmnt, veh_type, go_type, weigh, l_end)
values ('123456', '111', '12 ty', 'white cemt', '1 tons', 'mumbai');