Trying to create a table in PostgreSQL from the Commandline. Below is the code that I am try to execute to create the table.
execute to create the table.
CREATE TABLE RISK_DETAILS(
RISK_DTL_ID SERIAL PRIMARY KEY, DATE_RAISED TIMESTAMP NOT NULL, PROJECT_NAME VARCHAR (50),
RAISED_BY VARCHAR(50), DESCIPTION VARCHAR(1024), CATEGORY VARCHAR(50), PROBABILITY NUMERIC, IMPACT_ID INTEGER, INITIAL_RISL_VALUE NUMERIC,
CURRENT_RISK_VALUE NUMERIC, I
MITIGATING_ACTION VARCHAR(500),
MITIGATING_PLANNED_DATE TIMESTAMP NOT NULL, CREATED_ON TIMESTAMP NOT NUMM DEFAULT NOW,
CONSTRAINT(FK_IMPACT_ID) FOREIGN KEY(IMPACT_ID) REFERENCES IMPACT_DROPDOWN( IMPACT_ID)
);
When I run this code on the command Line, I get the following error.
ERROR: syntax error at or near "NOT" LINE 3: DATE_RAISED TIMESTAMP NOT NULL
If I remove the code which is causing the error and execute once more, then it complains at the varchar in Project name field.
I
2
Answers
After beautifying the query, you will see what the problem is.
The DESCIPTION column name was changed to DESCRIPTION.
The INITIAL_RISL_VALUE column name was changed to INITIAL_RISK_VALUE.
Delete "I" between CURRENT_RISK_VALUE NUMERIC, and MITIGATING_ACTION VARCHAR(500),
Changed NOT NUMM to NOT NULL
When setting the default value for the CREATED_ON column, parentheses were added to NOW.
The foreign key constraint FK_IMPACT_ID was added using the ALTER TABLE syntax.
Result Query is below
I formatted your query to make it easier to read:
After fixing these issues the query can run as:
See running example at db<>fiddle.