Appending data to an indexed table - ms-access

I'm attempting to append records to a table that has data indexed by an ID column. I'd like the appended data to continue that indexing and attach the next number to the first appended record and so on. However, that ID column is not an AutoNumber column--the first number needed to be 5001 so it's current Data Type is "Number". The data already in that table is entered via a form with this VBA to format the ID column:
If Nz(Me.ID, "") = "" Then
NewID = Int(DMax("ID", "tComplianceAll") + 1)
Else
NewID = Me.ID
End If
I currently have an append query to try to append the new data to the table with this SQL for that ID column : Int(DMax("ID","tComplianceAll")+1) AS Expr1
That, however, only works for the first record. The rest do not get appended due to key violations since it's trying to assign the same ID number for all appended records. Is there a way to change that SQL so that it properly indexes the newly appended data?

You surely can use a standard Autonumber ID value here!
Edit table tComplianceAll and change the ID column type to AutoNumber
Then, set the next value using this SQL:
ALTER TABLE tComplianceAll ALTER COLUMN ID AUTOINCREMENT(5001,1)
Actually, you would change 5001 to the latest value returned by (DMax("ID", "tComplianceAll") + 1)
That should do it, providing that tComplianceAll is not in any relationships with other foreign tables using the ID field.

Related

How to uniquely identify all rows in a MySQL table without a primary key?

I use MySQL (in NodeJS). I have a table with unknown data.
I present the data as an HTML table by looping out the items. Then I add a click event to the cells. When that happens I want to load the data from the current cell.
How can I do that?
What I could do (but don't want to)
I could let the user add a custom_identifier to specify that in this table, use the id as identifier.
/*
From click event. I click on the cell with `column slug` with `id 3`.
cell = {
identifier: 3,
column: slug,
};
*/
const custom_identifier = "id";
const sql = `SELECT ${cell.column} FROM WHERE ${custom_identifier} = ${cell.identifier}`;
Because this approach requires the user to specify the identifier for each table, I don't want to use this approach.
Problem
When making SQL queries and wanting a particular row, we need a WHERE statement and an identifier to get the correct row. Some tables don't have an id or even a unique identifier at all.
Question
How can I get the data from a row where I don't know an id?
Ideas
Is there a kind of index I could use that is not bound to a column name?
I can add more stuff to my data attributes in the HTML if needed, index or something else. I just don't want to put the load on the user.
You could use this technique to add a row number to each row. Here's an example of it being used to generate an ID column with incrementing integer values starting from zero:
SELECT #idvar := #idvar + 1 AS id,
tbl.*
FROM tbl,
(SELECT #idvar := -1) alias;
As the order of results from a SELECT isn't guaranteed, you may need to use this to create either a temporary table or a non-temporary table that is later deleted - e.g:
CREATE TABLE new_tbl AS
SELECT #idvar := #idvar + 1 AS id,
tbl.*
FROM tbl,
(SELECT #idvar := -1) alias;
Note: You'll get a "Duplicate column name 'id'" error if id is already the name of an existing column in the table. So unless you know this can't be the case it could be worth going with a more obscure column alias name, e.g. id_zyxwv or some other gobbledygook.
See working demo here: https://rextester.com/DVRGNM12570

Copying records from mysql to pgsql with serial id field

I have a web-based database application that currently uses a mysql database. We're moving from mysql to pgsql so we're moving data from the mysql db to the new pgsql db. In the data there are header records in one table, detail records in another table. The header records have a serial field for a record id and the detail records have a numeric field that holds the header record id to tie the detail record to the header record. Since the application that uses this data relies on the database to generate record ids when records are created, that same structure exists in the target pgsql database. How can data be copied from the mysql database to the pgsql database and maintain the header/detail id relationship? It would seem the header records will get whatever serialized value is next for their id, and the detail records will get added still holding the old mysql header record id.
Postgres uses sequences for tracking auto increment primary keys. You can insert your primary and foreign keys as-is from MySQL. You will then need to update the sequences to have the max(id) based on your data.
see: https://www.postgresql.org/docs/current/functions-sequence.html
for example:
SELECT setval('your_table_name_seq', select max(id) from your_table_name);
Note the "_seq" suffix for the setval param.
class Employee(NamedTuple): # inherit from typing.NamedTuple
name: str
id: int = 3 # default value
employee = Employee('Guido')
assert employee.id == 3

Pentaho Dimension lookup/update

I have seen Dimension Lookup/Update documentation here and a few other blogs. But I cannot seem to get a clear idea.
I have a table with the following structure:
Key Name Code Status IN Out Active
The key name code status active comes from a csv file .
I need to use Dimension lookup/update step for scd type2 and populate the IN/Out.
After setting up the connection details,
I have set the Keys to KEY and the Fields to all the other fields with the option Date of last insert (without stream field as source). I need to create a new row in the database if there is change in any of the other fields. That row would have the same key and the updated details with the out set to infinity and the in set to current system date.
Date Range start field is set to IN and Table Date range End is set to OUT column of database .
I don't understand the concept of this technical key as the key also comes from the csv file .
When I click on preview there is an error:
DB2 SQL error: SQLCODE: -407, SQLSTATE: 23502, SQLERRMC:
Please let me know for more details and any step or setting I must have missed.
The key points of Dimension Lookup / Update step (used in Update mode) when using for building SCD II. table are:
Keys - Key fields: Here you define an id column from your source data (I guess its Key from your CSV file). It is used to look up previously stored rows with same Key, so the step can compare incoming row to rows already stored in SCD II. and evaluate the row was changed or not.
Technical key field: Technical key is extra new column you need to add to your table (e.g. technical_key). Also new within your PDI record stream (named it same as it is in your table technical_key). Set it to auto increment. It will auto increment itself when inserting any new row into the table and its value is unique within the table (so can be used as table's primary key).
Stream Datefield: Usually here you put last_updated_date column from your source data which changes its value to actual date every time the row record is updated in source data. Or you can use actual time from when the transformation is executed (obtained from Get System Info.system date step).
Date range start field, Table date range and: Every row in SCD II. table needs to have validity period (a scope within row data is valid). This period is defined by two dates - start (Date range start field) and end date (Table date range end). These two fields you set to IN and Out (name of the table columns). The step will automatically determine their values (set up Min. year = 1900 and Max. year = 2199) using "Range start, end" and Stream Datefield values:
When the row (identified by Key) is new:
Key = 1; technical_key = 123; In = 1900-01-01; Out = 2199-12-31; Name = X
E.g. Next day. The same row updated (Stream Datefield's value = '2015-03-13'):
Key = 1; technical_key = 123; In = 1900-01-01; Out = 2015-03-13; Name = X
Key = 1; technical_key = 158; In = 2015-03-13; Out = 2199-12-31; Name = A
Fields - Update fields: Here you define all the data fields you want to store: Name Code Status Active. As a Type of dimension update set Insert (used for SCD II. attributes).
The Date of last insert (without stream field as source) cannot be used because it only simply writes out actual date-time to particular dimension field and you cannot define stream field along with this option.
The Punch through option can be used when you are not interested about the attribute's history (SCD I.). It overwrites all occurrences among the rows with the same Key. E.g. Punch through on Name (New value 'A'):
Key = 1; technical_key = 123; In = 1900-01-01; Out = 2015-03-13; Name = A
Key = 1; technical_key = 158; In = 2015-03-13; Out = 2199-12-31; Name = A

adding next sequential number for primary key in append query

I need to create an append query, that appends many records to a table. this table has a primary key, that is a sequential number. How do I make my append query, append records to the table and automatically assign the next sequential number for the primary key? I woudl need to run this query on a live multi-user MYSQL server throughout the day
thanks!
If the PK is a true auto-incremental field, you should be able to leave the PK out of your 'append' query. The table will automatically assign the next value in sequence to your data row(s) that you are inserting.
ex: If you have this data in table names
id name
1 Ken
2 Jon
3 Steve
And you run this query
INSERT INTO names (name) VALUES ('Peter')
Your table should automatically assign id # 4 to Peter
If the sequential PK is maintained manually, I would suggest you alter that field to be a true auto-incremental field if at all possible, or create a new auto-increment field and drop the old one. Just make sure you update any other related tables before you drop the field.

How to insert values into an Access table's AutoNumber field?

I'm working on a project using jsp and Microsoft Access.
There are three tables in which a same field is used as Receipt_No.
This can get from Recp table, which has a only one field: Receipt_No (AutoNumber, Primary Key).
Now my question is, how can I insert in this AutoNumber field with every generation of receipt?.
WARNING This is ghetto. (But then again I'm thinking a table with ONLY an autonumber field is kinda ghetto too, so oh well.)
INSERT INTO yourTable (your_autonumber_field) SELECT max(your_autonumber_field) + 1 FROM yourTable
Not elegant, but it works. That will create a new record in your Receipt table. It defeats the whole purpose of having an autonumber field but I don't see another way to use SQL to create a record in a table with only an autonumber field. You can then retrieve the newly created receipt ID with a SELECT max(your_autonumber_field) FROM yourTable for use in your FK fields in the other tables.
The autonumber field has the property that it gets numbered accordingly as the records are entered into the table. its smthng like autoincrement