I believe this line of code will help me create a unique id based on three columns, while ignoring any time the combination of these three columns is duplicated, yet ignore seems depreciated with mysql and I can not find another way to do this?
ALTER IGNORE TABLE `x` ADD UNIQUE INDEX `id`('student', 'class', 'session');
Related
I have one table and want to remove duplicate in laravel using DB:: not eloquent
my tables name is sale_details and fields are
id,orderId,shop,user,item,quantity,price,total
where orderId,shop,user,item are unique
I want to delete rows where orderId,shop,user,item are duplicate based on these 4 fields, not one.
how can I do it the best way?
The best option would be to add an index on your table :
ALTER IGNORE TABLE sale_details ADD UNIQUE (orderId,shop,user,item);
With IGNORE, only the first found row is kept, and the others are removed.
You should keep this UNIQUE if you don't want future duplicates, if not, you can drop it just after.
I have A table with almost 20 fields which several of those are Foreign Key that already has been indexed by Mysql, now I want to create a multi-indexes index that it contains 3 FK field,
First tried was based on Fields
ALTER TABLE `Add`
Add INDEX `IX_Add_ON_IDCat_IDStatus_IDModeration_DateTo_DateAdded`
(`IDCategory`,`IDStatus`,`IDModeration`,`DateTo`,`DateAdded`);
But I think it's better to have an index on indexes instead of fields but my following effort faced with error: Error Code: 1072. Key column 'FK_Add_Category' doesn't exist in table
ALTER TABLE `Add`
Add INDEX `IX_Add_ON_IDCat_IDStatus_IDModeration_DateTo_DateAdded`
(`FK_Add_Category`,`FK_Add_AddStatus`,`FK_Add_AddModeration`,
`IX_Add_DateTo`,`IX_Add_DateAdded`);
My question is is it possible to add an index on exists Indexes ( FK index in my case ) or not and there is the only way to create an index on Columns? if yes How I create that?
An index is an ordered list of values. It is used to make it more efficient to find rows in the table.
Think about the common, real-life, example of INDEX(last_name, first_name). It makes it easy to look up someone if you have their last name and first name. And sort of easy if you have only their last name.
But it is useless if all you have is their first name.
FOREIGN KEYs necessitate a lookup. Apparently you have a FK to AddStatus, since I see FK_Add_AddStatus. That FK generated a lookup for AddStatus. Think of that as being like a separate index on first_name. It is totally separate from the index on last_name & first_name.
5 columns is usually too many to put into a single index.
MySQL uses only one index for a given SELECT.
So, now, I ask, what SELECT might use that 5-column index? Please show us it. We can discuss whether it is useful, and whether the columns are in the optimal order.
Im looking to use INSERT IGNORE so that I avoid entering duplicate fields into my database but i want to check this across 2 columns not 1.
So only if columns DateandTime AND Horse don't exists together in the same row
You shoud work with indices.
If you want to check that the values for DateandTime and Horse are both not set, give each a unique index.
If you want to check it the combinaiton of both doesn't exist jet, give them a combined index that is set to unique.
I recommend to use phpmyadmin. You can set indices in the Structure tab of a table.
There is no php needed to achive this.
Tt you Comment about a combined index:
ALTER TABLE Race_Records ADD UNIQUE INDEX unique_index ( `Horse`, `DateandTime` ) ;
the unique_index is the name of the index. You can choose that yourself, it you warn.
All Arguments in the () are used as the cols, that are added to that index.
And you need to set the UNIQUE to make it an unique index. Otherwise it will only be a normal index that only helps with searching.
I want to know whether it is possible to avoid duplicate entries or data without any keys or group by statement
Create Unique key constrait.
ALTER TABLE Comment ADD CONSTRAINT uc_Comment UNIQUE (CommentId, Comment)
In above case Comment duplication will not be done as we are creating the unique combination of COmmentId and Comment.
Hope this helps.
More info : http://www.w3schools.com/sql/sql_unique.asp OR
SQL Server 2005 How Create a Unique Constraint?
If you want to suppress duplicates when querying, use SELECT DISTINCT.
If you want to avoid putting duplicates into a table, just don't insert records that are already there. It doesn't matter whether you have a primary/unique key: those will make the database not allow duplicate records, but it's still up to you to avoid trying to insert duplicates (assuming you want your queries to succeed).
You can use SELECT to find whether a record already exists before trying to insert it. Or, if you want to be fancy, you can insert the new records into a temporary table, use DELETE to remove any that are already present in the real table, then use INSERT ... SELECT to copy the remaining records from the temporary table into the real one.
Appears that MySQL's UNIQUE constraint is on a columns by column basis, I'm looking for a way to make sure the row are UNIQUE on a row by row basis; guessing the answer is to create a hash from by concatenating the columns per row I want to be UNIQUE then but a UNIQUE on the column storing the hash for that row. Also, the rows themselves unless I create a control will always be UNIQUE, since the ID for the row is a SURROGATE_KEY; meaning it's an integer sequential growing by +1 of the ID of the last row's integer.
You can create a multiple-column UNIQUE index.
The main ways to ensure a row is completely unique is to use the UNIQUE constraint on every column or create an analagous UNIQUE index on every column. If you can allow two columns to accept the same input but you need to uniquely distinguish them, there's no reason why you would need anything more than a primary key (basically an ID column). The other thing you could do is switch to a column-store database such as InfiniDB, which has a MySQL front-end, or MonetDB, both of which are open source alternatives.