I have a Composite Key (field1, field2 and field3).
I have a non primary key (field4, field5, field6 and field7) also.
FindById which will work for Composite Key If I have all 3 values. Incase I have only one value like I know field1 value then how to use the findById method?
Related
I am creating entities and am trying to create composite keys as my primary keys in the data modeler.
However I am not able to do that as I dont know how to. The question is how do I create a composite key in the data modeler.
Here is the snapshot:
here is the composite keys I am trying to create.
Primary key TBD_ID + TBA_STAGE + TBA_ASSIGNEEPNO + TBA_STATUS combination column, Foreign Key
TBD_ID
TBD_ID is the foreign key from T_BPM_Details and I want to add that to T_BPM_Assignee with the following colums to make a composite key as the primary key.
How do I do that
Just toggle the PK checkbox for each of the columns.
Then if you preview the DDL generated, you can see -
CREATE TABLE composite_keys (
key1 INTEGER NOT NULL,
key2 INTEGER NOT NULL,
key3 INTEGER NOT NULL,
description VARCHAR2(256 BYTE)
)
LOGGING;
ALTER TABLE composite_keys
ADD CONSTRAINT composite_keys_pk PRIMARY KEY ( key1,
key2,
key3 );
Cassandra database
It seems that there is no way to order on anything but primary keys.
I have two columns: ID and timestamp.
I only want ONE(!) row per ID, but I want to filter (basically same as sort?) my results based on timestamp.
I want to run this command:
SELECT id, timestamp FROM " + TableName + " WHERE timestamp < ? ALLOW FILTERING;
How can I do this while making sure I have only a single row per id (which is not possible if my primary key consists of both (ID and timestamp)
I have yet to thoroughly test this method. But I think that I've found a workaround.
I will allow timestamp to be part of the primary key
CREATE TABLE IF NOT EXISTS " + TableName + " ( key blob, timestamp bigint, PRIMARY KEY( key, timestamp )
But before calling UPDATE/PUT I will delete all rows
WHERE id = ?
Such that it will replace the current timestamp for that ID. In that way I should still be able to sort on timestamp, while preserving one row per ID.
Using cassandra and spark and datastax's spark-cassandra-connector.
In the spark-cassandra-connector it support gives such filter example:
sc.cassandraTable("test", "cars").select("id", "model").where("color = ?", "black").toArray.foreach(println)
Basically it filters the color column with black. However, can I filter the row based on a range? Like I want to filter the range column which is a long type and the range falls between 100000 and 200000 ? Does cql support such a range filter?
CQL supports range queries only on clustering columns. Range queries can be expressed as in SQL by using two bounding conditions on the same field, for instance, in spark-cassandra-connector you will write:
.where("my_long >= ? and my_long < ?", 1L, 100L)
This will work as long as the "my_long" column is the first clustering column. Clustering columns are the columns that follows the declaration of the partition columns in the primary key.
For instance, you can run range queries on my_long column if the primary key is declared as follows:
PRIMARY KEY (pk1, my_long)
PRIMARY KEY (pk1, my_long, pk3)
PRIMARY KEY ((pk1, pk2), my_long)
PRIMARY KEY ((pk1, pk2), my_long, pk4)
...
As you see, in all the preceding cases, my_long follows the declaration of partition key in the primary key.
If the column belongs to the clustering columns but it's not the first one, you have to provide an equality condition for all preceding columns.
For example:
PRIMARY KEY (pk1, pk2, my_long) --> .where("pk2=? and my_long>? and my_long
PRIMARY KEY (pk1, pk2, pk3, my_long) --> .where("pk2=? and pk3=? and my_long>? and my_long
PRIMARY KEY ((pk1, pk2), pk3, my_long) --> .where("pk3=? and my_long>? and my_long
PRIMARY KEY ((pk1, pk2), pk3, my_long, pk5) --> .where("pk3=? and my_long>? and my_long
Note: spark-cassandra-connector adds by default the clause "ALLOW FILTERING" in all the queries. If you try to run the examples above in cqlsh, you have to specify that clause manually.
I have a table with three columns, [Id,QTY,Date]. out of these three, two columns [id and date], should be set as primary keys, because I need to fetch the record one by one, from this table, into a reference.
the data to be inserted into this table is
101,10,NULL
101,20,201220
101,7,201440
102,5,null
102,8,201352
date is in yyyyww format
How do I define two columns as composite primary keys when they have null values, duplicates?
alter table abc add constraint pk primary key (ID, DATE);
if I try to alter the table the error appears
Error report:
SQL Error: ORA-01449: column contains NULL values; cannot alter to NOT NULL
01449. 00000 - "column contains NULL values; cannot alter to NOT NULL"
*Cause:
*Action:
Using table level constraint, you can use this query
alter table your_table add constraint pkc_Name primary key (column1, column2)
but first you need to declare the columns NOT NULL. All parts of a primary key need to be NOT NULL.
The column name of your table is ID and it is still null and non-unique, how is it possible. If it is primary key of other table try adding a surrogate key column for this table and make it primary key.
In case of composite primary key, it should have atleast one not null value(For each row) in the combination of columns. And the combination of column must be unique at all case.
For further details check, http://docs.oracle.com/cd/B10500_01/server.920/a96524/c22integ.htm
Correction - If composite primary key is made up of 3 columns, then no column (among 3) can hold NULL value. And the combination of those 3 columns must be unique.
E.g. (1,2,2)
(1,2,1)
(2,2,1)
(1,2,2) - not valid
Say I have these table in an Oracle database
Person:
PersonId <- PK ID (int, from sequence)
PersonFirstNameID <-- int
PersonLastNameID <-- int
PersonSecurityID <-- int
PersonDetails
PersonFirstNameID -CompositeKey
PersonLastNameID -CompositeKey
PersonSecurityID -CompositeKey
PersonDetailKey
PersonDetailValue
PersonDetailRisk
Now I want to model the one to many relation from Person to PersonDetails in NHibernate.
How can I do this? I've tried setting up a component representing the composite Id and feeding this into the one to many via the property ref however this generate SQL with duplicate columns and throws the following:
System.ArgumentException: Identifier type mismatch; Found: Expected:
The NHibernate documentation talks only about doing this when the composite Id is the same on both..
Yes... Its not my DB schema, its a legacy DB and access is very limited.
Not quite clear.
For a foreign key relationship to work, the child must reference the primary key of the parent, so the data structure suggests that the primary key of person is FirstNameId/lastNameid/securityid (and hence your best move would be to ignore the personid column).
Is the Person_Id actually the primary key (defined as a PK in the DB), or is the database's version of PERSON primary key actually FirstNameId/lastNameid/securityid ?