trdsql DB import
trdsql does not have an option to import data into a database. However, since it can execute SQL other than SELECT, it is possible to import data by SQL.
The following example imports data into a memory database, which will disappear when it is closed, so it is recommended to connect to a non-memory database.
CREATE TABLE AS
To create a table and import data, use CREATE TABLE AS.
PostgreSQL CREATE TABLE AS
First, let’s try importing data into PostgreSQL.
If you add CREATE TABLE table_name AS to the content you have been executing with SELECT so far, a table will be created and the data will be imported.
If successful, it will exit without displaying anything. If it fails, an error will be displayed.
Since the result of SELECT is imported, you can change the column name, specify the column type, and specify the data to be imported on the SELECT side.
If you want to create a table first and then insert data later, add WITH NO DATA.
MySQL CREATE TABLE AS
Just like PostgreSQL, if you add CREATE TABLE table_name AS in front of it, the data will be imported.
There are limitations to the CAST of the result of SELECT, so it may not be sufficient. Since the syntax CREATE TABLE table_name (column_name type_name) can be used, it is better to specify the type name according to the result of SELECT.
In MySQL, if you only want to create a table, set the number of SELECTs to 0 with LIMIT 0 or WHERE 1=2.
SQLite3 CREATE TABLE AS
In SQLite3, if you add CREATE TABLE table_name AS in front of it, the data will be imported.
In SQLite3, the actual data type will be one of the basic types (INTEGER, REAL, TEXT, BLOB), so CAST is only necessary when it is one of these types.
Otherwise, it is better to add it as a constraint later.
If you only want to create a table in SQLite3, set the number of SELECTs to 0 with LIMIT 0 or WHERE 1=2.
SQLite3 does not allow you to add a primary key later.
INSERT
If you already have a table and want to import the contents of a file, add INSERT INTO table_name before SELECT and execute it.
If you have not added a primary key, it will be inserted as it is if you execute it repeatedly.
If you have not added a primary key, it will be inserted as it is if you execute it repeatedly.
If you want to insert only the difference, you can use the INSERT ON CONFLICT syntax.
If there is already a row with the same id, do nothing.
If there is already a row with the same id, update it.
MySQL allows you to avoid duplicate errors with INSERT IGNORE.
To update a row with the same id, use ON DUPLICATE KEY UPDATE as follows.
Or you can use the REPLACE INTO statement.
If a table with a primary key has already been created, you can also use the REPLACE INTO statement in SQLite3.