How to join two tables in HTSQL specific to any columns - join

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

Related

Entity Framework many-to-many relation with filter in mapping

I'm trying to create a relation using Fluent API, but I can't get it to work. There are 2 tables: SecurityEntities and SecurityEntityRelations. The SecurityEntities table contains users and groups, the SecurityEntityRelation table is an association table that contains relations between the users and groups (it defines which groups contain which members). I already mamaged to relate the 2 entities within EntityTypeConfiguration:
HasMany(se => se.Groups).WithMany(g => g.Members).Map(seg =>
{
seg.MapLeftKey("ser_EntityName");
seg.MapRightKey("ser_MemberOf");
seg.ToTable("ser_SecurityEntityRelations");
});
This populates the SecurityEntity.Groups and Members properties. But one of the security groups is being concidered a "primary" group, which should be stored in a separate SecurityEntity.PrimaryGroup property. In SQL, this is implemented by a column in the SecurityEntityRelation table, which marks the relation as being "primary". (I know, a self-referencing foreign key in the SecurityEntity table would have been a better solution, but this was designed quite some time ago, and we're stuck with this implementation because our legacy code is also written this way) So, now my question is: how can I create an EF mapping like the one above, which specifies the same relation, but only when column "ser_IsPrimaryGroup" equals a fixed value of '1'?

Rails multitable id incrementation

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

changing the database scheme to avoid having multiple database

We have a web portal in our company that is written in asp.net MVC . right now each department have their own database but we want to avoid having multiple database because the database scheme is the same only it has diffrent data inside for each department.like each department has their own projects etc. how the database model should be changed in such a way to avoid having multiple database ?
also we want to share some elments like projects between diffrents department. how it could be done ?
The single database that you have in mind would have to have its table re-designed such that you would be able to tell, for each record in each table, what department the record relates to. Essentially you would need to add a "DeptId" column to each root-level table. By root level table I mean only the parents of foreign key relationships. Eg, if you have an OrderHeader and OrderLines, only the OrderHeader table would need to have this DeptId column. The lines relate to the header, so you won't need to add this column to the lines table as well. Alternatively, if you have a Customer table, such that each customer belongs to only one department, then you would add the DeptId column to this customer table and then you won't need it on the OrderHeader table (since order header should be referencing Customer table)
Those tables/elements that you want to share between databases just would not have the DeptId column added to them.

Join queries in Doctrine on tables without specified relations

I have two tables that do not have a relation defined with each other in the schema.yml.
However, table 1 has a foreign key reference to the primary key of table 2. Clearly, I goofed up by not designing the database well, but now it's mitigation time.
I must do a left join between the two tables coupled with a where clause that will retrieve the select rows I want. And to do this, I do:
Doctrine_Query::create()->select('t.*, l.lid')->from('Taxonomy t')->leftJoin('t.Cid c') ->leftJoin('c.Lesson l')->where('t.section = ?','Critical reading');
This should typically do it, but it does not because what it returns is all the rows from taxonomy table irrespective of the where condition. I am thinking, is this because of the relation not being specified in the column? That would be ridiculous cause the query works, only in a doctrine context it does not.
Thanks
In doctrine you can only join using the relations you defined on your schema, this is a know limitation. You may use the Native SQL feature as a workaround.

How to layout tables and relationships for MVC project

So I am working on an MVC project to put to work the studying I have been doing. I am wrestling with the concept of Database Table relationships and foreign keys. I am working on a simple ecommerce site (displays products, shopping cart, user accounts..etc).
I have the following tables to start out with:
1) Products
2) Categories
I setup the Products and Categories tables to have a ProductId and CategoryId respectively. In my MySQL db, I created a FK on the Products Table to relate to the CategoryId field on the Categories table (I am not sure this was correct to begin).
My expectations for the way the database would handle the table relationship: I didn't want the DB to do anything with the products table if I deleted a category out of the Categories table, or vise versa. The only thing would be that the category field in a Product would be blank (or default) if their category was removed.
Finally, do I have to do anything in my entity classes such as in the Products class, add the ProductId to the Category.ProductId?
Eventually, when I Orders and Users to the project, I can see a relationship where each user -> many orders -> each order has many products -> and each product is in one category.
But I am having a hard time understanding how or if I should be setting up a Foreign key relationship in the two current tables of Products and Categories and if so how to setup my entity class in relation to that FK.
Any advice.
It has been my experience (with L2S) that you don't want to specify any relationships between tables. I have simply done the PK, FK logic myself. This keeps the L2S generated objects simple and is most likely the way you worked with them before in SQL. That, at least, is the case for me.

Resources