Postgres SQL syntax error near invisible 'REFERENCES'? - sql

I am trying to create two tables with the following code.
CREATE TABLE assessments (
id INT UNIQUE PRIMARY KEY,
name VARCHAR(100),
type VARCHAR(10),
total_points NUMERIC,
weight NUMERIC
CHECK(weight >= 0)
CHECK(weight <= 100),
due_date DATE,
section_id INT REFERENCES sections(id)
);
CREATE TABLE enrollment_assessments (
id INT UNIQUE PRIMARY KEY REFERENCES enrollments(id),
assignment_id REFERENCES assignments(id),
assessment_type REFERENCES assessments(type),
points NUMERIC
CHECK (points >= 0)
CHECK (points <-100),
);
However, I get the error :
[Code: , SQL State: 42601] ERROR: syntax error at or near
"REFERENCES" Position: 112
which is saying the error is near the first line of code? I don't understand this error at all.

I do not like the comma in this part:
CHECK (points >= 0)
CHECK (points <-100),

you miss data types and extra comma has to be removed:
CREATE TABLE enrollment_assessments (
id INT UNIQUE PRIMARY KEY REFERENCES enrollments(id),
assignment_id INT REFERENCES assignments(id),
assessment_type VARCHAR(10) REFERENCES assessments(type),
points NUMERIC
CHECK (points >= 0)
CHECK (points <-100)
);

Related

ORA-00904: : Invalid Identifier in SQL

I am currently working on a SQL assignment, but first I need to create tables which it is not letting me do. I have 3 tables which are Customer, PurchasedDeal, and Usage. I manage to create the first 2 tables successfully, but I am having a little difficulty with creating the Usage table. For some reason it is giving me this error.
Error at line 2:
ORA-00904: : Invalid Identifier
If anyone could help me understand why it is giving me this error I would really appreciate it. Thanks. It is saying it is having problems with uID INT and I am using putty to create these tables.
CREATE TABLE Customer(
CustomerID INT NOT NULL PRIMARY KEY,
CustomerName VARCHAR(100),
Phone VARCHAR(15)
);
CREATE TABLE PurchasedDeal(
DID INT NOT NULL PRIMARY KEY,
dealName VARCHAR(100),
cost FLOAT,
totalValue FLOAT,
balance FLOAT,
CustomerID INT,
FOREIGN KEY(CustomerID) REFERENCES Customer(CustomerID)
);
CREATE TABLE Usage(
uID INT,
uDate DATE,
cost FLOAT,
DealID INT,
PRIMARY KEY(DealID, uID),
FOREIGN KEY(DealID) REFERENCES PurchasedDeal(DID)
);
The term ‘uid’ is reserved in Oracle. This term cannot be used as a column name in the Oracle environment.
Query: SELECT uid FROM t1
Result: will execute correctly
Query: SELECT U.”uid” FROM x.t1 U
Result: will through error:
ORA-00904: “U”.”uid”: invalid identifier 00904. 00000 – “%s: invalid identifier”
Query: SELECT U.”UID” FROM x.t1 U
Result: will execute correctly as uid is replaced with UID (in capital)
Rectification in below table and we are good to go.
CREATE TABLE Usage(
uID1 INT,
uDate DATE,
cost FLOAT,
DealID INT,
PRIMARY KEY(DealID, uID1),
FOREIGN KEY(DealID) REFERENCES PurchasedDeal_test(DID)
);

sql- not null with default, check constraint

I have created a table and I'm asked to add a constraint to a column called no_semesters. the condition is that the no has to be >= 0 and it should be not null with default, and the default value is 6. When I run this statement:
INSERT INTO PREMIERD08/PGM (PGM_ID, PGM_NAME)
VALUES('CPD', 'COMPUTER PROGRAMMING')
I get an error message saying column - no_semesters cant be null. why wont it set to the default value which is 6?
this was the statement -
CREATE TABLE PREMIERD08/PGM (
PGM_ID CHAR(3) NOT NULL,
PGM_NAME CHAR(40) NOT NULL,
NO_SEMESTERS NUMERIC ( 1) NOT NULL WITH DEFAULT 6,
CONSTRAINT PGM_PGM_NAME_UK UNIQUE (PGM_NAME),
CONSTRAINT PGM_PGM_ID_CK CHECK (PGM_ID IN ('CPA', 'CPD') ),
CONSTRAINT PGM_PGM_ID_PK PRIMARY KEY (PGM_ID), CONSTRAINT PGM_NO_SEMESTERS_CK CHECK (NO_SEMESTERS >= 0)
)
Your code works in Db2 (for LUW) - the only thing to change is the table name needs to be in "" because of the /
CREATE TABLE "PREMIERD08/PGM" .....
INSERT INTO "PREMIERD08/PGM" (PGM_ID, PGM_NAME)
VALUES('CPD', 'COMPUTER PROGRAMMING')

Use a condition while creating a table in SQL

I'd like to create a table with a condition
create table TOTO
(
Id int not null,
zip as (if(zip > '00999' and zip < '96000') then zip) ,
PRIMARY KEY (Id)
);
All I get is an error message.
Do you know how to do that with the "zip" in type char ?
Thank you for your help !
You may want to use CHECK constraint instead
CREATE TABLE TOTO
(
Id int NOT NULL,
zip char,
PRIMARY KEY (Id),
CHECK (zip > '00999' AND zip < '96000')
);
You can get this done either using a CHECK CONSTRAINT or using a BEFORE INSERT or INSTEAD OF trigger
An example:
CREATE TRIGGER ChkZip
ON TOTO
INSTEAD OF INSERT
AS
BEGIN
IF (inserted.zip > '00999' and inserted.zip < '96000')
BEGIN
INSERT INTO TOTO (id,zip) VALUES (insered.id, inserted.zip)
END
ELSE
BEGIN
RAISERROR ('The entered zip code doesn't match criteria.' ,10,1)
ROLLBACK TRANSACTION
END
END
CREATE TABLE TOTO
(
Id INT NOT NULL,
zip VARCHAR2(10),
PRIMARY KEY (Id),
CONSTRAINT CHK_zip CHECK (zip > to_number('00999') AND zip < to_number('96000'))
);
You could use check constraint - the above script is based on Oracle Sql function
You need to create a new CHECK constraint so that your field is guaranteed to comply with this constraint each time it is set or modified (insert or update).
Here is an example adapted to SQL server (as you seem to be using that dbms) where the constraint is named so you can identify it more easily (better for maintainability)
create table TOTO
(
Id int not null,
zip char ,
PRIMARY KEY (Id),
CONSTRAINT CHK_zip CHECK(zip > '00999' and zip < '96000')
);
Note that you could formulate it using patterns as in the link I provided.

Can't serialize transient record type postgres

I am trying to make my calculation dynamic based on certain criteria as below, but when I try to send the fields dynamically in to my calculation logic, it fails with the error " Can't serialize transient record type":
Create table statement :
create table calculation_t(
Id serial,
product_id integer not null,
metric_id integer not null,
start_date date,
end_date date,
calculation_logic varchar(50),
insert_timestamp timestamp default current_timestamp,
CONSTRAINT calculation_pk PRIMARY KEY(Id),
CONSTRAINT calculation_pid_fk FOREIGN KEY(product_id) REFERENCES Product_T(Product_id),
CONSTRAINT calc_mid_fk FOREIGN KEY(metric_id) REFERENCES metric_T(metric_id)
);
Insert statement :
insert into calculation_t(product_id,metric_id,calculation_logic)
select a.product_id,b.metric_id,
(case when b.metric_id=2 then
('$1-$2') else
'$1/$2' end) calc
from product_t a,metric_t b
Select statement which throws the mentioned error :
select *,(1,2,calculation_logic) from calculation_t
Note : I am using Greenplum database.
Try to remove parenthesis form your query:
select *,1,2,calculation_logic from calculation_t
It worked for me.
Thanx,

sql code for firebird doesn't compile

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.