Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
CREATE TABLE people (
id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(50) NOT NULL,
salary INT NOT NULL,
PRIMARY KEY (id)
);
My insert statement
INSERT INTO people
VALUES
('Austin', 65000),
('Josh', 100000),
('Pettea', 55000)
;
The columns amount (total columns amount in table structure while their list is skipped) must match the values amount strongly.
So either specify column names:
INSERT INTO people (first_name, salary)
VALUES
('Austin', 65000),
('Josh', 100000),
('Pettea', 55000);
or specify the value for each column:
INSERT INTO people
VALUES
(NULL, 'Austin', 65000),
(NULL, 'Josh', 100000),
(NULL, 'Pettea', 55000);
In the last code you may use zero value or DEFAULT keyword instead of NULL. Any of these 3 values causes new value generation.
PS. The former code is preferred - it does not need in any edition if the structure is altered.
PPS. I'd recommend to define autoincremented column datatype as UNSIGNED - this doubles the amount of possible autogenerated values twice.
It should be:
INSERT INTO people(first_name, salary)
VALUES
('Austin', 65000),
('Josh', 100000),
('Pettea', 55000);
try that.
Here's a working fiddle: https://www.db-fiddle.com/f/rW6QyPbyEMvZhcskipBpaX/0
click "Run".
Edit:
I think I misread your comment: In this case you need to specify which column(s) you want to insert data. For now your query is trying to insert a two columns data value into a three columns table. INSERT INTO people is similar to INSERT INTO people(id,first_name,salary) so inserting two columns worth of data returns Column count doesn't match value count at row 1 error. It's acceptable if you do like this:
INSERT INTO people
VALUES
(1,'Austin', 65000),
(2,'Josh', 100000),
(3,'Pettea', 55000);
But you're making id column as AUTO_INCREMENT for a reason hence it's unnecessary to manually populate it.
Related
I am in need to insert a row in a table if it does not exist, or update it otherwise, but based in a column that is not unique or primary.
I am doing the insert part with :
INSERT INTO table (`match`, `to`, `status`, `type`, cat, rel, tag) VALUES ('$match_tag', '$match_tag_url', '1', 'redirection', 'confirmed', '', '$tag')
but if match exists already I'd like to update the to value, all in one query if possible.
I apologise in advance for the novice question and thank everyone for any input.
I am thinking the solution might relate to the ON DUPLICATE KEY UPDATE statement, but all the examples I have found so far involve a unique or primary item which I do not have in my case.
I added a new column named roomNumber to my hotel table which already has some values. But when I try inserting new values into hotel the values would get inserted from where the current data finished rather than at the beginning.
my query
INSERT INTO hotel
(roomNumber)
VALUES
(20),
(60),
(100),
(20),
(20),
(150),
(50),
(50),
(30),
(50);
The values started inserting from where id = 10 , rather I want it to insert where id = 1 (at the beginning)
If you INSERT data you will create new rows for the new data. If you want to change existing rows you need to UPDATE those rows.
So, to update your three existing rows you need three UPDATE queries*:
UPDATE hotel set roomNumber = 20 where id=8
UPDATE hotel set roomNumber = 60 where id=9
UPDATE hotel set roomNumber = 100 where id=10
I've assumed your primary key column is called id - you may have a different name for it.
Then to insert your remaining data you can run an INSERT query:
INSERT hotel (name, roomNumber)
values
('Excelsior',20),
('Manor Park Hotel', 20)
etc.
I've invented some hotel names here since it seems to make little sense to insert a room count with no hotel name.
Note that 'beginning of the table' has no real meaning in SQL. The order of rows is not guaranteed unless you specify an ORDER BY clause, but that's not relevant here. To update a specific row you need to specify a WHERE clause that identifies that row. The Primary Key is a common way to do that.
* You could do the three updates with a single UPDATE query if you use a suitable WHERE clause, but that seems needlessly complicated for three rows.
When using SQL insert you are inserting new rows into a table. You want to update the existing rows, then run an insert for the rest of the new data.
UPDATE statement with multiple WHERE conditions
So you want to insert the data from ID 1?
You need to use UPDATE, not INSERT. As the rows already exist.
This question already has answers here:
Column count doesn't match value count at row 1
(6 answers)
Closed 2 years ago.
Error Code: 1136. Column count doesn't match value count at row 1
roll_no INT PRIMARY KEY NOT NULL unique,
name CHAR(15) NOT NULL,
class integer(3),
section CHAR(1),
sibling boolean default false);
insert into school
values(1,'ramesh',12,'D');
If you don't provide values for all the columns, you need to explicitly state what columns the values refer to:
insert into school(roll_no, name, class, section)
values(1, 'ramesh', 12, 'D');
You didn't specify the column names in the insert, and therefore MySQL is expecting data for all columns. Qualify your target column names, and the insert should work:
INSERT INTO school (roll_no, name, class, section)
VALUES
(1, 'ramesh', 12, 'D');
Best practice for SQL inserts is to always list out the target columns for the insert. One reason for this is that, if you don't, an insert statement could break if either a) the number of columns changes, or b) the order of columns in the table changes.
I'm pretty much a complete newbie to SQL, I'm using MySQL with SQLyog. I have five fields, StudentForename, StudentSurname, StudentAge, StudentHouse and StudentID for the Primary Key. The StudentID field is set as a Primary Key and Not Null and AutoIncrement. I'm trying to use an INSERT INTO statement without having to entering the primary key - apparently I shouldn't need to, it should update itself. But it's not working, it's returning the error "Column count doesn't match value count at row 1". Here's the code I'm using. I've already set up the table, so I haven't got the code for the query that
INSERT INTO students VALUES('Harry', 'Potter', 'Slytherin', 30)
You will need to explicitly state which columns you will provide values for, otherwise it is assumed you will provide values for all columns. E.g.
INSERT INTO students (`first_name`, `last_name`, `house`, `age`) VALUES('Harry', 'Potter', 'Slytherin', 30)
(I made up column names, swap these with your columns)
I have been trying to do insert / update records in a mysql table. Cannot use ON DUPLICATE KEY because i have nothing to do with the primary key.
Basically i have to update a record in the database
INSERT INTO table (city, state, gender, value) VALUES ("delhi","delhi","M",22)
If a record of that city, state, gender exists, then simply overwrite the value.
Can i achieve this without sending two queries from the programming language
actually you can still use ON DUPLICATE KEY, just add a unique index on the following columns, eg
ALTER TABLE tbl_name ADD UNIQUE index_name (city, state, gender)
your query now will be,
INSERT INTO table (city, state, gender, value)
VALUES ('delhi','delhi','M', 22)
ON DUPLICATE KEY UPDATE value = 22
Keep in mind that constructs such as ON DUPLICATE KEY and REPLACE INTO were specifically designed to prevent exactly that. The only other way to prevent two queries from your application layer is by declaring a database function that does the same things.
Therefore, add either a UNIQUE(city, state, gender) key or a primary key that spans the same columns. The difference between the two lies in the value range of each column; primary keys force NOT NULL whereas UNIQUE allows for columns to be NULL.
The difference is subtle but can sometimes lead to unexpected results, because NULL values are considered to be unique. For example, let's say you have this data in your database:
nr | name
123 | NULL
If you try to insert another (123, NULL) it will not complain when you use UNIQUE(nr,name); this may seem like a bug, but it's not.