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
Related
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.
I have two tables TableA and TableB where TableA has columns col1, col2, col3, col4, col5 and col1, col2 and col3 combine to form its primary key. TableA and TableB has one-to-many relationship on the same column(col1, col2 and col3) as its foreign key constraint. Now how can I update just col2 values in both TableA and TableB in SQL and in Hibernate?
Doesn't matter if is hibernate or the number of columns on the primary key. You cant remove/edit a primary key value if is already use as a foreign key. That is a CONSTRAINT FK VIOLATION, and the constraint function is precisely there to avoid any row become orphan by mistake and keep data integrity.
This need to be done in three steps:
Insert a new row with your new PK
Update all your FK related to old PK to your new PK
Delete old PK
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.
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?