How to make a column based auto_incrementer in mysql - mysql

Basically I want to make a column auto_increment based on another column's value. For instance I have column A and column B. Column A is an int 1-100 and column b is the auto_incrementer. If I do something with Column A = 2 column b's incrementer value needs to increase by 1. Let's say its 5 now. Now I change something with Column A = 8 and B changes from 11 to 12. My question is how can I implement something like this without creating a lot of tables? Or is creating a lot of tables the most efficient method?

You need to create a trigger, which updates column B based on the value in column A. The trigger function will handle all computations required for the value of column B

Related

Best way permute values in a unique constrained column in MySQL in a single instruction (or non-complex instruction)

I have a trouble trying to update some data in sql.
I tried to permutate values in a column with unique constraint in a single query.
For example, I have this rows:
id
name
order
300
first
10
500
second
15
The order column has the unique constraint. Now, I want to update the rows to:
id
name
order
300
first
15
500
second
10
In a single query.
I tried to use some stretegies like:
Start transaction.
Save the first order value into a variable A.
Set to null the first order value.
Save the second order value into a variable B.
Update the second order value using the variable A.
Update the first order value using the variable B.
Commit.
But If I have more than once permutation (for example, 10 permutations) the query it's more complex. For that, I want to know if there is another way to do this more generic (for N permutations) and less complex.

Truncating rows based on column value in mysql

Let's say I have have a MySQL table with many rows that I want to limit in a specific way. Each row has a column, userID, that is indexed but is not unique. Each row also has a datetime column for createDate.
I'd like for the table to never contain more than X number (say, 1000) of rows for a given userID. So, anytime a row is inserted, if that means that there are now greater than X rows where userID = Y then the DB would delete a row based on the value of createDate.
Is this possible to achieve inside the database only? What performance concerns should I have about this kind of approach?

mysql colomn value in other column with extra value

In my table in mysql, I have 4 columns, but I added one extra column. And the value of the last column must be the value of column 1 with extra value.
for example:
UPDATE users SET user = 'column_1_value'."00";
It's like merging column value with extra value.
Thanks inadvance !
since you did not provide enough context like the datatypes your table consists of I can just guess. If they are string types (like varchar) you can use concat:
UPDATE users SET column_4 = CONCAT(column_1, "00");

SELECT all COLUMN_NAMES from a table that have a specific entry

I have a table with 21 columns and I need to find all column names that have a specific value in them. This goes beyond my knowledge of database querying.
If this is possible, how would I do this?
Do you need to find columns in which the name of the column contains a certain value or there exists a row in which the given column contains the given value?
In the first case you could try the show columns: http://dev.mysql.com/doc/refman/5.0/en/show-columns.html

Update a MySQL table that has only one row with no id column

I have a MySQL table called settings. It has multiple columns, where every column is an item with a single value. So it has only one row and no id column. The design is final (I don't plan to add more columns).
How can I update the value in a single column (change one setting's value)?
What's the problem with using this --> http://dev.mysql.com/doc/refman/5.0/en/update.html
?
UPDATE table1 SET column1 = value
In case you have more than one row, you can add:
WHERE table1.column = matching_value;
making sure the match criteria is only the row you need.
to update a value of certain item column you must specify the row contains the value to be updated, since you don't have a primary key, you can depend on the nature of item values to act as row identifier and i don't recommend that.. the best way is to update your design and add column for the primary key