is it possible to go through the whole database and where value equals value to change it to other value at any key name??
like go through the tables and change the value at any key.
Database>
table1>
somekey> x
table2>
otherkey> x
now I want to change the x at any key to a y value
Related
I have a MySQL table with Columns (id, key, value). Keys are predefined strings. As an example key can be color, height, width, etc,...values can be single-valued or multi-valued.
Ex:-
height and width are single value keys
color is a multi-value key (For an object there can be multiple colors)
values for multi-value keys are stored in the database as a single string (with comma separated values)
My Problem is How can I enforce single value keys to be single-valued while inserting.
Ex:-
For the height key and width key value must be single-valued
For the color key value can be multi-valued
How Can I Enforce this condition while inserting? What will be the best way?
Edit :
Single valued attributes can be String ,Int Or Boolean.
Multi Valued attributes are Strings
To enforce this condition while inserting you can create BEFORE INSERT trigger and make your checks there. As far I understand your logic for "multi-valued" keys you can check whether they are in the database table and for "single-valued" you can check whether the value is a number.
I suppose you can add a CHECK constraint. A minimal example:
create table t(
id int,
`key` varchar(100),
`value` varchar(100),
check (case
when `key` in ('width', 'height') then `value` not like '%,%'
end = 1)
)
It is also possible to use REGEXP clause to check if numbers are in fact numbers.
DB<>Fiddle
Another improvement would be to store the values a JSON (boolean, string, number or array) then use json_type to check the type of value being saved.
I want the standard value of column B to be the value of Column A in the same row until it is changed through a user triggered UPDATE. Is that possible?
I'm using Mysql 8 and phpmyadmin, if that matters.
MySQL does not allow you to default a value using an expression with other columns.
One solution is an insert trigger on the table to set B to A. However, if A changes, this doesn't keep track -- unless you have an update trigger. But then you don't know when B is set manually or not. Oh, this is hard to keep track of.
You can do something else using a computed column -- assuming that you never want B to be set manually to NULL. This looks like:
create table t as (
. . . ,
a int not null,
_b int,
b int generated always as (coalesce(_b, a))
);
Then, in your code, you use b for querying. When you want to set the value, you would set the value of _b.
Of course, you could extend this to have a boolean flag on whether a or _b is used for b. However, using NULL to distinguish whether a value is set illustrates the idea and is simpler.
Please see the answer by John C in
MySQL: set field default value to other column
I was able to alter a table setting the default value as the concatenation of two other columns in that table:
ALTER TABLE t_table MODIFY IDOne_IDTwo varchar(12) DEFAULT ( concat(IDOne,'-',IDTwo) );
Be sure to wrap parentheses around your expressions and leave the literals bare.
MySQL documentation regarding allowing expressions as default values: Explicit Default Handling as of MySQL 8.0.13.
I am in need of a Mysql Column which will contain values in a certain range. For example, I have a column name called "apiCallCount" value of this column should within a range of 1 to 1000. Also, we needed to make this column as auto incremented(please note the table has a primary key which is auto incremented)
That is, the value of the column("apiCallCount") will be "1" when you insert the first row, it should continue to increment till 1000 as each insertion happen. Next insertion after the value become 1000 should create a row with the value "1" in "apiCallCount" column.
Honourable Dipu, try to use RANGE_MAX column assign it to the number that you wish to be the maximum (which is 1000 according to you query) then make the field to auto increment while you are creating you database.
I am wondering how an elegant solution for this issue could look like:
I have a table with approx. 100 entries and one primary key column which has auto increment enabled. The keys start at 200.
Now I would like to disable the auto increment feature and update the key column so that the keys start at "1". Of course, I could just create a second table and just select/insert these values.
But I would like to know whether it is possible to update the key values directly. As the current keys start at 200 and there are less values, it should work somehow, right?
I don't know why do you need to do like this.I think these queries will work
SET #INDEX = 0;
UPDATE `tablename1` SET ID = (#INDEX:=#INDEX+1);
ALTER TABLE `tablename1` AUTO_INCREMENT = 100;
AUTO_INCREMENT is set as 100 to update for next row. you need to set AUTO_INCREMENT with correct value in your query to generate ID that the way you want.
I created a table with two columns in phpMyAdmin and I set the first column as a primary key.
Let's say that we have stored some data in the table. For example:
Column A Column B
x1 y1
x2 y2
x3 y3
If I try to send some data like (x1, y4), will y1 be set to y4?
If not is there a setting to do it from phpMyAdmin?
Or Do I need to use UPDATE?
You should use UPDATE or INSERT ON ON DUPLICATE KEY UPDATE
MORE INFO # http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
trying to insert a row with an existing primary key will cause an error, and that's exactly why no one uses things that might be duplicated as primary keys, to change the value of the existing row, you must use UPDATE, there is no other solution, but if you want to insert a new row, my advice is to go main stream, either use an autoincremented id as primary key, or use things like GUID(windows only).