sql code for firebird doesn't compile - sql

I am getting strange errors while trying to create a simple database using isql tools from the Firebird package.
The same code for creating a table works for other tables with other names.
I've tried with and without quotes surrounding fields and table names, no success.
It is Firebird 2.5 server version.
The code I'm trying to execute:
SET SQL DIALECT 3;
SET NAMES UTF8;
CREATE DATABASE 'localhost:C:\fuzzdb.fdb' user 'SYSDBA' password 'masterkey'
DEFAULT CHARACTER SET UTF8;
CREATE TABLE RULES (
RULE_ID INTEGER NOT NULL,
IF_FUZZY SMALLINT,
CONSTRAINT PK_RULE_ID
PRIMARY KEY (RULE_ID),
);
CREATE TABLE VARS (
VAR_ID INTEGER NOT NULL,
VRULE_ID INTEGER,
INPOUTP SMALLINT,
RANGE_STRT INTEGER,
RANGE_END INTEGER,
VAR_NAME VARCHAR(40),
FUZ_SET INTEGER,
CONSTRAINT PK_VAR_ID
PRIMARY KEY (VAR_ID)
);
CREATE TABLE FUZZSETS (
FS_ID INTEGER NOT NULL,
FS_NAME VARCHAR(40),
INPOUTP SMALLINT,
PAR1 FLOAT,
PAR2 FLOAT,
PAR3 FLOAT,
PAR4 FLOAT,
PAR5_HEDGE INTEGER,
FUZ_SET INTEGER,
CONSTRAINT PK_FS_ID
PRIMARY KEY (FS_ID)
);
CREATE TABLE FRULES (
FRULE_ID INTEGER NOT NULL,
RULE_ID INTEGER,
VAR_ID INTEGER,
FS_ID INTEGER,
INPOUTP SMALLINT,
CONSTRAINT PK_FRULE_ID
PRIMARY KEY (FRULE_ID)
);
CREATE TABLE LINK_RV (
LINK_RULES INTEGER,
LINK_VARS INTEGER,
CONSTRAINT FK_LINK_RV
PRIMARY KEY (LINK_RULES, LINK_VARS)
);
CREATE TABLE LINK_VARFS (
LINK_VRS INTEGER,
LINK_FS INTEGER,
CONSTRAINT FK_LINK_VARFS
PRIMARY KEY (LINK_VRS, LINK_FS)
);
CREATE TABLE LINK_RLVR (
LINK_RULE INTEGER NOT NULL,
LINK_VR INTEGER NOT NULL,
CONSTRAINT FK_LINK_RLVR
PRIMARY KEY (LINK_RULE, LINK_VR)
);
CREATE TABLE LINK_FRL_RL (
LINK_FRULE INTEGER NOT NULL,
LINK_RULE INTEGER NOT NULL,
CONSTRAINT FK_LINK_FRL_RL
PRIMARY KEY (LINK_FRULE, LINK_RULE)
);
CREATE TABLE LINK_FRL_VAR (
LINK_FRULE INTEGER NOT NULL,
LINK_VAR INTEGER NOT NULL,
CONSTRAINT FK_LINK_FRL_VAR
PRIMARY KEY (LINK_FRULE, LINK_VAR)
);
CREATE TABLE LINK_FRL_FS (
LINK_FSRULE INTEGER NOT NULL,
LINK_FS INTEGER NOT NULL,
CONSTRAINT FK_LINK_FRL_FS
PRIMARY KEY (LINK_FRULE, LINK_FS)
);
ALTER TABLE LINK_FRL_FS
ADD CONSTRAINT FK_LINK_FSRULE
FOREIGN KEY(LINK_FSRULE)
REFERENCES FRULES(FRULE_ID);
ALTER TABLE LINK_FRL_FS
ADD CONSTRAINT FK_LINK_FS
FOREIGN KEY(LINK_FS)
REFERENCES FUZZSETS(FS_ID);
ALTER TABLE LINK_FRL_VAR
ADD CONSTRAINT FK_LINK_FRULE
FOREIGN KEY(LINK_FRULE)
REFERENCES FRULES(FRULE_ID);
ALTER TABLE LINK_FRL_VAR
ADD CONSTRAINT FK_LINK_VAR
FOREIGN KEY(LINK_VAR)
REFERENCES FUZZSETS(VAR_ID);
ALTER TABLE LINK_FRL_RL
ADD CONSTRAINT FK_LINK_FRULE
FOREIGN KEY(LINK_FRULE)
REFERENCES FRULES(FRULE_ID);
ALTER TABLE LINK_FRL_RL
ADD CONSTRAINT FK_LINK_RULE
FOREIGN KEY(LINK_RULE)
REFERENCES RULES(RULE_ID);
CREATE GENERATOR GEN_RULE_ID;
CREATE GENERATOR GEN_VAR_ID;
CREATE GENERATOR GEN_FS_ID;
CREATE GENERATOR GEN_FRULE_ID;
SET TERM ^ ;
CREATE TRIGGER BI_RULES FOR RULES
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
IF (NEW.RULE_ID IS NULL) THEN
NEW.RULE_ID = GEN_ID(GEN_RULE_ID, 1);
END^
CREATE TRIGGER BI_VARS FOR VARS
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
IF (NEW.VAR_ID IS NULL) THEN
NEW.VAR_ID = GEN_ID(GEN_VAR_ID, 1);
END^
CREATE TRIGGER BI_FUZZSETS FOR FUZZSETS
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
IF (NEW.FS_ID IS NULL) THEN
NEW.FS_ID = GEN_ID(GEN_FS_ID, 1);
END^
CREATE TRIGGER BI_FRULES FOR FRULES
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
IF (NEW.FRULE_ID IS NULL) THEN
NEW.FRULE_ID = GEN_ID(GEN_FRULE_ID, 1);
END^
SET TERM ; ^
COMMIT;
The output from the isql commmand:
Use CONNECT or CREATE DATABASE to specify a database
Statement failed, SQLSTATE = 42000
Dynamic SQL Error
-SQL error code = -104
-Token unknown - line 6, column 3
-)
At line 10 in file c:\fdb.sql
Statement failed, SQLSTATE = 42000
unsuccessful metadata update
-Unknown columns in index FK_LINK_FRL_FS
After line 82 in file c:\fdb.sql
Statement failed, SQLSTATE = 42S02
Dynamic SQL Error
-SQL error code = -204
-Table unknown
-LINK_FRL_FS
-At line 1, column 13.
After line 89 in file c:\fdb.sql
Statement failed, SQLSTATE = 42S02
Dynamic SQL Error
-SQL error code = -204
-Table unknown
-LINK_FRL_FS
-At line 1, column 13.
After line 94 in file c:\fdb.sql
Statement failed, SQLSTATE = 42000
unsuccessful metadata update
-could not find UNIQUE or PRIMARY KEY constraint in table FUZZSETS with specifie
d columns
After line 104 in file c:\fdb.sql
Statement failed, SQLSTATE = 42S11
unsuccessful metadata update
-Index FK_LINK_FRULE already exists
After line 109 in file c:\fdb.sql
Statement failed, SQLSTATE = 42000
unsuccessful metadata update
-Table RULES not found
After line 114 in file c:\fdb.sql
Statement failed, SQLSTATE = 42S02
Dynamic SQL Error
-SQL error code = -204
-Table unknown
-RULES
-At line 1, column 29
At line 130 in file c:\fdb.sql
I don't get why it's impossible to create the first table "RULES" although the commands are similar to other tables.
Even without all the triggers and foreign keys (alter table..) I am getting at leaast the last error.
it says "Unknown columns in index FK_LINK_FRL_FS" but no mention of other similar indexing tables.
I am just starting working with databases and it could be that I missed or mixed something,
but I tried to compile with too many changes and still getting errors.
I've found more or less similar code here
http://sergworks.wordpress.com/category/firebird/
and I was able to compile it without problems.
Could somebody point me in the right direction or show how it is done in another way?

You have an unnessesary comma in the end of the PK constraint:
CONSTRAINT PK_RULE_ID
PRIMARY KEY (RULE_ID),
So the parser expexts definition of field or constraint but it finds ")". Delete the comma and you should be OK.

Related

How to fix the trigger with check compiler log error?

Please, help!
I have trigger:
CREATE TRIGGER check_reservation BEFORE INSERT ON order
FOR EACH ROW
DECLARE mistake INTEGER;
BEGIN
SELECT count(*) INTO mistake FROM order join reserving
on id_order = reserving.order_id_order
WHERE reserving.room_num_room=:new.room_num_room
AND (order.reservation_from < :new.reservation_from AND :new.reservation_from < order.reservation_to) OR
(order.reservation_from < :new.reservation_from AND :new.reservation_to < order.reservation_to) OR
(:new.reservation_from <= order.reservation_from AND order.reservation_to <= :new.reservation_to);
IF mistake>0 THEN
raise_application_error(-20001,'reservation already exists');
END IF;
END;
The idea of the trigger is not to allow make a reservation on already booked room. When I run it I had check compiler log error message. How can I change trigger?
I have following tables:
CREATE TABLE order (
id_order CHAR(100) NOT NULL,
reservation_from DATE NOT NULL,
reservation_to DATE NOT NULL,
);
ALTER TABLE order ADD CONSTRAINT order_pk PRIMARY KEY ( id_order );
CREATE TABLE room (
num_room CHAR(100) NOT NULL,
type VARCHAR2(100) NOT NULL,
);
ALTER TABLE room ADD CONSTRAINT room_pk PRIMARY KEY ( num_room );
CREATE TABLE reserving (
room_num_room CHAR(100) NOT NULL,
order_id_order CHAR(100) NOT NULL
);
ALTER TABLE reserving ADD CONSTRAINT reserving_pk PRIMARY KEY ( room_num_room,
order_id_order );
ALTER TABLE reserving
ADD CONSTRAINT reserving_order_fk FOREIGN KEY ( order_id_order )
REFERENCES order ( id_order );
ALTER TABLE reserving
ADD CONSTRAINT reserving_room_fk FOREIGN KEY ( room_num_room )
REFERENCES room ( num_room );
I tried recreating the trigger with the statements above. The statements failed with several errors, it looks as if they were not tested before posting them as question. Please take some time posting a quality question.
Example:
CREATE TABLE room (
num_room CHAR(100) NOT NULL,
type VARCHAR2(100) NOT NULL, << this trailing comma makes this statement fail.
);
After fixing all errors I ran the "CREATE TRIGGER" and it errored out with
PLS-00049: bad bind variable 'NEW.ROOM_NUM_ROOM'
That is because the column ROOM_NUM_ROOM does not exist in the "ORDER" table.
If I remove the reference to 'NEW.ROOM_NUM_ROOM' the trigger compiles successfully.
However, as gsalem pointed out, this will not work because it will raise a mutating table error. In the trigger code you cannot execute DML referencing the table that the trigger is on. There is plenty of documentation on how to avoid mutating table errors.

Create Domain Error

I am trying to define a new domain "rating_int" that I would be able to use in the table "Review". Anytime I try to execute this query, it returns this message:
ERROR: column "rating_int" does not exist
********** Error **********
ERROR: column "rating_int" does not exist
SQL state: 42703
I followed the exact same code format as the postgresql example and I do not know why my code is returning an error.
CREATE DOMAIN rating_int AS INTEGER
CHECK(rating_int > 0 AND rating_int <=10);
CREATE TABLE Review
(
paper_id INTEGER,
rv_email VARCHAR(55) NOT NULL,
reccomendation TEXT,
a_comments TEXT,
c_comments TEXT,
technical_merit rating_int,
readability rating_int,
orginality rating_int,
relevance rating_int,
PRIMARY KEY(paper_id, rv_email),
FOREIGN KEY(paper_id) REFERENCES Paper
ON DELETE RESTRICT ON UPDATE CASCADE,
FOREIGN KEY(rv_email) REFERENCES Reviewer
ON DELETE SET NULL ON UPDATE CASCADE
);
You use value, not the name of the domain. Like this:
CREATE DOMAIN rating_int AS INTEGER
CONSTRAINT chk_rating_int CHECK (value > 0 AND value <= 10);
You don't actually need to give the constraint a name -- your error was rating_int instead of value. So this works:
CREATE DOMAIN rating_int AS INTEGER
CHECK (value > 0 AND value <= 10);
However, I think it is a good idea to give constraints names.
Also, you might want to make the type a smallint or decimal(2), if you want to save on space.

Insert fails due to "column not allowed here" error

I am a beginner with SQL. I have created 4 tables and added data to my SHIP table. I am having some issues with inserting data into the CRUISE table. I get the error message at the bottom.
I have researched and can not figure out what i am doing wrong. Is there an issue with my sequence and/or trigger that is not allowing me to do this or is my syntax in the CREATE TABLE CRUISE causing the error? Everything i have done has been successful up until trying to insert the first column into the CRUISE table.
The tables:
CREATE TABLE SHIP
( Ship_Name VARCHAR2(100) PRIMARY KEY,
Ship_Size INTEGER,
Ship_Registry VARCHAR2(50),
Ship_ServEntryDate INTEGER,
Ship_PassCapacity INTEGER,
Ship_CrewCapacity INTEGER,
Ship_Lifestyle VARCHAR2(40),
CONSTRAINT Size_ck CHECK (Ship_Size > 0),
CONSTRAINT Registry_ck CHECK (Ship_Registry IN ('Norway','Liberia','The Netherlands','Bahamas'))
)
CREATE TABLE CRUISE (
Cruise_ID INTEGER Primary Key,
Ship_Name VARCHAR(100),
Cruise_DeptDate DATE NOT NULL,
Cruise_DeptCity VARCHAR(80) NOT NULL,
Cruise_Duration INTEGER,
FOREIGN KEY (Ship_Name) REFERENCES SHIP(Ship_Name)
)
CREATE TABLE PASSENGERS (
Pass_ID INTEGER PRIMARY KEY,
Pass_Name VARCHAR(100) NOT NULL,
Pass_City VARCHAR(80),
Pass_Telephone VARCHAR(15),
Pass_NextOfKin VARCHAR(100)
)
CREATE TABLE RESERVATIONS (
Pass_ID INTEGER NOT NULL,
Cruise_ID INTEGER NOT NULL,
Res_TotalCost NUMERIC(9,2),
Res_BalanceDue NUMERIC(9,2),
Res_SpecialRequest VARCHAR(30),
Res_Room VARCHAR(10),
FOREIGN KEY (Pass_ID) REFERENCES PASSENGERS(Pass_ID),
FOREIGN KEY (Cruise_ID) REFERENCES CRUISE(Cruise_ID),
CONSTRAINT Cost_ck CHECK (Res_TotalCost >= 0),
CONSTRAINT BalanceDue_ck CHECK (Res_BalanceDue >= 0),
CONSTRAINT SpecialRequest_ck CHECK (Res_SpecialRequest IN ('Vegetarian','Vegan','Low salt','Gluten free','Kosher','Other'))
)
The sequence/trigger is an attempt to auto number Cruise_ID.
Create SEQUENCE cruise_id_sq
START WITH 1
INCREMENT BY 1;
CREATE OR REPLACE TRIGGER cruise_id_t
BEFORE INSERT
ON CRUISE
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
if(:new.Cruise_ID is null) then
SELECT cruise_id_sq.nextval
INTO :new.Cruise_ID
FROM dual;
end if;
END;
ALTER TRIGGER cruise_id_t ENABLE;
Inserting into SHIP is okay....
INSERT INTO SHIP
(Ship_Name, Ship_Size, Ship_Registry,Ship_ServEntryDate, Ship_PassCapacity,Ship_CrewCapacity,Ship_Lifestyle)
Values ('Carribean Princess',142000,'Liberia',1000,3100,1181,'Contemporary');
INSERT INTO SHIP
(Ship_Name, Ship_Size, Ship_Registry,Ship_ServEntryDate, Ship_PassCapacity,Ship_CrewCapacity,Ship_Lifestyle)
Values ('Carribean Sunshine',74000,'Norway',1992,1950,760,'Premium');
INSERT INTO SHIP
(Ship_Name, Ship_Size, Ship_Registry,Ship_ServEntryDate, Ship_PassCapacity,Ship_CrewCapacity,Ship_Lifestyle)
Values ('Ship of Dreams',70000,'Liberia',2004,1804,735,'Contemporary');
INSERT INTO SHIP
(Ship_Name, Ship_Size, Ship_Registry,Ship_ServEntryDate, Ship_PassCapacity,Ship_CrewCapacity,Ship_Lifestyle)
Values ('Sunshine of the Seas',74000,'The Netherlands',1990,2354,822,'Luxury');
Inserting into CRUISE fails...
INSERT INTO Cruise
(Ship_Name, Cruise_DeptDate,Cruise_DeptCity,CruiseDuration)
Values ('Sunshine of the Seas',25-may-15,'Miami',10);
Error starting at line : 1 in command - INSERT INTO Cruise (Ship_Name,
Cruise_DeptDate,Cruise_DeptCity,CruiseDuration) Values ('Sunshine of
the Seas',25-may-15,'Miami',10) Error at Command Line : 3 Column : 35
Error report - SQL Error: ORA-00984: column not allowed here
00984. 00000 - "column not allowed here"
*Cause:
*Action:
Oracle thinks that 25-may-15 is the expression 25 minus may minus 15. In looking up the value for may Oracle finds that there is nothing there. Thus the error.
You can, but probably don't want to, quote it like so, '25-may-15'. This will make a string that may or may not be implicitly converted to a date, depending on the settings of NLS_DATE_FORMAT and or NLS_TERRITORY.
To form a date independent of session setting one can use the TO_DATE function with explicit date format, to_date('25-may-15', 'DD-Mon-YY'). Another option is a date literal, date '2015-05-25', which is always YYYY-MM-DD no matter the session settings..

Executing script on Firebird error Token unknown - line 1, column 5 TERM. Error Code: -104

I am trying to write a script that creates a table, adds some constraints and then make a column auto-increment. As far as I read it happens with generator and before insert trigger.
Here is my code:
CREATE TABLE BLANKS
(
ID INT NOT NULL PRIMARY KEY,
BLANK_ID INT NOT NULL,
DATABASE_ID SMALLINT NOT NULL,
BLANK_VERSION DECIMAL,
CONSTRAINT ROW_UNIQUENESS UNIQUE(BLANK_ID, DATABASE_ID, BLANK_VERSION)
)
/* Autoincrement for field (ID) */
CREATE GENERATOR GEN_BLANKS_ID;
SET TERM ^ ;
CREATE TRIGGER BLANKS_BI FOR BLANKS
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
IF (NEW.ID IS NULL) THEN
NEW.ID = GEN_ID(GEN_BLANKS_ID,1);
END^
SET TERM ; ^
But it just doesn't work. It creates the table and the generator but when it reaches SET TERM ^ ; the script fails with error:
SQL Error: Dynamic SQL Error SQL error code = -104 Token unknown - line 1, column 5 TERM. Error Code: -104. can't format message 13:896 -- message file C:\Program Files (x86)\SQL Maestro Group\firebird.msg not found The SQL: SET TERM ^ ;
;
Do you have any suggestions?
Add a semicolon after the create table statement and remove the set term ^ syntax if your SQL client doesn't like it:
CREATE TABLE BLANKS
(
ID INT NOT NULL PRIMARY KEY,
BLANK_ID INT NOT NULL,
DATABASE_ID SMALLINT NOT NULL,
BLANK_VERSION DECIMAL,
CONSTRAINT ROW_UNIQUENESS UNIQUE(BLANK_ID, DATABASE_ID, BLANK_VERSION)
);
/* Autoincrement for field (ID) */
CREATE GENERATOR GEN_BLANKS_ID;
CREATE TRIGGER BLANKS_BI FOR BLANKS
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
IF (NEW.ID IS NULL) THEN
NEW.ID = GEN_ID(GEN_BLANKS_ID,1);
END

update postgresql with syntax errors

I have one table called test, which has 4 columns:
id INT
v_out INT
v_in INT
label CHARACTER
I'm trying to update the table with the following query:
String sql = "
update
test
set
v_out = temp.outV
, v_in = temp.inV
, label = temp.label
from (
values
(1,234,235,'[abc]') // these value are read from other places
,(2,234,5585,'[def]') //[abc] = object.toString();
) as temp (e_id, outV, inV, label)
where
id = temp.e_id;
When I execute it, I got the error:
org.postgresql.util.PSQLException: ERROR: syntax error at or near "["
before get this error, i already update over 3000 rows.
so whats the reason caused this? is it because "[" this character?
this is the original table:
create table edges(
id serial not null primary key,
vertex_out int,
vertex_in int,
label character varying(255),
constraint fk_vertex_out foreign key (vertex_out) references vertices(id) on delete cascade,
constraint fk_vertex_in foreign key (vertex_in) references vertices(id) on delete cascade
);
The most likely problem here is string interpolation in SQL query (it is a very bad practice).
Look at this example:
values
(1,234,235,'[abc]''),
(2,234,5585,'[def]')
The ' symbol in the name of the first object breaches the string boundaries causing ERROR: syntax error at or near "[": on the second line.
You can search for SQL Injection in the internet to get details about this problem.