PostgreSQL CREATE TABLE: weird syntax error [closed] - sql

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have the following CREATE TABLE command:
create table user_payment_status(
ID serial primary key,
user_ID serial not null,
last_pay_date time with time zone,
next_pay_date time with time zone,
due_days integer,
warning_email_sent boolean not null,
user_visible boolean not null,
user_searchable boolean not null,
user_on_free_plan boolean not null,
created_at time with time zone,
CONSTRAINT fk_user
FOREIGN KEY(user_id)
REFERENCES users(id)
);
but I always get a misterious SQL Error [42601]: ERROR: syntax error at or near "not" error message for the line user_on_free_plan boolean not null,.
It doesn't matter where I place this line into the command, the problem is always in this line. Can you help me, what am I missing here, what should be the problem?
I have a lot of similar CREATE TABLE-s, all of them are executed without any problem.

Works here:
create table user_payment_status
( id SERIAL primary key
, user_id INTEGER not null -- <<--
, created_at TIMESTAMP with time zone -- <<--
, last_pay_date TIMESTAMP with time zone -- <<--
, next_pay_date TIMESTAMP with time zone -- <<--
, due_days integer
, warning_email_sent boolean not null
, user_visible boolean not null
, user_searchable boolean not null
, user_on_free_plan boolean not null
-- , CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(id)
);
Some notes:
The FOREIGN KEY may refer to a serial, but it itself should be an int
the time fields should probably be timestamps

Thanks for the comments, dbfiddle and the idea of invisible not allowed ASCII character solved the issue:
between on_free_plan and boolean there was an ASCII "A0" character instead of ASCII "20" (space). Changed it to space, and works like a charm.
Thanks again! :-)

Related

i keep getting a "missing right parenthesis" error. But cannot see anything visibly wrong [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I am new to SQL and I have been trying to make this table however when I put the duration of the tour as an integer that would last an hour and a half. however, it keeps showing up as red and giving the error message that it's "missing the right parentheses". I've only been taught how to use integers but not sure if this is the right one to use in this situation.
and for the table that will be referenced from that table is saying that there is nothing to reference and rightfully so because I have yet to make the table. but if anybody could help with this problem it would be much appreciated!
create table qualification
( tour_id varchar2(8) not null
, guide varchar2(8) null
, date_passed date null
, primary key (tour_id, guide)
, foreign key (guide) references guide(guide_no)
, foreign key (tour_id) references tour(tour_id)
);
create table tour
( tour_id varchar2(8) primary key
, tour_name varchar2(20) not null
, duration_of_tour integer (180) null
, standard_cost integer (6,2) not null
);
Oracle has the NUMBER datatype.
INTEGER(n,m) wouldn't work anywhere.
Standard SQL would be DECIMAL(n,m) then.
So this statement will work
create table tour
( tour_id varchar2(8) primary key
, tour_name varchar2(20) not null
, duration_of_tour number(38,0) null
, standard_cost number(6,2) not null
);
But the size of duration_of_tour might be too big.
At the best of my knowledge, you can't define a column with a type of integer WITH precision. The "int" or "integer" are just aliases for type number with already defined precision. I'm almost sure it is aliases for max allowed - "number(38)"
So, it's either "integer" or "number(x,y)"
However if you updated your col type from integer(180) to number(180) you'll hit another error because number can't be 180 digits long.
Check this doc, perhaps you'll find something useful there

SQL Server 2019 ProgrammingError: Incorrect syntax near 'AUTO_INCREMENT' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying to figure out why I'm getting this error and how to solve it.
The code:
CREATE TABLE sqlalchemy_generic_types
(
'No.' INT NOT NULL AUTO_INCREMENT,
'Object Name' VARCHAR(25) NOT NULL,
'Description' VARCHAR(100) NOT NULL,
PRIMARY KEY('No.')
);
The error:
(pyodbc.ProgrammingError) ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near 'AUTO_INCREMENT'. (102) (SQLExecDirectW)")
[SQL: /* Creating a table */
CREATE TABLE sqlalchemy_generic_types (
'No.' INT NOT NULL AUTO_INCREMENT,
'Object Name' VARCHAR(25) NOT NULL,
'Description' VARCHAR(100) NOT NULL,
PRIMARY KEY('No.')
);]
(Background on this error at: http://sqlalche.me/e/13/f405)
SQL Server doesn't support auto-increment. Nor does SQL Server -- or any other database -- support single quotes for column names (as far as I know).
I would recommend writing the statement as:
CREATE TABLE sqlalchemy_generic_types (
sqlalchemy_generic_type_id INT IDENTITY(1, 1) PRIMARY KEY,
ObjectName VARCHAR(25) NOT NULL,
Description VARCHAR(100) NOT NULL
);
Note the changes:
IDENTITY() is assigns an increasing value to the id.
The id is given a meaningful name.
The space is removed from ObjectName, so the name does not need to be escaped.
No escape characters are needed to define the table.
You need to provide the double quotes for column names if they contain special characters or spaces.
Auto_increment? -- you must be looking for identity(1,1)
You can use following query:
CREATE TABLE sqlalchemy_generic_types (
"No." INT NOT NULL identity(1,1),
"Object Name" VARCHAR(25) NOT NULL,
"Description" VARCHAR(100) NOT NULL,
PRIMARY KEY("No.")
);
Db<>fiddle

DEFAULT constraint not working in SQL Oracle? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying to get a constraint (this is just test code) but when I add the data (complete the whole table) and run it with leaving out Apptime data in the values it doesn't default to hello in the table it's just '-'. I'm using Oracle Live SQL. Any clue as to how this is done? Would be good if I could do it in the schema and not a constraint but if it has to be a constraint outside that's okay.
Thank you and apologies if I did anything wrong in this question, I'm new haha.
DROP TABLE Bok;
CREATE TABLE Bok (
BokID number(3) NOT NULL PRIMARY KEY,
Appdate varchar2(4),
Apptime varchar2(5) DEFAULT 'hello'
);
In order to default to work the insert query has to use DEFAULT keyword or skip the column
INSERT INTO Bok(BokID, Appdate, appTime)
VALUES (1, 'a', DEFAULT);
INSERT INTO Bok(BokID, Appdate)
VALUES (1, 'a');
One more option when DEFAULT ON NULL is defined:
CREATE TABLE Bok (
BokID number(3) NOT NULL PRIMARY KEY,
Appdate varchar2(4),
Apptime varchar2(5) DEFAULT ON NULL 'hello'
);
INSERT INTO Bok(BokID, Appdate, appTime)
VALUES (1, 'a', NULL);

"ORA-00984: column not allowed here" when attempting to create table using SQL [duplicate]

This question already has answers here:
Comparing Dates in Oracle SQL
(5 answers)
Closed 6 years ago.
I am attempting to run this script to create a table, however I get a column not allowed error. After doing some research it seems that it could be that it could be a syntax error concerning values, however I am not inserting any values.
CREATE TABLE SALESPERSON (
sales_id VARCHAR2(10) PRIMARY KEY,
sales_fname VARCHAR2(35) NOT NULL,
sales_lname VARCHAR2(35) NOT NULL,
sales_email VARCHAR2(35) NOT NULL,
sales_region VARCHAR2(35) NOT NULL CHECK(sales_region IN ('NORTH','SOUTH','EAST','WEST')),
sales_phone CHAR(10) NOT NULL,
hire_date DATE DEFAULT 01-JAN-2001 NOT NULL);
What am I overlooking?
I am getting a different error message, but probably due to the same mistake. You can't set the default to 01-JAN-2001. Perhaps just putting it in single quotes will fix it; better, to_date('01-JAN-2001', 'DD-MON-YYYY').
Put quotes on your default date:
hire_date DATE DEFAULT '01-JAN-2001' NOT NULL);

Oracle Application Express - 'ORA-01843: not a valid month error' in one table only

I am creating a database in Oracle Application Express and am having a problem inserting a date into one of the tables.
INSERT INTO VIEWING( VIEWING_ID, VIEWING_DATE, TIME, PROPERTY_ID, AGENT_ID)
VALUES('3', '12-07-2015' ,'10:00','1', '101');
I've tried every combination of date format, and trying to force the date to my correct format
to_date('12-07-2015','MM-DD-YYYY')
But nothing is working
CREATE TABLE Viewing (
Viewing_ID number(10) NOT NULL,
Viewing_Date date NOT NULL,
Time timestamp(7) NOT NULL,
Property_ID number(10) NOT NULL,
Agent_ID number(10) NOT NULL,
PRIMARY KEY (Viewing_ID));
ALTER TABLE Viewing ADD CONSTRAINT FK_Viewing_Agent_ID FOREIGN KEY (Agent_ID) REFERENCES Agent (Agent_ID);
ALTER TABLE Viewing ADD CONSTRAINT FK_Viewing_Property_ID FOREIGN KEY (Property_ID) REFERENCES Property (Property_ID);
Every Resource I have found suggests it is most likely a parsing or syntax error but so far nothing has helped.
I have a second table in the schema that I can insert dates into without a problem, the only difference on this table is that the date is required (I have tried making it nullable to test and I still get the same error)
I should point out that Im am completely new to Oracle and this is part of a study project. If I had I choice I would be using SQL Server! But Ive been at this for hours and think its time to admit defeat!
Thanks
It is due to TIME column, not VIEWING_DATE. This worked:
INSERT INTO VIEWING( VIEWING_ID, VIEWING_DATE, TIME, PROPERTY_ID, AGENT_ID)
VALUES(4, date '2015-12-07' , timestamp '2015-12-07 10:00:00',1, 101);