set system date/ constraint - oracle

I'm trying to set the default date of the EmpDate column as the current system date. How do I do it in oracle sql? Besides, how do I add multiple column in one command(instead of using two separate ALTER like the code shown below)?
The question is "Add two columns to the EMPLOYEES table. One column, named EmpDate, contains the date of employment for each employee, and its default value should be the system date. The second column, named EndDate, contains employees’ date of termination."
ALTER TABLE EMPLOYEES
Add EmpDate Date;
ALTER TABLE EMPLOYEES
Add EndDate Date;
ALTER TABLE EMPLOYEES
ADD CONSTRAINT empdate
DEFAULT GETDATE() FOR EmpDate;

alter table Employees add Empdate date default sysdate;
alter table Employees add Enddateq date ;

Related

MODIFY or ADD to add NOT NULL constraint to a column? Oracle sql

ORDERS table in the Oracle Database:
ORDERS
ORDER_ID NOT NULL NUMBER(4)
ORDATE_DATE DATE
CUSTOMER_ID NUMBER(3)
ORDER_TOTAL NUMBER(7,2)
The ORDERS table contains data and all orders have been assigned a customer ID. I'm trying to add a NOT NULL constraint to the CUSTOMER_ID column. Would I use MODIFY CONSTRAINT or ADD CONSTRAINT? I was told you have to drop the constraint and ADD the new one, but if there is no existing constraint to Customer ID number, would it be MODIFY?
alter table orders modify customer_id not null;
Just MODIFY the column:
alter table orders modify customer_id not null;
Alternatively, you could add an [overkill] constraint in the form:
alter table orders add constraint nn1 check (customer_id is not null);
Just use the first form.
As a side note, some databases (such as Oracle) consider those two constraint different and somewhat separate: the former is a column constraint, while the latter is a table constraint. Oracle keeps track in case you drop one, while the other is still in effect.

How to make constraint on Dates

I come the to problem with creating database table over JPA. I have some atributes for creating database table together with two fields for Date. I need to make sure, that input for DateEnd must be bigger than input for DateStart
#Column(name="START")
private LocalDate dateStart
#Column(name="END")
private LocalDate dateEnd
Thanks for any advice
It's not possible on entity level, you should define your custom costraint with its javax.validation.ConstraintValidator implementation. See here for documentation.
As you tagged it as an "Oracle" question, and if you want to do that in the database, a natural choice would be the CHECK constraint.
Here's an example (note that the date literal's format is 'YYYY-MM-DD'):
SQL> create table test (id number, date_start date, date_end date);
Table created.
SQL> alter table test add constraint ch_test_date check (date_start <= date_end);
Table altered.
SQL> insert into test values (1, date '2018-03-19', date '2018-05-10');
1 row created.
SQL> update test set date_end = date '2010-01-02' where id = 1;
update test set date_end = date '2010-01-02' where id = 1
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.CH_TEST_DATE) violated
SQL> insert into test values (1, date '2018-03-19', date '2018-01-02');
insert into test values (1, date '2018-03-19', date '2018-01-02')
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.CH_TEST_DATE) violated
SQL>

Oracle SQL Developer- How to force 00:00:00 hour when inserting a new DATE value

In my Oracle SQL Developer, i have a table with a column with DATE format. When i insert a new row into this table, and insert a new value in this column, it automatically suggestes me the current date with the current hour.
I would like that it automatically suggestes me current date, but with 00:00:00 hour . Is there some setting or parameter that i can set in my SQL Developer to have this result?
We can't able to insert 00:00:00 hours ... the hour value should be between 1 to 12...
we can use below query to insert 00:00:00 hours but the value will be changed to 12:00:00
INSERT INTO TABLE (DATE_COL) VALUES
( TO_DATE ('11/16/2017 00:00:00 ', 'MM/DD/YYYY HH24:MI:SS '));
It seems to me that your DATE column is set with a DEFAULT of SYSDATE. This means, for any INSERT operations which do not specify a value in your DATE column, the current date and time will populate for that row. However, if INSERT operations do specify a value in your DATE column, then the specified date value will supersede the DEFAULT of SYSDATE.
If an application is controlling INSERT operations on that table, then one solution is to ensure the application utilizes the TRUNC() function to obtain your desired results. For example:
INSERT INTO tbl_target
(
col_date,
col_value
)
VALUES
(
TRUNC(SYSDATE, 'DDD'),
5000
)
;
However, if there are multiple applications or interfaces where users could be inserting new rows into the table, (e.g. using Microsoft Access or users running INSERT statements via SQL Developer) and you can't force all of those interfaces to utilize the TRUNC() function on that column during insertion, then you need to look into other options.
If you can ensure via applications that INSERT operations will not actually reference the DATE, then you can simply ALTER the table so that the DATE column will have a DEFAULT of TRUNC(SYSDATE). A CHECK CONSTRAINT can be added for further integrity:
ALTER TABLE tbl_target
MODIFY
(
col_date DATE DEFAULT TRUNC(SYSDATE, 'DDD') NOT NULL
)
ADD
(
CONSTRAINT tbl_target_CHK_dt CHECK(col_date = TRUNC(col_date, 'DDD'))
)
;
However, if users still have the freedom to specify the DATE when inserting new rows, you will want to use a TRIGGER:
CREATE OR REPLACE TRIGGER tbl_target_biu_row
BEFORE INSERT OR UPDATE OF col_val
ON tbl_target
FOR EACH ROW
BEGIN
:NEW.col_date := TRUNC(SYSDATE, 'DDD');
END tbl_target_biu_row
;
This will take of needing to manage the application code of all external INSERT operations on the table. Keep in mind, the above trigger is also modifying the DATE column if a user updates the specified value column.

Hive partition table by month from daily timestamp?

Is it possible to create partition like 01 from date like 2017-01-02' where 01 is month ?
I have daily sales record and I need to do query like select * from sales where month = '01'. So it will be better if I could partition my daily sales by month.but my data has date of format 2017-01-01 and doing
create table tl (columns ......) partitioned by (date <datatype> ) will create partition on daily basis which is the last thing I want .
I need to create partition dynamically.
CAUTION:- You need to escape date column(by using ` i.e. backtick around column name) in create statement. Because date is a datatype in hive.
You can create partitions dynamically:-
by setting below parameter in query.
set hive.exec.dynamic.partition.mode=nonstrict;
Along with that you need to select only month part from source table:-
insert into table sales partition(date) select columns...,SUBSTR(date,5,2) from source_table
This insert statement will create partitions like.
show partitions sales
date=01
date=02
date=03
date=04

Oracle creating a table

How should we create a table which has a column as the average of the totals present in previous columns?
For ex :Departments (depno,depname,noofempl,totalsal,avgsal)
here value in avgsal must be (totalsal/noofempl)
If you are using Oracle 11 you might use virtual columns otherwise you can create a view that selects all your table columns and adds the average column
CREATE TABLE DEPARTMENTS
(
DEPNO...
DEPNAME...
noofempl...
totalsal...
);
CREATE VIEW VW_DEPARMENTS AS
SELECT DEPNO, DEPNAME, noofempl, totalsal, totalsal/noofempl as avgsal
FROM DEPARTMENTS;

Resources