Update a specific column with a specific value - sql

I have a table with 3 columns: username, password and ID.Every password and username have a specific ID. Table name is "Account" for example.
I want to update a password with a specific ID. I have tried:
UPDATE Account SET password = "newPassword"
where id = 1
However, it does not work. It will complain that "newPassword" is not a valid column name. I try my queries in SQL management studio.

Use single quotes for string literals in MS SQL Server:
UPDATE Account
SET password = 'newPassword'
WHERE id = 1;
Double quotes in SQL are generally reserved for database objects, such as table and column names (sometimes called identifiers). Your current update is attempting to assign the password column to a column named newPassword. This column doesn't exist, hence you are getting an error.

Related

How to get some value from the sql database based on certain criteria

Suppose I have given such a table in sql
According to Gmail (which I have given in advance) I want to get the Username value.
For example, if "Gmail" would be "Gela1#gmail.com" I should get string - "Gela gela".
if "Gmail" would be "mamuka#gmail.com" I should get string - "Mamuka snaia".
How to do this?
You can easily make an SQL query like:
SELECT Username from tableName where Gmail = 'Gela1#gmail.com'
Replace tableName with your table name.
https://www.w3schools.com/sql/sql_select.asp

Is that possible that access to a sql table with question mark?

I have 3 sql tables customer, employee and manager. I want to access dynamically to my tables. I had a statement like this,
"update customer set AMOUNT where ID= ?"
But in this situation i can only access to customer. I need to access all of the tables for different operations. Is that possible to write this,
"update ? set AMOUNT where ID=?"
or what can i do to access for example employee for a different class.
The parameters can be used only in the place where you could otherwise use a literal value, like a quoted string or a numeric value.
Parameters cannot be used for identifiers like table names. Nor expressions. Nor SQL keywords.
All those other parts of the query must be fixed in the SQL query string before you prepare the query.
To query other tables, you just have concatenate the table name into the string.
String query = "update " + tableName + " set amount where ID=?";
It's up to you to make sure your variable tableName in fact only contains one of your table names. A good way to do this is to compare it to a list of known table names, and if it isn't in the list, throw an exception.

Why do you only need double quotation marks in SQL for particular cases?

I have a column in my database table named UID. For some reason queries fail unless I surround the column name with double quotation marks (" "). None of the other columns require these quotation marks.
For example, this doesn't work:
SELECT user_name FROM user_table WHERE UID = '...'
But this does:
SELECT user_name FROM user_table WHERE "UID" = '...'
Is UID some kind of keyword? Why is it only happening to that column? How do I know if I need to use double quotes for other columns?
By the way, I'm running JDK 1.8_221 and using an oracle JDBC driver if that makes a difference.
Yes, it is about keywords. You can double quote everything (tables, columns) to avoid this but I can understand you don't want to do this.
To have a list of standard keywords: SQL Keywords
But you can see UID is not in this list as I assume it is a reserved keyword by your database implementation. I had the same problem with a table called "order" as it contains orders. ORDER is a keyword so I had to quote it each time.
So best is to test your statements using a SQL client tool.
Since you mention Oracle: Oracle keywords: "You can obtain a list of keywords by querying the V$RESERVED_WORDS data dictionary view."
If your create table command for user_table looks something like this:
create table user_table ("UID" varchar2(10))
then you will have to use quotes around UID in your query. This query:
select * from user_table where UID = 'somestring'
means to use the Oracle predefined UID pseudo column and your table's UID column will not be accessed.
If your table doesn't have a user-defined UID column, then using "UID" should fail.
My guess is your table does indeed have a UID column and when you say it "doesn't work" without using the quotes you probably mean it motivates an ORA-1722.
The type of failure, when using UID without quotes, depends on the content of the string 'somestring'. If the content of that string can be cast as a number then you probably won't get the rows you expect. If it cannot be cast as a number then you'll get an ORA-1722.
As an aside, if you try to execute this, then you'll get an ORA-904:
create table user_table (UID number)
Yes, it is keywords and return
UID returns an integer that uniquely identifies the session user (the
user who logged on).
By default, Oracle identifiers (table names, column names, etc.) are case-insensitive. You can make them case-sensitive by using quotes around them when creating them (eg: SELECT * FROM "My_Table" WHERE "my_field" = 1). SQL keywords (SELECT, WHERE, JOIN, etc.) are always case-insensitive.
You can use it for more information here.

SQL: Regex for replace query

I have SQL table as UserInfo with columns [id, username, details]. I need to update a part of a string from the column details.
For example. details column will look like,
{"username":"user.sample#nomail.org","rollno":45,"gender":"male"}
P.S: In actual DB this column is really long with more details, for example, I quoted above few details.
Condition is that the username value in the details column should be the same as the value in the username column.
I have n number of records mismatching in the DB, meaning username column value is different from username information in the details column.
Ex
id - username - details
23 - new-user#nomail.org - {"username":"user.sample#nomail.org","rollno":45,"gender":"male"}
Hence I am trying to replace the "username": "value" in details column with the username column value
EX - Expected should be like,
id - username - details
23 - new-user#nomail.org - {"username":"new-user#nomail.org","rollno":45,"gender":"male"}
For updating UserInfo, I am trying like
update UserInfo
set details = replace(details, \Regex\ ,"username:"+ username)
where id not in (select id from UserInfo where details like '%'+userid+'%' )
I need to regex pattern matching, ["username":"mail-id"], so that I can able to replace that particular string in that details column.
Any idea/suggestion for the regex pattern would be helpful

SQL Server: If, Else Select Statements to populate a grid

I'm trying to populate a data-grid with information from my table. One of the fields in this table is Active basically used as a flag set to either True or False.
I have a query on my website that checks if the currently logged-in user's is an administrator or not.
Using that in a TOKEN I can input [LSX:IsAdmin] that will return either true or false.
All data in my table that is NOT flagged Active=True i'd like to display only to Administrators which is what my TOKEN is for.
I'm having some difficulties with my query below to achieve that.
IF('LSX:IsAdmin'='True')
SELECT * FROM SaleEvents
ELSE
SELECT * FROM SaleEvents WHERE Active = True
Error Message Invalid object name 'SaleEvents'.
First picture shows how to find out schema name (CIS, dbo, TestCars, TestDoc, TestMoney). 'dbo' schema usually exists and default.
However, you can change default schema for user (second picture).
You do not specify schema name in your query, that's why default schema is used.
To specify a schema name you should add it before table name
SELECT * FROM <your schema>.SaleEvents WHERE Active = 1