I have a field that stores product codes. The codes are unique, but some products simply doesn't have a code. I can't invent codes because those are providers codes.
Is this kind of constraint possible in MySQL?
I'm a noob with stored procedures and triggers, so if the solution involves one of these, please be patient.
Update:
The column is NOT Null. That's why I was unable to do this.
Yes, you can do this. See the MySQL reference (version 5.5).
A UNIQUE index creates a constraint such that all values in the index must be distinct. An error occurs if you try to add a new row with a key value that matches an existing row. For all engines, a UNIQUE index allows multiple NULL values for columns that can contain NULL.
Yes, if you make the product code column nullable (not declared with NOT NULL), the unique key will allow multiple rows with NULL product codes.
MySQL still allows for multiple rows to have a value of NULL in a unique column.
Related
I have a field that stores product codes. The codes are unique, but some products simply doesn't have a code. I can't invent codes because those are providers codes.
Is this kind of constraint possible in MySQL?
I'm a noob with stored procedures and triggers, so if the solution involves one of these, please be patient.
Update:
The column is NOT Null. That's why I was unable to do this.
Yes, you can do this. See the MySQL reference (version 5.5).
A UNIQUE index creates a constraint such that all values in the index must be distinct. An error occurs if you try to add a new row with a key value that matches an existing row. For all engines, a UNIQUE index allows multiple NULL values for columns that can contain NULL.
Yes, if you make the product code column nullable (not declared with NOT NULL), the unique key will allow multiple rows with NULL product codes.
MySQL still allows for multiple rows to have a value of NULL in a unique column.
See we have one table with unique key on one field,
now whenever we are inserting any record,how mysql checking that that field value is exist in table or not?
whats the logic here?
its internally fires queries to check that that value is exist in table ? I think thats not possible.
here unique filed can be any id,email,username anything
MySQL creates index for primary and unique keys. So it checks index to see that new inserting value is unique actually. Since it is checking index, it does not hurt performance.
A UNIQUE index creates a constraint such that all values in the index
must be distinct. An error occurs if you try to add a new row with a
key value that matches an existing row. For all engines, a UNIQUE
index permits multiple NULL values for columns that can contain NULL.
Well it has to find the record in the index anyway when you inserting a value in order to write the data and to update all the indexes with your new value. Not much trouble to read that location and compare it to the new values.
so yes it just runs your query.
I have mysql database table for addresses, it contains separate columns for post code / street /town.
How can I make sure a full table entry is unique rather than just one of the columns alone?
Make a composite key on {post code, street, town}.
A key can be primary (PRIMARY KEY) or alternate (UNIQUE constraint).
BTW, can two different towns have the same post code? If not, you don't need the town in the key. In fact, consider normalizing your model by "extracting" the town to a separate table.
mysql allows you to have a unique key for n number of fields or columns. When you declare this column as unique , mysql checks for the constraint for duplication.
For more info read this mysql unique key
"The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a column or set of columns."
But, i suggest the front end validation for making the user comfortable (just incase u had not thought of it ;) )
A database table may have multiple unique keys, so it will reject any INSERT queries that violate the constraints you set in the table.
Simply set the other columns to be unique and you should find that your problem is solved.
Lets say I have only 2 tables in my DB. The two tables are: InputType and HardwareType. Each table uses GUIDs as their primary key. There is a foreign key from InputType to HardwareType.
The problem I am having is that there will be multiple rows of InputType that refer to the same row in HardwareType (ie. there are several inputs per type of hardware). The issue with this is that the column I have in InputType that refers to the GUID in HardwareType will not allow duplicates -- therefore, not allowing multiple inputs per hardware type.
I could probably get around this by setting the datatype of the column in InputType to a "varchar" or something instead of "uniqueidentifier", but do I have to do it this way? Is there someway to allow duplicate entries of GUIDs when it isn't the primary key, but instead a foreign key to another table?
Help is appreciated! Thanks.
Just because the datatype is UNIQUEIDENTIFIER doesn't imply you cannot have the same value in that column multiple times!
You CAN in fact - unless of course, you've explicitly added a UNIQUE CONSTRAINT or UNIQUE INDEX on that column - this is your choice, but there's nothing applied by default, unless you do something about it yourself.
So you should be able to reference HardwareType from InputType using the UNIQUEIDENTIFIER - even if multiple rows in InputType will reference the same row in HardwareType - no problems at all.
In postgres we have a constraint defined that essentially allows us to limit the number of entries in a table with a certain value to one. We created this constraint:
create unique index list$default$uk on list_group(visitor_uid) where list_type = 'default';
Which means that the unique constraint is only applied when the list_type='default' so that there can only be one 'default' list per visitor in the table.
It looks like MySql doesn't support the additional where on the unique constraint. Is there another way to support this in the database schema for MySQL?
MYSQL doesn't support such types of constraints.
You should use stored procudures for inserting data instead, so you can do some checking and validation.
Why don't you define your default as such that it must have 1 as primary key? This way a normal unique constraint on the pk would be enough already.
If nothing fits to you, you could also consider changing your data model.
Actually it exists. Most contraints depend on the table Engine.
I think InnoDB supports this.
To do it, you have to add a UNIQUE index with the unique combination:
ALTER TABLE visitor ADD UNIQUE unique_default_visitor( visitor_uid, list_type );
I supposed you'd have to write a trigger to check for it.
I never use MySQL but maybe you can create an index like this:
CREATE UNIQUE INDEX list$default$uk ON list_group ((CASE WHEN list_type='default' THEN NULL ELSE visitor_uid END));
Explanation:
A unique index should not care about NULL values. Therefore, make sure the index expression returns NULL for every row where list_type <> 'default'.
My two cents:
why don't you create a column only to store the unique value (maybe you can call it something like is_list_type_default) and another to store all the values. If you do this, you can put a unique constraint on the first column.