HTSQL works when two tables are properly linked with foreign key.
For example:
school can be easily joined with department if we have properly defined a foreign key say school_code of department table referencing code of school.
So far I'm not able to find any way to join two tables on some other columns.
Can any one help me how to do this in HTSQL?
The tweak.override extension allows you to define foreign-keys in a config file even if they're not present in the database itself. http://htsql.org/doc/admin/usage.html#extension-reference
tweak.override:
foreign-keys:
- program(school_code) -> school(code)
- program(school_code, part_of_code) -> program
I have a rails application using Postgres - I need to have some tables that have the same data in essence. So I need to have a unique key that increments for all my tables, so there is virtually a pk constraint over all the table's ids.
Now, the question is - how do I do it? can I write a migration that defines the id of all the tables to increment for each insert to any of those tables? or must I do it on the database level?
If you have such a link between two tables, you shouldn't make them have the same primary key. This is not a good use of a database.
You should instead give one of these tables a foreign key to the other one, and use this relation to identify the linked rows between the two tables.
In Rails, it's called a "has_one" relation, and it's very handy : http://guides.rubyonrails.org/association_basics.html#the-has-one-association
i have tables witch have no foreign key and no relation in doctrine
but logically they are related by some fields.
how can i join them with DQL and Query over them.
can i use ON between their fields?
as I found here:
Custom left join with unrelated models:
This is unfortunately not possible with the Doctrine 1 ORM. I think you would be best served to work directly with the database in this case.
I'm using MS SQL Server 2008R2, but I believe this is database agnostic.
I'm redesigning some of my sql structure, and I'm looking for the best way to set up 1 to many relationships.
I have 3 tables, Companies, Suppliers and Utilities, any of these can have a 1 to many relationship with another table called VanInfo.
A van info record can either belong to a company, supplier or utility.
I originally had a company_id in the VanInfo table that pointed to the company table, but then when I added suppliers, they needed vaninfo records as well, so I added another column in VanInfo for supplier_id, and set a constraint that either supplier_id or company_id was set and the other was null.
Now I've added Utilities, and now they need access to the VanInfo table, and I'm realizing that this is not the optimum structure.
What would be the proper way of setting up these relationships? Or should I just continue adding foreign keys to the VanInfo table? or set up some sort of cross reference table.
The application isn't technically live yet, but I want to make sure that this is set up using the best possible practices.
UPDATE:
Thank you for all the quick responses.
I've read all the suggestions, checked out all the links. My main criteria is something that would be easy to modify and maintain as clients requirements always tend to change without a lot of notice. After studying, research and planning, I'm thinking it is best to go with a cross reference table of sorts named Organizations, and 1 to 1 relationships between Companies/Utilities/Suppliers and the Organizations table, allowing a clean relationship to the Vaninfo table. This is going to be easy to maintain and still properly model my business objects.
With your example I would always go for 'some sort of cross reference table' - adding columns to the VanInfo table smells.
Ultimately you'll have more joins in your SP's but I think the overhead is worth it.
When you design a database you should not think about where the primary/foreign key goes because those are concepts that doesn’t belong to the design stage. I know it sound weird but you should not think about tables as well ! (you could implement your E/R model using XML/Files/Whatever
Sticking to E/R relationship design you should just indentify your entity (in your case Company/supplier/utilities/vanInfo) and then think about what kind of relationship there is between them(if there are any). For example you said the company can have one or more VanInfo but the Van Info can belong only to one Company. We are talking about a one – to- many relationship as you have already guessed. At this point when you “convert” you design model (a one-to many relationship) to a Database table you will know where to put the keys/ foreign keys. In the case of a one-to-Many relationship the foreign key should go to the “Many” side. In this case the van info will have a foreign keys to company (so the vaninfo table will contain the company id) . You have to follow this way for all the others tables
Have a look at the link below:
https://homepages.westminster.org.uk/it_new/BTEC%20Development/Advanced/Advanced%20Data%20Handling/ERdiagrams/build.htm
Consider making Com, Sup and Util PKs a GUID, this should be enough to solve the problem. However this sutiation may be a good indicator of poor database design, but to propose a different solution one should know more broad database context, i.e. that you are trying to achive. To me this seems like a VanInfo should be just a separate entity for each of the tables (yes, exact duplicate like Com_VanInfo, Sup_VanInfo etc), unless VanInfo isn't shared between this entities (then relationships should be inverted, i.e. Com, Sup and Util should contain FK for VanInfo).
Your database basically need normalization and I think you're database should be on its fifth normal form where you have two tables linked by one table. Please see this article, this will help you:
http://en.wikipedia.org/wiki/Fifth_normal_form
You may also want to see this, database normalization:
http://en.wikipedia.org/wiki/Database_normalization
Hi Im currently working with .sdf database (Server Compact Version 4.0) and sql express. I'm trying to setup a cascade delete on a same table (category - sub category) but I get that I cant add relation to the same table.
A foreign key constraint had and
update or a delete cascade rule, and
self-references a column in the same
table, is not allowed
What can I do about this?
EDIT
I'm the only one with this problem?
As your SQLException suggested, this is a limitation of SQL Server in general and has nothing to do with EF or Code First. Basically, SQL Server does not allow creating cascade actions on Inner relationships – when the cascade path goes from column col1 in table A to column col2 also in table A. A->A.
In fact, Code First was trying to use Declarative Referential Integrity (DRI) to enforce cascade deletes and SQL Server throws.
The only way to enforce cascade deletes for this relationship is to use Triggers. You can write a Delete Trigger on the category table that either deletes the dependent rows or sets all corresponding foreign keys to NULL (based on your requirements).