check duplication using mysql select - mysql

I have a category say ecommerce.add ebay and amazon.when i update ebay as amazon,it should n't update.How do i d it?

I suggest you check out unique indexes and primary keys. These will cause an insert or update to fail rather that allow duplicate entries to be made.

CREATE UNIQUE INDEX name_unique ON tablename (name(10));
Replace name_unique with the name you want for the index, tablename with the name of your table, and name(10) with the column name and how many characters you want to be unique (the length of the column if you want the entire value to be unique).

Related

Number of rows that are using the foreign key

I have a problem with counting the number of items/rows that are using the foreign key.
For example.
Table "A" has a foreign key of "group_id" and there is a row with "group_id" of "5" and a couple of items/posts/rows in table "B" are using the foreign key (in this case the ID of "5"). How would I know how to cont the rows that are using that "group_id"?
Here's what I mean.
Thanks very much.
UPDATE
Here is a sketch that explains my problem
PK = Primary Key
FK = Foreign key
You can query the number of rows like this:
SELECT group_id, COUNT(*) FROM table_B GROUP BY group_id;
Your image seems to show that you want table_A to keep the count persistently as an attribute column.
Some people design triggers for this. For example, in an INSERT trigger on table_B, increment the count attribute column of table_A by 1. In a DELETE trigger on table_B, decrement the count column of table_A by 1.
This sort of works, but it causes more overhead for inserts and deletes, and it causes more exclusive locking on table_A.
It's easier to do the SELECT query that I showed above.

UPDATE/INSERT based on non-unique keys?

I have a table that contains
id | user | date | data1 | data2 ......
where id is the primary unique key.
I'm trying to write a query that can UPDATE if both user and date exist while INSERT if either one of them doesn't exist
I thought about the INSERT INTO...ON DUPLICATE KEY...UPDATE method, but that requires using the unique key, which I do have but not using.
What would be a good way to deal with this issue?
Per discussion in comments, you should make (user, date) a unique key.
This will trigger the INSERT INTO ... ON DUPLICATE KEY UPDATE query as expected, updating rows with matching user and date fields, and inserting new ones where no match is found.
The only valid option is to implement this UPSERT in the programming language that you use with mysql, because MySQL needs a unique key for both INSERT ... INTO and REPLACE.
Or to add a unique index on the user and date columns which seems to be in concordance with your business logic anyway.

Remove duplicate values without ID

I have a table like this:
uuid | username | first_seen | last_seen | score
Before, the table used the primary key of a "player_id" column that ascended. I removed this player_id as I no longer needed it. I want to make the 'uuid' the primary key, but there's a lot of duplicates. I want to remove all these duplicates from the table, but keep the first one (based off the row number, the first row stays).
How can I do this? I've searched up everywhere, but they all show how to do it if you have a row ID column...
I highly advocate having auto-incremented integer primary keys. So, I would encourage you to go back. These are useful for several reasons, such as:
They tell you the insert order of rows.
They are more efficient for primary keys.
Because primary keys are clustered in MySQL, they always go at the end.
But, you don't have to follow that advice. My recommendation would be to insert the data into a new table and reload into your desired table:
create temporary table tt as
select t.*
from tt
group by tt.uuid;
truncate table t;
alter table t add constraint pk_uuid primary key (uuid);
insert into t
select * from tt;
Note: I am using a (mis)feature of MySQL that allows you to group by one column while pulling columns not in the group by. I don't like this extension, but you do not specify how to choose the particular row you want. This will give values for the other columns from matching rows. There are other ways to get one row per uuid.

Strategies for preventing the insertion of duplicate table rows

Scenario:
User A and B executes at the same time select id from Product where id = ?, if the there are no results, both create a new product with given ID.
Problem:
This could lead to the creation of duplicate rows.
Question:
What are the possibles strategies to prevent that? I know that I can use compound/unique keys, to guarantee this, but are there any other strategies? Is there any SQL statement to lock query with same parameters?
You can use unique constraints
ALTER TABLE Persons ADD UNIQUE (P_Id)
or
ALTER TABLE Persons
ADD CONSTRAINT uc_PersonID UNIQUE (P_Id,LastName)
That way it would be impossible for a duplicate to be inserted.
Put a UNIQUE key on the field in question:
https://dev.mysql.com/doc/refman/5.0/en/constraint-primary-key.html

How to reset all ID AUTO Increment to sequencial value on each INSERT Or DELETE Query Execution..!

I have a table with name as listings and inside there I have a COLUMN namely as when some rows are deleted so the AUTO Incrmementation columns namely as "ID" goes into soemthing very bad values..Like missing values in between which I don't like and don't suit like a professional way..so therefore I want please if you people can guide me to how reset all ID columns values in rows on each INSERT or DELETE Query Exeution please..!
If you really want to find the lowest unused key value, don't use AUTO_INCREMENT at all, and manage your keys manually. However, this is NOT a recommended practice.
AS explained at Auto Increment after delete in MySQL
Primary autoincrement keys in database are used to uniquely identify a
given row and shouldn't be given any business meaning. So leave the
primary key as is and add another column called for example
courseOrder. Then when you delete a record from the database you may
want to send an additional UPDATE statement in order to decrement the
courseOrder column of all rows that have courseOrder greater than the
one you are currently deleting.
As a side note you should never modify the value of a primary key in a
relational database because there could be other tables that reference
it as a foreign key and modifying it might violate referential
constraints.
Well it is not recommended but you insisted , so use this to re order
By using something like:
ALTER TABLE table_name AUTO_INCREMENT = 1;