I have a php form with three text boxes (webmeasurementsuiteId, webmeasurementsId, Id) and the values in the text boxes are retrieved from other tables of the database. Now my task is to submit the retrieved values in this php form named (mapping) to the database. I have created the table with the following syntax:
CREATE TABLE `mapping` (
`webmeasurementsuiteId` INT NOT NULL,
`webmeasurementsId` INT NOT NULL,
`Id` INT NOT NULL,
PRIMARY KEY (Id)
);
But I am getting an sql error as follows:
INSERT INTO mapping(webmeasurementsuiteId,webmeasurementsId,Id) values ('','','7')
ERROR: Incorrect integer value: '' for column 'webmeasurementsuiteId' at row 1
Can anyone correct my error?
all your columns are INT that means numbers while your insert statement is inserting STRINGs (text) remove the ' around the values in the INSERT-statement and it should work
example:
INSERT INTO mapping(webmeasurementsuiteId,webmeasurementsId,Id) values (0,0,7)
So you used '' for webmeasurementsuiteId which indicates it as a string. just leave the integers to 0 without the parantheses to indicate them as an integer. values (0,0,7) should probably do it.
you cannot insert blank in the values field .
try 0 instead of ''
INSERT INTO mapping(webmeasurementsuiteId,webmeasurementsId,Id) values (0,0,'7')
or alter the columns to take the null values
Related
I have initialized a database users like this create table users (id not null auto_increment primary key, first_name varchar(256) not null, last_name varchar(256) not null);
However, I can just execute insert into users values (2,'James','Bond'); and no error is thrown. I want to create a table so that the primary key gets auto incremented and one can simply add entries like so: insert into users values ("firstname","lastname"); is this even possible?
When you use the syntax insert into users values ('firstname','lastname'); i.e. you do not specify a list of columns, this implies your VALUES clause will contain values for all columns. This is not related to using auto_increment, it applies to any table.
You can work around this by specifying the columns:
insert into users (firstname, lastname) values ('firstname','lastname');
By omitting id from the list of columns, it should also be omitted from the VALUES clause.
Another form of INSERT syntax supported by MySQL (although it's nonstandard) might be more clear. It also allows you to omit columns you don't want to set in your INSERT statement.
insert into users SET firstname='firstname', lastname='lastname;
If you do include the id column either implicitly or explicitly, you can trigger the auto-increment behavior by using NULL, DEFAULT, or if the sql mode permits it, 0.
If you specify any other value for the id, it overrides the auto-increment.
This is all documented here: https://dev.mysql.com/doc/refman/8.0/en/example-auto-increment.html and related pages linked by that page.
you can name the columns that you want to insert.
But if you have more t columns , you have to define DEFAULT values, so that Mysql knows how to fill the not inserted values
INSERT INTO users (first_name ,last_name ) VALUEs("firstname","lastname");
The fact a column is auto-incrementing doesn't mean you can just ignore it in an insert statement. You could, however, avoid assigning a value to it and letting the auto_increment property handle it by using the default keyword:
insert into users values (default, 'firstname', 'lastname');
Is it possible to LOAD DATA a csv into mysql without having to add empty values for non existing columns at the end?
All my optional columns are sorted at the end of the schema:
CREATE TABLE `person` (
id int(20) NOT NULL AUTO_INCREMENT,
firstname varchar(30) NOT NULL,
lastname varchar(30) NOT NULL,
optional1 varchar DEFAULT NULL,
optional... varchar DEFAULT NULL,
optional50 varchar DEFAULT NULL,
PRIMARY KEY (`id`)
) engine=innodb AUTO_INCREMENT=0;
sample.csv:
1;john;doe
2;jabe;doe;;;opt val3;;;;;;opt val9;;;;;;...
Important: I don't want to explicit list all the columns in my LOAD DATA INFILE sql statement (I know that this would work by using a combination of IFNULL and #var).
But can't I just load into the table, telling mysql to ignore any missing fields at the end of each line?
The documentation of MySQL LOAD DATA syntax provides the following information:
By default, when no column list is provided at the end of the LOAD DATA statement, input lines are expected to contain a field for each table column. If you want to load only some of a table's columns, specify a column list.
[...]
If an input line has too few fields, the table columns for which input fields are missing are set to their default values. For numeric types, the column is set to 0.
[...]
An empty field value is interpreted different from a missing field: for string types, the column is set to the empty string.
So given your sample data:
1;john;doe
2;jabe;doe;;;opt val3;;;;;;opt val9;;;;;;...
Record with id 1 will have all optional columns set to NULL (ie their default). For id2, optional string columns will be set to the empty string. .
I cannot tell whether this would be OK for your use case or not. If you do want consistent values in the optional columns, available options would be:
input pre-processing: use SET to set to NULL columns that contains an empty string
LOAD DATA INFILE 'file.txt' INTO TABLE t1
SET
optional1 = NULLIF(optional1, ''),
optional2 = NULLIF(optional1, ''),
...
set up a BEFORE INSERT trigger on the table that sets to NULL empty values
run an update on the table after it was populated
UPDATE t1 SET optional1 = NULLIF(option1, ''), optional2 = NULLIF(optional1, '')
WHERE '' IN (optional1, optional2, ...)
I found out it works as expected if adding the IGNORE keyword to the LOAD DATA statement:
LOAD DATA INFILE 'sample.csv' IGNORE INTO TABLE persons
Thereby, I can define all my optional columns as DEFAULT NULL, and if values are missing, they are set to NULL during import.
I noticed that I can insert multiple NULL values into a table with a UNIQUE key :
INSERT INTO table (autoincrementkey, uniquekey, id) VALUES (NULL, NULL, 0), (NULL, NULL, 1), (NULL, NULL, 2)
This works fine as I get the 3 lines inserted (and yes, the uniquekey is NULL = Yes)
However, when I try to use the Load Data from a file with the same query, it only inserts 1 NULL value, which is the first.
LOAD DATA LOCAL INFILE $file.csv INTO TABLE table FIELDS TERMINATED BY ',' ENCLOSED BY '"'
Is there any way I can insert multiple NULL values on a UNIQUE column, with the LOAD DATA INTO please ?
Thanks in advance!
=============
[UPDATE]
I noticed that uniquekey gets a 0 instead of a NULL, which is why only the 1st entry gets to the database.
In the CSV file it looks like this :
,,1
,,2
,,3
But uniquekey is set to NULL by default, I don't get why it gets 0 :
ALTER TABLE `table` CHANGE `uniquekey ` `uniquekey ` INT(10) DEFAULT NULL;
[UPDATE 2]
OK I got it!!
I thought (,,1) would insert NULL, NULL, 1.
But it's not true, it has to be (NULL,NULL,1) to get what you see.
I have user table. The primary key is user_id which has a datatype bigint(20).
I generate a user_id using UUID_SHORT() via trigger below. The issue is I get a warning when I try to insert a record as follows:
Warning: #1366 Incorrect integer value: '' for column 'user_id' at row 1
My phpMyAdmin trigger is as follows:
BEGIN
SET NEW.user_id=UUID_SHORT();
END
Any reason why I am getting this warning? Have I set the datatype correctly?
INT is a four-byte signed integer, while UUID_SHORT() returns a 64-bit (i.e. 8 byte) unsigned integer. You are trying to store a 64-bit data type into a 4-byte INT and MySQL appears to be storing an empty string '' into the field user_id.
From the MySQL manual:
UUID_SHORT()
Returns a “short” universal identifier as a 64-bit unsigned integer (rather than a string-form 128-bit identifier as returned by the UUID() function).
You should use the BIGINT UNSIGNED type instead.
will not work if you try to insert from PMA, as it generate this kind of query :
INSERT INTO `users` (`user_id`, `name`) VALUES ('', 'sami')
you can replace the '' with NULL, or remove user_id from the insert
INSERT INTO `users` (`name`) VALUES ('sami')
this will solve the warning problem.
In phpMyAdmin, as we create table there is not null constraints by default for all fields...and as per my knowledge when we set the constraint to not null...it doesn't allow user to remain field empty which are not null as per this link.....
http://www.techopedia.com/definition/27370/not-null-constraint
now my question is..according to this link, not null means every row of data must contain a value - it cannot be left blank during insert or update operations.....but when i insert data programatically like insert into, i am able to insert data in just two fields and other remains blank although there is not null constraints on that fields ...and still not generates any error....so i don't understand how not null works???
for example, i create table with lets say 5 fields...
create table myTable
(
Column1 int not null,
Column2 int not null,
Column3 int not null,
Column4 int not null,
Column5 int not null,
)
and insert values in just two fields like
"INSERT INTO myTable (column1,column2) VALUES(10,20)";
but other fields i don't give any '' so it takes 0 as value....and still i am able to insert data with no error...how is that possible??
Everything that has the NOT NULL constraint set needs to contain data. If you insert data programmatically and you do not insert data for a NOT NULL cell, then you will get an SQL Error.
e.g. you have this table:
CREATE TABLE test (
id INTEGER PRIMARY_KEY AUTO_INCREMENT,
some_value INTEGER NOT NULL,
some_other_value INTEGER);
Then some_value will contain data in every data set returned, some_other_value may or may not contain data in every data set returned. The only thing to work around this would be this:
CREATE TABLE test (
id INTEGER PRIMARY_KEY AUTO_INCREMENT,
some_value INTEGER NOT NULL DEFAULT 0,
some_other_value INTEGER);
If you now set data programatically and do not set data for some_value, some_value will default to 0 (or to whatever data you set the default to on table creation).
Maybe you can refer to this link:
For multiple-row INSERT statements or INSERT INTO ... SELECT
statements, the column is set to the implicit default value for the
column data type. This is 0 for numeric types, the empty string ('')
for string types, and the “zero” value for date and time types. INSERT
INTO ... SELECT statements are handled the same way as multiple-row
inserts because the server does not examine the result set from the
SELECT to see whether it returns a single row. (For a single-row
INSERT, no warning occurs when NULL is inserted into a NOT NULL
column. Instead, the statement fails with an error.)
If a column definition includes no explicit DEFAULT value and it is defined as "Not Null" then Mysql will automatically assign default value to the column based on datatype. e.g. 0 for integer and "" for varchar
If you create a unique index on a column, the default value will be accepted with the first row but will give an error with subsequent inserts.