Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unique identify rows in a table without primary key

Tags:

sql-server

I'm importing more than 600.000.000 rows from an old database/table that has no primary key set, this table is in a sql server 2005 database. I created a tool to import this data into a new database with a very different structure. The problem is that I want to resume the process from where it stopped for any reason, like an error or network error. As this table doesn't have a primary key, I can't check if the row was already imported or not. Does anyone know how to identify each row so I can check if it was already imported or not? This table has duplicated row, I already tried to compute the hash of all the columns, but it's not working due to duplicated rows...

thanks!

like image 789
user1082693 Avatar asked Dec 15 '11 19:12

user1082693


People also ask

How we can uniquely identify a row if it doesn't have any primary key?

A super key or simply key is a combination of all possible attribute which can uniquely identify the rows(tuples) in a table. This means that a superkey may have some extra attribute which isn't necessary for uniquely identifying the rows in the table.

What if a table has no primary key?

Every table can have (but does not have to have) a primary key. The column or columns defined as the primary key ensure uniqueness in the table; no two rows can have the same key. The primary key of one table may also help to identify records in other tables, and be part of the second table's primary key.

Can uniquely identify a row in a table?

A specific choice of columns which uniquely identified rows called primary key.

Can you reference a table without primary key?

Can you reference non-primary key? Yes, a foreign key can reference a non-primary key which is unique. A FOREIGN KEY constraint does not have to be linked only to a PRIMARY KEY constraint in another table; it can also be defined to reference the columns of a UNIQUE constraint in another table.


1 Answers

I would bring the rows into a staging table if this is coming from another database -- one that has an identity set on it. Then you can identify the rows where all the other data is the same except for the id and remove the duplicates before trying to put it into your production table.

like image 60
HLGEM Avatar answered Oct 23 '22 04:10

HLGEM