My plan is such as following:
I have the Asp.Net membership database that keeps info about users.
I want to create my own database in which some of the tables will have fields like userId as a foreign key to primary key field in the membership table Users. So how would I achieve this and what is the best practice for this? Or should I get the value of the user during run time, and then copy that value to my table without the need of any foreign key relations.
Just add tables to the existing database, and reference the pk from the Users table as needed. There's no need to use a separate database as long as it's all logically the same application.
Related
I have a requirement to provide a REST endpoint to create/delete tables for a privileged user.
When user makes a request to create table 'xyz', I create table with prefix "user_" and return response saying
'user_xyz' is created.
This way I know what tables are candidates for deletion when a request is made.
But I wish to create table "xyz" as requested by user and would like to add some label like "deletable" so that I can
query to find if a table can be deleted.
One option is adding a comment for table and but I have to query information_schema. Comment does not sound very correct.
Can this problem be solved using any other approach when I use MySql database.
I don't think you have much other options other than the one you suggest,
prefixing/suffixing table name
use table comment
create meta table with UserId(has the advantage of foreign keys)
and TableName fields (has the disadvantage of integrating the table
name with the actual very table as it can change without this
metatable being updated)
create separate schema for each user
I am using Talend Open Studio for data migration as I am upgrading my existing application architecture to a new one. I am using MySQL in both the applications but with different schema. I have migrated data successfully between single tables but while I am transferring data from a single table to a parent-child relationship table with a foreign key constraint, the data transfer is tremendously slow.
For e.g. I am migrating my Cities table to Cities and Citiesi18n and below is the schema for them:
My old schema :
CITIES (
id
city_name
status
created_at
)
The newly created schema where I need to migrate the data :
CITIES (
id
status
created_at
)
CITIESI18N (
id
lang_code
name
fk_city_id (// foreign key of cities table)
)
Below are the snapshots from my Talend jobs:
And here is the tmap configuration :
Now when I transfer the data without the foreign key the result are super fast. See below :
But the same when I transfer with a foreign key, my transfer is super slow :
(Note: I have taken province table for example as it is similar to cities table)
I think with Foreign key constraint it must be indexing the columns while transferring the data making it slower, but I am not sure. Is there any way I can fix this as I have a lot of tables similar to this which needs to be migrated. I am just curious to know the reason.
I don't know why you have this behaviour : you can try to redirect 'provience_i18n' to a tHashOutput (cache component), then link to a subjob with tHashInput (refering to your tHashOutput)-->tMySQLOutput. You'll have 2 subjobs, one for each insertion.
You are loading data to the parent & child at the same time. Using one tmap. When you are inserting foreign key in the second table, there is also insertion being made in the foreign/parent table. What you could alternatively do is: Load the data in the main CITIES table first, then onSubJobOk, load into child/CITIESI18N table. It would be faster. Let me know if it works.
I have a MySQL database on which I want to generate entities with Dali JPA Tool built into Eclipse.
The database is not mine, and I prefer not to ask for the change of architecture.
The problem exists when I create entities because a table has a foreign key that is mapped to a column in another table that is not a primary key.
I have noticed that for this reason the entity of this table is not created by the tool (but it's added in persistence.xml).
Is there a way to generate the entity without having to change the database architecture?
Thanks a lot for every possible idea.
PS: I'm not a database expert, but is it ok to create foreign keys that are mapped to columns that are not primary keys?
I'm developing an Android application in which the data is stored in a SQLite database.
I have made sync with a MySQL database, in the web, to where I'm sending the data stored in the SQLite in the device.
The problem is that I don't know how to maintain the relations between tables, because the primary keys are going to be updated with AUTO_INCREMENT, and the foreign keys remain the same, breaking the relations between tables.
If this is a full migration, don't use auto increment during migration - create tables with normal columns. Use ALTER TABLE to change the model after import.
For incremental sync, the easiest way I see is additional column in each MySQL table called sqlite_id and filled with original id. Then you can update references using UPDATE (with joins).
Alternatives involve temporary tables for storing data and an auxiliary table used for pairing. Tedious for bigger data model.
The approach I tend to use, if possible, is to avoid auto increment in such situations. I have usaully an auxiliary table with four columns like this: t_import(tablename, operationid, sqlite_id, mysqlid).
Process is the following:
Import the primary keys into t_import. Use operationid to separate parallel imports if needed.
Generate new keys for data tables and store them into t_import table. This can be combined with step one.
Import the actual data and use t_import for setting new primary keys and restore relations.
That should work for most scenarios I know about.
Thanks or the help, you have given me some ideas.
I will try to add a id2 field to the tables that will store the same value as the primary key (_id)
When I send the information from SQLite to MySQL and the primary key is incremented I will have the id2 field with the original value of the primary key so that I can compare it with the foreign key of the other tables and update it.
Let’s see if it works.
Thanks
I am building a database on SQL Server 2014. I have a users table and a profiles table and I need to have a relationship with both these tables. I am relating the userid (primary key on user table) to the profiles table (userid there as foreign key). This is just an example to consider.
What I need to know is, what if the profiles table is on another server instance? Is there a way with which I can link both? The reason is that I don't want to overload the sql server with too many tables and data...
Thanks,
Sarin Gopalan
It is not at all possible to create foreign key relations between databases - let alone between server instances.
You might be able to create a trigger on the Profiles table, that checks if a userid exists in the User table in the other database, but I fear the performance of this approach will be very bad.
A much better solution would be to replicate one table to the other database, and then create the foreign key relations in a normal way. How you replicate the table (SSIS, CDC, triggers, etc.) is up to you.