ALTER TABLE syntax - missing DIRECTORY keyword - sql

I am trying to alter a table in Oracle database by adding two new columns to it with SQL query as below:
ALTER TABLE Members
ADD annual_dues NUMBER(5,2) not null DEFAULT '52.50',
ADD payment_date DATE;
On executing it, I am getting an error as below:
SQL Error: ORA-30649: missing DIRECTORY keyword
I have played around it but it didn't help. What is wrong in the SQL query?

I think you need to put NOT NULL after the DEFAULT 52.50:
ALTER TABLE Members
ADD ( annual_dues NUMBER(5,2) DEFAULT 52.50 NOT NULL
, payment_date DATE );

Related

Adding a computed column in Postgres SQL based on a date

Here my table.
CREATE TABLE annual_goals (
id INTEGER PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
content TEXT NOT NULL,
complete BOOLEAN DEFAULT false,
date_created TIMESTAMP DEFAULT now() NOT null,
date_modified TIMESTAMP DEFAULT now() NOT null
);
I want to alter it such that I can add a new column called month_num that returns the number of the month given date_created (i.e. if the date_created of an entry is 5/31/2019, I want the month_num to automatically populate 5).
I tried the following but I'm getting an error that states "ERROR: syntax error at or near "A
S"
ALTER TABLE annual_goals
ADD year_num
AS year(date_created);
Any help is greatly appreciated. Thanks
You have two errors in the code. One is that MySQL requires the type. The second is that the expression needs to be surrounded by parentheses:
ALTER TABLE annual_goals ADD year_num int AS ( year(date_created) );
EDIT:
In Postgres, you can use the syntax:
alter table annual_goals
add year_num int generated always as (extract(year from date_created)) stored;
Here is a db<>fiddle.
You could try this:
ALTER TABLE annual_goals
ADD year_num int;
UPDATE annual_goals
SET year_num=year(date_created);

How to modify column to auto increment in PL SQL Developer?

I have created one table in PL SQL Developer.
CREATE TABLE Patient_List
(
Patient_ID number NOT NULL,
Patient_Name varchar(50) NOT NULL,
Patient_Address varchar(100) NULL,
App_Date date NULL,
Descr varchar(50),
CONSTRAINT patient_pk PRIMARY KEY(Patient_ID)
);
I want to auto increment Patient_ID, I tried altering the table and modifying the Patient_ID column but it's showing an error "invalid ALTER TABLE option"
ALTER TABLE Patient_List
MODIFY Patient_ID NUMBER NOT NULL GENERATED ALWAYS AS IDENTITY;
Please help, Thanks in advance.
This is not possible.
Oracle 10g didn't even have identity columns, they were introduced in Oracle 12.1
But even with a current Oracle version, you can't convert a regular column to an identity column. You would need to add a new one.
Before identity columns, the usual way was to create a sequence and a trigger to populate the column.
See here: How to create id with AUTO_INCREMENT on Oracle?
If anybody wants to modify existing column as auto_increment use this three lines
alter table Product drop column test_id;
create sequence Product_test_id_seq INCREMENT BY 1 nocache;
alter table Product add test_id Number default Product_test_id_seq.nextval not null;

SQL Server CE : alter column Token in error = DEFAULT

I am using SQL Server CE and I have a table [accounts]. I was modifying the column [ondate] so that it can have a default of GetDate.
My query is
ALTER TABLE [accounts]
ALTER COLUMN [ondate] DATETIME NULL DEFAULT GETDATE()
But I get this error :
Token in error = DEFAULT
I have tried getdate without parenthesis, with single quotes (') but the error is still the same.
How to modify the column [ondate] to have default of getdate?
Here is the correct syntax
ALTER TABLE accounts ADD DEFAULT getdate() FOR [ondate]
It is always better to add a name to constraints
ALTER TABLE accounts ADD CONSTRAINT DF_ONDATE_GETDATE DEFAULT getdate() FOR [ondate]
This will work for you
Alter Table [accounts] ADD DEFAULT getdate() for [ondate]

Why does this 'modify table' statement fail?

I'm trying to add a 'not null' constraint to a column in Oracle 9.
ALTER TABLE user_roles modify group_id varchar2(36 char) set not null;
However, the operation fails with an odd error:
Error report:
SQL Error: ORA-12987: cannot combine drop column with other operations
12987. 00000 - "cannot combine drop column with other operations"
*Cause: An attempt was made to combine drop column with other
ALTER TABLE operations.
*Action: Ensure that drop column is the sole operation specified in
ALTER TABLE.
Any ideas why this is failing?
Remove set:
ALTER TABLE user_roles modify group_id varchar2(36 char) not null
And yes, Oracle's errors can be very misleading.
It turns out the syntax of the above statement is wrong. It should be:
ALTER TABLE user_roles modify group_id varchar2(36 char) not null;
Still, the presence of an erroneous 'set' leads to a very odd error!
I'm trying to add a 'not null'
constraint to a column in Oracle 9.
If you are really trying jsut to make the column NOT NULL (i.e. you don't want to change the datatype at the same time) you just need to
ALTER TABLE user_roles modify not null;

Why does Oracle think I'm missing a right parenthesis?

In Oracle 10i, I'm running the following command:
ALTER TABLE jnrvwchnglst ADD
( jnrvwchnglst_userid NUMBER(10) NOT NULL DEFAULT 1 )
Yes jnrvwchnglst is an existing table and no jnrvwchnglst_userid is not an existing column.
The Oracle error message is:
ORA-00907: missing right parenthesis
What's wrong with this query and why does Oracle think I'm missing a parenthesis?
ALTER TABLE jnrvwchnglst ADD
( jnrvwchnglst_userid NUMBER(10) DEFAULT 1 NOT NULL )
"(NOT) NULL" must be the last statement in the "ALTER" syntactically when present, so when Oracle saw that - and that the next char (your "DEFAULT" stmt) wasn't the expected terminating right ")", threw the error.
I have had this issue before where you can't add a column AND set the default/constraints in the same statement. The 'missing right parenthesis' is a red-herring. Oracle loves to use that error for cases that are unrelated to parenthesis (My guess is that their parsing logic falls through to 00907).
Try
ALTER TABLE jnrvwchnglst ADD ( nrvwchnglst_userid NUMBER(10) );
ALTER TABLE jnrvwchnglst ALTER ( nrvwchnglst_userid SET DEFAULT 1 );
UPDATE jnrvwchnglst SET nrvwchnglst_userid = 1 WHERE nrvwchnglst_userid IS NULL;
ALTER TABLE jnrvwchnglst ALTER ( nrvwchnglst_userid SET NOT NULL );