Table name prefix for columns in UPDATE query - mysql

Is table name prefix for fields to be update not allowed in SQL? Like:
UPDATE tablename
SET tablename.mycolums = true
WHERE ...
What is the SQL standard?

The above query perfectly works in SQL SERVER environment.
The SQL standard for UPDATE statement has following form:
UPDATE table_name SET column_name = value [, column_name = value ...] [WHERE condition]
But Some databases uses non standard form by using FROM in UPDATE statement:
UPDATE alias_name
alias_name.mycolums=true
FROM tablename alias_name

It is not valid to prefix the column in the SET section with table alias in PostgreSQL, mentioned in the documentation:
column
The name of a column in table. The column name can be qualified with a subfield name or array subscript, if needed. Do not include the table's name in the specification of a target column — for example, UPDATE tab SET tab.col = 1 is invalid.
It is also mentioned that this behavior conforms to the SQL standard.

Yes, ofcourse it is allowed in Sql-Server. If you write a query like below, its working fine.
UPDATE KKDb SET KKDb.StdName = 'Sai' WHERE (KKDb.StdNo = 1)

Related

Adding prefix in an update statement

I have the following MySQL code:
UPDATE opened_pw SET opened_date_week = CONCAT('WK', WEEK(opened_date))
What I intend to do here is change the opened_date_week columns with a prefix of 'WK' followed by the week conversion of the opened_date column.
EDIT:
How do I add a 'WK ' standard prefix to all the conversions so that whatever is stored in opened_date_week will be like this WK 13 WK 14?
If I execute: UPDATE opened_pw SET opened_date_week = WEEK(opened_date) It makes the changes but that statement doesn't include the prefix of 'WK '
You need to change the datatype of opened_date_week column to VARCHAR.
ALTER TABLE opened_pw MODIFY opened_date_week VARCHAR(10);
After altering the datatype you can now execute your update query and check the result.
UPDATE opened_pw SET opened_date_week = CONCAT('WK ', WEEK(opened_date));

UPDATE statement format analogous to INSERT statement

I know that the UPDATE statement has the following format:
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
I was wondering whether there exists a format analogous to INSERT statement, something like this:
UPDATE table_name
SET (column1,column2,column3,...)
VALUES (value1,value2,value3,...)
WHERE some_column=some_value;
Is this a valid query?
I'm using MySQL.
The short answer - no.
All the variants of the update syntax have a set clause of the form colulmn1=..., column2=... etc. For a complete list of the supported syntax variants, you can check out the documentation.

MySQL query where column may be missing

I have a Microsoft stored procedure that queries two MySQL databases using OpenQuery. The two MySQL databases should be have the same schemas, so I can run the same query on both.
However, we will soon alter the MySQL schemas, and add a column to a table. But the two MySQL databases won't happen at the same time, and I don't know the exact date of the releases.
I therefore want to write the query so that if the new column exists, then I use it in my select. If not, then I use a default value.
Is this possible? (That is have a query that handles differences in the table schema?)
(Not to be confused with 'coelesce' where the field definitely exists, but is simply null.)
You can use the following SELECT statement:
SELECT *
FROM information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'database name' AND TABLE_NAME = 'your table name'
AND COLUMN_NAME = 'the column name you want to check for'
If the above returns a value, your column is there. If not, then run your alternative SELECT statement
Updated statement:
IF EXISTS (SELECT *
FROM OPENQUERY(servername, 'SELECT *
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = ''database name''
AND TABLE_NAME = ''your table name''
AND COLUMN_NAME = ''the column name you want to check for'' ))

Replace space with underscore in table

How can I write a SQL query to replace all occurrences of space in a table with underscore and set all characters to lowercase?
To update a single column in a single table, you can use a combination of LOWER() and REPLACE():
UPDATE table_name SET column_name=LOWER(REPLACE(column_name, ' ', '_'))
To "duplicate" the existing column, and perform the updates on the duplicate (per your question in a comment), you can use MySQL's ALTER command before the UPDATE query:
ALTER TABLE table_name ADD duplicate_column_name VARCHAR(255) AFTER column_name;
UPDATE table_name SET duplicate_column_name = LOWER(REPLACE(column_name, ' ', '_'));
Just be sure to update the data-type in the ALTER command to reflect your actual data-type.
When using the UPDATE statement in SQL, always remember to include a WHERE clause -- so says MYSQL Workbench! :D
My Answer though:
REPLACE(string1, find_in_string, replacementValue);

MySQL Update statement error

I have two scripts, one for the Insert, and another for Update.
My update button script is using the latest inserted Id, and goes on something like this:
Update tblsurvey
set WouldLikeToBeSeenOnSite = 'sadffas'
and DislikedOnSite = 'asdfsadfsadf'
and OtherNewsWebsitesRead = 'asdfsadfa'
and LikedOnOtherNewsSites = 'asdfsadfas'
and IPAddress = '172.16.0.123'
and DateAnswered = current_date()
where SurveyResponseId in (select max(SurveyResponseId) from tblsurvey);
Apparently, the "where" clause generates an error:
1093 - you cant specify target table 'tblsurvey' for update in FROM clause.
Is there any other way in which i could use the latest inserted ID of the same table i am updating?
Thanks.
wait a second. why are you using AND to delimit SET claus elements? it must be comma separated.
you cannot use the same table (in this case, table tblsurvey) for both the subquery FROM clause and the update target.
Its illegal to use same table for updating/deleting and subquery for UPDATE and DELETE operations.