As per my knowledge, unique key stores only one null. But in this case, it accepts more null.I don't know why?
This what exactly happens
No, Unique key stores multiple null.
Oracle:
Oracle Thread
MySql:
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.
MySql Thread
Related
The table already has duplicates entries. I want to create a unique constraint in MQSL DB without deleting the existing duplicates. If any duplicate entries coming onwards then it will show an error. Given blow queries not working in MYSQL.
ALTER TABLE presence
ADD CONSTRAINT present uniqueness UNIQUE (employee_id,roll_number) where id >10000;
or
ALTER TABLE presence
ADD CONSTRAINT present uniqueness UNIQUE (employee_id,roll_number) where id <> (343,34534,34534)
Do we have something like that solution in SQL?
Add an additional column to the table that indicates the existing values.
Set it to NULL for the existing values. And give it a constant value, say 1, for the new rows. Then create a unique index or constraint on this column:
alter table t add constraint unique (employee_id, is_old)
Actually, I realize that you probably don't want duplicates with singleton old values and new values. That is just an issue of setting the value to NULL only for duplicates in the history. So, one row would have a constant value (say 1) in the historical data.
MySQL allows duplicate values on NULL, which is why this works.
I have AUTO_INCREMENT PRIMARY KEY and another column that I can't set UNIQUE because unlike standard RDBMS like MySQL or PostgreSQL, MemSQL only allow only one of them, not both.
Is there workaround to prevent duplicate rows without sacrificing the auto_increment column?
I can use unique as primary key and use atomic counter in other product/service like Redis/atomic variable, but when I need to update the unique column I have to delete it first then reinsert, which is bad/unpreferred way for me..
MemSQL does support multiple unique keys together with a primary key. However, MemSQL requires that the columns in each unique or primary key must be a superset of the columns in the shard key - i.e. that all values that would be considered duplicate under each unique key have the same shard key, so that they get mapped to the same partition. This further implies that all the unique/primary keys must share at least one column in common.
For your case, it is not possible to have both a unique/primary key on the autoincrement column and the other column. But you can have a unique/primary key on the other column, without a unique key on the auto_increment column - just define it as a non-unique key. The automatically generated values will still be unique. Do note that then the table won't be able to enforce uniqueness if you manually insert values that are duplicate with other auto_increment values.
I have 2 tables. users and post. I want to use users(id) column (which is pk) as foreign key in post(user_id) column. I used:
ALTER TABLE post ADD FOREIGN KEY (user_id) REFERENCES users(id);
and that action succeeded without any errors but I see only null in (user_id) column. Do I miss something or what? Shouldn't it copy the id values from users table?
Q: Why foreign key is null?
A: Whether or not a column can contain a NULL value is determined by the presence or absence of a NOT NULL constraint. This is entirely independent of whether the column is referenced in a foreign key constraint.
The value in the column is NULL because that's the value that was assigned when the row was inserted. The value was assigned, whether it was explicitly set, or whether it was derived from the default value for the column. (If the column was added to an existing table, then the values in the new column was the default value for the column.)
--
Q: Do I miss something or what?
A: The behavior and results you observe are exactly as we expect.
Q: Shouldn't it copy the id values from users table?
A: If you're asking if MySQL should automatically populate the user_id column in the post table, the answer to that question is no, it shouldn't.
I think maybe you've clued in on a key idea:
The "relationship" between a row in one table to a row in another table is represented in the relational database by storing a common value.
But the database doesn't know which row is related to which row. You have to tell it. You have to provide that information.
When you insert a row into the post table, you can provide a value for the user_id column. You would provide a value that's equal to the id value of some row in user.
The idea with a FOREIGN KEY constraint is that it's restriction. It only allows valid values. It prevents an invalid value from being stored. (That's true with InnoDB if FOREIGN_KEY_CHECKS=1, that's not true for MyISAM, because MyISAM doesn't enforce foreign key constraints.)
The foreign key is saying that you want to "constrain" the values that can be stored. It's saying that it's not going to allow rows in post to have have user_id values that point to a "missing" row in the users table.
It's perfectly acceptable to store a NULL in a foreign key column. When a NULL value is stored, that's saying that the row is not related to a row in the users table.
Disallowing NULL values in a column is done with a different kind of constraint, a NOT NULL constraint.
It's possible to define both a foreign key constraint and a NOT NULL constraint on the same column. That's a design decision, whether you want to allow NULL values or not. In some cases, we may want to disallow NULL values in a foreign key. For example, if we were to add a NOT NULL constraint on the user_id column of post, that would effectively be saying that a row cannot exist in post if it's not related to a row in users. And that's a very common pattern.
Adding foreign key to any column in MySQL does not copy any values.
I have the following table, I am trying to create a constraint on the table that states that the Ter_ID column can have the same Ter_ID repeating however the corresponding Status cannot be the same at anytime, so if for example one is trying to update Ter_ID 100P Status from U to A it wouldn't go through seeing that there is already a row with the corresponding data.
Ter_ID Status Address
100P A Road1
100P U Road2
200R A Road2
You need to add a composed unique index using the two columns.
Example:
ALTER TABLE `tablename` ADD UNIQUE `unique_index`(`Ter_ID`, `Status`);
Be aware of the different NULL behaviour depending of the MySQL engine that you are using. From the official MySQL documentation:
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. This constraint does not apply to NULL values except for the BDB storage engine. For other engines, a UNIQUE index permits multiple NULL values for columns that can contain NULL. If you specify a prefix value for a column in a UNIQUE index, the column values must be unique within the prefix.
I have a column in one of my tables. It's optional, so it can be left blank. However, if a value is provided for that column, it must be unique. Two questions:
How do I implement this in my database design (I'm using MySQL Workbench, by the way)
Is there a potential problem with my model?
Just use a UNIQUE index on the column. See:
http://dev.mysql.com/doc/refman/5.1/en/create-index.html
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. If you specify a
prefix value for a column in a UNIQUE
index, the column values must be
unique within the prefix.
It's can be null (and not blank) and unique. By default value can be null. There is no problem for me.
You can create a UNIQUE index on a table. In MySQL workbench that is the UQ checkbox when creating/editing the table.
Step 1, ALTER the table and MODIFY the field so NULLs are allowed.
ALTER TABLE my_table MODIFY my_field VARCHAR(100) NULL DEFAULT NULL;
Step 2, add a UNIQUE index on the field.
ALTER TABLE my_table ADD UNIQUE INDEX U_my_field (my_field);
It's fine to use -- my only hesitation with putting a UNIQUE index on a nullable field it that it is slightly counter-intuitive at first glance.
1) Move the column to a new table, make it unique and non-nullable. You can now have a row in this new table only when you have a value for it.
2) Yes. Keys and dependencies on keys are the basis of data integrity in a relational database design. If an attribute is supposed to be unique then it should be implemented as a key. A nullable "key" is not a key at all and anyway is never necessary because it can always be moved to a new table and made non-nullable without loss of any information.