Create Table - Time Statement - sql

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]

Related

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)
);

SQL Server 2012 - Create Data Type from existing table

I have a stored procedure which accepts a user-defined table type called dbo.NodeTableType:
ALTER PROCEDURE [dbo].[InsertNonExistingNode]
(#TableVariable dbo.NodeTableType READONLY)
It seems redundant, because the dbo.NodeTableType is identical to an actual table in my database dbo.Nodes.
Do I really have to create this data-type in order to except it as a parameter in my stored procedure?
If the answer to #1 is "Yes", then is there a way to create this data-type by pointing it at the table? Currently, I have to create it this way:
CREATE TYPE NodeTableType AS TABLE
(NodeTypeId SMALLINT NOT NULL,
Location NVARCHAR(50) NULL,
DisplayName NVARCHAR(100) NULL,
AccessLevel SMALLINT NOT NULL,
IsEnabled BIT NOT NULL,
CreatedOn DATETIME2(7) NULL,
CreatedBy NVARCHAR(150) NULL,
ModifiedOn DATETIME2(7) NULL,
ModifiedBy NVARCHAR(150) NULL,
NativeId BIGINT NOT NULL,
SourceId INT NOT NULL,
Name NVARCHAR(100) NOT NULL,
Alias NVARCHAR(100) NULL
)
The only way to define a user-defined table type is through CREATE TYPE statement according to documentation at this URL: User Defined Types. So you cannot use a table for this.
I would recommend to stick to the standard practice of passing a table valued parameter that has been created with CREATE TYPE.
Another approach as outlined below can be used provided you can live without passing a table valued parameter to your procedure, and I am not sure if that is possible in your case.
In your stored procedure, you could populate a table variable of the same type as the original NodeTableType type. Of course, you would need to decide the logic for populating this table variable; I have assumed that the logic is something as simple as NodeId < 10 as an example only; in your case this rule would be different and probably more complex.
DECLARE #myTable dbo.NodeTableType;
INSERT INTO #myTable(
NodeTypeId SMALLINT NOT NULL,
Location NVARCHAR(50) NULL,
DisplayName NVARCHAR(100) NULL,
AccessLevel SMALLINT NOT NULL,
IsEnabled BIT NOT NULL,
CreatedOn DATETIME2(7) NULL,
CreatedBy NVARCHAR(150) NULL,
ModifiedOn DATETIME2(7) NULL,
ModifiedBy NVARCHAR(150) NULL,
NativeId BIGINT NOT NULL,
SourceId INT NOT NULL,
Name NVARCHAR(100) NOT NULL,
Alias NVARCHAR(100) NULL)
SELECT * from dbo.Nodes where NodeId < 10;
--now you can use #myTable rather than #TableVariable
--in your stored procedure

How to Sql defined function as columns e.g User_Id int when creating tables [duplicate]

I am creating tables in Sql Management Studio 2012 using SQL. How do I make fields or columns with names that are already defined in Sql Server e.g User_ID, User_Name. I want to use them as fields in my tables.
Table definition from Duplicate Post:
create table Ticket(
Ticket_Id varchar(10) not null,
TicketType_Id varchar(3) not null,
Ticket_PurchaseDate DateTime null,
LottoDraw_Id int null,
User_Id int null,
Ticket_IsWinner bit null
Primary Key(Ticket_Id,TicketType_Id)
)
Warp the column name like in brackets [ ] ... such as
create table Ticket(
Ticket_Id varchar(10) not null,
TicketType_Id varchar(3) not null,
Ticket_PurchaseDate DateTime null,
LottoDraw_Id int null,
[User_Id] int null,
Ticket_IsWinner bit null
Primary Key(Ticket_Id,TicketType_Id)
)

SQL server 2012 tables issue, sysdate and to_date issue

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');