Adding a computed column in Postgres SQL based on a date - sql

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

Related

Postgres a timestamp column constraint from NOT NULL to NULL

I'm trying to run a migration and make basically a column "modified" which is NOT NULL to be NULL. Like there is no need to have that constraint on it, I've run yoyo migrations and have the following output
psycopg2.ProgrammingError: syntax error at or near "timestamp"
LINE 1: ALTER TABLE shop ALTER COLUMN modified timestamp NULL
Pointing to the timestamp ^
the table itself looks
CREATE TABLE shop (
id SERIAL PRIMARY KEY,
uuid uuid NOT NULL UNIQUE,
created timestamp with time zone NOT NULL,
modified timestamp with time zone NOT NULL,
deleted timestamp with time zone
);
I've tried to search the web, and found a few similar articles on stackoverflow but it didn't help, so hopefully someone here can help.
Edit:
steps = [
step("""ALTER TABLE phrases
ALTER COLUMN modified TYPE timestamp,
ALTER column modified SET NULL
;""")
]
In yoyo migration
In Postgres, you can make a column nullable with DROP NOT NULL:
ALTER TABLE shop ALTER column modified DROP NOT NULL;
If you want to change the datatype at the same time, then:
ALTER TABLE shop
ALTER column modified DROP NOT NULL,
ALTER COLUMN modified TYPE timestamp
;
Demo on DB Fiddle

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;

how to add today date as default value in sql server

in sql server. my database name RequestInfo, I have a table name RequestedDate, which have data type is datetime. now i want when ever the value of other columns of table is inserted, then automatically in the RequestedDate column, today date should inserted.
i am using this query but it shows no today in built function.
alter table RequestInfo add default Today() for RequestedDate
Define Default Value When Creating Table
CREATE TABLE TestTable
(
ID INT PRIMARY KEY NOT NULL,
DATECOLUMN DATETIME DEFAULT GETDATE() --<-- Default Value
)
Already Existing Table on already Existing Column
ALTER TABLE TestTable
ADD CONSTRAINT DF_YourTable DEFAULT GETDATE() FOR DATECOLUMN
Add a new Column to an Existing Table With Default Value
ALTER TABLE TestTable
ADD New_DATE_COLUMN DATETIME DEFAULT GETDATE()
This worked for me:
ALTER TABLE YOUR_TABLE ADD Date_Created TIMESTAMP DEFAULT CURRENT_TIMESTAMP;

ALTER TABLE syntax - missing DIRECTORY keyword

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

SQL Date option

I have a little doubt, I want to create a table that had a date that can't be bigger the 2012/12/31, i searched on google but only had exemples on SELECT. I'm gonna put an example:
CREATE TABLE example(
IDExample number (8) primary key,
DateExample date // Here i want to put that condition, is it possible?
);
If you're using SQL Server you can add check contraint on the column in the following way.
ALTER TABLE dbo.example ADD CONSTRAINT CK_DateExample
CHECK (DateExample < '20130101')
If you're using Oracle, the syntax is very similar:
ALTER TABLE dbo.example ADD CONSTRAINT CK_DateExample
CHECK (DateExample < DATE '2013-01-01')