three servers
server 1 is central point which links the other two servers.
the other two servers have a table which has a field which should act like a foreign key
problem
well i do not know how to do this, using vs08 or sql server 08
diagram view
table on server 1
sv1pg1 id -- primary key
details
tables same schema on srvr2 and srvr3
linkedSRVid -- pk
linkedto -- fk constrain should be between (server 1 primary key and this field)
note that
"this is just a simplest! way i can think of to represent the prob, the real dbs contain
many fields and tables and the following applies"
it's not always that the network connection between the server remains up.
the link only need for "writing purpose" not viewing, as viewing done by direct
connection to the respective server.
A foreign key constraint can reference other tables only within the same database. This means that even if those databases were on the same server it still would not work.
Considering that the schemas are the same, you may want to look into replication.
Related
While building my app, I came across a problem. I have some database tables with information, I want to reuse for different applications. Mainly for authentication and user privileges.
That is why i decided to split my database into two, one for user data (data I will need for other applications) and another for application related data (data I will need only for this).
In some cases, I need to reference a foreign key from one database on another database. I had no problem doing so while databases are in the same connection. I did it like so:
CREATE TABLE `database1`.`table1` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`foreign_key` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `table1_foreign_key_foreign` (`foreign_key`),
CONSTRAINT `table1_foreign_key_foreign` FOREIGN KEY (`foreign_key`) REFERENCES `database2`.`table2` (`id`)
);
Now here is my problem. I am getting to know Docker and I would like to create a container for each database. If my understanding is correct, each container acts as a different connection.
Is it even possible to reference a foreign key on different database connection?
Is there another way of referencing a foreign key from one Docker container on another?
Any suggestions or comments would be much appreciated.
Having a foreign key cross database boundaries is a bad idea for multiple reasons.
Scaling out: You are tying the databases to the same instance. Moving a database to a new instance becomes much more complicated, and you definitely do not want to end up with a FK constraint running over a linked server. Please, no. Don't.
Disaster Recovery: Your DR process has a significant risk. Are your backups capturing the data at the exact same point in time? If not, there is the risk that the related data will not match after a restore. Even a difference of a few seconds can invalidate the integrity of the relationship.
Different subsystems: Each database requires resources. Some are explicit, others are shared, but there is overhead for each database running in your instance.
Security: Each database has its own security implementation. Different logins and access permissions. If a user in your DATA database needs to lookup a value against the USER database, you'll need to manage permissions in both. Segregating the data by database doesn't solve or enhance your security, it just makes it more complicated. The overhead to manage the security for the sensitive data doesn't change, you'll still need to review and manage users and permissions based on the data (not the location of the data). You should be able to implement exactly the same security controls within the single database.
No, that is not possible. You can not create FK to different instance of DB (or other Docker container in your case).
You may try to make this check on application level.
Say I have two tables, Address and Person, which resides on two totally separate servers. I want to refer to Address in Person by a foreign key.
I want to put them in separate servers, because they are with the databases that perform jobs closer to each of them. e.g. the DB person resides in may define a lot of tables that relate to Person, but none of them except Person relate to Address, and vice versa. Failing of the server Person resides on will have no impact on the Address's server, unless the foreign key is involved in the action.
If this makes sense, how can I do that in mysql? And how can I do that in java using hibernate?
No, but you can have trigger to ensure foreign key constraint.
Let's say I have a bunch of MyISAM tables in a MySQL database. I know that if my tables are InnoDB MySQL Workbench will pick up the relationships between the tables automatically. But if my tables are MyISAM, is there a way that I can select which column to use as a foreign key in MySQL Workbench without MySQL Workbench adding a column in my table schema? For example, let's say I have these columns in a table called users:
id PRIMARY KEY
username VARCHAR (255)
password VARCHAR (255)
email VARCHAR (255)
user_type_id INT
As you can see above, user_type_id would be a foreign key coming from a table called user_types. If I add a many-to-one relationship between my tables users and user_types in MySQL Workbench, a column user_types_id will be automatically added to the schema of my table users (because that's what MySQL Workbench considers as a foreign key usually). I don't want that to happen, I want to be able to tell MySQL Workbench to use the column user_type_id as my foreign key. Any way I can do that?
Thank you
NOTE: There is a Foreign Keys tab when I double click on a table on the model view in MySQL Workbench, but when I do so, I get the following text:
Note: foreign keys can only be defined for certain storage engines (like InnoDB). The server accepts foreign key definitions for other storage engines but silently ignores them. Switch your table engine to one that supports foreign keys to allow adjustments here.
You could temporarily switch to InnoDB, define the relationship and switch back to MyISAM. The relationships will remain. BUT, they are of no real use except to document your intention.
If that's all you want then go ahead.
Btw: the FK tab page is only unavailable on Windows (for engines that don't support FKs), as we have seen many complaints from users that defined FKs for MyISAM, just to see no effect in their target DB. On Linux + OS X you can work on FKs regardless of the selected storage engine.
If I'm understanding your question correctly, I believe you can do this from the Foreign Keys tab on the table itself. If you double click a table from the model overview, on the bottom tabs there is a Foreign Key tab. You can manually add in foreign keys however you choose from that dialog. Does that make sense?
How can I add a constraint that references a foreign column from another database?
Some time ago I read that it can be done with linked server and others say with triggers. What's the preferred way of doing this if that's possible at all?
Thanks!
Linked servers will not work.
FK's must point to local tables.
The preferred way is not to do this, though you can pull a few hacks to make it happen.
For example you could have triggers cause the reference to be created/checked but I wouldn't consider that equivalent to a FK constraint.
Related question:
Can you have a Foreign Key onto a View of a Linked Server table in SQLServer 2k5?
Use an instead of trigger (you may or may not need linked servers depending on whether the other database is on a differnt server). Make sure the trigger can handle multiple row inserts/updates/deletes. I'd also suggest moving the records that fail the check to an exception table.
Foreign keys cannot go across database boundaries. If you try to do this, you'll get:
Msg 1763, Level 16, State 0, Line 1
Cross-database foreign key
references are not supported. Foreign key ***
Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.
If you need to enforce some kind of relationship between two separate databases, then yes - you might need a linked server (if that second database is on a second server), and possibly triggers - but all of those things will be very hard to get right, very inefficient and very error prone.
One way you might be able to do this would be data replication - replicate the table you want to reference into your source database, and then establish a foreign key relationship with that replicated table. But that will never be quite "live" and "real-time" - there will also be a bit of a lag in the data replication.
I have recently switched jobs and at this new company we are using MySQL. I don't have any expereince with MySQL, although I have used SQL Server and Oracle for over 4 years now.
Now the strange thing I see with MySQL is that it does not seem to resepect some of the basic things like Foreign Key Constraints (meaning a column is a foregin key but i can insert any value here no matter if it's present in the other table where this FK related to). Now I know in SQL Server there is this concept of a NOCHECK foriegn key constraint but the guy at new company responsible for MySQL db say that not respecting a FK is a normal thing in MySQL and it does not need to have any special settings (like NOCHECK FK constraint).
I fail to understand that in a database system how can you ensure referential integirty without having these basic checks in place. I am not sure if the local mySQL "expert" know it well or it's just that mySQL really does not respect FK rules. Any thoughts?
Check that your tables are using the InnoDB engine. When using the MyISAM engine (which was the default until recently), foreign keys declarations are not enforced.
MySQL have different DB Engines -
MyISAM - default, no FK support
InnoDB - have FK support - but no fulltext search like in MyISAM
On both engines you can create table and try to create FK, but MyISAM will simply ignore it.
Also, make sure foreign keys are being enforced. For some reason they weren't on mine, leading to one week of headache!
Check:
SELECT ##FOREIGN_KEY_CHECKS
Set:
SET FOREIGN_KEY_CHECKS=1