i search the web for answer, but all the answers refer to composite id PK.
i want two map two columns of type long to PK.
one should be regular generated id, and the other should be regular long field.
i have the following mapping:
<class name="com.company.MyTable" table="My_Table">
<id name="id" column="id">
<generator class="assigned"/>
</id>
<property name="jobId" column="job_id" type="long" index="oes_job_id_idx" />
<property name="serverId" column="server_id" type="long"/>
</class>
i want to add to the PK the job_id column.
how do i do that?
Primary keys, by definition should be the unique key with the fewest columns possible:
your can't have multiple primary keys
you shouldn't use an additional column for the primary key, if the first column is already unique
It doesn't really give you a benefit to create a separate index either - so stick to the generated field as primary key. So, hibernate doesn't support that because it is a wrong thing to do.
Please look at this questions there is a full solution for composite Primary keys:
Mapping same class relation
And then
Mapping same class relation - continuation
Hope it helps.
Related
I need to alter a table to modify the order of the indexes created from the composite key for the below mentioned changeset.
<changeSet author="demo (generated)" id="demo-11">
<createTable tableName="customersalesdata">
<column name="id" type="BIGINT">
<constraints unique="true" primaryKey="true" primaryKeyName="customersalesdata_pkey"/>
</column>
<column name="customerid" type="NVARCHAR(255)">
<constraints primaryKey="true" unique="true" primaryKeyName="customersalesdata_pkey"/>
</column> </createTable>
</changeSet>
The reason for altering is that ordering of the columns in an index makes a big difference. Since the customerid is the second column, it will not be used. The query is performing an index scan because of this. Since the table has two indexes that start with id, having the id, customerid in this order is a waste (in most cases).
So I need to change the column order to customerid and id. And the another problem is customerid which is the composite key, is referred as the foreign key in the another table.
My question is should I need to drop FK first and then drop Composite Keys and then form the composite key in the order as shown below
<changeSet id="2">
<addPrimaryKey columnNames="customerid, id"
constraintName="customersalesdata_pkey"
tableName="customersalesdata"
validate="true"/>
</changeSet>
Or just create a another index on top of composite key by combining both of the fields as shown below
<changeSet author="demo" id="demo-id">
<createIndex tableName="customersalesdata" indexName="idxn_customer_id_id">
<column name="customer_id"/>
<column name="id"/>
</createIndex>
</changeSet>
Also in both the cases will there be any chances of data loss? Can you please suggest the best approach here.
A similar question was asked a while back on this post.
I'll paraphrase the two most popular answers from that thread.
#1
You can read the Liquibase Documentation and there is also a similar problem to reference here. In the case of the situation presented in "Adding composite unique constraint in Liquibase" linked above, the solution is
<changeSet author="liquibase-docs" id="addUniqueConstraint->example">
<addUniqueConstraint
columnNames="product_id, tournament_id"
constraintName="your_constraint_name"
tableName="person"
/>
</changeSet>
#2
I am pretty certain that:
#1 You can't do it inside the createTable tag itself, but you can do it within the same changeset as when the table is created.
#1 It does create a composite unique constraint on the two columns. One way you can check is to run liquibase with the command to generate the SQL for an update rather than running the update command and checking what it does for your database. On the command line, rather than running liquibase update, you would run liquibase updateSQL.
When my instructor taught my SQL class about 2NF, they mentioned that it's violated if there's partial dependency - that is, when a table has a composite key and a non-key column depends only on one of the keys, and not all of the columns that comprise the PK.
If there's an entity with a single-columned PK, and there's a non-key attribute that does not depend on this PK, does it mean that it's in 2NF because the entity does not have a composite key and partial dependency is not possible, and would therefore never be violated (an attribute is only either dependent or not dependent on the PK)?
Thank you!
I'm certainly not an expert on this subject but to quote GeeksforGeeks:
"Second Normal Form applies to relations with composite keys, that is, relations with a primary key composed of two or more attributes. A relation with a single-attribute primary key is automatically in at least 2NF." (https://www.geeksforgeeks.org/second-normal-form-2nf/)
So, at least according to them, the answer is yes.
Is it possible to create a composite uniqueKey in schema.xml? Or is it better to concatenate the unique fields into one unique string id field in the source data?
If it's possible, and if it's not that big of a difference, I would prefer to do the former because it would save me a bit of time.
As discussed here How to set multiple fields as uniqueKey in solr? or there http://lucene.472066.n3.nabble.com/Multiple-uniqueKey-fields-td472939.html u cannot simply add multiple fields. Ud need to combine them into one field, but this can not be a multivalued field.
I have two tables:
"Projects", that have three (3) field. One composite key of two (2) fields: Donor_Source & Project_Number and Project title
Please note that Donor_Source field is indexed as Yes(Duplicates OK) and Project_Number field is indexed as Yes(No Duplicates).
It has to be this way because a donor can support multiple projects.
Lastly there is also the PRF_Table, it has many fields but since I want to relate it to the Project table, I made two fields that are used as foreign keys of Projects table:
Please note that both fields of the foreign key are indexed as: NO.
As I was trying to relate the two tables, I managed to relate of project field from both tables but could not relate the donor source field of both tables:
As can be seen from the picture above, I managed to get many:1 relation between PRF_Table & Project, which is correct. PRF_Table can have many records on a specific project, but that project is listed only once in the Project table
The problem rises when trying to relate the Donor_Source field: I always get indeterminate relation (something that I want to avoid). I guess the problem might be because the Donor_Source field in the Project table, although indexed, it still can have duplicates and it of course has duplicates in the PRF_Table.
What should I do in order to get many:1 relation (PRF_Table:Projects)?
All fields in a compound key must be addressed to create referential integrity.
Thus, you must:
Create field Agrmnt_ID in PRF_Table and include that in the relation to the junction table.
Include field Donor_Source in PRF_Table in the relation to table Projects.
You are not required to create a field for Agrmnt_ID in your PRF_Table to have referential integrity. What you are doing so far between PRF_Table and PRF-PO_Junction_Table is fine.
Regarding the link between Projects and PRF_Table, it appears that your intentions are for each record in Projects to be able to relate to multiple records in PRF_Table. If so, then your solution is to change your Primary Key in Projects and consequently your relationship between the two tables.
In table Projects, remove your current composite Primary Key and create a single AutoNumber field (i.e. named ProjectID) as your Primary Key.
Now, in the Projects table, create your unique index on the Donor_Source and Project_Number fields (a composite, unique index), which will give you the same effect as your current composite Primary Key scenario, each Donor can be on multiple Projects, but the same Donor can't be on the same Project more than once.
Now, you will create the same field in PRF_Table that you created as your new Primary Key in Projects from step 1 (i.e. ProjectID)
Create your relationship between your new Primary Key in Projects and your new field in PRF_Table. This will allow every Project/Donor record in Projects to have multiple records in PRF_Table.
Composite Primary Keys are most useful in junction tables, like how you are using one with PRF-PO_Junction_Table. However, in any other link, you want try and have a single Primary Key field and use just a unique composite index to enforce uniqueness in two or more fields.
What's the term describing the relationship between tables that share a common primary key?
Here's an example:
Table 1
property(property_id, property_location, property_price, ...);
Table 2
flat(property_id, flat_floor, flat_bedroom_count, ...);
What you have looks like table inheritance. If your table structure is that all flat records represent a single property but not all property records refer to a flat, then that's table inheritance. It's a way of modeling something close to object-oriented relationships (in other words, flat inherits from property) in a relational database.
If I understand your example correctly, the data modeling term is Supertype/Subtype. This is a modeling technique where you define a root table (the supertype) containing common attributes, and one or more referencing tables (subtypes) that contain varying attributes based on the entities being modeled.
For example, you could have a Person table (the supertype) containing columns for attributes pertaining to all people, such as Name. You could then have an Employee table (the subtype) containing attributes specific to employees only, such as rate of pay and hire date. You could then continue this process with additional tables for other specializations of Person, such as Contractor. Each of the subtype tables would have a PersonID key column, which could be the primary key of the subtype table, as well as a foreign key referencing the Person table.
For additional info, search Google for "supertype and subtype entities", and see the links below.
http://www.learndatamodeling.com/dm_super_type.htm
http://technet.microsoft.com/en-us/library/cc505839.aspx
There isn't a good name for this relationship in common database terminology (as far as I know). It's not a one-to-one relationship because there isn't guaranteed to be a record in the "extending" table for each record in the main table. It's not a one-to-many relationship because there a maximum of one record allowed on what would otherwise be the "many" side of the relationship.
The best I can do is a one-to-one-or-none or a one-to-one-at-most relationship. (I will admit to sloppy terminology myself — I just call it a one-to-one relationship.)
Whatever you decide to call it, you can model it properly and maintain integrity in your database by making the property_id column in property a PK and the property_id column in flat a PK and also an FK back to property.
"Logic and Databases" advances the term "at most one to at most one" for this kind of relationship. (Note that it is insane to assign names to tables on account of which relationships they participate in.)
Beware of the people who have suggested things like "foreign key", "table inheritance", brief, all the other answers given here. Those people are making assumptions that you have not explicitly stated to be valid, namely that one of your two tables will be guaranteed to contain all key values that appear in the other.
(Disfunctionality of the site prevents me from adding this as a comment in the proper place.)
"How would you interpret "...that share a common primary key?" "
I interpret that in the only reasonable sense possible: that within table1, attribute values for the primary key are guaranteed to be unique, and that within table2, attribute values for the primary key are guaranteed to be unique. And that furthermore, the primary key in both tables has the same [set of] attribute names, and that the types corresponding to the primary key attribute[s] are also pairwise the same. Nothing more and nothing less.
In particular, "sharing a primary key" means "having a primary key in common", and that means in turn "having a certain 'internal uniqueness rule' in common", but that commonality guarantees in no way that a primary key value appearing in one table must also appear in the second table.
"Can you give an example involving two tables with shared primary keys where one table wouldn't contain all the key values that appear in the other?" "
Table1: column A of type integer, primary key A
Table2: column A of type integer, primary key A
Rows in table1: {A:1}. Satisfies the primary key for table1.
Rows in table2: {A:2}. Satisfies the primary key for table2.
Convinced ?
"Foreign key"?