select unique row identifier in mysql for updating - mysql

in infomix i have seen queries like this
select rowid from table where condition
and for update they use the same row id
update table set field="val" where rowid=rowid
is there anything similar in mysql database
does uuid function does the same in mysql .
actually my problem is, there is no primary key in the table so when porting infomix query to mysql i need to consider all the fields in where condition .
please help if there is an alternate solution ,

why not...
select rowid from table where condition
|
V
update table set field="val" where condition

If you are porting your queries from informix to mysql, why don't you modify your tables to include a primary key?
you could even name the PK rowid to maintain portability between the informix and mysql queries.
ALTER TABLE table
ADD rowid MEDIUMINT NOT NULL AUTO_INCREMENT KEY

you can use same condition for both query which you pass on first query, it returns you unique row..
try this
select rowid from table where condition
and for update use the same condition as above instead of row id
update table set field="val" where rowid=rowid

Related

using ifnull with index creation in mysql

I am trying to create an index in MySQL whereas the query will first check what column is not null. After checking, it will create the index on the column that is not null. However, I am not successful in creating this and it says I have an error, can someone help me? please see my code below
create index IDX_KSE_NO_01 on tb_kse(ifnull(ss_no, id_no);
#lad2025 is correct that MySQL does not support function-based indexes (like PostgreSQL does), but MySQL 5.7 introduced a feature for virtual generated columns based on expressions, and then you can create an index on a virtual column.
ALTER TABLE tb_kse ADD COLUMN either_no VARCHAR(10) AS (IFNULL(ss_no, id_no));
CREATE INDEX IDX_KSE_NO_01 ON tb_kse(either_no);
MySQL does not support function-based index. You should create normal index:
create index IDX_KSE_NO_01 on tb_kse(ss_no);
create index IDX_KSE_NO_02 on tb_kse(id_no);
And rewrite your query (OR-Expansion):
SELECT *
FROM tb_kse WHERE ss_no = ?
UNION
SELECT *
FROM tb_kse
WHERE ss_no IS NULL AND id_no = ?;
DBFiddle Demo
Another way is to create generated column and create index on top of it:
CREATE TABLE tb_kse(id_no INT, ss_no INT,
gen_col INT GENERATED ALWAYS AS (ifnull(ss_no, id_no)) STORED);
create index IDX_KSE_NO_01 on tb_kse(gen_col);
SELECT *
FROM tb_kse
WHERE gen_col = ?;
DBFiddle Demo 2

How can I construct another primary key in a table of MySQL?

Here's the table columns:
id | A | detail
id itself is primary key auto increment. A is the field that I want it to be unique.
I don't want to do select id from table where A = "XXX" every time to check whether there is a same A in the table already.
What I want:
When there is a same A in the table, don't insert, just return the id for me.
When there is not a same A in the table, insert it, after which return the id for me.
I'm using mybatis, and the amount of records is very large, 10 million or so, so I need the solution to be effective enough.
Could anyone give me an idea how to do that? Thanks a lot.
The only way to guarantee that column A is unique is to declare it unique. Only the dbms can guarantee uniqueness.
When there is a same A in the table, don't insert, just return the id for me.
When there is not a same A in the table, insert it, after which return the id for me.
MySQL supports an INSERT...ON DUPLICATE KEY UPDATE statement. It's documented behavior is
If a table contains an AUTO_INCREMENT column and INSERT ... ON DUPLICATE KEY UPDATE inserts or updates a row, the LAST_INSERT_ID() function returns the AUTO_INCREMENT value.
Use with caution--the linked documentation cites bug reports.
I'm sure mybatis provides an easy way to get the value of last_insert_id().
ALTER TABLE tablename ADD UNIQUE (A);
That should make A unique.

#1062 - Duplicate entry '' for key 'unique_id' When Trying to add UNIQUE KEY (MySQL)

I've got an error on MySQL while trying to add a UNIQUE KEY. Here's what I'm trying to do. I've got a column called 'unique_id' which is VARCHAR(100). There are no indexes defined on the table. I'm getting this error:
#1062 - Duplicate entry '' for key 'unique_id'
When I try to add a UNIQUE key. Here is a screenshot of how I'm setting it up in phpMyAdmin:
Here is the MySQL query that's generate by phpMyAdmin:
ALTER TABLE `wind_archive` ADD `unique_id` VARCHAR( 100 ) NOT NULL FIRST ,
ADD UNIQUE (
`unique_id`
)
I've had this problem in the past and never resolved it so I just rebuilt the table from scratch. Unfortunately in this case I cannot do that as there are many entries in the table already. Thanks for your help!
The error says it all:
Duplicate entry ''
So run the following query:
SELECT unique_id,COUNT(unique_id)
FROM yourtblname
GROUP BY unique_id
HAVING COUNT(unique_id) >1
This query will also show you the problem
SELECT *
FROM yourtblname
WHERE unique_id=''
This will show you where there are values that have duplicates. You are trying to create a unique index on a field with duplicates. You will need to resolve the duplicate data first then add the index.
This is 3rd time i am looking for solution to this problem so for the reference I am posting the answer here.
Depending on the data we may use IGNORE keyword with Alter command. If IGNORE is specified, only the first row is used of rows with duplicates on a unique key, The other conflicting rows are deleted. Incorrect values are truncated to the closest matching acceptable value.
The IGNORE keyword extension to MySQL seems to have a bug in the InnoDB version on some version of MySQL.
You could always, convert to MyISAM, IGNORE-ADD the index and then convert back to InnoDB
ALTER TABLE table ENGINE MyISAM;
ALTER IGNORE TABLE table ADD UNIQUE INDEX dupidx (field);
ALTER TABLE table ENGINE InnoDB;
Note, if you have Foreign Key constraints this will not work, you will have to remove those first, and add them back later.
Make unique_id NULL from NOT NULL and it will solve your problem
select ID from wind_archive
where ID not in (select max(ID) from wind_archive group by unique_id)
and this is what you should remove from the table before you succesfully add the unique key.
this also works for adding unique key with 2 or more columns.
such as -
delete from wind_archive
where ID in (
select * from (select ID from wind_archive where ID not in (
select max(ID) from wind_archive group by lastName, firstName
) ORDER BY ID
) AS p
);
because of you write in your query, unique_id be NOT NULL and previous rows all of them are null and you want this column be unique, then after run this query, you have several rows with the same value it means this column is not unique, then you have to change unique_id NOT NULL to unique_id NULL in your query.
I was getting the same error (Duplicate entry '' for key 'unique_id') when trying to add a new column as unique "after" I had already created a table containing just names of museums. I wanted to go back and add a unique code for each museum name, with the intention of inserting the code values one at a time. Poor table planning on my part.
My solution was to add the new column without making it unique; then entered the data for each code one row at a time; and then changing the column structure to make it unique for future entries. Lucky there were only 10 rows.

Remove duplicate rows from table with 1 column only?

I believe my question was asked on SO, but I didn't find the answer.
There is a mysql table mytable with one column mycolumn.
What is the mysql query to remove duplicates from a table?
Only one column without pk or another column that you can use for see if they are different?
if yes, this is a bad practice. Consider inserting a new column (number) and insert id for every record, than you can try this query:
delete from table
where counter > 1 and inner_query.mycolumn = table.mycolumn and inner_query.col_id = table.col_id from
(select mycolumn, col_id, count (mycolumn) counter
from table group by mycolumn
) inner_query
than, you can add a primary key
Here is one way to go about it as long as there are no triggers or foreign keys. Not tested because I'm on my phone, but should work. After this, maybe create a unique index on mycolumn to keep from getting duplicates.
Create table _mytable
Select distinct mycolumn from mytable;
delete from mytable;
Insert into mytable(mycolumn)
Select mycolumn from _mytable;
Drop table _mytable;

mysql innodb indexing confused

I am building a mysql table: ID (auto int), cc char(9), tt int(11), mm char(3)
Now I have set the ID to be the primary index.
Every query will be either select or update with WHERE id='numberhere' LIMIT 1. (so its just 1 row at a timw ever needed)
Now, To get the correct performance benifit from using innodb, do I just leave ID as primary and only index in the table? or should I set everything as an index? I am unsure...
EDIT: no joins in the table, it is literally SELECT * FROM table WHERE id='2341...' everytime
or same but with update...
You might set everything you join on as an index. Maybe even everything you use in a where. Too many indexes slow insertions/updates, as you have to create them, so it's down to use.
As Nanne responded, if you are always dealing with a specific key, having that column as its own index is perfectly fine and always have that table as the first table in your SQL-Select statements and the first condition in your WHERE clause as you sampled... If you ARE ever doing a JOIN to another table, make sure the OTHER table has an index on the column you would be matching to for optimizing THAT join portion...