Unique Key in MySql - sql

I have a table with four Columns: Col1, Col2, Col3, and Col4.
Col1, Col2, Col3 are strings, and Col4 is a integer primary key with Auto Increment. Now my requirement is to have unique combination of Col2 and Col3.
I mean to say like.
Insert into table(Col1, Col2, Col3) Values ('val1', 'val2', 'val3');
Insert into table(Col1, Col2, Col3) Values ('val4', 'val2', 'val3');
the second statement has to throw error as the same combination of 'val2','val3' is present in the table. But i cant make it as a primary key as i need a auto increment column and for that matter the col4 has to be primary. Please let me know a approach by which i can have both in my table.

You can set in the database schema a requirement that a combination of two or more keys be unique. This can be found here:
http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
This could be done with a command such as
ALTER TABLE YourTable ADD UNIQUE (Col2,Col3);

You can do this by creating a unique index on those fields.
http://dev.mysql.com/doc/refman/5.0/en/create-index.html
From that doc:
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 allows 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 would check for these values as part of your insert logic. Before you insert, write something like...
if exists
select 1 from table
where col2 = inputCol2 and col3 = inputCol3
then -- combo already exists
do nothing / set error
else -- combo doesnt exist yet
insert into table

Related

ADD combination constraint into a dabase table

I want to make a combination of columns in a table unique which they can not have any duplicates value.
For example, I have a table of Person which contain a name, age, address and phone_number.
I want that the combination of (name, age, phone_number) must be unique.
The table person contain this data:
"Name1"|22|"adresse1"|000000
So the combination (''Name1'' and 22 and 000000) it has to be unique but if only one of them changed for example ("name2",22,000000) it will be permitted to insert data to the table.
How can I manage that with SQL please?
If you want a column or combination of columns to be unique, you can implement a unique constraint or index (they are functionally equivalent for this purpose):
create unique index unq_t_col1_col2_col3 on t(col1, col2, col3);
And for a constraint:
alter table t add constraint unq_t_col1_col2_col3
unique (col1, col2, col3);
Any attempt to insert values into the columns that already exists will result in an error, as will any attempt to update the columns with values in a different row.

Primary key conflict when merging databases (auto-increment)

I have two SQLite databases which I would like to merge. I'm doing it by using the following commands:
ATTACH "c:\other.db" AS SecondaryDB
INSERT INTO MyTable SELECT * FROM SecondaryDB.MyTable
The problem is that MyTable has a primary column id which is auto-incremented in both databases. Thus, there is a primary key conflict.
How can I insert the rows from the secondary database such that auto-increment is used for the id column starting from the last value of the first database?
You want to copy all rows from the source, but without the auto-incremented column.
Just enumerate the columns in the insert and select clauses - all, expected the primary key column. SQLite will automatically assign new values to the auto-incremented column.
Say that the columns of your table is id, col1, col2, col3, you would do:
ATTACH "c:\other.db" AS SecondaryDB
INSERT INTO MyTable(col1, col2, col3)
SELECT col1, col2, col3 FROM SecondaryDB.MyTable

How to prevent duplicate rows in insert query using Oracle?

I have table A in Oracle that has a primary key (id). I need to insert data into this table.
How do I prevent duplicate rows?
If the id column is marked as the PK you will not be able to insert a duplicate key, the server will throw an exception.
If you will insert duplicate data with different key - that's a logic that you need to deal with (like putting a unique constraint on the actual data) or do a check before you insert.
If you mean that you have rows that are identical (apart from the primary key) and you want to know how to delete them then do:
select col2, col3, ...coln, min(id) from A
group by col2, col3, ...coln
(I.e. select on all columns except the id.)
To get the unique instances do
delete from A where id not in
(select min(id) from A
group by col2, col3, ...coln) as x
to delete all rows except the unique instances (i.e. the duplicates).
First you would create a unique id on this table that has a sequence. Then when you insert into that table, in the insert statement you can use:
tableName_sn.nextval
in a statement like:
inserti into tableName (id) values (tableName_sn.nextval)
to get the next unique key in the sequence. Then if you use this key you will guarantee that it is unique. However, the caveat to that is if someone just entered a key not using the nextval function you will get primary key violations.

sql unique and setting constraints on inserts

I have a database where I need to avoid inserting duplicates. The requirements are:
For the subset of rows with matching column 1, there can not be any that have the same column 2.
For the subset of rows with matching column 1, there can not be any that have the same column 3 and 4.
I'm new to SQL so is there a way of setting these relationships when I create the database (create table) or do I have to do a select and do these checks manually before inserting into the table?
In effect, you need the column 1 and 2 to be unique, and also columns 1,3 and 4 to be unique. So when you create the table, you can use two UNIQUE constaints:
CREATE TABLE tbl (
col1 varchar(255),
col2 varchar(255),
col3 varchar(255),
col4 varchar(255),
CONSTRAINT uc_first UNIQUE(col1, col2),
CONSTRAINT uc_second UNIQUE(col1, col3, col4)
)
Just to get the ball rolling ...
Could you go back immediately after the insert and delete duplicated rows based on the contraints you mentioned?

Field value must be unique unless it is NULL

I'm using SQL Server 2005.
I have a field that must either contain a unique value or a NULL value. I think I should be enforcing this with either a CHECK CONSTRAINT or a TRIGGER for INSERT, UPDATE.
Is there an advantage to using a constraint here over a trigger (or vice-versa)? What might such a constraint/trigger look like?
Or is there another, more appropriate option that I haven't considered?
I create a view with the an index that ignores the nulls through the where clause...i.e. if you insert null into the table the view doesn't care but if you insert a non null value the view will enforce the constraint.
create view dbo.UniqueAssetTag with schemabinding
as
select asset_tag
from dbo.equipment
where asset_tag is not null
GO
create unique clustered index ix_UniqueAssetTag
on UniqueAssetTag(asset_tag)
GO
So now my equipment table has an asset_tag column that allows multiple nulls but only unique non null values.
Note:
If using mssql 2000, you'll need to "SET ARITHABORT ON" right before any insert, update or delete is performed on the table. Pretty sure this is not required on mssql 2005 and up.
Here is an alternative way to do it with a constraint. In order to enforce this constraint you'll need a function that counts the number of occurrences of the field value. In your constraint, simply make sure this maximum is 1.
Constraint:
field is null or dbo.fn_count_maximum_of_field(field) < 2
EDIT I can't remember right now -- and can't check it either -- whether the constraint check is done before the insert/update or after. I think after with the insert/update being rolled back on failure. If it turns out I'm wrong, the 2 above should be a 1.
Table function returns an int and uses the following select to derive it
declare #retVal int
select #retVal = max(occurrences)
from (
select field, count(*) as occurrences
from dbo.tbl
where field = #field
group by field
) tmp
This should be reasonably fast if your column as a (non-unique) index on it.
You can accomplish this by creating a computed column and put the unique index on that column.
ALTER TABLE MYTABLE
ADD COL2 AS (CASE WHEN COL1 IS NULL THEN CAST(ID AS NVARCHAR(255)) ELSE COL1 END)
CREATE UNIQUE INDEX UQ_COL2 ON MYTABLE (COL2)
This is assuming that ID is the PK of your table and COL1 is the "unique or null" column.
The computed column (COL2) will use the PK's value if your "unique" column is null.
There is still the possibility of collisions between the ID column and COL1 in the following example:
ID COL1 COL2
1 [NULL] 1
2 1 1
To get around this I usually create another computed column which stores whether the value in COL2 comes from the ID column or the COL1 column:
ALTER TABLE MYTABLE
ADD COL3 AS (CASE WHEN COL1 IS NULL THEN 1 ELSE 0 END)
The index should be changed to:
CREATE UNIQUE INDEX UQ_COL2 ON MYTABLE (COL2, COL3)
Now the index is on both computed columns COL2 and COL3 so there is no issue:
ID COL1 COL2 COL3
1 [NULL] 1 1
2 1 1 0
In Oracle, a unique key will permit multiple NULLs.
In SQL Server 2005, a good approach is to do your inserts through a view, and disable direct inserts into the table.
Here is some sample code.
Is there a primary key on this table, maybe an Identity column? You could create a unique key that is a composite of the field you are enforcing uniqueness on in combination with the primary key.
There is a discussion about just this kind of issue here: http://blog.sqlauthority.com/2008/09/07/sql-server-explanation-about-usage-of-unique-index-and-unique-constraint/
FYI - SQL Server 2008 introduces filtered indexes which would allow you to approach this a bit differently.
Usually a trigger will allow you to provide a more verbose and explanatory message than a check constraint, so I have used those to avoid the "which column was bad" game in debugging.
A constraint is far lighter than a trigger, even though a unique constraint is effectively an index.
However, you are only allowed one NULL in a unique constraint/index.
So, you'll have to use a trigger to detect duplicates.
It's been requested from MS to ignore NULLS, but SQL 2008 has filtered indexes (as mentioned while I type this)