I'm trying to create a UNIQUE INDEX constraint for two columns, but only when another column contains the value 1. For example, column_1 and column_2 should be UNIQUE only when active = 1. Any rows that contain active = 0 can share values for column_1 and column_2 with another row, regardless of what the other row's value for active is. But rows where active = 1 cannot share values of column_1 or column_2 with another row that has active = 1.
What I mean by "share" is two rows having the same value(s) in the same column(s). Example: row1.a = row2.a AND row1.b = row2.b. Values would be shared only if both columns in row1 matched the other two columns in row2.
I hope I made myself clear. :\
You can try to make multi-column UNIQUE index with column_1, column_2 and active, and then set active=NULL for the rows where uniqueness not required. Alternatively, you can use triggers (see MySQL trigger syntax)
and check for each inserted/updated row if such values are already in the table - but I think it would be rather slow.
I'm trying to create a UNIQUE INDEX constraint for two columns, but only when another column contains the value 1
You can set the value of "another column" to a unique value that does not equal to 1. for example the id of a record.
Then the unique index constraint could be applied to all three columns including the "another column". Let's call the "another column" columnX.
Set the value of columnX to 1 if you want to apply the unique constraint to a record. Set the value of columnX to a unique value if you don't want to apply the unique constraint.
Then no extra work/triggers needed. The unique index to all three columns could solve your problem.
I am not sure about MySQL syntax, but it should have pretty much the same thing that SQL Server has:
CREATE UNIQUE INDEX [UNQ_Column1Column2OnActive_MyTable]
ON dbo.[MyTable]([column1,column2)
WHERE ([active] = 1);
This index will make sure if active=1 then column1 and column2 combination is unique across the table.
In SQL Server this could be accomplished with check constraints, however I do not know if MySQL supports anything similar.
What will work on any database, is that you can split the table in two. If the records where active =0 are just history records, and will never become active again, you could just move them to another table, and set a simple unique constraint on the original table.
I am not sure I understand you 100% but lets say you have a table that has a status column and you want to make sure there is only one raw with a status of 'A' (Active). You are OK with many rows with statuses of 'I' or 'Z' or anything else. Only one row is allowed with status of 'A'.
This will do the trick.
CREATE UNIQUE INDEX [Idx_EvalHeaderOnlyOneActive]
ON [dbo].[EvalHeader]([Hdr Status])
WHERE [Hdr Status] = 'A';
indexes are agnostic of external influences. This kind of constraint would have to be implemented outside your database.
Related
Is it possible to write a query like the one below?
UPDATE sale SET sale_order='123456789' WHERE **COLUMN_1** = 2
where I don't explicitly pass the column name? Only its position?
I could get the column names but I am trying to avoid querying the database only to get them.
Thanks.
To answer your question, no, there is no syntax in SQL to reference the column by its position. This goes back to relational theory, in the sense that a table is a set of columns, and members of a set are unordered.
You will either have to know the column name, or else query it from the database:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA=SCHEMA() AND TABLE_NAME='sale'
AND ORDINAL_POSITION=1;
It looks like you are trying to design a query that updates a row by primary key, by assuming the first column is the primary key. The primary key isn't necessarily the first column. It isn't necessarily an integer. It isn't necessarily a single column.
So you are already making assumptions about the table definition. You might as well assume the primary key column is named id or some other convention.
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 do the following:
INSERT INTO table1 (term1, term2, number) values ('A','B',10);
however if values A and B are already present in table1 regardless of their order, ie. the predicate
(term1='A' && term2='B') OR (`term1='B' && term2='A')
holds true, then I just want to update column number. Is there any way of doing that?
A (perhaps the) clean way to handle this situation is to use INSERT ... ON DUPLICATE KEY UPDATE, read the documentation.
If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that
would cause a duplicate value in a UNIQUE index or PRIMARY KEY, MySQL
performs an UPDATE of the old row
The important part is would cause a duplicate value in a UNIQUE index. Therefore you need to create an multicolumn unique index.
Now I'm not sure if you can manage the order that way, therefore I'd create an extra field with the concatenation of the sorted values of your fields, and have that field uniquely indexed.
EDIT: Instead of storing the concatenation your fields, you could also just store the hash and index it.
Thanks #okiharaherbst,
This is what I did: I added new column "uniqueKey" as primary key and insert goes as follows:
INSERT INTO table1(term1,term2,num,uniquekey) VALUES ( "a","b",10,
concat(greatest(term1,term2),least(term1,term2))) on duplicate key update num=10;
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
A MySql 5.3 table with 100K rows has a primary key.
There is also an integer column which is not part of the key. I would like to update this column to contain a unique number for the table. E.g. for the first record it should contain 1, for the second 2 etc.
This could as well be an auto-increment column, but MySql does not allow auto-increment on non-key columns. I don't want this column to be part of the key, because of the way it gets populated from a file etc.
So how such a query would look like?
I don't know why do you want to do something like this, but a possible solution is this:
set #rownum:=0;
update <table> set column = #rownum:=rownum+1 order by <field>