SQL server 2012 tables issue, sysdate and to_date issue - sql

I have two issues while trying to create tables.
For **sysdate*** it says invalid column name
for *TO_DATE('01-JAN-2008','DD-MON-YYYY')));* it says TO_DATE is not a reconigized built-in function name.
^ both are in the Table Invoice.
This is using SQL SERVER 2012
CREATE TABLE VENDOR(
V_CODE INTEGER NOT NULL UNIQUE,
V_NAME VARCHAR(35) NOT NULL,
V_CONTACT VARCHAR(15) NOT NULL,
V_AREACODE CHAR(3) NOT NULL,
V_PHONE CHAR(8) NOT NULL,
V_STATE CHAR(2) NOT NULL,
v_ORDER CHAR(1) NOT NULL,
PRIMARY KEY (V_CODE));
CREATE TABLE PRODUCT(
P_CODE VARCHAR(10) NOT NULL,
P_DESCRIPT VARCHAR(35) NOT NULL,
P_INDATE DATE NOT NULL,
P_QOH SMALLINT NOT NULL,
P_MIN SMALLINT NOT NULL,
P_PRICE DECIMAL(8,2) NOT NULL,
P_DISCOUNT DECIMAL(5,2) NOT NULL,
V_CODE INTEGER,
PRIMARY KEY (P_CODE),
FOREIGN KEY(V_CODE) REFERENCES VENDOR ON UPDATE CASCADE);
CREATE TABLE CUSTOMER(
CUS_CODE DECIMAL PRIMARY KEY,
CUS_LNAME VARCHAR(15) NOT NULL,
CUS_FNAME VARCHAR(15) NOT NULL,
CUS_INITIAL CHAR(1),
CUS_AREACODE CHAR(3) DEFAULT '615' NOT NULL,
CHECK(CUS_AREACODE IN ('615','713','931')),
CUS_PHONE CHAR(8) NOT NULL,
CUS_BALANCE DECIMAL(9,2) DEFAULT 0.00,
CONSTRAINT CUS_UI1 UNIQUE (CUS_LNAME, CUS_FNAME));
CREATE TABLE INVOICE (
INV_NUMBER DECIMAL PRIMARY KEY,
CUS_CODE DECIMAL NOT NULL REFERENCES CUSTOMER(CUS_CODE),
INV_DATE DATE DEFAULT SYSDATE NOT NULL,
CONSTRAINT INV_CK1 CHECK (INV_DATE > TO_DATE('01-JAN-2008','DD-MON-YYYY')));

You have two issues:
the function to get the current system date and time is called SYSDATETIME() in T-SQL/SQL Server (not sysdate)
the way to convert a string to a date or datetime in T-SQL/SQL Server is using CAST or CONVERT (not TO_DATE - there is no such function in T-SQL)
Use something like
SELECT CAST('01-JAN-2008' AS DATE)
or something like that (it's highly dependent on your language/date format settings in SQL Server whether it'll work or not). If you need to specify a specific format, you can use CONVERT which allows you to use one of the many predefined formats (see relevant details in the MSDN documentation).
If that's still not enough - SQL Server 2012 has a new function called PARSE which allows you to specify any arbitrary date format that your string is formatted in. Again, see the relevant MSDN documentation for details.
The best thing is to avoid converting dates back and forth to and from strings if ever possible, and in your case, this should be easily doable! Just use:
INV_DATE DATE DEFAULT SYSDATETIME() NOT NULL,
CONSTRAINT INV_CK1 CHECK (INV_DATE > '20080101');

Related

Syntax error in CREATE TABLE statement - SQL (MS Access 2016)

I took this example from my class textbook and pretty much copied word for word from the text. The following is the code that I wrote into MS Access:
CREATE TABLE PRODUCT(
P_CODE VARCHAR(10) NOT NULL UNIQUE,
P_DESCRIPT VARCHAR(35) NOT NULL,
P_INDATE DATE NOT NULL,
P_QOH SMALLINT NOT NULL,
P_MIN SMALLINT NOT NULL,
P_PRICE NUMBER(8, 2) NOT NULL,
P_DISCOUNT NUMBER(5, 2) NOT NULL,
V_CODE INTEGER,
PRIMARY KEY (P_CODE),
FOREIGN KEY (V_CODE) REFERENCES VENDOR ON UPDATE CASCADE
);
This code produces a syntax error every time it's run within MS Access.
I tried running this query to create the table PRODUCT within my db. When reviewing the code, I couldn't find anything specific that would have caused the error, but I could be wrong due to my inexperience with SQL.
Any help would be greatly appreciated!
Per the comments the problem is the Decimal type. I'm updating the answer here for Access 2016: How do I create a decimal field in Access with Alter Table?
First enable the SQL Ansi 92 standard. In 2016 this has moved to File-Options-Object Designers-Query Design. It seems you can only enable it for new databases. So do so and create a new database then open the sql-tab of a blank query and paste the following code:
'make sure you have a VENDOR table first for instance:
CREATE TABLE VENDOR
(V_CODE AutoIncrement CONSTRAINT PrimaryKEY PRIMARY KEY);
'Then with slightly less old syntax (varchar would usually be text, smallint would usually be integer with a size, and PrimaryKey includes Not null and unique)
CREATE TABLE PRODUCTS(
P_CODE VARCHAR(10) CONSTRAINT PrimaryKey PRIMARY KEY,
P_DESCRIPT VARCHAR(35) NOT NULL,
P_INDATE DATE NOT NULL,
P_QOH SMALLINT NOT NULL,
P_MIN SMALLINT NOT NULL,
P_PRICE DECIMAL(8, 2) NOT NULL,
P_DISCOUNT DECIMAL(5, 2) NOT NULL,
V_CODE LONG REFERENCES VENDOR(V_CODE)
);
'Alter Table also works now
ALTER TABLE PRODUCTS ADD COLUMN P_PRICE DECIMAL(8,2) NOT NULL;
Caveat
At least the sql-pane tells you that something in your DDL is wrong if not what. if you are not using the default Jet database as your backend then you are stuck concatenating strings using vba and ADO according to here: https://learn.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/create-table-statement-microsoft-access-sql
At that point I would manually try manually adding the decimal columns if there where not too many.

Postgresql generated column fails when concating not null columns

I have this table definition in pgAdmin4:
CREATE TABLE IF NOT EXISTS cdr_event
(
id bigint primary key generated always as identity,
start_time timestamptz NOT NULL DEFAULT now(),
end_time timestamptz NULL,
group_id VARCHAR(10) NOT NULL,
environment VARCHAR(10) NOT NULL,
level VARCHAR(10) NOT NULL,
schema VARCHAR(30) NOT NULL,
instance INTEGER NOT NULL,
hive_instance_db_name VARCHAR(100) GENERATED ALWAYS AS (group_id||'_'||environment||'_'||level||'_'||schema||'_'||instance) STORED,
hive_static_db_name VARCHAR(100) GENERATED ALWAYS AS (group_id||'_'||environment||'_'||level||'_'||schema) STORED,
);
this fails with
ERROR: generation expression is not immutable
SQL state: 42P17
Why does postgres consider the concat mutable when the dependent columns are all NOT NULL? This thread suggests it should work
Is there anyway to create a concat-ed generated column without creating a custom concat function?
Thanks
Try keeping the involved columns of the same type, e.g. casting instance to text should do the trick:
CREATE TABLE IF NOT EXISTS cdr_event
(
id bigint primary key generated always as identity,
start_time timestamptz NOT NULL DEFAULT now(),
end_time timestamptz NULL,
group_id VARCHAR(10) NOT NULL,
environment VARCHAR(10) NOT NULL,
level VARCHAR(10) NOT NULL,
schema VARCHAR(30) NOT NULL,
instance INTEGER NOT NULL,
hive_instance_db_name VARCHAR(100) GENERATED ALWAYS AS (group_id||'_'||environment||'_'||level||'_'||schema||'_'||instance::text) STORED,
hive_static_db_name VARCHAR(100) GENERATED ALWAYS AS (group_id||'_'||environment||'_'||level||'_'||schema) STORED
);
Consider using text instead of varchar.
Demo: db<>fiddle

what does the error "missing right parenthesis" in oracle sql means

I'm trying to run this code and it seems correct to me but I'm getting an error stating that there's a right parenthesis missing.
The code is the following:
CREATE TABLE CUSTOMER
(
CUSTOMER_ID INT NOT NULL,
NAME VARCHAR(30) NOT NULL,
DATE_OF_BIRTH DATE,
PHONE_NB CHAR(8) NOT NULL,
ADDRESS VARCHAR(50),
TOTAL_SPENDING FLOAT NOT NULL DEFAULT 0.0,
PRIMARY KEY(CUSTOMER_ID)
);
Can anyone help me in solving my problem?
Since you tagged SQL Developer...
...the tool tries to give you a heads-up there will be a problem before you even hit the Execute button
The default value for the column is confusing the parser because it's not expected at that point.
Move it to after the data type and you'll be good
CREATE TABLE customer (
customer_id INT NOT NULL,
name VARCHAR2(30) NOT NULL,
date_of_birth DATE,
phone_nb CHAR(8) NOT NULL,
address VARCHAR(50),
total_spending FLOAT DEFAULT 0.0 NOT NULL,
PRIMARY KEY ( customer_id )
);
PS In oracle, use VARCHAR2, not VARCHAR. While VARCHAR will 'work', it's reserved and could mean something different in a future release.
You are using wrong order of column definition clauses: the constraint (NOT NULL) should follow the default value.
This is the right way:
CREATE TABLE CUSTOMER
(
CUSTOMER_ID INT NOT NULL,
NAME VARCHAR(30) NOT NULL,
DATE_OF_BIRTH DATE,
PHONE_NB CHAR(8) NOT NULL,
ADDRESS VARCHAR(50),
TOTAL_SPENDING FLOAT DEFAULT 0.0 NOT NULL ,
PRIMARY KEY(CUSTOMER_ID)
);

Missing Right parenthesis error?

I'm trying to create a table in sqldeveloper however I keep getting a missing right parenthesis error when there are no missing right parenthises. Any fixes for this or am i just trying to create a table the wrong way?
CREATE TABLE Patient_T1(
PATIENT_ID INT(100) NOT NULL,
FIRST_NAME VARCHAR(20) NOT NULL,
LAST_NAME VARCHAR(30) NOT NULL,
DOB CHAR(10) NOT NULL,
P_STREET_ADRESS VARCHAR(50) NOT NULL,
PATIENT_CITY VARCHAR(30) NOT NULL,
PATIENT_STATE CHAR(2) NOT NULL,
PATIENT_ZIP CHAR(5) NOT NULL,
PATIENT_PHONE CHAR(12) NOT NULL,
PATIENT_ROOM INT(1000) NOT NULL,
CONSTRAINT PATIENT_PK PRIMARY KEY(PATIENT_ID));
Not sure why Oracle gives that error message instead of something more helpful, but the cause is the precision applied to INT, switch from INT(100) and INT(1000) to just INT:
CREATE TABLE Patient_T1(
PATIENT_ID INT NOT NULL,
FIRST_NAME VARCHAR(20) NOT NULL,
LAST_NAME VARCHAR(30) NOT NULL,
DOB CHAR(10) NOT NULL,
P_STREET_ADRESS VARCHAR(50) NOT NULL,
PATIENT_CITY VARCHAR(30) NOT NULL,
PATIENT_STATE CHAR(2) NOT NULL,
PATIENT_ZIP CHAR(5) NOT NULL,
PATIENT_PHONE CHAR(12) NOT NULL,
PATIENT_ROOM INT NOT NULL,
CONSTRAINT PATIENT_PK PRIMARY KEY(PATIENT_ID));
There are multiple issues with your table DDL:
INT(100) - In Oracle, an INTEGER is an ANSI SQL data type which refers to numeric values which have only an integer portion and no floating point or decimal part. That is, an INTEGER will only store whole numbers ONLY.
VARCHAR(20) - Oracle strongly recommends to use VARCHAR2.
From documentation,
VARCHAR Datatype
The VARCHAR datatype is synonymous with the VARCHAR2 datatype. To
avoid possible changes in behavior, always use the VARCHAR2 datatype
to store variable-length character strings.
CHAR(10) - better use VARCHAR2 as CHAR is blank-padded to the fixed length. That's a wastage of storage.
From documentation,
CHAR Datatype
The CHAR datatype stores fixed-length character strings. If you give a
shorter value, then the value is blank-padded to the fixed length.
Only the issue# 1 would throw an error, anyway fixing all the above issues would let you create the table.
For example,
SQL> CREATE TABLE Patient_T1
2 (
3 PATIENT_ID NUMBER NOT NULL,
4 FIRST_NAME VARCHAR2(20) NOT NULL,
5 LAST_NAME VARCHAR2(30) NOT NULL,
6 DOB DATE NOT NULL,
7 P_STREET_ADRESS VARCHAR2(50) NOT NULL,
8 PATIENT_CITY VARCHAR2(30) NOT NULL,
9 PATIENT_STATE VARCHAR2(2) NOT NULL,
10 PATIENT_ZIP VARCHAR2(5) NOT NULL,
11 PATIENT_PHONE VARCHAR2(12) NOT NULL,
12 PATIENT_ROOM NUMBER NOT NULL,
13 CONSTRAINT PATIENT_PK PRIMARY KEY(PATIENT_ID)
14 );
Table created.

Create Table - Time Statement

I am having trouble trying to create a table using MS Access.
I know for sure my problem lies in the "datetime" field but I can not figure what I am missing or doing wrong.
When I click "run" I get the
"Syntax Error in Field Definition"
Create Table Appointments
(DocID char(4) not null primary key,
PatID char(8) not null,
Day varchar(8) not null,
Time datetime not null,
Procedure varchar(50) null);
Time and procedure are reserved words, and therefore should be escaped:
Create Table Appointments
(DocID char(4) not null primary key,
PatID char(8) not null,
[Day] varchar(8) not null,
[Time] datetime not null,
[Procedure] varchar(50) null);
Or better yet, find names that aren't reserved words:
Create Table Appointments
(DocID char(4) not null primary key,
PatID char(8) not null,
AppointmentDay varchar(8) not null,
AppointmentTime datetime not null,
MedicalProcedure varchar(50) null);
Here Procedure and Time are reserved words and so need to be escaped using [] like below. See Documentation for more information
Create Table Appointments
(DocID char(4) not null primary key,
PatID char(8) not null,
[Day] varchar(8) not null,
[Time] datetime not null,
[Procedure] varchar(50) null);
As Time & Procedure are reserved keyword, so wrap Time & Procedure column in brackets [] or choose alternate names, if possible. See List of Keyword
[Time] and [Procedure]