Making a Database table in SQL, weird error - sql

This is the code for the Database.Its giving me an error that for every TABLE created something has already taken that name, one of the error messages is at the end of the post.
COMMIT;
CREATE TABLE Team (
Team_ID INTEGER GENERATED ALWAYS AS IDENTITY,
Team_Name NVARCHAR2(40) NOT NULL,
Team_Manager VARCHAR(20),
CONSTRAINT kkeyconst PRIMARY KEY(Team_ID)
);
COMMIT;
insert into Team(TEAM_NAME, TEAM_MANAGER) VALUES ('Red Sox', 'Jon');
insert into Team(TEAM_NAME, TEAM_MANAGER) VALUES('White Sox', 'Tony');
CREATE TABLE Driver (
Driver_id INTEGER NOT NULL,
Teams_ID INTEGER,
Driver_age INTEGER,
Driver_Name NVARCHAR2(20) NOT NULL,
CONSTRAINT DriverAge CHECK (Driver_age BETWEEN '19' AND '65'),
CONSTRAINT driverpk PRIMARY KEY(Driver_ID),
CONSTRAINT Teams_PK FOREIGN KEY (Teams_ID) REFERENCES Team(Team_ID)
);
COMMIT;
insert into Driver(DRIVER_AGE,DRIVER_NAME) VALUES ('21', 'Jon');
insert into Driver(DRIVER_AGE, DRIVER_NAME) VALUES ('20', 'Tony');
CREATE TABLE Participation (
TeamName_ID INTEGER,
Driver_ID INTEGER,
PointsEarned INTEGER,
CONSTRAINT TeamName_FK FOREIGN KEY (TeamName_ID) REFERENCES Team(Team_ID),
CONSTRAINT Driver_FK FOREIGN KEY(Driver_ID) REFERENCES Driver(Driver_ID)
);
COMMIT;
insert into Participation(PARTICIPATION_POINTS_EARNED) VALUES (150);
CREATE TABLE Finish (
Racer_ID INTEGER,
Finish_Position INTEGER NOT NULL,
Fishish_Result VARCHAR(50) NOT NULL,
CONSTRAINT Racer_FK FOREIGN KEY (Racer_ID) REFERENCES Driver(Driver_id)
);
COMMIT;
insert into Finish(Finish_POSITION, Finish_RESULT) VALUES ('1', 'Winner');
insert into Finish(Finish_POSITION, Finish_RESULT) VALUES ('3', 'Third Place');
CREATE TABLE RaceComponent (
RC_ID INTEGER NOT NULL,
Driver1_ID INTEGER,
RC_Type VARCHAR(25),
CONSTRAINT Rcpk PRIMARY KEY(RC_ID),
CONSTRAINT Driver1_FK FOREIGN KEY (Driver1_ID) REFERENCES Driver(Driver_ID)
);
COMMIT;
insert into RaceComponent(RC_TYPE) VALUES ('Hot Wheels');
insert into RaceComponent(RC_TYPE) VALUES ('Tonka');
CREATE TABLE Race (
Race_Id INTEGER,
RC_ID INTEGER,
Race_Title VARCHAR(30) NOT NULL,
Race_Location VARCHAR(50) NOT NULL,
Race_Date DATE,
CONSTRAINT RACPEK PRIMARY KEY(RACE_ID),
CONSTRAINT RC_FK FOREIGN KEY (RC_ID) REFERENCES RaceComponent(RC_ID)
);
COMMIT;
insert into Race(RACE_TITLE, RACE_LOCATION, RACE_DATE) VALUES ('Tonys race', 'Moncton', DATE '2016-04-25');
insert into Race(RACE_TITLE, RACE_LOCATION, RACE_DATE) VALUES ('Mikes Racing', 'San-Francisco', DATE '2015-04-25');
ERROR:
Commit complete.
Error starting at line : 3 in command -
CREATE TABLE Team (
Team_ID INTEGER GENERATED ALWAYS AS IDENTITY,
Team_Name NVARCHAR2(40) NOT NULL,
Team_Manager VARCHAR(20),
CONSTRAINT kkeyconst PRIMARY KEY(Team_ID)
)
Error report -
SQL Error: ORA-00955: name is already used by an existing object
00955. 00000 - "name is already used by an existing object"
*Cause:
*Action:
Commit complete.
I thought it might be because I am calling the Foreign keys the same name as their primary key value, but im not 100% sure what is causing it.

If the table was created during an earlier run of your script, you will get that error. It is always good to check to see if your table already exists before trying to create it. Here is a good snippet of code from StackOverflow for checking to see if the table already exists or not.
IF (EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'TheSchema'
AND TABLE_NAME = 'TheTable'))
BEGIN
--Do Stuff
END

Related

Integrity Constraint violated error when trying to insert into tables

I need a new set of eyes to help me understand what I'm doing wrong here!
I created these tables:
CREATE TABLE bankkund(
PNR VARCHAR2(11) PRIMARY KEY,
FNAMN VARCHAR2(25) NOT NULL,
ENAMN VARCHAR2(25) NOT NULL,
PASSWD VARCHAR2(16) NOT NULL,
UNIQUE(PASSWD));
CREATE TABLE konto(
KNR NUMBER(8)PRIMARY KEY,
KTNR NUMBER(6)NOT NULL,
REGDATUM DATE NOT NULL,
SALDO NUMBER(10,2),
FOREIGN KEY(ktnr) REFERENCES kontotyp(ktnr));
CREATE TABLE kontoägare(
RADNR NUMBER(9)PRIMARY KEY,
PNR VARCHAR2(11)NOT NULL,
KNR NUMBER(8)NOT NULL,
FOREIGN KEY(pnr) REFERENCES bankkund(pnr),
FOREIGN KEY(knr) REFERENCES konto(knr));
then this sequence:
create sequence radnr_seq
start with 1
increment by 1;
and then whenever I want to insert the following values I get an integrity constraint error:
INSERT INTO kontoägare(radnr,pnr,knr)
VALUES(radnr_seq.NEXTVAL,'540126-1111',123);
INSERT INTO kontoägare(radnr,pnr,knr)
VALUES(radnr_seq.NEXTVAL,'691124-4478',123);
INSERT INTO kontoägare(radnr,pnr,knr)
VALUES(radnr_seq.NEXTVAL,'540126-1111',5899);
INSERT INTO kontoägare(radnr,pnr,knr)
VALUES(radnr_seq.NEXTVAL,'691124-4478',8896);
COMMIT;
What am I missing??
The error you're seeing is expected. Table "kontoägare" has 2 foreign keys: FOREIGN KEY(pnr) REFERENCES bankkund(pnr)and FOREIGN KEY(knr) REFERENCES konto(knr)). This means that the values for "pnr" need to exist in table "bankkund" and the value for "knr" needs to exist in "konto". If either of those values do not exist when you do the insert, an integrity constraint error will be raised.

The INSERT statement conflicted with the CHECK constraint when inserting values

Trying to insert data into a table but keep getting the following error: The INSERT statement conflicted with the CHECK constraint "CK__etudiant__dateNa__3D5E1FD2". The conflict occurred in database "gestionAbsEtud", table "dbo.etudiant", column 'dateNaissance'.
create table etudiant(
numInscription int primary key,
nom char(20),
prenom char(20),
sexe char check(sexe in('M','F')),
dateNaissance date check(datediff(year, datenaissance, getdate()) between 15 and 30 ),
email varchar(20),
adresse varchar(20),
idFiliere varchar(10) foreign key references filier);
go
and this is the value i want to add :
insert into etudiant values
(1,'elbaraa','HAMMAN','m','20001126','contact#baraa.tk','DI1');
List the columns explicitly:
insert into etudiant (numInscription, nom, prenom, sexe, dateNaissance, email, adresse)
values (1, 'elbaraa', 'HAMMAN', 'm', '20001126', 'contact#baraa.tk', 'DI1');
In general, this is a best practice.

"missing the right parenthesis" in Oracle SQL

I've uploaded all my code. I think I'm using Oracle SQL Developer, which isn't the right one I'm assuming?
It's for a university assignment.
--CREATESCRIPTS
CREATE TABLE Bug --Creating the bug table.
(
Bug_ID VARCHAR2 (6)NOT NULL,
Bug_Name VARCHAR2 (100)NOT NULL,
Bug_Desc VARCHAR2 (255)NOT NULL,
Found_Date DATE NOT NULL,
CONSTRAINT PK_Bug PRIMARY KEY (Bug_ID) --creating the primary key for Bug.
);
CREATE TABLE Engineer --Creating the engineer table.
(
Engineer_ID VARCHAR2(6)NOT NULL,
Engineer_First VARCHAR2 (12)NOT NULL,
Engineer_Last VARCHAR2 (12)NOT NULL,
CONSTRAINT PK_Engineer PRIMARY KEY(Engineer_ID)
);
COMMIT;
CREATE TABLE Project --Creating the project table.
(
Project_ID VARCHAR2 (8)NOT NULL,
Project_Name VARCHAR2(10)NOT NULL,
CONSTRAINT PK_Project PRIMARY KEY (Project_ID)
);
COMMIT;
CREATE TABLE Note --Creating the note table.
(
Note_ID VARCHAR2(4) NOT NULL,
note_name VARCHAR2 (4)NOT NULL,
Note_Hours spent HOUR(4)NOT NULL,
Note_content VARCHAR2(254)NOT NULL,
Engineer_ID VARCHAR2(6)NOT NULL,
Bug_ID VARCHAR2(6)NOT NULL,
CONSTRAINT PK_Note PRIMARY KEY (Note_ID),
CONSTRAINT FK_BugNoteID FOREIGN KEY(bug_ID) REFERENCES Bug(Bug_ID),
CONSTRAINT FK_NoteEngineerID FOREIGN KEY(Engineer_ID) REFERENCES Engineer (Engineer_ID)
);
COMMIT;
CREATE TABLE Project_bug --Creating the Project bug table.
(
Bug_ID VARCHAR2(6),
Project_ID VARCHAR2(8),
CONSTRAINT FK_bug_project_bugID FOREIGN KEY (Bug_ID) REFERENCES bug (bug_ID),
CONSTRAINT FK_bug_project_projectID FOREIGN KEY (Project_ID) REFERENCES Project (Project_ID),
CONSTRAINT PK_Bug_Project PRIMARY KEY (Bug_ID, Project_ID),
);
COMMIT;
CREATE TABLE Allocation_fix--creating the allocation fix table.
(
Engineer_ID VARCHAR2(6),
Bug_ID VARCHAR2(6),
CONSTRAINT FK_Engineer_FixAllocatedID FOREIGN KEY (Engineer_ID) REFERENCES Engineer (Engineer_ID),
CONSTRAINT FK_Bug_FixAllocatedID FOREIGN KEY (Bug_ID) REFERENCES Bug (Bug_ID),
CONSTRAINT PK_Fix_Allocation PRIMARY KEY (Bug_ID, Engineer_ID)
);
COMMIT;
CREATE TABLE Allocation_test --creating the allocation test table.
(
Engineer_ID VARCHAR2(6),
bug_ID VARCHAR2 (8),
CONSTRAINT FK_Engineer_TestAllocatedID FOREIGN KEY (Engineer_ID) REFERENCES Engineer (Engineer_ID),
CONSTRAINT FK_Bug_TestAllocatedID FOREIGN KEY (Bug_ID) REFERENCES Bug (Bug_ID),
CONSTRAINT PK_Test_Allocation PRIMARY KEY (Bug_ID, Engineer_ID)
);
COMMIT;
--INSERT SCRIPTS
INSERT INTO bug VALUES ('B1','bug_1','first bug','6-DEC-98')
INSERT INTO bug VALUES ('B2','bug_2','second bug','7-DEC-98')
INSERT INTO bug VALUES ('B3','bug_3','third bug','8-DEC-98')
INSERT INTO bug VALUES ('B4','bug_4','fourth bug','9-DEC-98')
INSERT INTO bug VALUES ('B5','bug_5','fifth bug','10-DEC-98')
INSERT INTO bug VALUES ('B6','bug_6','sixth bug','11-DEC-98')
COMMIT;
INSERT INTO Engineer VALUES ('E1','Jones','Lee') --tester
INSERT INTO Engineer VALUES ('E2','Friend','Kane') --fixer
INSERT INTO Engineer VALUES ('E3','Ingham','Darcie') --tester
INSERT INTO Engineer VALUES ('E4','Jones','Carly') --fixer
INSERT INTO Engineer VALUES ('E5','Evergreen','Damien') --tester
INSERT INTO Engineer VALUES ('E6', 'Danes', 'Lucy') --showing the optionality of having an engineer that isnt a tester or a fixer
COMMIT;
INSERT INTO Note VALUES ('N1','Test1','4','testing was carried out by E1')
INSERT INTO Note VALUES ('N2','Fix1','5','fix was applied by E2')
INSERT INTO Note VALUES ('N3','Test2','7','testing was carried out by E1')
INSERT INTO Note VALUES ('N4','Fix2','3','fix was applied by E3')
INSERT INTO Note VALUES ('N5','Test3','3','testing was carried out by E4')
INSERT INTO Note VALUES ('N6','Fix3','1','fix was applied by E5')
INSERT INTO Note VALUES ('N7','Test4','5','testing was carries out by E4')
INSERT INTO Note VALUES ('N8','Fix4','9','fix was applied by E5')
COMMIT;
INSERT INTO project VALUES ('P1', 'Bethesda')
INSERT INTO project VALUES ('P2', 'Activsion')
INSERT INTO project VALUES ('P3', 'Valve')
INSERT INTO project VALUES ('P4', 'Mojang')
INSERT INTO project VALUES ('P5', 'Blizzard')
INSERT INTO project VALUES ('P6', 'Rockstar')
COMMIT;
INSERT INTO Project_bug VALUES ('B1', 'P1')
INSERT INTO Project_bug VALUES ('B2', 'P2')
INSERT INTO Project_bug VALUES ('B3', 'P3')
INSERT INTO Project_bug VALUES ('B4', 'P4')
INSERT INTO Project_bug VALUES ('B5', 'P5')
INSERT INTO Project_bug VALUES ('B6', 'P6')
COMMIT;
INSERT INTO Allocation_fix VALUES ('E1','B1' ) --this data set shows the optionality of engineers being "fixers" or "testers"
INSERT INTO Allocation_fix VALUES ('E2','B2')
INSERT INTO Allocation_fix VALUES ('E3','B3')
INSERT INTO Allocation_fix VALUES ('E4','B4')
INSERT INTO Allocation_fix VALUES ('E5','B5')
INSERT INTO Allocation_fix VALUES ('E1','B6')
COMMIT;
INSERT INTO Allocation_test VALUES ('E1','B1') -- this data set shows the optionality of engineers being "fixers" or "testers"
INSERT INTO Allocation_test VALUES ('E2','B2')
INSERT INTO Allocation_test VALUES ('E3','B3')
INSERT INTO Allocation_test VALUES ('E4','B4')
INSERT INTO Allocation_test VALUES ('E5','B5')
INSERT INTO Allocation_test VALUES ('E1','B6')
COMMIT;
--SELECT SCRIPTS
-- 1: List of all the bugs, and their details.
SELECT*From bug;
-- Query 2 : List of all bugs, and their notes.
SELECT bug_type, note_id, note_content
FROM bug,note
WHERE bug.bug_id=note.bug_id;
-- Query 3 : List of all bugs, with their notes, and the engineers who have written them; sorted by name of engineer.
SELECT bug.bug_id,bug_type, note_id, note_content, engineer.engineer_id, engineer_firstname, engineer_lastname
FROM bug, note, engineer
WHERE bug.bug_id=note.bug_id AND note.engineer_id=engineer.engineer_id
ORDER BY engineer_lastname ASC;
--Query 4: List the bugs and how much cumulative time (in hours) they have taken; ordered by time taken.
SELECT bug.bug_id, SUM(note.hours_spent)
FROM note,bug
WHERE bug.bug_id=note.bug_id
GROUP BY bug.bug_id
ORDER BY SUM(note.hours_spent) ASC;
--Query 5 : The bug that has taken most time to fix and the projects it is connected to.
SELECT bug_taken_most_time_to_fix.bug_id, bug_project.project_id, bug_taken_most_time_to_fix."Total_Hours" FROM
(
SELECT bug.bug_id, SUM(note.hours_spent) AS "Total_Hours"
FROM note,bug
WHERE bug.bug_id=note.bug_id
GROUP BY bug.bug_id
ORDER BY SUM(note.hours_spent) DESC FETCH FIRST ROW ONLY
)
bug_taken_most_time_to_fix, bug_project
WHERE bug_taken_most_time_to_fix.bug_id = bug_project.bug_id;
--DROP SCRIPTS
DROP TABLE allocation_test;
DROP TABLE allocation_fix;
DROP TABLE Project_bug;
DROP TABLE Note;
DROP TABLE Engineer;
DROP TABLE Bug;
COMMIT;
You have some syntax error in your CREATE table syntax of note table. -
CREATE TABLE Note --Creating the note table.
(
Note_ID VARCHAR2(4) NOT NULL,
note_name VARCHAR2 (4)NOT NULL,
Note_Hours_spent NUMBER(4)NOT NULL,
Note_content VARCHAR2(254)NOT NULL,
Engineer_ID VARCHAR2(6)NOT NULL,
Bug_ID VARCHAR2(6)NOT NULL,
CONSTRAINT PK_Note PRIMARY KEY (Note_ID),
CONSTRAINT FK_BugNoteID FOREIGN KEY(bug_ID) REFERENCES Bug(Bug_ID),
CONSTRAINT FK_NoteEngineerID FOREIGN KEY(Engineer_ID) REFERENCES Engineer (Engineer_ID)
);
And have a comma at the last of Project_bug table -
CREATE TABLE Project_bug --Creating the Project bug table.
(
Bug_ID VARCHAR2(6),
Project_ID VARCHAR2(8),
CONSTRAINT FK_bug_project_bugID FOREIGN KEY (Bug_ID) REFERENCES bug (bug_ID),
CONSTRAINT FK_bug_project_projectID FOREIGN KEY (Project_ID) REFERENCES Project (Project_ID),
CONSTRAINT PK_Bug_Project PRIMARY KEY (Bug_ID, Project_ID)
);
Last your insert statement into note table is giving error in this fiddle as you do not have enough values to insert into that table.

Store PL/SQL Statement in a CLOB value, escape characters not working

I've got this statement which inserts some information plus a full trigger inside of a CLOB. However, I receive this error: ORA-01756: quoted string not properly terminated
I have added the escape character (') in front of every quote in the trigger. Yet I don't understand why I keep getting this error.
This is the Table creation query:
CREATE TABLE Template (id number(10) GENERATED BY DEFAULT ON NULL AS IDENTITY (START WITH 2), triggerEvent clob NOT NULL, triggerCode clob NOT NULL, TemplateTypeid number(10) NOT NULL, PRIMARY KEY (id));
And this is the whole insert query:
drop table template CASCADE CONSTRAINTS PURGE;
drop table TemplateType CASCADE CONSTRAINTS PURGE;
drop table BusinessRuleType CASCADE CONSTRAINTS PURGE;
drop table Operator CASCADE CONSTRAINTS PURGE;
drop table Category CASCADE CONSTRAINTS PURGE;
drop table BusinessRule CASCADE CONSTRAINTS PURGE;
drop table TargetDatabase CASCADE CONSTRAINTS PURGE;
drop table BusinessRuleType_Operator CASCADE CONSTRAINTS PURGE;
drop table DatabaseType CASCADE CONSTRAINTS PURGE;
drop table RuleValues CASCADE CONSTRAINTS PURGE;
drop sequence ARNG_seq;
CREATE SEQUENCE ARNG_seq;
CREATE TABLE Template (id number(10) GENERATED BY DEFAULT ON NULL AS IDENTITY (START WITH 2), triggerEvent clob NOT NULL, triggerCode clob NOT NULL, TemplateTypeid number(10) NOT NULL, PRIMARY KEY (id));
CREATE TABLE TemplateType (id number(10) GENERATED BY DEFAULT ON NULL AS IDENTITY (START WITH 2), category varchar2(255) NOT NULL, DatabaseTypeid number(10) NOT NULL, PRIMARY KEY (id));
CREATE TABLE BusinessRuleType (id number(10) GENERATED BY DEFAULT ON NULL AS IDENTITY (START WITH 2), name varchar2(255), Categoryid number(10) NOT NULL, Templateid number(10) NOT NULL, PRIMARY KEY (id));
CREATE TABLE Operator (id number(10) GENERATED BY DEFAULT ON NULL AS IDENTITY (START WITH 5), operatorType varchar2(255) NOT NULL, name varchar2(255) NOT NULL, value varchar2(255) NOT NULL, PRIMARY KEY (id));
CREATE TABLE Category (id number(10) GENERATED BY DEFAULT ON NULL AS IDENTITY (START WITH 2), name varchar2(255) NOT NULL, PRIMARY KEY (id));
CREATE TABLE BusinessRule (id number(10) GENERATED BY DEFAULT ON NULL AS IDENTITY (START WITH 2), generatedCode clob, name varchar2(255) NOT NULL, errorMessage varchar2(255) NOT NULL, validationFailureSeverity varchar2(50) NOT NULL, targetColumn varchar2(255) NOT NULL, targetTable varchar2(255) NOT NULL, BusinessRuleTypeid number(10) NOT NULL, SavedTargetDatabaseid number(10) NOT NULL, Operatorid number(10) NOT NULL, PRIMARY KEY (id));
CREATE TABLE TargetDatabase (id number(10) GENERATED BY DEFAULT ON NULL AS IDENTITY (START WITH 2), name varchar2(255) NOT NULL, url varchar2(255) NOT NULL, username varchar2(255) NOT NULL, password varchar2(255) NOT NULL, DatabaseTypeid number(10) NOT NULL, PRIMARY KEY (id));
CREATE TABLE BusinessRuleType_Operator (BusinessRuleTypeid number(10) GENERATED BY DEFAULT ON NULL AS IDENTITY (START WITH 2), Operatorid number(10) NOT NULL, PRIMARY KEY (BusinessRuleTypeid, Operatorid));
CREATE TABLE DatabaseType (id number(10) GENERATED BY DEFAULT ON NULL AS IDENTITY (START WITH 2), databaseType varchar2(255) NOT NULL, PRIMARY KEY (id));
CREATE TABLE RuleValues (id number(10) GENERATED BY DEFAULT ON NULL AS IDENTITY (START WITH 2), name varchar2(255) NOT NULL, stringValue varchar2(255), intValue number(10), clobValue clob, BusinessRuleid number(10) NOT NULL, PRIMARY KEY (id));
ALTER TABLE BusinessRuleType ADD CONSTRAINT FKBusinessRu863439 FOREIGN KEY (Categoryid) REFERENCES Category (id);
ALTER TABLE BusinessRule ADD CONSTRAINT FKBusinessRu277804 FOREIGN KEY (BusinessRuleTypeid) REFERENCES BusinessRuleType (id);
ALTER TABLE BusinessRuleType ADD CONSTRAINT FKBusinessRu443116 FOREIGN KEY (Templateid) REFERENCES Template (id);
ALTER TABLE Template ADD CONSTRAINT FKTemplate586491 FOREIGN KEY (TemplateTypeid) REFERENCES TemplateType (id);
ALTER TABLE BusinessRuleType_Operator ADD CONSTRAINT FKBusinessRu447733 FOREIGN KEY (BusinessRuleTypeid) REFERENCES BusinessRuleType (id);
ALTER TABLE BusinessRuleType_Operator ADD CONSTRAINT FKBusinessRu382431 FOREIGN KEY (Operatorid) REFERENCES Operator (id);
ALTER TABLE BusinessRule ADD CONSTRAINT FKBusinessRu873463 FOREIGN KEY (SavedTargetDatabaseid) REFERENCES TargetDatabase (id);
ALTER TABLE TemplateType ADD CONSTRAINT FKTemplateTy236989 FOREIGN KEY (DatabaseTypeid) REFERENCES DatabaseType (id);
ALTER TABLE TargetDatabase ADD CONSTRAINT FKTargetData209438 FOREIGN KEY (DatabaseTypeid) REFERENCES DatabaseType (id);
ALTER TABLE BusinessRule ADD CONSTRAINT FKBusinessRu447639 FOREIGN KEY (Operatorid) REFERENCES Operator (id);
ALTER TABLE RuleValues ADD CONSTRAINT FKRuleValues255277 FOREIGN KEY (BusinessRuleid) REFERENCES BusinessRule (id);
INSERT INTO Category (id, name) VALUES (1, 'Static');
INSERT INTO Category (id, name) VALUES (2, 'Dynamic');
INSERT INTO Operator (id, operatortype, name, value) VALUES (1, 'Compare', 'Equals', '=');
INSERT INTO Operator (id, operatortype, name, value) VALUES (2, 'Compare', 'Not Equals', '!=');
INSERT INTO Operator (id, operatortype, name, value) VALUES (3, 'Range', 'Between', 'BETWEEN');
INSERT INTO Operator (id, operatortype, name, value) VALUES (4, 'Range', 'Not Between', 'NOTBETWEEN');
INSERT INTO DatabaseType (id, databaseType) VALUES (1, 'Oracle');
INSERT INTO TargetDatabase (id, name, url, username, password, DatabaseTypeid) VALUES (1, 'School Database', 'XXXX', 'XXX', 'XX', 1);
INSERT INTO TemplateType (id, category, DatabaseTypeid) VALUES (1, 'Constraint', 1);
INSERT INTO Template (id, triggerevent, triggercode, templatetypeid) VALUES (1, 'insert, update <<column>> | <<table>>', q'[CREATE OR REPLACE TRIGGER BRG_{code}_{attribute_table}_TRG
BEFORE DELETE OR INSERT OR UPDATE
ON {attribute_table}
FOR EACH ROW
DECLARE
L_OPER VARCHAR2(3);
L_ERROR_STACK VARCHAR2(4000);
BEGIN
IF INSERTING
THEN
L_OPER := 'INS';
ELSIF UPDATING
THEN
L_OPER := 'UPD';
ELSIF DELETING
THEN
L_OPER := 'DEL';
END IF;
DECLARE
L_PASSED BOOLEAN := TRUE;
BEGIN
IF L_OPER IN ('INS', 'UPD')
THEN
IF '{operand}' = '=' THEN
L_PASSED := :NEW.{attribute_column} >= {range_min} AND :NEW.{attribute_column} <= {range_max};
ELSE
L_PASSED := :NEW.{attribute_column} < {range_min} OR :NEW.{attribute_column} > {range_max};
END IF;
IF NOT L_PASSED
THEN
raise_application_error(-20000, {error});
END IF;
END IF;
END;
END;]' , 1 );
INSERT INTO BusinessRuleType (id, name, categoryid, templateid) VALUES (1, 'Attribute Range Rule', 1, 1);
INSERT INTO BusinessRule (id, name, errorMessage, validationFailureSeverity, targetColumn, targetTable, BusinessRuleTypeid, SavedTargetDatabaseid, Operatorid) VALUES (1, 'BRG_NMR_CNS_RNG_01', 'Zit niet in de range', 'Informational Warning', 'cijfer', 'rapport', 1, 1, 1);
INSERT INTO RuleValues (id, name, intValue, BusinessRuleid) VALUES (1, 'MinValue', 1, 1);
INSERT INTO RuleValues (id, name, intValue, BusinessRuleid) VALUES (2, 'MaxValue', 10, 1);
INSERT INTO BusinessRuleType_operator (businessruletypeid, operatorid) VALUES (1,1);
The reason I need this entire trigger saved in the database, as a CLOB, is because my Java backend will work on it.
I had no trouble executing these two statements. Also, you might consider using the Q character to avoid the need to escape your quotes. As in:
q'[insert into xxx values ('a', 'b', 'c')]'
But as I said above, there were no problems with the code you posted, so far as I can tell.
CREATE TABLE template
(
id NUMBER (10)
GENERATED BY DEFAULT ON NULL AS IDENTITY ( START WITH 2),
triggerevent CLOB NOT NULL,
triggercode CLOB NOT NULL,
templatetypeid NUMBER (10) NOT NULL,
PRIMARY KEY (id)
)
/
BEGIN
INSERT INTO template (id,
triggerevent,
triggercode,
templatetypeid)
VALUES (
1,
'insert, update <<column>> | <<table>>',
'CREATE OR REPLACE TRIGGER BRG_{code}_{attribute_table}_TRG
BEFORE DELETE OR INSERT OR UPDATE
ON {attribute_table}
FOR EACH ROW
DECLARE
L_OPER VARCHAR2(3);
L_ERROR_STACK VARCHAR2(4000);
BEGIN
IF INSERTING
THEN
L_OPER := ''INS'';
ELSIF UPDATING
THEN
L_OPER := ''UPD'';
ELSIF DELETING
THEN
L_OPER := ''DEL'';
END IF;
DECLARE
L_PASSED BOOLEAN := TRUE;
BEGIN
IF L_OPER IN (''INS'', ''UPD'')
THEN
IF ''{operand}'' = ''BETWEEN'' THEN
L_PASSED := :NEW.{attribute_column} >= {range_min} AND :NEW.{attribute_column} <= {range_max};
ELSE
L_PASSED := :NEW.{attribute_column} < {range_min} OR :NEW.{attribute_column} > {range_max};
END IF;
IF NOT L_PASSED
THEN
raise_application_error(-20000, {error});
END IF;
END IF;
END;
END;',
1);
END;
For this quoting mechanism I prefer to use q'~blbelbe~' - ~ is very rare char.
And your problem is solved here Oracle 12c . Text literals
If the opening quote_delimiter is one of [, {, <, or (, then the closing quote_delimiter must be the corresponding ], }, >, or ). In all other cases, the opening and closing quote_delimiter must be the same character.

Model a Table that depends on a set of values in referenced table that can or not be present at the same time

I have a table 'medical_observations' that in one field references other table 'sypstoms_at_arriving' that describes a list of possible symptoms.
CREATE TABLE `patients`(
id_patient INTEGER NOT NULL PRIMARY KEY,
name VARCHAR(25) ,
address VARCHAR(50) ,
CONSTRAINT `uc_Info_Patient` UNIQUE (`id_patient`)
);
INSERT INTO `patients` values (1,'joe','joe´s address');
INSERT INTO `patients` values (2,'moe','moe´s address');
INSERT INTO `patients` values (3,'karl','karle´s address');
INSERT INTO `patients` values (4,'lenny','lenny´s address');
CREATE TABLE `symptoms_at_arrival` (
symptom_at_arrival varchar(30) primary key
);
INSERT INTO `symptoms_at_arrival` values ('vomit');
INSERT INTO `symptoms_at_arrival` values ('urine');
INSERT INTO `symptoms_at_arrival` values ('dizziness');
INSERT INTO `symptoms_at_arrival` values ('convulsion');
CREATE TABLE `medical_observations`(
id_medical_observation INTEGER NOT NULL PRIMARY KEY,
id_patient INTEGER NOT NULL,
symptom_at_arrival VARCHAR(30),
FOREIGN KEY (id_patient) references `patients` (id_patient),
FOREIGN KEY (symptom_at_arrival) references `symptoms_at_arrival` (symptom_at_arrival ),
CONSTRAINT `uc_Info_medical_Observation` UNIQUE (`id_medical_observation`,`id_patient`)
);
My doubt is how to model or store th case when patient has several symptoms... and not just one.
If that would be the case the name of symptom would be enough...
But if patient show several symptoms at the same time?
Update
I have done a sqlfiddle, I was thinking to add a kind of table with 1's and 0's representing if patient shows certain symptom... Would that be correct?
You'll have to make connection in the foreign keys
|patient| |medical_observations| |symptoms_at_arriving|
--------- ---------------------- ----------------------
**id** 1 ----| **id_medical_observation** |-----1 **id**
name |-M **id_patient** | symptom_at_arrival
**symptom_at_arrival** M---|
Try this, don't have mysql here to test, making table multi primary key to support multiple symptoms at same time
CREATE TABLE `symptoms_at_arriving` (
id integer not null primary key autoincrement,
symptom_at_arrival varchar(30)
);
INSERT INTO `symptom_at_arrival' values ('vomit');
INSERT INTO `symptom_at_arrival` values ('urine');
INSERT INTO `symptom_at_arrival` values ('dizziness');
INSERT INTO `symptom_at_arrival` values ('convulsion');
CREATE TABLE `medical_observations`(
id_medical_observation INTEGER NOT NULL,
id_patient INTEGER NOT NULL,
symptom_at_arrival integer not null,
FOREIGN KEY (id_patient) references `patients` (id_patient),
FOREIGN KEY (symptom_at_arrival) references `symptoms_at_arriving` (symptom_at_arrival,
PRIMARY KEY (id_medical_observation, id_patient, symptom_at_arrival)
);