MySQL database one-to-one relation - mysql

If I have a database with multiple tables all with one to one relations. What is the best approach for when I register a row in one table, it's also registers a row across the related tables. I know you can use cascade to update and delete using foreign keys so is there a similar approach for creating these related rows with default values?

Creating null rows is not great design. Better create them only when necessary using foreign keys. If you still must, look at transactional stored procedures.

Related

Best practice to delete files from S3 after record is deleted from MySQL DB

I have a database with several tables, containing object keys to an S3 object. I understand this is a common practice. What would be the best way to delete the objects from S3 when the record is deleted from DB?
The tables contain different foreign key constraints, which are set to cascade. So I can't just delete the objects when deleting the record.
The foreign key constraints may change frequently and there are often multiple foreign keys, so it doesn't make sense to structure the S3 objects like /objects/{{foreign_key}}/object either. Would be much harder to keep that in sync.
The only way I could think of is to have another table in DB like s3Archive. Then, use a BEFORE DELETE TRIGGER in MySQL to add the object key to the table, and run a cron job to batch delete the objects from this table.
I just found out while typing this, MySQL triggers are not run on foreign key actions. All hope is lost. Tbh, it would still be feasible to add the trigger on every table that can trigger the cascade. Does this make sense or am I missing something obvious?

MySQL Import data from two different database to one database Or merge two database data

I have two databases i need to merge both database in one database. for example
let_client1 (same fields and table number that on let_client2 database)
let_client2
I have 99 tables on both databases, also 5 views in both databases.
There are many indexing, primary key and foreign key relations so how could i merge both database in one database. is there any best method in Mysql?
As a starting point you can use an insert into select statement as such:
INSERT INTO let_client1.table1 (field1, field2, etc)
SELECT field1, field2, etc
FROM let_client2.table1
This will merge the data from let_client2.table1 into let_client1.table1, however beware of primary key violations and foreign key restrictions so you will need to perform this operation on the tables that have dependants first.
If you want a purely automatic solution I would suggest using cursors to select the tables and pass their names into a procedure that does the merging, but you will easily run into the issues I just described above.
http://dev.mysql.com/doc/refman/5.7/en/cursors.html
As for the indexes, if they need transferring, here's a related question already with answers: MySql, how can I export indexes from my development database to my production database?
There's no single easy method to do this, because it depends on the table definitions.
If you use auto-increment primary keys, your two databases might both be using the same values. These would have to be adjusted somehow.
Foreign key constraints require your tables to be loaded in a particular order. Or else you could use SET FOREIGN_KEY_CHECKS=0 while you do the data load.
I think you'll have to write a script to do the data merging in a way that works for your specific case.
You can use the INFORMATION_SCHEMA to get a list of tables and a list of foreign key constraints.
You don't have to transfer indexes or views. They will both be populated as you load data into your tables. MySQL views do not store data, they only query the base tables.

Migrate SQLIte Data in to MySQL and manage/update the foreign 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

Generating SQL from an ERD - how are relations mapped into SQL?

I'm using Mysql workbench EER where I draw my ERD.
There are all kind of relationships between the tables (optional/mandatory,non identifying/identifying)
and I use the Forward engineering to generate the underlying SQL.
As far as the relationships go, for an optional relationship it generates a NULL FK
while in a mandatory a NOT NULL FK and that's it.
I mean shouldn't it also based on the relationships generate Cascades deletes for example?
i.e if I have an identifying relationships, then when the parent is deleted the child should be deleted too thus a cascade delete would have been generated
Or, in other words the relationships modeled in an ERD have no practical value other than conceptually know how your database is modeled on paper?
For example what should be the generated sql code for an identifying mandatory relationship?
What you are referring to is called a relationship. In database management terms a relation is something different.
Your relationships should get implemented as foreign keys. The practical value of a foreign key is that it enforces referential integrity. Cascaded deletes are not typically the desired behaviour and in SQL the default referntial integrity action is that delete of a row in a parent table is not permitted if the row is being referenced in another table. If you want cascaded deletes then you have to specify that. Note that in MySQL only the InnoDB database engine supports referential integrity.

Linq to sql - delete some related records

I´m using linq to sql and I have a lot of tables with foreign keys leading to the UserId.
Is it possible to have some of these foreign tables cleaned upon deletion.
For example I want the users profile (other table) to be deleted automatically with the user but not the users forum posts.
Is this possible or do I have to handle this with code?
I think this link is very usefull.
LINQ to SQL does not support or
recognize cascade-delete operations.
If you want to delete a row in a table
that has constraints against it, you
must complete either of the following
tasks:
Set the ON DELETE CASCADE rule in the foreign-key constraint in the
database.
Use your own code to first delete the child objects that prevent the
parent object from being deleted.
I am not sure with code, but couldn't you set the Cascade on Delete option in SQL?