I wanted to make sure there were no duplicate timestamps in my table but then I found that it may be problematic if I simply use UNIQUE, because right now my table has:
_ID field (autoincrement)
Account ID (integer, links to an account table)
Category ID (integer, links to a category table)
Value (the value of this category for this account)
Timestamp (the timestamp of this value of this category for this account)
Is there a way to designate the timestamp field as unique within the context of the account ID and category ID? As in, it should not be possible to put in two values for a single timestamp, with respect to account and category. But the timestamp may show up multiple times in the table as a whole, either because it corresponds to other Categories and/or Accounts.
you can create a trigger and check whatever constraint you need:
#Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
database.execSQL("CREATE TRIGGER UNIQUE_TIMESTAMP_TRIGGER " +
"AFTER INSERT ON MY_TABLE FOR EACH ROW " +
"BEGIN " +
" SELECT RAISE(ABORT, 'Timestamp not unique') WHERE " +
" EXISTS(SELECT TIMESTAMP MY_TABLE T2 " +
" WHERE T2.TIMESTAMP = NEW.TIMESTAMP AND T2.ACCOUNT_ID = NEW.ACCOUNT_ID) " +
"END; ");
}
Related
I'm trying to create some type of update endpoint that when you hit it does an insert from one table to another table. Basically it takes from a table named funds which was fundId, reportingfrequency and country. IT retains the fundIds and for the value it gets a List of Dates that are a result of a join with a Postgres stored procedure/function. Here is an example of the SQL query in it's entirety. So ideally if there are 100 rows in the table it would insert all 100 rows with an array of dates into the due_dates table.
INSERT INTO due_dates (fund_id, listdate)
SELECT fundid,
(
SELECT array_agg(weekdays::date)
FROM generate_series(date'2021-01-01', date'2021-12-31', interval '1' day) as t(weekdays)
LEFT JOIN holidays.poland(2021, 2021) f ON (weekdays = f.datestamp)
WHERE f.datestamp IS NULL
AND extract(dow from weekdays) BETWEEN 1 AND 5
)
FROM funds
WHERE reportingfrequency = 'Daily';
Now my issue is I'm not sure how to... programmatically do this so that depending on the rows from the funds table... I'm not sure how to grab the country field from the row to do a specific stored procedure call. For instance if a single row has Poland in it's country field then it would ideally call holidays.poland(2021, 2021)... and if the country was USA it would call holidays.usa(2021, 2021). Here's how my current NativeQuery.
entityManager.createNativeQuery("INSERT INTO due_dates (fund_id, listdate)" +
"SELECT fundid," +
"(" +
"SELECT array_agg(weekdays::date)" +
"FROM generate_series(date'2021-01-01', date'2021-12-31', interval '1' day) as t(weekdays)" +
"LEFT JOIN holidays." + country + "(" + year + "," + year + ") f ON (weekdays = f.datestamp)" +
"WHERE f.datestamp IS NULL" +
"AND extract(dow from weekdays) BETWEEN 1 AND 5" +
")" +
"FROM funds" +
"WHERE reportingfrequency = 'Daily'; ")
.executeUpdate();
Is there something I need to do to tweak the original SQL statement before I can achieve what I want to do?
I have a problem with a SQL statement. I have a java app with a button to add a column into a database. For every new date, I have a new column created, which is done using the following query
final String queryCreate = "alter table Currency add '"+ newColumn + "' decimal ";
When I try to populate the column with data using the following query:
final String queryAdd = "insert into Currency( '" + newColumn + "' ) values(1.95583)";
The data is added below the last row of the previous column.
like this:
https://postimg.org/image/579gjmyzj/
My question is why the insert statement does what it does in my situation, what am I doing wrong?
INSERT creates new records, if you want to modify existing records you need to use UPDATE.
For example, to modify the first record:
"UPDATE Currency SET " + newColumn + " = 1.95583 WHERE Currency_ID = 1"
use update query
assuming strCurrencyID="1";
strCurrencyName="EUR";
final String queryAddUpdate =
if exists(Select Top 1 1 from Currency where Currency_ID=" + strCurrencyID +")
Begin update Currency set " + newColumn + "=1.95583 where Currency_ID=" +strCurrencyID + "
End
Else
Begin
insert into Currency(Currency_id, Currency_name, '" + newColumn + "' ) values("+ strCurrencyID +",'" + strCurrencyName + ", 1.95583)
End"
This will update the value in in column if currency id exists if not this will insert new row.
But I think database design should be change can you explain your business requirement.
I need to search multiple columns in a table for the same value; ie. find all rows that have Acme in either company name, name, or email column.
I need to search multiple columns in a table for the same value. IE find all rows that have Acme in either company name, name or email column.
Sure. You can use QueryBuilder to use the same text to query multiple fields. Maybe something like:
QueryBuilder<Foo, Long> qb = dao.queryBuilder();
Where<Foo, Long> where = qb.where();
String name = "acme";
where.like(Foo.FIELD_COMPANY, "%" + name + "%");
where.or();
where.like(Foo.FIELD_NAME, "%" + name + "%");
where.or();
where.like(Foo.FIELD_EMAIL, "%" + name + "%");
List<Foo> results = where.list();
Here are more docs about the query-builder.
I am using org.apache.hadoop.hbase library to query hbase.
In h-base I have following:
table name : Employees
in this table I have following fields:
emp_id,name, age, designation, joining_date
how can I find designation of employee if I have his joining_date and name only.
I am using something like:
public static void getOneRecord (Configuration conf, String tableName, String rowKey, String rowKey2) throws IOException{
HTable table = new HTable(conf, tableName);
Get get = new Get(rowKey.getBytes());
Result rs = table.get(get);
for(KeyValue kv : rs.raw()){
System.out.print(new String(kv.getRow()) + " " );
System.out.print(new String(kv.getFamily()) + ":" );
System.out.print(new String(kv.getQualifier()) + " " );
System.out.print(kv.getTimestamp() + " " );
System.out.println(new String(kv.getValue()));
}
}
how can I extend it to 2 row keys.
Note: It is guaranteed that only one value will be returned by this pair query.
You want to get a record based on the columns which are not row keys. Please confirm this. If yes, following are the options
Good row key design for performance. If you would be querying more based on a column other than row key, you can consider looking at modifying row key design to append that column as part of row key. Then you can use get or scan with row key range, row filter or FuzzyRowFilter
Secondary index is one option.
Use filters(in your case two SingleColumnValueFilter) and add them as part of scan.here
I am building an android app project with SQLite DB.
I got stuck on One-To-Many RelationShip.
This is One
private static final String createTableOrders =
"CREATE TABLE " + TABLE_ORDER + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
...
+ KEY_COLUMN_FORMATS + " INTEGER REFERENCES " + TABLE_FORMATS + "(" + KEY_ID + ")"
+ ");";
This is Many
private static final String createTableFormats =
"CREATE TABLE " + TABLE_FORMATS + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
...
+ ");";
My problems is with set/get methods.
Lets say that I would need to get list of ids of all Formats that are in one Order.
I guess same thing goes for set method.
I tried to find this kind of question on SO but most of the question were just for SQL part.
P.S. Spent 3 hours trying to make it by myself but the only thing I got to was using GSON to code list of ids into String but it was just a dead-end.
EDIT: I need to get that information inside the code.
P.S.S I have been doing this for 18 hours so sorry if this is uber-stupid question.
A one-to-many relationship requires the foreign key column in the "many" table:
CREATE TABLE Orders (
ID PRIMARY KEY
);
CREATE TABLE Formats (
ID PRIMARY KEY,
OrderID REFERENCES Orders(ID)
);
To get all formats belonging to an order, you just look up rows with the ID of that order:
SELECT * FROM Formats WHERE OrderID = ?
In Jave:
Cursor cursor = db.query(TABLE_FORMATS,
new String[] { whatever columns you need },
"OrderID = " + orderID,
null, null, null, null, null);
while (cursor.moveToNext()) {
// read one order from the cursor
}
cursor.close();