Query to update particular column field in SQL - mysql

I tried the following queries to change the column field of the table 'role' from 'Admin' to 'Administrator'. I am not informed about the values of id. my table 'role' contains two columns 'name' and 'id'.
None of the queries worked. Where am I going wrong?
update table role set name=replace('Administrator','Admin');
update table role set name='Administrator' where name='Admin';
select replace('Admin','Administrator') from role;

You don't use a table keyword so the second example is almost correct
update role set name='Administrator' where name='Admin';

The second would be the standard way:
update role -- "table" is not appropriate
set name = 'Administrator'
where name = 'Admin';
My guess is that you have no row that matches the condition -- presumably because of bad/hidden characters in name. Try this:
select *
from role
where name = 'Admin';
If this returns no rows, then try like:
where name like 'Admin%'
where name like '%Admin'
where name like '%Admin%'
Or maybe, using regexp:
where name regexp '[[:<:]]admin[[:>:]]'
(note that regexp is, by default, not case sensitive)
If one of these matches the row you want, then use it in the update.

you need to alter the table structure so the Mysql comand should be:
ALTER TABLE role CHANGE Admin Administrator [Admin column data type]

Related

How to I add a column to a table, that combines strings from other columns?

I am using Mysql Browser.
I have a table called "users". I am looking to create a column called, "opp". I know how to do this, with a simple Alter Table, Add statement. Where I am struggling is figuring out how to add data to the column. I want the column to be a combination of strings using concat().
The column should end looking like:
"Team(name)" where name is data from a column titled "name" in the table "users".
I have tried a Insert into statement like this:
Insert Into users (opp)
VALUES ('Team', '(', name, ')')
But that did not work. It tells me there is no such column as "name", even though there is...
Thanks for the help!
Once you have altered the table you should use an UPDATE
update users
set opp = concat( 'team(',name ,')' )
;
anyway column like this should not be store .. because you can obatin this with simply
select concat( 'team(',name ,')' ) opp from user
Why bother adding a column? You can use a view:
select u.*, concat('team('name , ')') as opp
from u;

Trying to use update query to only update fields that are blank in Microsoft Access

I am trying to use an update query to update fields from one table to another for fields but only if the fields in the table that i am updating into is blank. If they contain information, I do not want to overwrite the existing data
e.g
Field: Name
Table: Table 1
Update to: [Table2.][Name]
Criteria:
I am unsure of what to put in the criteria. I tried, 'Is Null', Like "".
Here is an example:
UPDATE MyTable SET MyTable.FieldB = "MyNewValue" WHERE (((MyTable.FieldB) Is Null));
Looking at the Query from within Access, you can switch to SQL view. You just need to put Is Null in the criteria column: UPDATE MyTable SET MyTable.FieldB = "MyNewValue" WHERE (((MyTable.FieldB) Is Null));
Furthermore, you can just write Is Null on alternate lines and it will count as OR.

Inserting Data into a Column within the same table

I would like to Populate the username field/column with the first letter of the survey_responders firstname concatenated with the last name of the survey responder.
Every time I run my code it gives me an error :
You can't specify target table 'survey_responders' for update in FROM clause"
Any help with what I'm doing wrong anyone?
update survey_responders
set username = ((select CONCAT(left(first_name,1), last_name)
from survey_responders)
);
try this:
UPDATE survey_responders SET username = CONCAT(LEFT(first_name,1), last_name);
You don't need to do the SELECT query that you did. In UPDATE command, in every row you can use all the columns that you have.

Creating new column using keywords

I'm looking to create a new column with values that correspond with keywords in an existing column in mysql.
I'm trying to determine if a practice is private or public based on keywords in their establishment's name.
In plain english what i'm trying to get is a new column called Practice and insert value private if keywords are specialist, gleneagles.
Would appreciate any help on this matter.
If I understand what you're asking, you want to add a column to an existing table and then set that column's value (for each row) based on another column's value in that same row?
First, to create the column named Practice you can use MySQL's ALTER TABLE command:
ALTER TABLE YourTable ADD Practice VARCHAR(20);
I'm not sure what type you are aiming for here, so I'm just going with VARCHAR(20); you may also benefit from an enum based on the values you're using (or even a foreign-key to a lookup table).
After you've created your column, you can set its value:
UPDATE YourTable
SET Practice = CASE
WHEN SomeColumn = 'specialist' THEN
'private'
WHEN SomeColumn = 'gleneagles' THEN
'private'
ELSE
'public'
END
If you don't want records that don't match to be set, simply drop the ELSE 'public' and they'll be defaulted to null.
UPDATE (to select with wildcards)
If you need to update rows with the related data being contained within larger text and you need wildcards, you can use MySQL's LIKE operator:
UPDATE YourTable
SET Practice = CASE
WHEN SomeColumn LIKE '%specialist%' THEN
'private'
WHEN SomeColumn LIKE '%gleneagles%' THEN
'private'
ELSE
'public'
END
Alternatively, if you're only going to update to a single-value based on text containing multiple values, you can use a Regular Expression (via REXEXP) instead:
UPDATE YourTable
SET Practice = CASE
WHEN SomeColumn REGEXP 'specialist|gleneagles' THEN
'private'
ELSE
'public'
END
You are asking about denormalization, which means storing derived data. My initial reaction would be to not add another column, unless there were significant performance problems, instead I would create a view that gas the calculated value.
However, if you need the column, I would create a boolean column:
alter table mytable
add is_private boolean;
Then populate it like this:
update mytable
set is_private = (name like '%specialist%' or
name like '%gleneagles%')

Changing from an enum to a linked table

We are going to change something that we have an enum to an id of a linked table instead.
How would we go about that?
Our current enum name: strat
Our new linked name: stratid
What I was thinking was something along the lines of:
UPDATE table_name
SET stratid = (SELECT id FROM link_table WHERE stratname = table_name.strat);
I have not created the link table yet, right now it is all theory.
Will the above work?
Is there anything I should change in order to transfer from an enum to the linked table?
No gotchas for making the update other than that you need want to make triple sure that your link_table.ids are populated strictly in the order that table_name.strat options are defined.
For example, if strat is enum('FOO', 'BAR') then in linked_table the record with id == 1 should be the "FOO" record.
After that, you 'd perhaps want to make the stratid column NON NULL; this is not strictly equivalent to your previous arrangement, but it would probably be closer to what you want.
Yes, create the link table first,
set the stratname as unique,
use an auto increment ID
A lazy solution for insert link_table:
insert into link_table
select distinct strat from table_name order by strat;
However, I not sure is all the predefined enum is being used.
Also, without knowing size of the enum,
I can't suggest you do a manual insert.
If you look at enum ...
enum('...', '...', ...) <-- is just a comma separated value
So, here is the query to get the CSV :-
select column_type from information_schema.columns
where schema_name="table_name" and column_name = "strat";
You can combine with an programming language to do the link_table insertion.
Lastly, you UPDATE query is not very optimize, you can switch to use INNER JOIN.
But I assume is one-time job, so be it!