INSERT values to MySQL - 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');

Related

Can't insert data when use foreign key together with trigger in Mariadb [duplicate]

So I read the other posts but this question is unique. So this SQL dump file has this as the last entry.
INSERT INTO `wp_posts` VALUES(2781, 3, '2013-01-04 17:24:19', '2013-01-05 00:24:19'.
I'm trying to insert this value to the table...
INSERT INTO `wp_posts` VALUES(5, 5, '2005-04-11 09:54:35', '2005-04-11 17:54:35'
it gives me the error, "Column count doesn't match value count at row 1." So I'm lost on the concept of how the column and row apply here.
Doesn't 2781,3 mean row 2781 and column 3? And doesn't 5,5 mean row 5 and column 5?
The error means that you are providing not as much data as the table wp_posts does contain columns. And now the DB engine does not know in which columns to put your data.
To overcome this you must provide the names of the columns you want to fill. Example:
insert into wp_posts (column_name1, column_name2)
values (1, 3)
Look up the table definition and see which columns you want to fill.
And insert means you are inserting a new record. You are not modifying an existing one. Use update for that.
you missed the comma between two values or column name
you put extra values or an extra column name
You should also look at new triggers.
MySQL doesn't show the table name in the error, so you're really left in a lurch. Here's a working example:
use test;
create table blah (id int primary key AUTO_INCREMENT, data varchar(100));
create table audit_blah (audit_id int primary key AUTO_INCREMENT, action enum('INSERT','UPDATE','DELETE'), id int, data varchar(100) null);
insert into audit_blah(action, id, data) values ('INSERT', 1, 'a');
select * from blah;
select * from audit_blah;
truncate table audit_blah;
delimiter //
/* I've commented out "id" below, so the insert fails with an ambiguous error: */
create trigger ai_blah after insert on blah for each row
begin
insert into audit_blah (action, /*id,*/ data) values ('INSERT', /*NEW.id,*/ NEW.data);
end;//
/* This insert is valid, but you'll get an exception from the trigger: */
insert into blah (data) values ('data1');
MySQL will also report "Column count doesn't match value count at row 1" if you try to insert multiple rows without delimiting the row sets in the VALUES section with parentheses, like so:
INSERT INTO `receiving_table`
(id,
first_name,
last_name)
VALUES
(1002,'Charles','Babbage'),
(1003,'George', 'Boole'),
(1001,'Donald','Chamberlin'),
(1004,'Alan','Turing'),
(1005,'My','Widenius');
You can resolve the error by providing the column names you are affecting.
> INSERT INTO table_name (column1,column2,column3)
`VALUES(50,'Jon Snow','Eye');`
please note that the semi colon should be added only after the statement providing values
In my case i just passed the wrong name table, so mysql couldn't find the right columns names.

How to insert a binary value into mysql

Is there a flag to indicate that data is of binary type, or does that need to be converted to another type. For example, is there a way to do:
INSERT INTO mytable (number) VALUES (0000 0010)
Instead of:
INSERT INTO mytable (number) VALUES (2)
I know the above is a trivial example but basically I'm wondering if you can type a binary/hex value directly into a query.
You can use a bit-value literal
INSERT INTO mytable (number) VALUES (b'00000010');
or
INSERT INTO mytable (number) VALUES (0b00000010)

SQL INSERT - Can I insert null values if column count doesn't match?

I get an error message that says "#1136 - Column count doesn't match value count at row 1" when I try to insert the values below. Is there any way to have the lines of data that are shorter automatically insert as null at the end without manually going into each line of code?
VALUES ('','BUF','','QB1','','QB BILLS','Face=0x52','#0','25','69','13','13','56','81','81','81','3','12','3');
VALUES ('','BUF','','QB2','frank REICH','Face=0x22','#14','25','69','13','13','31','44','25','50','3','3','2');
VALUES ('','BUF','','RB1','thurman THOMAS','Face=0x83','#34','38','69','63','25','75','50','10','7','8','8');
VALUES ('','BUF','','RB2','jamie MUELLER','Face=0x51','#41','44','69','25','88','50','25','5','3','6','2');
VALUES ('','BUF','','RB3','kenneth DAVIS','Face=0xa5','#23','38','69','25','19','50','31','5','2','7','2');
VALUES ('','BUF','','RB4','don SMITH','Face=0x8b','#30','38','69','25','19','50','31','3','3','8','2');
VALUES ('','BUF','','WR1','james LOFTON','Face=0x81','#80','25','69','38','13','50','56','1','8','13','6');
VALUES ('','BUF','','WR2','andre REED','Face=0xb6','#83','25','69','56','13','56','69','1','9','10','10');
VALUES ('','BUF','','WR3','don BEEBE','Face=0x40','#82','25','69','44','13','50','44','1','4','13','2');
VALUES ('','BUF','','WR4','al EDWARDS','Face=0x9a','#85','25','69','19','13','50','44','1','4','7','2');
VALUES ('','BUF','','TE1','keith MCKELLER','Face=0xb7','#84','25','69','38','50','50','50','1','6','7','6');
VALUES ('','BUF','','TE2','pete METZELAARS','Face=0x50','#88','25','69','19','44','50','31','1','3','5','2');
VALUES ('','BUF','','C','kent HULL','Face=0x1e','#67','25','69','38','69');
VALUES ('','BUF','','LG','jim RITCHER','Face=0x7','#51','25','69','38','56');
VALUES ('','BUF','','RG','john DAVIS','Face=0x24','#65','25','69','25','63');
VALUES ('','BUF','','LT','will WOLFORD','Face=0x48','#69','25','69','25','50');
VALUES ('','BUF','','RT','howard BALLARD','Face=0x88','#75','25','69','19','63');
VALUES ('','BUF','','RE','bruce SMITH','Face=0x88','#78','44','56','69','75','25','81','116','7');
VALUES ('','BUF','','NT','jeff WRIGHT','Face=0xb','#91','25','31','31','50','19','19','30','7');
VALUES ('','BUF','','LE','leon SEALS','Face=0xac','#96','25','31','38','44','31','50','25','18');
VALUES ('','BUF','','ROLB','darryl TALLEY','Face=0xad','#56','31','44','50','38','44','63','25','25');
VALUES ('','BUF','','RILB','ray BENTLEY','Face=0x30','#50','25','31','38','38','31','56','13','10');
VALUES ('','BUF','','LILB','shane CONLAN','Face=0x2f','#58','31','44','50','56','19','69','13','13');
VALUES ('','BUF','','LOLB','c. BENNETT','Face=0x82','#97','38','50','63','63','19','69','29','7');
VALUES ('','BUF','','RCB','nate ODOMES','Face=0xc3','#37','38','44','56','38','38','56','0','25');
VALUES ('','BUF','','LCB','kirby JACKSON','Face=0x89','#47','25','31','44','38','50','50','0','64');
VALUES ('','BUF','','FS','mark KELSO','Face=0x26','#38','31','38','50','38','44','44','2','39');
VALUES ('','BUF','','SS','leonard SMITH','Face=0x84','#46','31','38','50','44','44','50','2','40');
VALUES ('','BUF','','K','scott NORWOOD','Face=0x29','#11','56','81','81','31','44','44','6');
VALUES ('','BUF','','P','rick TUTEN','Face=0x20','#10','25','56','44','31','19','63','3');
Remove the unwanted column from your mysql statement
for example
mysqli_query($con,"INSERT INTO files (1, 2, unwanted, 4)
VALUES ('$01', '$02', '' ,'$04')");
make it
mysqli_query($con,"INSERT INTO files (1, 2, 4)
VALUES ('$01', '$02' ,'$04')");
or Add a variable which is null,
<?php
$null = '';
VALUES ('$null','BUF','$null','ROLB','darryl TALLEY','Face=0xad','#56','31','44','50','38','44','63','25','25');
?>
I have made the following assumptions base on your current post. I assume you want to add the score of a football player to the same table as the fixed data from each player. I would not recommend this because you will continue to add a new column every time you like to add a score.
I have made the following example for you how you can make a score table and player table that will separates the fixed data like name of the player, picture, etc from the scores.
Try this:
SQL query:
SELECT players.name, group_concat(score.score)
FROM players
JOIN score
ON players.id = score.id_players
GROUP BY players.id;
CREATE TABLE players (
id int auto_increment primary key,
name varchar(30)
);
CREATE TABLE score (
id int auto_increment primary key,
score int,
id_players int
);
INSERT INTO players (name)
VALUES ('QB BILLS'), ('frank REICH');
INSERT INTO score (id_players, score)
VALUES (1,25),(1,69), (1,13),(1,13),(1,56),(1,81),(1,81),
(1,81),(1,3),(1,12),(1,3), (2,25),(2,69),(2,13),(2,13),(2,31),(2,44),(2,25),(2,50),(2,3),(2,3),(2,2);
SQLFiddle demo

Can't Insert Row into Table (Column Count Mismatch)

I've got a script that's creating a table, and then inserting a row afterwards. Here is my SQL code executing to create the table:
CREATE TABLE polls (
id INT NOT NULL UNIQUE AUTO_INCREMENT,
name VARCHAR(255) NOT NULL UNIQUE,
author VARCHAR(255) NOT NULL,
created DATETIME NOT NULL,
expires DATETIME,
PRIMARY KEY(id)
)
And here is where I add a new row:
INSERT INTO polls
VALUES ('TestPoll'),('Billy Bob'),('2013-05-01 04:17:31'),('2013-05-01 04:17:31')
or
INSERT INTO polls
VALUES ('TestPoll','Billy Bob','2013-05-01 04:17:31','2013-05-01 04:17:31')
(I get the same error regardless)
I always get this error:
<class '_mysql_exceptions.OperationalError'>, OperationalError(1136, "Column count doesn't match value count at row 1"), <traceback object at 0x7f7bed982560>
Your syntax is wrong, try:
INSERT INTO polls
VALUES ('TestPoll','Billy Bob','2013-05-01 04:17:31','2013-05-01 04:17:31')
but if your table structure changes, your code will break, a safer version is:
INSERT INTO polls (name, author, created, expires)
VALUES ('TestPoll','Billy Bob','2013-05-01 04:17:31','2013-05-01 04:17:31')
Your INSERT query is not correctly formatted.
INSERT INTO polls (name, author, created, expires)
VALUES ('TestPoll','Billy Bob','2013-05-01 04:17:31','2013-05-01 04:17:31');
For more information, visit MySQL Reference Manual for the INSERT statement.
EDIT: It's always a good idea to explicitly type each column name, in case the table structure will change in some foreseeable future.
In mysql you have to pass column name in your insert query.After assigning column name your query will look like
INSERT INTO polls (name,author,created,expires) values ('TestPoll','Billy Bob','2013-05-01 04:17:31','2013-05-01 04:17:31');
Hope it helps.

REPLACE into keeping a value

I want to make a replace into in a table where cust_id is the primary key, but I do not want to modify the date field. So, a normal insert on this table would look like:
REPLACE INTO emails(cust_id, email, date)
VALUES(55, 'email#email.com', '2011-08-07 00:00');
Now, without having to modify the date field, it would be something such as:
REPLACE INTO emails(cust_id, email, date)
VALUES(55, 'email#email.com', date.value?);
But how do I exactly keep the date value?
Short answer, You can't keep the dates that way. from Mysql documentation
Values for all columns are taken from the values specified in the
REPLACE statement. Any missing columns are set to their default
values, just as happens for INSERT. You cannot refer to values from
the current row and use them in the new row
perhaps you want to use http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html instead.
insert ignore will skip insertion if any duplication
if you need to update certain fields ,
you can do
insert into some_table values (...)
on duplicate update email=?;