Linq To SQL delete a mapping object with dual primary keys - linq-to-sql

In Linq To SQL, how to delete a mapping object with dual primary keys?
If simply Remove() from EntitySet, SubmitChanges() fails

Related

MySQL JDBC driver getGeneratedKeys returning trigger-generated primary key values

In MySQL, I'm using a before-insert trigger to create a value for a primary key column instead of using an auto_increment column (since my logic is more complex).
Is it possible for the MySQL JDBC driver (Connector/J) Statement#getGeneratedKeys() to return the value of the trigger-generated primary key values?
So far I've only seen it returning auto_increment values.

"Segemented" foreign keys in MySQL?

I have a problem with foreign keys in MySQL or probably I am just thinking in the wrong direction... I have an activity log table where I need to reference key values from currently 2 other tables. So I am using a field that contains that foreign key value along with an indicator stating which table that foreign key value is from.
Table activitylog
...
RefID INT NOT NULL,
RefType INT NOT NUL,
...
Table offers
OfferID INT NOT NULL,
...
Table orders
OrderID INT NOT NULL,
...
If the user created an offer, the value of OfferID from table Offers would be writtten to RefID of activity log and RefType is set to 1. If it was an order then the value of OrderID goes into RefID and RefType is set to 2.
Of course I could add an additional field, name it OrderID, rename RefID to OfferID and discard RefType and use these fields. But if in future an new entity will be used I would have to add an additional field holding the key values of the new entity instead of just invent RefType 3 and continue having the key values in RefID.
I am now struggling with the definition of the foreing key constraints. The logic would be if RefType = 1 lookup the key in Offers, if RefType = 2 go into Orders.
Does anybody know if there is a way to achieve my current concept or do I have to add additional fields to the activitylog?
No. MySQL doesn't support enforcement of FOREIGN KEY constraints like you explain, a single column referencing multiple tables.
You could define the constraints with the MyISAM engine, but the FK constraints wouldn't be enforced.
If you define the FK constraints for tables using the InnoDB engine, then ALL of the foreign key constraints would be enforced, no matter what values are stored in other columns.
To have FK constraints on a table to reference two (or more) different, independent parent tables, you'd need two (or more) foreign keys columns, one for each table.
With your table design with InnoDB, you'd have to forgo declarative FOREIGN KEY constraints.
It might be possible for you to roll-your-own constraints by writing some messy triggers; have the trigger throw an exception when one of your constraint rules is violated.

Foreign Key Constraints on an Intersection Table

I am trying to import data from an Excel document to MySQL Management Studio, and when attempting to do so, the data fails to import on a specific intersection table, which has the following data:
The table I am inserting to is called TYearLeagues, and the error I receive states that:
An OLE DB record is available. Source: "Microsoft SQL Server Native Client 11.0" Hresult: 0x80004005 Description: "The INSERT statement conflicted with the FOREIGN KEY constraint "TYearLeagues_TYears_FK". The conflict occurred in database "dbSQL2", table "dbo.TYears", column "intYearID"."
The intersection table is set up like so:
CREATE TABLE TYearLeagues
(
intYearID INTEGER NOT NULL
,intLeagueID INTEGER NOT NULL
,CONSTRAINT TYearLeagues_PK PRIMARY KEY (intYearID, intLeagueID)
)
And the foreign key constraint like so:
ALTER TABLE TYearLeagues ADD CONSTRAINT TYearLeagues_TYears_FK
FOREIGN KEY ( intYearID ) REFERENCES TYears( intYearID )
Whereas I could understand an issue in a normal table, as TYearLeagues is an intersection table, and there are no duplicate records, I do not understand the issue.
EDIT: Added TYears table, for better clarity. At the very least, intYearIDs 1-3 are accounted for.
I don't think it's complaining about duplicates, it means you are inserting a row that refers to a year that does not exist in TYears

MySql Script to temporarily change Foreign Key Constraints to Cascade then back

I need to make changes to a complex DB with lots of “RESTRICTED” constraints.
Specifically, I need to renumber Id fields in a table UserInfo to which many other tables have Foreign Keys of “RESTICT” on Update and Delete.
I would like to create a script that:
Finds all the tables with constraints (Foreign Keys) to the “Id” field of “UserInfo” table
Loads the constraints (Foreign Keys) into a variable
Alter the dependent table(s) to “Drop” the Foreign Key
Alter the dependent table(s) to Add a the Foreign Key with “Cascade” on Delete and Update
… I will insert the update query here …
Alter the dependent table(s) to “Drop” the Foreign Key
Alter the dependent table(s) to Add back the original Foreign Key
I just can't figure out how to do step #2, loading the constraint into a variable.
I searched all through the information_schema DB and found the constraint names (i.e. userprefs_ibfk_1). But nowhere does it define what type of constraints they are (i.e. “RESTRICT on Delete”). Is there some secrect code to determining what type of constraint they are?
I might be able to use SHOW CREATE TABLE UserInfo but I cannot find a way to load the result into a variable (on which I can perform string functions).
I would greatly appreciate any help.
You were close, you need to check referential_constraints.update_rule or delete_rule:
SELECT constraint_name
FROM information_schema.referential_constraints
WHERE
referenced_table_name = 'UserInfo'
AND ( update_rule <> 'CASCADE' OR delete_rule <> 'CASCADE' )
You can wrap this query in a stored procedure, iterate over the results with a cursor, and build and execute prepared statements from it.

How do you model disjoint relationships between two relations in MySQL Workbench?

You have the option to uncheck Mandatory in the foreign key tab of the relationship window, but that doesn't fully capture the meaning of a disjoint relationships, which is an EITHER-OR relationship between multiple relations.
Your referring to the mandatory property of the foreign key makes me believe you are either misunderstanding the meaning of a disjoint relationship, or implementing it with a relation in the wrong "direction".
Let's say we want to implement the following schema:
class: Staff Member
class: Permanent (specialises Staff Member)
class: Temporary (specialises Staff Member)
a Staff Member is either a Permanent employee or a Temporary contractor
A corresponding EER schema would be (MySQL syntax):
CREATE TABLE staff_member (
id INT PRIMARY KEY,
name VARCHAR(20) NOT NULL
);
CREATE TABLE permanent (
id INT PRIMARY KEY,
next_appraisal DATETIME NOT NULL,
FOREIGN KEY (id) REFERENCES staff_member(id)
);
CREATE TABLE temporary (
id INT PRIMARY KEY,
contract_end DATETIME NOT NULL,
FOREIGN KEY (id) REFERENCES staff_member(id)
);
Notice the foreign key is from the specialised entity to the parent entity (id being the primary key, it is also always mandatory by definition).
This still doesn't answer your question. How to model the disjoint property of this relationship? You cannot do this easily (neither can you model that a specialisation is complete, by the way).
Many RDBMS support the use of CHECK constraints in order to enforce these extra conditions, but MySQL does not (beware, the syntax is accepted by the MySQL parser, but the declaration is ignored). However, simple workarounds exist that result in the same effect.