I am adding the same data again and again into CnosDB, which is a time series database. I suppose there should be unique constraint. Probably the tag?
The duplicate won't trigger an error.
So, do we have the uniqueness check here?
Related
I'm adding a UUID column to one of my tables so I can easily provision API keys. Should I bother adding a unique constraint to the column? I don't want to have duplicate API keys but on the other hand, the odds of a collision on generating the UUID values is infinitesimal.
I think you need to take into consideration if you are going to join tables based on this column or perform any operations like filter etc. If so, you will need to create a unique key on the UUID column as it will help retrieve data faster.
I'm attempiting to use cx_OracleTool's CopyData.py script to copy data between two tables on separate Oracle schemas/instances:
http://cx-oracletools.sourceforge.net/cx_OracleTools.html
When I run it against my tables, I get the error:
No primary or unique constraint found on table.
I don't know much about Oracle, to be honest, but from what I can tell the tables don't seem to have any PK constraint or anything like that defined.
The merits of this aside, I think it's simply been setup that way for expediency, and it's unlikely to change anytime nearterm.
Is there any way to get copyData.py to run in this scenario without a PK constraint?
Cheers,
Victor
The issue is that CopyData checks to see if the row exists in the destination table, and it can't do that without a unique key.
If it is acceptable to insert all rows and not update changed ones, use the --no-check-exists option. According to the code this will bypass the primary key check.
Otherwise, use the --key-columns=COLS option to manually specify the columns to be used as the unique key. This will also bypass the primary key check.
To prevent duplicate table entries in a database I use a primary key. I just add the information and if it is a duplicate then the primary will be a duplicate and it will not add to the table.
Should I also do a SQL query (before trying to add to the database) to see if the entry exists? Or is this redundant since I already have the primary key set?
It is redundant to check for presence of a value if you already have a constraint to prevent duplicates.
But it would also be ineffective to check before you insert, because some other concurrent client might insert that value in the moment between your check and your insert. So even if you check first, you'd still need to handle duplicate key errors.
Defining "unique constraint" on table with desired column will fix everything. If there's a dupplicate you will get error.
With most database platforms, when you create the primary key, the operation will fail if there are duplicate entries, so there should be no need to test for this beforehand.
Usually you'd get an exception or an error code from the call to SQL engine. If you need to handle that or not depends on your application logic. For example if it is a new username, and it already exists in the database, then exception is part of you application logic, and you will provide new user with a message about why registration failed.
I created a constraint on my tsql table like this:
alter table disabledqualities
add constraint uc_uIdQualCode
unique (userId, qualitycode)
In MSSMStudio the constraint shows up under indexes rather then under constraints.
Why?
--EDIT--
I understand that it creates an index to enforce the constraint, but then why is there a node called "constraints"?
SQL Server creates an index behind the scene to enforce the constraint
here is another way of writing that by adding nonclustered telling sql server to use a nonclustered index, you can also create a clustered on providing that you don't have a PK that is clustered (the default) or another clustered index already
alter table disabledqualities
add constraint uc_uIdQualCode
unique nonclustered (userId, qualitycode)
[edit]
that node is to add check constraint, unique constraints are added under indexes
either way stay away from wizards
Check constraints and default constraints are shown under the constraints node.
SQL uses indexes to enforce unique constraints.
I understand that it creates an index to enforce the constraint, but then why is there a node called "constraints"?
This node is to display CHECK constraints.
The true answer is "because Microsoft said so. If you want the answer, you'll have to ask them".
Because UNIQUE constraints can be found in sysindexes system view
select * from sysindexes where name = 'uc_uIdQualCode'
It is simply logical
To add new Unique constaint from Studio you need to click Manage Index and Keys button. So if you are adding it from Indexes, you have to see it in Indexes :)
I'm thinking perhaps you don't understand what might show up in constraints becasue you don't know what a check constraint is. A check constraint will check the data on insert or update to see if it meets some sort of business rule. It is used for ensuring data integrity. For instance if you have an integer field that should only contain the values of 1,4 or 5 then you would set up a check constraint to make sure that 9 isn't ever added to the field. A check constraint on date field might specify that it must be later than the current date and time for a field that is the PlannedCompletionDate or that CompletionDate must be later than StartDate. These are the kind of things that show up under constraints.
I want to learn the answer for different DB engines but in our case;
we have some records that are not unique for a column and now we want to make that column unique which forces us to remove duplicate values.
We use Oracle 10g. Is this reasonable? Or is this something like goto statement :) ? Should we really delete? What if we had millions of records?
To answer the question as posted: No, it can't be done on any RDBMS that I'm aware of.
However, like most things you can work around it, by doing the following.
Create a composite key, with a new column and the existing column
You can make it unique without deleting anything by adding a new column, call it PartialKey.
For existing rows you set PartialKey to a unique value (starting at Zero).
Create a unique constraint on the existing column and PartialKey (you can do this because each of these rows will now be unique).
For new rows, only use a default value of Zero for PartialKey (because zero has already been used), this will force the existing column to have unqiue values in the table.
IMPORTANT EDIT
This is weak - if you delete a row with partial key 0. Now another row can be added with a value that is already in the existing column, because the 0 in partial key will guarentee uniqueness.
You would need to ensure that either
You never delete the row with
partial key 0
You always have a dummy row with
partial key 0, and you never delete
it (or you immediately reinsert it automatically)
Edit: Bite the bullet and clean the data
If as you said you've just realised that the column should be unique, then you should (if possible) clean up the data. The above approach is a hack, and you'll find yourself writing more hacks when accessing the table (you may find you've two sets of logic for dealing with queries against that table, one for where the column IS unique, and one where it's NOT. I'd clean this now or it'll come back and bite you in the arse a thousand times over.
This can be done in SQL Server.
When you create a check constraint,
you can set an option to apply it
either to new data only or to existing
data as well. The option of applying
the constraint to new data only is
useful when you know that the existing
data already meets the new check
constraint, or when a business rule
requires the constraint to be enforced
only from this point forward.
for example
ALTER TABLE myTable
WITH NOCHECK
ADD CONSTRAINT myConstraint CHECK ( column > 100 )
You can do this using NOVALIDATE ENABLE constraint state, but deleting is much more preferred way.
You have to set your records straight before adding the constraints.
In Oracle you can put a constraint in a enable novalidate state. When a constraint is in the enable novalidate state, all subsequent statements are checked for conformity to the constraint. However, any existing data in the table is not checked. A table with enable novalidated constraints can contain invalid data, but it is not possible to add new invalid data to it. Enabling constraints in the novalidated state is most useful in data warehouse configurations that are uploading valid OLTP data.