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?
Related
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
I have two tables A and B and need to insert records (few columns not all) from A into B.
Of course I guess I can do:
INSERT INTO B (col2)
SELECT DISTINCT col2
FROM A
However, Col1 in table B (named ID) has a type of INT so it is causing this error:
Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'ID', table 'MyDB.dbo.Visitor'; column does not allow nulls. INSERT fails.
How can I make SQL Server ignore this column and just insert the data I need?
Thanks.
A primary key must fulfill two conditions:
It must be unique in the table (that is, any row in the table can be identified by its primary key), and
The fields that are part of the primary key cannot be NULL. That's because allowing NULL values in the primary key will make the uniqueness of the primary key impossible to hold, because a NULL value is non-equal to any other value, including another NULL.
Quoting from here:
A unique key constraint does not imply the NOT NULL constraint in practice. Because NULL is not an actual value (it represents the lack of a value), when two rows are compared, and both rows have NULL in a column, the column values are not considered to be equal. Thus, in order for a unique key to uniquely identify each row in a table, NULL values must not be used.
This should work assuming you don't insert duplicates into B:
INSERT INTO B (col2)
SELECT DISTINCT col2
FROM A
WHERE col2 IS NOT NULL
Set ID column in table B to "auto-increment".
SQL Server will provide automatically unique values for ID column if you define it as IDENTITY
In your case you can calculate the maximum value of ID column and start IDENTITY from the value that exceeds that maximum.
See the accepted answer for SQL Server, How to set auto increment after creating a table without data loss? for such code.
You need to create a relationship between the two tables and do an update statement.
Update table b set valueb = valuea from table a where a.id = b.id
You also need to rethink your design a little bit it sounds like.
I am trying to add a check constraint on multiple columns in Oracle table that restricts user from inserting NULL into 3 columns simultaneously. However each column in the table can accept NULL independently but not 3 of the columns together.
ALTER TABLE table1 ADD CONSTRAINT CK_not_null
CHECK (col1 IS NOT NULL AND col2 IS NOT NULL AND col3 IS NOT NULL);
This check constraint is not allowing NULL in any of the three columns. Any thought on this?
This constraint will not achieve your needs - it checks that all three columns are not null. The behavior you're describing can be achieved by negating (with the not boolean operator) a condition where all three columns are null:
ALTER TABLE table1
ADD CONSTRAINT
ck_not_null CHECK
(NOT (col1 IS NULL AND col2 IS NULL AND col3 IS NULL))
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
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.