SQL insert new values into new column from the beginning of table - mysql

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.

Related

Issues with MySQL inserting columns below desired location

I have been having some frustration attempting to add data values to this table students. I have all the other data values and have dropped and created the column student_id. However, when trying to add the data with this query:
insert into students(student_id) values('1'),('2'),('3'),('4'),('5');
The data does not insert correctly, as it creates new columns below the first 5 which contain data.
It must be because of my not null values, but I can't not have the not null identifier.
Is there a query command that allows me to change data within already existing value-filled columns? I have been unsuccessful in finding this so far.
Here are some images to explain the problem further.
The query I have made to add my values to the table:
The data was inserted but as it is underneath the columns I need to map with a foreign key, I cannot use the column as the top 5 values are still my not null default, which is required to let me create the foreign key
Looks like you already have your records initially created without the student_id field, you want to UPDATE the current records but you're actually INSERTING new records.
You're meant to update your students with update statements such as "UPDATE students SET student_id = X where condition = Y"
Then it looks like your student_id is your primary key which you should set to AUTO_INCREMENT value.
Regards
INSERT is the wrong command since you want to update existing rows. The problem here lies within the fact that the order of the rows is nondeterministic and I think you cannot update them in one statement. One solution would be as follows:
UPDATE students SET student_id = 1 WHERE first_name = 'Berry';
UPDATE students SET student_id = 2 WHERE first_name = 'Darren';
I hope you really do have only 5 columns to update :-)

Do not instert a record if a key is already present

I am a newbie to mysql. Please help.
I have a table people like this. The only primary key of people is id
id name age sex
1. John 16 M
2. Peter 18 K
I would like to write some sql to insert some rows to people. But if the name is already exist
in the table. I do not insert new row. For example, if I insert the row with the name John and
Peter. I do not insert rows.
I have a variable name as var_name;
I have search out for the web for a very long time.
I use the following sql recommend by the web
INSERT into People(name) values(var_name) where not exists(SELECT name from People
where name = var_name)
But the sql syntax error comes out. Why would this happen. And is there any fast way to acheieve my goal.
The best way to do this is to create a unique index on name:
create unique idx_people_name on people(name)
Then, when you insert, use on duplicate key update:
INSERT into People(name)
values(var_name)
on duplicate key update name = values(name);
The update piece does nothing -- it is a "no-op". But this puts the logic in the database and enforces that names need to be unique.
For your query to work, you need insert . . . select. The values clause doesn't take a where statement:
INSERT into People(name)
select var_name
from dual
where not exists(SELECT name from People where name = var_name);
If you have a unique constraint on the name, I believe you can use:
INSERT IGNORE People(name) VALUES (var_name);

inserting data into a new column of an already exsisting table

I added a new column to an already existing table, I just need to get data into this new column...I assume I can use a variant of the insert command, yet I do not know how to specify the column name and then how to order the values going into the new column.
Ok, after some conversation through the comments lets go to an answer.
I suppose your table is something like id, name, age, dateBirth, etc fields. But whoever create this table forget to add the gender for the registries. As you said that the new column is an sex enum('m', 'f') you will have to update every registry on this table one by one. Like this:
update matches set sex = 'm' where id = 1;
Pay attention that with this command I just updated the row on the table where the id=1 and Im assuming that id is your primary key. On the where caluse you have to put your primary key, otherwise you may update more then one column.
If your table has many registries there is a way that you can do it cuting down the heavy work (at least a little)
In order to update many rows at once you have to do an Update with a LIKE filter, you will set a filter that can identifie many womans at a time then many men at time as this:
update matches set sex = 'f' where name like '%Jheniffer%'
Since Jheniffer is a female name most likely you will update every registry which has part of the name as Jheniffer like 'Jheniffer Smith'. So repeat this process for the common names until the work is done. For all womens then repeat for the men.
Hope it help you to understand
you have to use update command, insert is for adding new rows.
update myTABLE set NewColumn = ?
Why INSERT here? You need to UPDATE data to column inserted.
Here is the list of steps
Alter the table then add column with constraint is NULLABLE.
Process the update the column added using UPDATE command.
If you want the column added is NOT nullable. Just re-alter the column and change it to NOT nullable.
You can use UPDATE command.
UPDATE my_table SET new_col='new_val'
if you need to set new column value just in few rows add WHERE clause
UPDATE my_table SET new_col='new_val' WHERE condition='true'

MySQL - insert into statement requires me to enter primary key

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)

MySQL: Insert rows using a column from another new table

I've been fighting to get the SQL right on this insert statement.. here's what I'm trying to do:
I have new empty table that has a 1-to-1 relationship with another table. Since all the other columns have default values I only want to insert the primary key column as new rows from the old table. Table names are dealer and dealer_nav and the fields are id and dealer_id respectively.
Thanks!
INSERT INTO dealer_nav(dealer_id) SELECT id FROM dealer (or vice versa, I'm not sure which of 2 tables is new and which is old)