SQL Informix - adding foreign key - sql

I am struggling while creating new database tables with foreign keys in Informix DB.
I need to migrate from one database to another. I am using DBeaver as DB browsing and editing tool. I am trying to run script like this (I just changed names). The commands come from DBeaver DDL export itself, so this is how it should be.
DROP TABLE IF EXISTS eapp:test_table1;
CREATE TABLE eapp:test_table1 (
column1 smallint NOT NULL,
PRIMARY KEY (column1) CONSTRAINT table1_pk
);
DROP TABLE IF EXISTS eapp:test_table2;
CREATE TABLE eapp:test_table2 (
column1 smallint NOT NULL,
column2 varchar(100) NOT NULL,
column3 smallint NOT NULL,
column4 smallint,
PRIMARY KEY (column1) CONSTRAINT table2_pk,
FOREIGN KEY (column4) REFERENCES eapp:test_table1(column1) ON DELETE RESTRICT ON UPDATE RESTRICT CONSTRAINT table2_fk
);
However, execution keeps crashing with a weird trimmed error message:
The language is Czech and it says something like "Syntax error. (Important notice: Database servers, that". But the rest is trimmed even in detail, so I have no idea what is going on.
Full stack:
org.jkiss.dbeaver.model.sql.DBSQLException: SQL Error [42000]: Chyba syntaxe. (Důležitá poznámka: Databázové servery, které
at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.executeStatement(JDBCStatementImpl.java:134)
at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.executeStatement(SQLQueryJob.java:473)
at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.lambda$0(SQLQueryJob.java:412)
at org.jkiss.dbeaver.model.exec.DBExecUtils.tryExecuteRecover(DBExecUtils.java:148)
at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.executeSingleQuery(SQLQueryJob.java:405)
at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.extractData(SQLQueryJob.java:865)
at org.jkiss.dbeaver.ui.editors.sql.SQLEditor$QueryResultsContainer.readData(SQLEditor.java:2833)
at org.jkiss.dbeaver.ui.controls.resultset.ResultSetJobDataRead.lambda$0(ResultSetJobDataRead.java:98)
at org.jkiss.dbeaver.model.exec.DBExecUtils.tryExecuteRecover(DBExecUtils.java:148)
at org.jkiss.dbeaver.ui.controls.resultset.ResultSetJobDataRead.run(ResultSetJobDataRead.java:96)
at org.jkiss.dbeaver.ui.controls.resultset.ResultSetViewer$17.run(ResultSetViewer.java:3356)
at org.jkiss.dbeaver.model.runtime.AbstractJob.run(AbstractJob.java:103)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)
Caused by: java.sql.SQLException: Chyba syntaxe. (Důležitá poznámka: Databázové servery, které
at com.informix.util.IfxErrMsg.getSQLException(IfxErrMsg.java:413)
at com.informix.jdbc.IfxSqli.a(IfxSqli.java:3537)
at com.informix.jdbc.IfxSqli.E(IfxSqli.java:3853)
at com.informix.jdbc.IfxSqli.dispatchMsg(IfxSqli.java:2653)
at com.informix.jdbc.IfxSqli.receiveMessage(IfxSqli.java:2569)
at com.informix.jdbc.IfxSqli.executeExecute(IfxSqli.java:2446)
at com.informix.jdbc.IfxSqli.executeExecute(IfxSqli.java:2421)
at com.informix.jdbc.IfxResultSet.b(IfxResultSet.java:393)
at com.informix.jdbc.IfxStatement.a(IfxStatement.java:1326)
at com.informix.jdbc.IfxStatement.executeImpl(IfxStatement.java:1296)
at com.informix.jdbc.IfxStatement.c(IfxStatement.java:1007)
at com.informix.jdbc.IfxStatement.execute(IfxStatement.java:893)
at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.execute(JDBCStatementImpl.java:338)
at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.executeStatement(JDBCStatementImpl.java:131)
... 12 more
Caused by: java.sql.SQLException
at com.informix.util.IfxErrMsg.getSQLException(IfxErrMsg.java:413)
at com.informix.jdbc.IfxSqli.E(IfxSqli.java:3858)
... 23 more
I was unable to Google message like that, so I am trying to ask here, if anyone has some experience with Informix DB and could possibly give me some hints.
I already tried to remove FK declaration from table creation and add it afterwards via ALTER TABLE, but it yields the same...

DBeaver is not generating valid Informix DDL. Informix does not understand ON DELETE RESTRICT ON UPDATE RESTRICT syntax.
From the online manual:
REFERENCES Clause
|--REFERENCES--table--+------------------+---------------------->
| .-,------. |
| V | |
'-(----column-+--)-'
>--+-------------------------+----------------------------------|
| (1) |
'-------ON DELETE CASCADE-'

Related

error: You have an error in your SQL syntax when creating table in mysql [duplicate]

This question already has an answer here:
Create a table with a space in the name
(1 answer)
Closed 9 months ago.
I know what a syntax error is but cannot find any error when creating this table:
my code
CREATE TABLE branch supplier (
branch_id INT,
supplier_name VARCHAR(40),
supply_type VARCHAR(40),
PRIMARY KEY (branch_id, supplier_name),
FOREIGN KEY (branch_id) REFERENCES branch(branch_id) ON DELETE CASCADE
);
and the error I'm getting
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'supplier ( branch_id INT, supplier_name VARCHAR(40), supply_type ' at line 1
It is not allowed to use table names with space(s) unless you use `. This means you need to change this to...
CREATE TABLE `branch supplier`...
or to something without space, as example
CREATE TABLE branch_supplier...

Error when loading table from MSSQL Server to Informix DB

I am trying to load a table from MSSQL - IBM Informix DB. Tables without Primary key are getting loaded but ones with Primary key are not loading and throwing following error
Stream component 'st_2_Informix_Target' terminated
Stream component failed at subtask 2, component st_2_Informix_Target
Error executing data handler
Handling new table 'dbo'.'region3' failed
execute create primary key failed, statement ALTER TABLE dbo.region3 ADD CONSTRAINT region3_PK__region3__3213E83F82CE48A3 PRIMARY KEY ( id )
RetCode: SQL_ERROR SqlState: 42000 NativeError: -201 Message: [Informix][Informix ODBC Driver][Informix]A syntax error has occurred.
Failed (retcode -1) to execute statement: 'ALTER TABLE dbo.region3 ADD CONSTRAINT region3_PK__region3__3213E83F82CE48A3 PRIMARY KEY ( id )'
Someone (I guess SQLServer or DMS) is trying to add a primary key to the Informix table using SQLServer SQL syntax which is not valid in Informix.
> ALTER TABLE dbo.region3 ADD CONSTRAINT region3_PK__region3__3213E83F82CE48A3 PRIMARY KEY ( id );
201: A syntax error has occurred.
Error in line 1
Near character position 40
> ALTER TABLE dbo.region3 ADD CONSTRAINT PRIMARY KEY ( id ) CONSTRAINT region3_PK__region3__3213E83F82CE48A3;
Table altered.
>
I suggest to check if there are any options to disable PK creation when doing the load task.

Remove Unique constraint on a column in sqlite database

I am trying to remove a UNIQUE constraint on a column for sqlite but I do not have the name to remove the constraint. How can I find the name of the UNIQUE constraint name to remove it.
Below is the schema I see for the table I want to remove the constraint
UNIQUE (datasource_name)
sqlite> .schema datasources
CREATE TABLE "datasources" (
created_on DATETIME NOT NULL,
changed_on DATETIME NOT NULL,
id INTEGER NOT NULL,
datasource_name VARCHAR(255),
is_featured BOOLEAN,
is_hidden BOOLEAN,
description TEXT,
default_endpoint TEXT,
user_id INTEGER,
cluster_name VARCHAR(250),
created_by_fk INTEGER,
changed_by_fk INTEGER,
"offset" INTEGER,
cache_timeout INTEGER, perm VARCHAR(1000), filter_select_enabled BOOLEAN, params VARCHAR(1000),
PRIMARY KEY (id),
CHECK (is_featured IN (0, 1)),
CHECK (is_hidden IN (0, 1)),
FOREIGN KEY(created_by_fk) REFERENCES ab_user (id),
FOREIGN KEY(changed_by_fk) REFERENCES ab_user (id),
FOREIGN KEY(cluster_name) REFERENCES clusters (cluster_name),
UNIQUE (datasource_name),
FOREIGN KEY(user_id) REFERENCES ab_user (id)
);
SQLite only supports limited ALTER TABLE, so you can't remove the constaint using ALTER TABLE. What you can do to "drop" the column is to rename the table, create a new table with the same schema except for the UNIQUE constraint, and then insert all data into the new table. This procedure is documented in the Making Other Kinds Of Table Schema Changes section of ALTER TABLE documentation.
I just ran into this myself. An easy solution was using DB Browser for SQLite
It let me remove a unique constraint with just a checkbox in a gui.
PRAGMA foreign_keys=off;
BEGIN TRANSACTION;
ALTER TABLE table_name RENAME TO old_table;
CREATE TABLE table_name
(
column1 datatype [ NULL | NOT NULL ],
column2 datatype [ NULL | NOT NULL ],
...
);
INSERT INTO table_name SELECT * FROM old_table;
COMMIT;
PRAGMA foreign_keys=on;
Source: https://www.techonthenet.com/sqlite/unique.php
I was just working through this issue on a small database and found it easier to dump the data as SQL statements, it prints out your tables exactly as they are and also adds the INSERT INTO statements to rebuild the DB.
The .help terminal command shows:
.dump ?OBJECTS? Render database content as SQL
and prints the SQL to the terminal, you can update it in a TXT file. For once off changes and tidying this seems like a reasonable solution albeit a little inelegant

Table <name> already exits Error (3010)

I am new to SQL and I am trying to run a CREATE TABLE query in Ms Access 2016 but I get an error saying that "mytablename" already exits which can't be true because I also ran a DROP TABLE "mytablename" query and I got an error saying "mytablename" does not exist. Please help. Point me in the right direction at least. Here is the CREATE TABLE query.
CREATE TABLE Team(
Team_ID AUTOINCREMENT UNIQUE NOT NULL,
Name VARCHAR(40) NOT NULL,
Origin VARCHAR(40) NOT NULL,
NetWorth CURRENCY NOT NULL,
PRIMARY KEY(Team_ID)
);
See check by VBA and check by SQL for check existence of your database.
If table exists you can recreate (drop and create again) table. Alternative way is to create table if table is not exist and do nothing if table exists.

In H2 Database, add index while table creation in single query

I am trying to create table having different indexes with single query but H2 gives Error for example:
create table tbl_Cust
(
id int primary key auto_increment not null,
fid int,
c_name varchar(50),
INDEX (fid)
);
but this gives error as
Unknown data type: "("; SQL statement:
[Error Code: 50004]
[SQL State: HY004]
Due to this I have to run 2 different queries to create table with Index. First query to create table and then second query to add index with
create INDEX c_fid on tbl_Cust(fid);
Is there something wrong in my query or H2 simply does not support this creation of table with index in single query?
Interesting question. The solution is even more interesting, as it involves MySQL compatibility mode.
It's actually possible to perform the exact same command you wrote without any modification, provided you just add to your jdbc url the MySQL mode.
Example URL like this: jdbc:h2:mem:;mode=mysql
SQL remains:
create table tbl_Cust
(
id int primary key auto_increment not null,
fid int,
c_name varchar(50),
INDEX (fid)
);
Update count: 0
(15 ms)
Too bad I did not see this question earlier... Hopefully the solution might become handy one day to someone :-)
I could resolve the problem. According to
http://www.h2database.com/html/grammar.html#create_index
I modified the query. It works fine with my H2 server.
CREATE TABLE subscription_validator (
application_id int(11) NOT NULL,
api_id int(11) NOT NULL,
validator_id int(11) NOT NULL,
PRIMARY KEY (application_id,api_id),
CONSTRAINT subscription_validator_ibfk_1 FOREIGN KEY (validator_id) REFERENCES validator (id) ON UPDATE CASCADE
);
CREATE INDEX validator_id ON subscription_validator(validator_id);