Updating multiple rows in SQL - sql

Using phpMyAdmin, I want to update multiple rows of data with some new information.
The 'uid' is unique to each record.
The 'pid' field is the same for all records.
I modified an INSERT INTO statment after I exported the data from another database.
UPDATE `tabme_name` SET (`uid`, `pid`, `title`, `category`) VALUES
(230952, 1902, 112, 27634),
(230953, 1902, 179, 27641),
(230954, 1902, 75, 27630),
(230955, 1902, 38, 27626);
I can only find information on updating one record, or updating records with the same information.
Could somebody show me a correct SQL Statment? I havn't got a WHERE clause in it!?!

Assuming the uid is the pk you must execute multiple updates:
UPDATE tabme_name SET pid = 1902, title =112, category=27634 WHERE uid=230952
UPDATE tabme_name SET pid = 1902, title =179, category=27641 WHERE uid=230953
...

Related

SQL Server invalid object name insert

Recently I realized I should have a primary key in this table, so I tried to add one by doing the following steps:
Added a new column with nullable values
Filled all the values with integers indexed from 1 - 178
Made the column not allow nulls - this saved fine with no warnings that there
were still null values
I set the identity column to Id (new column I created)
Tried to insert a new row and got the following message
Msg 208, Level 16, State 74, Procedure tr_dbo_SaveState_7d7e8c09-470a-4510-ac78-12bf952f3a14_Sender, Line 63 [Batch Start Line 0]
Invalid object name 'dbo_SaveState_7d7e8c09-470a-4510-ac78-12bf952f3a14/StartMessage/Insert'.
I thought maybe I had to set the identity seed to the lastvalue+1 but still got the same message. I can insert into other tables just fine, and when I put the wrong number of arguments into the VALUES() function I get a different (expected) error, but when doing what I do for other tables, and used to do for this table I now get the above error. Does it seem the insert function is missing for this table? BTW this user has all permissions so that shouldn't be an issue
I also tried undoing this but it seems like the INSERT method for this table has just been deleted, or is somehow unavailable.
I can still select from the table just fine
When I try to update values I don't get the error message if the WHERE clause always results to false, but if there is a row that it tries to update I get an error message - this is not the case when I right-click on the table and Edit top 200 rows, only when I try and do it through query

Invalid Column Name when Inserting Data

I have a local installation of SQLExpress, and accessing it using SQL Server Management Studio.
I created a new database BirdSite in SSMS, with a single table TMasterCountry, with these columns:
Id (int)
CountryName (varchar(50))
where Id is the primary, auto-incrementing key.
When I use the Script table as... option in the object explorer, I am able to view the empty table with this bit of SQL:
USE [BirdSite]
GO
SELECT [Id]
,[CountryName]
FROM [dbo].[TMasterCountry]
GO
without any issues.
However, using the same option to try inserting some data, brings up this SQL:
USE [BirdSite]
GO
INSERT INTO [dbo].[TMasterCountry]
([CountryName])
VALUES
(<CountryName, varchar(50),>)
GO
So I changed the line under VALUES to
(CountryName, 'test')
But when I try running the code, I get these errors:
Msg 207, Level 16, State 1, Line 4
Invalid column name 'CountryName'.
Msg 110, Level 15, State 1, Line 4
There are fewer columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.
Does anyone know what might be going on here? The column clearly does exist, as I can SELECT the data just fine.
Following some similar questions on SO, I have tried restarting SSMS and also tried refreshing the local Intellisense cache, but with no luck. I also tried surrounding CountryName in square brackets but with the same result.
Change it to:
USE [BirdSite]
GO
INSERT INTO [dbo].[TMasterCountry]
([CountryName])
VALUES
('test')
GO
<CountryName, varchar(50),> refers to name of column and it's datatype.
Syntax error; Should be
INSERT INTO [dbo].[TMasterCountry]
([CountryName])
VALUES
('test')
You need to replace the entire content between < and > with the value:
INSERT INTO [dbo].[TMasterCountry]
([CountryName])
VALUES
('test')

assistance needed with sql statements/expressions

New to sql statements etc and I have an issue with what i am doing using squirrelSQL on linux machine
I Created a table and used the following sql statements:-
INSERT INTO FIRSTTABLE VALUES
(11,'TEN','STEVE'),(21,'TWENTY','JO'),(31,'THIRTY','KIDS')
ALTER TABLE FIRSTTABLE
ADD SURNAME VARCHAR(15);
this works fine however when i attempt to insert data/values into the the surname row i keep experiencing errors, the SQL statement i am using is:-
INSERT INTO FIRSTTABLE (SURNAME)
VALUES ('THOMAS'),('THOMAS'),('THOMAS'),('THOMAS');
This particular statement returns the following error:-
Error: Column 'ID' cannot accept a NULL value.
SQLState: 23502
ErrorCode: 30000
I only wish to add data/values into the surname column,after creating a new column with the alter table statement, i have tried many different combinations including using a SELECT statement prior to the INSERT statement above which also gives errors any guidance will be greatly appreciated,
You are inserting into Surname, without assigning a value to the other fields. You are getting this error message because ID is blank, and should not.
Understand that INSERT creates new rows. If you wish to modify existing rows, use UPDATE
In this case you could use UPDATE FIRSTTABLE SET SURNAME='THOMAS';
Omitting the WHERE clause affects all the fields in the table.
Hope it helps, and good luck in your learning process!
The approach is wrong, you need to:
UPDATE FIRSTTABLE SET SURNAME='THOMAS' WHERE ID IN (11, 21, 31)
Inserting will add a new row to the table. So you need to update a row using
UPDATE FIRSTTABLE SET SURNAME="THOMAS" WHERE ID=11

Sqlite database with single row

I want to create a sqlite database table having single row and multiple columns.
Each time entire row will be updated.
In the sqlite i can create the table but first time updating entire row is not working.
The update entire row works only after the first INSERT command. Instead of doing first time INSERT command is it possible to execute UPDATE command as soon as the table is created?
I am doing following steps:
$ sqlite3 test.db
sqlite> create table company (Id int primary key not null, name text, age int);
sqlite> update company set id=1, name='test', age=30;
sqlite> select * from company;
sqlite>
Here select statement is not returning anything.
Is there any restriction in SQL to use the UPDATE command very beginning?
You seem to be ignoring how sql works. You perform an update of nothing and expect that a row magically exists after that.
You have to do an insert first.
You want to try
insert into company values (1, 'test', 30);
in order to have one record.
update only changes what is alread there, it cannot be used to create (that is: insert) new records.
Now, after having created your first record, you can change it with a
update company set age = 45 where id = 1;

batch insert /update at the same time

Suppose in database i have some 1000 record of user , hisfriend, timeStampOfChat (i.e this are column)
Now for user (suppose subrata) , we have 30 friends name and its timestamp value. [so there are still 970 records of other user and their friends.]
Now i want to update a new data set where user subrata has 50 records out of which few are new and few are old. (for new it should insert the whole and for old it should update the time stamp.)
i want to use batch update/insert.
Now thats how i want to write the logic
if data base has user subrata then batch update time stamp where user = subrata
with this i will update the existing records but how to insert the new records out of that 50 record.
You haven't tagged this question with a RDBMS, but the correct way to do this in MySQL is an INSERT INTO...ON DUPLICATE KEY UPDATE query.
As long as you define an appropriate index, when the table already contains the row you're trying to insert, it will update the time instead of inserting a copy.
INSERT INTO table (user, hisfriend, timeStampOfChat) VALUES (1, 2, 3)
ON DUPLICATE KEY UPDATE timeStampOfChat = VALUES(timeStampOfChat)