c# + sql server: I have to import a few tables from Access into sql server. The new sql server tables are already defined and created. The Access tables have primary key - foreign key relationships that have to be maintained. I would like to have the new sql tables use identity values for the primary keys, but I can't easily load the legacy rows and maintain the relationships if I do that. I could possibly load the data using identity_insert but I have to back way out of my ORM software (subsonic) to do that. Perhaps I can make my new primary keys non-identity types, but then there is the hassle of generating unique ids for all the rows I add later. I'm sure this has a decent solution out there somewhere.
Could you import the primary keys as non-identity types, then once all the data from all the tables is imported change them to identity types?
Create the tables with the PKs as "int" (without identity), load your data, and then change the PK fields to int identity...
Related
I have MS Access tables that are indexed but accept duplicates. Tables have been transferred to SQL Server and linked to Access.
How do you replicate primary keys that accept duplicates?
You should consider using SSMA
Sql Server Migration Assistant for Access
It can move up tables.
It will move up related data, setup PK columns for you.
It will maintain, and create relationships for you.
It will maintain and create all indexes you have now.
If you just have a few tables, say 2-5 tables? Then sure, just import them and setup the relatonships and indexs your self.
However, the last few migrations of data from Access to SQL server? There was in excess of 80 tables - and HUGE numbers of reatonships, indexes, and of course PK settings. The migration wizard thus can send up all of the tables - and setup all of the tables correctly on SQL server for you. Including PK values, FK values (Foreign Keys (related tables)),
and even constraints are in most cases correctly move up to sql server.
what is nice, is then you can re-link the tables in your Access application, and you now using SQL server for the back end database.
SSMAA can be found here:
https://www.microsoft.com/en-us/download/details.aspx?id=54255
I think you're confusing an "index" with a "primary key".
An "index" is a structure that helps optimise queries. Indexes don't have to be unique. A "primary key" is a logical constraint on a column which requires that all values in the column are unique.
It sounds like what you want to do is import the data into SQL and create an index to help speed up queries, but where that index is not constrained to be unique.
Here's the syntax to do that. Suppose we have some table T:
create table T(i int, j int, k int);
We want to create an index on column i to speed up queries, but i is not unique. To do that we create a regular (non unique) index:
create index MyIndexName on T(i);
As a rule, I tend to name my indexes based on what they are indexing. So in the above case I wouldn't call the index "MyIndexName", I would call it something like ix_T_i.
I should create a database on ms-acces. I uploaded the relational database schema. I've created the tables for employee,department,dept_locations,project and dependent.I've assigned the relationships of these. But I don't understand the works_on part.
How can i assign 2 primary keys to a table? What should I do?
Relational Database Schema
what you need is called a composite primary key. In MS Access, the way you get this is by holding down the Ctrl Key while clicking each field in the primary key.
Here is a tutorial.
As mentioned in the title, is it possible to create many-to-many relationship between two tables that belong to two different databases? If yes, how can i perform that with PostgreSQL?
The standard way of using foreign key constraints to enforce referential integrity is only possible within the same database - not db cluster. But you can operate across multiple schemas in the same database.
Other than that, you can create tables just the same way. And even join tables dynamically among remote databases using dblink or FDW. Referential integrity cannot be guaranteed across databases by the RDBMS, though.
Does not matter much whether the other DB is on the same physical machine or even in the same DB cluster - that just makes the connection faster and more secure.
Or you can replicate data to a common database and add standard constraints there.
It should be possible, but as has been stated you cannot expect much in the way of referential integrity.
If you follow the standard design pattern of using a linking table, you can generate a sort of M2M relationship.
DB1.dbo.Users has the USER_ID primary key
DB2.dbo.Tasks has the TASK_ID primary key
you could create a table on either DB1 or DB2 that is UsersToTasks
DB1.dbo.UsersToTasks
USER_ID - KEY
TASK_ID - KEY
This way, a unique pairing of USER_IDs and TASK_IDs are used as a key in that table. The only thing is you cannot create a foreign key to the other table.
As a pseudo workaround, you could write a trigger on DB2.dbo.Task that would write the TASK_ID to DB1.dbo.TASK_IDS and link that as the foreign key on the linking table above. I'm not sure, but you could also potentially create a delete trigger that would remove the TASK_ID as well.
http://solaimurugan.blogspot.com/2010/08/cross-database-triggers-in-postgresql.html
I have a Table which has a Primary Key field which is not set to auto-increment.
I want to change one of these primary keys to something different.
The problem arises with the other tables relations. The thing is, the guy who built this system did not build relations in SQL Server, but rather manually coded some override in the program that uses it - a VB 6 program.
How would I update a Primary Key and all instances of the Primary Key in other databases? I have to manually look for the instances(although I do know they are in only two tables) of the Primary Key and change them, but how do I do that?
Even though the person who first created the tables didn't include foreign keys to them, you can add them now if foreign keys are honored in your tables. When you create the foreign keys, create them with ON UPDATE CASCADE option. This way, when you update your primary key, the related foreign keys will also be updated.
One thing I would suggest is using the query of:
select * from INFORMATION_SCHEMA.Columns where Column_Name = 'FieldID'
This queries the metadata to see all where that field exists, just in case there are more. Then, just write an update script to change the key, unfortunately its a manual process but being that the relationship is missing it will make scripting easier.
I'm currently developing an ASP.NET MVC application with a ton of PK-FK relationships in the database. In the beginning of development, the team I WAS working with voted against my recommendation to use INTs for all PKs... they decided to use GUIDs.
Long story long... the team has split ways, and now I have control to change things... I switched from WebForms to MVC and I would like to convert all of the Guids to Ints... I hate the ugly URL's like " YUCK!! Plus the overhead involved in indexing guids... plus it probably doesn't help my Linq to Entities model.
Using SQLServer 2008, ASP.NET MVC 2, Linq to Entities (Entity Framework)
Anyone know a quick way to convert those pesky guids into ints?
I assume you also want the INTs to be IDENTITYs. Here is how to migrate the data:
Put application offline, no DB updates can occur, suspend all SQL Agent jobs, imports etc
For each PK:
create a copy table with same structure + 1 new int identity(1,1) column
copy data from old table to new table
use sp_rename to swap the tables so that the new table becomes the old table
Rinse, cycle and repeat for all tables with PKs (tables now have both GUIDs and INTs)
For each FK:
add a new INT column on the FK source table (the one that has the constraint)
run an update that joins the two tables on the GUIDs and assigns the new int PKs to the relation column
Add back FK constaints, now on the INT columns
Drop GUID columns
Script the generation of all primary and foreign keys
Drop all primary and foreign keys
Raname all GUID columns from SomethingID to SomethingGUID
Create new int SomethingID columns
Recreate primary keys
Write scripts to update all foreign keys using GUID relationships
Recreate foreign keys
Drop GUID columns