I'm trying to create a table named MEMBER. The datatype for REGISTERDATE is DATE and the default value is SYSDATE. There is another column named EXPIRYDATE, datatype is DATE and the default value is 1 year after SYSDATE. So how am I going to create for the column of EXPIRYDATE when creating the table definition?
I tried REGISTERDATE DATE DEFAULT SYSDATE,
EXPIRYDATE DATE + 365,
It showed error..
You're so close, you're just missing the default keyword and sysdate after expirydate date
-
create table MEMBER (
REGISTERDATE date default sysdate,
expirydate date default sysdate+365
)
test:
insert into MEMBER(REGISTERDATE) values (sysdate)
output:
Not all years have 365 days; on average, only 3 out of 4. But you can literally define an expression that expresses "1 year after":
create table member (
id number(*,0) not null primary key,
registerdate date default sysdate,
expirydate date default sysdate + interval '1' year
);
Remember that Oracle won't use the default value if you provide your own, even if it's null. You need to complete omit the field from the INSERT INTO statement.
Demo
Related
I'm working on my table which is supposed to store data about rented cars.
And there are 3 important columns:
RENT_DATE DATE DEFAULT TO_DATE (SYSDATE, 'DD-MM-YYYY'),
DAYS NUMBER NOT NULL,
RETURN_DATE DATE DEFAULT TO_DATE(SYSDATE+DAYS, 'DD-MM-YYYY')
My problem is that RETURN_DATE column is giving me error:
00984. 00000 - "column not allowed here"
What i want is that RENT_DATE set automatically date when record is added.
DAYS column is to store for how much days someone is renting car.
And the last column should store date of when car should be returned.
Thank you for any type of help.
This doesn't make sense:
DEFAULT TO_DATE (SYSDATE, 'DD-MM-YYYY')
SYSDATE is already a date. TO_DATE requires a char, so this takes a date, Oracle implicitly turns the date into a char, and then TO_DATE converts it back to a date. This is risky/unreliable because it uses a hardcoded date format to operate on a date that has been implicitly turned to a string using the system default format, which might one day not be DD-MM-YYYY (you're building a latent bug into your software)
If you want a date without a time on it use TRUNC(SYSDATE)
The other problem doesn't make sense either: you're storing a number of days rented for and also the return date, when one is a function of the other. Storing redundant data becomes a headache because you have to keep them in sync. My person class stores my birthdate, and I calculate how old I am. I don't store my age too and then update my table every day/year etc
Work out which will be more beneficial to you to store, and store it, then calculate the other whenever you want it. Personally I would store the return date as it's absolute, rather than open to interpretation of "is that working days, calendar days? what about public holidays? if the start date is jan 1 and the rental is for 10 days, is the car brought back on the 10th or the 11th?"
If you're desperate to have both columns in your DB consider using a view to calculate it or a function based column (again, to calculate one from the other) so they stay in sync
All in, you could look at this:
create table X(
RENT_DATE DATE DEFAULT TRUNC(SYSDATE) NOT NULL,
RETURN_DATE DATE NOT NULL,
DAYS AS (TRUNC(RETURN_DATE - RENT_DATE) + 1)
)
I put the days as +1 because to me, a car taken on the 1st and returned on the second is 2 days, but you might want to get more accurate - if it's taken on the first and returned before 10am on the second then it's one day otherwise it's 2 etc...
Use a virtual column:
CREATE TABLE table_name (
RENT_DATE DATE
DEFAULT TRUNC( SYSDATE )
CONSTRAINT table_name__rent_date__nn NOT NULL
CONSTRAINT table_name__rent_date_chk CHECK ( rent_date = TRUNC( rent_date ) ),
DAYS NUMBER
DEFAULT 7
CONSTRAINT table_name__days__nn NOT NULL,
RETURN_DATE DATE
GENERATED ALWAYS AS ( RENT_DATE + DAYS ) VIRTUAL
);
Then you can insert values:
INSERT INTO table_name ( rent_date, days ) VALUES ( DEFAULT, DEFAULT );
INSERT INTO table_name ( rent_date, days ) VALUES ( DATE '2020-01-01', 1 );
And:
SELECT * FROM table_name;
Outputs:
RENT_DATE | DAYS | RETURN_DATE
:------------------ | ---: | :------------------
2020-09-12T00:00:00 | 7 | 2020-09-19T00:00:00
2020-01-01T00:00:00 | 1 | 2020-01-02T00:00:00
db<>fiddle here
there is a table INCASSO in my database:
CREATE TABLE "GEC_AP"."INCASSO"
("ID_INCASSO" VARCHAR2(50 BYTE) NOT NULL ENABLE,
"ID_FATTURA" VARCHAR2(50 BYTE) NOT NULL ENABLE,
"ID_PIANO_RATE" VARCHAR2(22 BYTE) DEFAULT -1 NOT NULL ENABLE,
"DATA_ESECUZIONE" DATE DEFAULT SYSDATE NOT NULL ENABLE,
...)
The primary key includes four fields:
CONSTRAINT "PK_INCASSO" PRIMARY KEY ("ID_INCASSO", "ID_FATTURA", "ID_PIANO_RATE", "DATA_ESECUZIONE")
It seems there is a duplicated record when I run the following query:
select id_incasso, id_fattura, id_piano_rate, data_esecuzione
from incasso
where id_incasso = 'TO_20110521258225'
But with another query, 0 records are extracted:
select id_incasso, id_fattura, id_piano_rate, data_esecuzione, count(*)
from incasso where id_incasso = 'TO_20110521258225'
group by id_incasso, id_fattura, id_piano_rate, data_esecuzione
having count(*) > 1
The database is on Oracle 11.2.0.1.0 and I'm using SQL Developer 4.1.1.19.
In SQL Developer, the date format is:
I would to know if the records are different or there is a format date problem in the editor. If the records are different based on the date, in which part of the date they are different? If it's a format date problem in the editor, how can I fix it?
Change the date format to DD-MON-YYYY HH24:MI:SS and you are likely to see the difference in that the dates have different centuries.
Using RR to format the year can hide that one date is 1911 and the other is 2011
Try:
SELECT TO_CHAR( DATE '2011-01-01', 'RR-MM-DD' ),
TO_CHAR( DATE '1911-01-01', 'RR-MM-DD' )
FROM DUAL
Both will output the same although they are different dates and will not be grouped together.
If the dates are still the same then look for additional spaces or other hidden characters in the strings; you can use LENGTH() to check the size of the strings or DUMP() to get the byte values of the contents:
select id_incasso,
id_fattura,
LENGTH( id_fattura ) AS f_length,
id_piano_rate,
LENGTH( id_piano_rate ) AS pr_length,
TO_CHAR( data_esecuzione, 'YYYY-MM-DD HH24:MI:SS' ) AS data_esecuzione
from incasso
where id_incasso = 'TO_20110521258225'
I've changed the date format used in SQL Developer to DD-MON-YYYY HH24:MI:SS
and the following query:
select id_incasso, id_fattura, id_piano_rate, data_esecuzione from incasso where id_incasso = 'TO_20110521258225'
show that the difference is in the year:
Thanks everyone.
I'm trying to create the following table:
CREATE TABLE helpToolStatsTest (
customer nvarchar2(25),
data nvarchar2(7),
myDate Date NOT NULL DEFAULT CURRENT_DATE()
)
but I'm getting this error:
ORA-00907: missing right parenthesis
According to the documentation:
CREATE TABLE helpToolStatsTest (
customer nvarchar2(25),
data nvarchar2(7),
myDate Date DEFAULT CURRENT_DATE NOT NULL
)
I'm trying to create a table with date constraint that will make the date value of MM/YY for credit card expiration date. When I try to create the below table I receive the error:
CONSTRAINT exp_check CHECK TO_CHAR(exp_date,'MM/YY'),
*
ERROR at line 19:
ORA-00906: missing left parenthesis
Create table Orders
(o_id int NOT NULL,
c_id int NOT NULL,
p_id int NOT NULL,
s_id int NOT NULL,
order_date date DEFAULT sysdate,
o_qty number NOT NULL,
order_total number NOT NULL,
card_type varchar2(50) NOT NULL,
cc_number varchar2(50) NOT NULL,
exp_date date NOT NULL,
shipping_date date,
shipping_status varchar2(50) DEFAULT 'Not Shipped Yet',
UNIQUE (o_ID, c_ID, p_ID),
CONSTRAINT order_ID PRIMARY KEY (O_ID),
CONSTRAINT fk_cust_id FOREIGN KEY (C_ID) REFERENCES customers(C_ID),
CONSTRAINT fk_ship_id FOREIGN KEY (S_ID) REFERENCES shipping(S_ID),
CONSTRAINT qty_check CHECK (o_qty >= 0),
CONSTRAINT exp_check CHECK TO_CHAR(exp_date,'MM/YY'),
CONSTRAINT s_check CHECK (shipping_status IN
('Not shipped yet', 'Shipped', 'Delivered'))
);
Once I'm able to create the table I will create a executed stored procedure that will execute the below:
SET SERVEROUTPUT ON;
DECLARE
i_result varchar2(100);
BEGIN
place_order(2200,1,'Regular','VISA','1111-1111-2222-3333',11/19,1040,i_result);
END;
A date always has a day. month, year, hour, minute, and second. You could store a date that is midnight on the first of the month and validate that the exp_date = trunc(exp_date, 'MM'). When you display the value to the user, you'd then use the to_char expression to display it in the format that you want.
Alternately, you could store a string in the format MM/YY and your check constraint could put various validations on that string (i.e. the first two characters are a number between 1 and 12, the fourth and fifth characters are a number between 15 and 25, etc.).
As others have said, a DATE value in Oracle always has the fields YEAR, MONTH, DAY, HOURS, MINUTES, and SECONDS. I think what you want to do here is to store the last day/hour/minute/second of the expiration date in your EXP_DATE column, e.g. if expiration shown on the card is '07/19' you'd want to store 31-JUL-2019 23:59:59, which is the last possible second that the card is valid, in the EXP_DATE field. Then when you want to display the expiration date in its usual MM/YY format you just have to format it with TO_CHAR, e.g. TO_CHAR(EXP_DATE, 'MM/YY').
Best of luck.
I am trying to create a table that has a default value for a date that is sysdate - 2 in oracle
Oracle seems to be fine with sysdate as the default but not sysdate - 2. Is this possible?
You need to specify the DEFAULT value in brackets:
Create the table:
CREATE TABLE order_status (
order_id NUMBER,
last_modified DATE DEFAULT (SYSDATE - 2)
);
Insert a record to test the default:
INSERT INTO order_status
(order_id)
VALUES
(1);
Select the data from the table to confirm the default worked (Current date 14/11/2011):
SELECT *
FROM order_status;
ORDER_ID LAST_MODIFIED
1 12/11/2011
DB Version 10g.
Hope it helps...