How to INSERT a DATE? - oracle

Hi I am new to Oracle and forgot how to enter a date. I have tried many variations and can't seem to get it, plus I have looked on the internet but can't find the correct example. My table is set up like this: hiredate DATE, (DATE being I assume the default input, but what is the default).
Thanks for any help.
Here's my code:
SQL> SET LINESIZE 120
SQL> SET PAGESIZE 45
SQL> SET FEEDBACK 1
SQL> SET ECHO ON
SQL>
SQL> DROP TABLE employee
2
SQL> CREAT TABLE student
SP2-0734: unknown command beginning "CREAT TABL..." - rest of line ignored.
SQL> CREATE TABLE employee
2 (
3 empno NUMBER(4)CONSTRAINT employee_empno_PK PRIMARY KEY,
4 empname VARCHAR(10),
5 job VARCHAR(10),
6 manager NUMBER(4),
7 hiredate DATE,
8 salary NUMBER(7,2),
9 commission NUMBER(7,2),
10 deptno NUMBER(2)
11 );
Table created.
SQL> INSERT INTO employee
2 VALUES(7839, 'President', NULL, 11/17/1981, 5000, NULL, 10);
INSERT INTO employee
*
ERROR at line 1:
ORA-00947: not enough values
SQL> VALUES(7839, 'President', NULL, '11/17/1978', 5000, NULL, 10);
SP2-0734: unknown command beginning "VALUES(783..." - rest of line ignored.
SQL> INSERT INTO employee
2 VALUES(7839, 'President', NULL, '11/17/1978', 5000, NULL, 10);
INSERT INTO employee
*
ERROR at line 1:
ORA-00947: not enough values
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, 11/17/1981, 5000, NULL, 10);
VALUES(7839, 'King', 'President', NULL, 11/17/1981, 5000, NULL, 10)
*
ERROR at line 2:
ORA-00932: inconsistent datatypes: expected DATE got NUMBER
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, 19811117, 5000, NULL, 10);
VALUES(7839, 'King', 'President', NULL, 19811117, 5000, NULL, 10)
*
ERROR at line 2:
ORA-00932: inconsistent datatypes: expected DATE got NUMBER
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, '11/17/1981', 5000, NULL, 10);
VALUES(7839, 'King', 'President', NULL, '11/17/1981', 5000, NULL, 10)
*
ERROR at line 2:
ORA-01843: not a valid month
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, '1978/11/17
3 )
4 ;
ERROR:
ORA-01756: quoted string not properly terminated
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President' NULL, '1978/11/17', 5000, NULL, 10);
VALUES(7839, 'King', 'President' NULL, '1978/11/17', 5000, NULL, 10)
*
ERROR at line 2:
ORA-00917: missing comma
Hello, I am new to MySQL and forgot how to properly INSERT at DATE when using MySQL Plus. I have tried many different variations as you will be able to see and I can't seem to figure it out. I also looked on the internet but always find examples where they make there own formats. What is the correct way to insert a date when the table was created using: hiredate
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, '1981/11/17', 5000, NULL, 10);
VALUES(7839, 'King', 'President', NULL, '1981/11/17', 5000, NULL, 10)
*
ERROR at line 2:
ORA-01861: literal does not match format string
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, '1981/17/11', 5000, NULL, 10);
VALUES(7839, 'King', 'President', NULL, '1981/17/11', 5000, NULL, 10)
*
ERROR at line 2:
ORA-01861: literal does not match format string
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, '17/11/1981', 5000, NULL, 10);
VALUES(7839, 'King', 'President', NULL, '17/11/1981', 5000, NULL, 10)
*
ERROR at line 2:
ORA-01843: not a valid month
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, '81/11/17', 5000, NULL, 10);
VALUES(7839, 'King', 'President', NULL, '81/11/17', 5000, NULL, 10)
*
ERROR at line 2:
ORA-01847: day of month must be between 1 and last day of month
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, '17/11/81', 5000, NULL, 100);
VALUES(7839, 'King', 'President', NULL, '17/11/81', 5000, NULL, 100)
*
ERROR at line 2:
ORA-01843: not a valid month
SQL> INSERT INTO employee
2 VALUES(7839, 'King, 'President', NULL, '11/17/1981', 5000, NULL, 10);
ERROR:
ORA-01756: quoted string not properly terminated
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, '11/17/81', 5000, NULL, 10);
VALUES(7839, 'King', 'President', NULL, '11/17/81', 5000, NULL, 10)
*
ERROR at line 2:
ORA-01843: not a valid month
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, '17/11/81', 5000, NULL, 10);
VALUES(7839, 'King', 'President', NULL, '17/11/81', 5000, NULL, 10)
*
ERROR at line 2:
ORA-01843: not a valid month
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, '81/17/11', 5000, NULL, 10);
VALUES(7839, 'King', 'President', NULL, '81/17/11', 5000, NULL, 10)
*
ERROR at line 2:
ORA-01847: day of month must be between 1 and last day of month
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, '17/81/11', 5000, NULL, 10);
VALUES(7839, 'King', 'President', NULL, '17/81/11', 5000, NULL, 10)
*
ERROR at line 2:
ORA-01843: not a valid month
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, '81/11/17', 5000, NULL, 10);
VALUES(7839, 'King', 'President', NULL, '81/11/17', 5000, NULL, 10)
*
ERROR at line 2:
ORA-01847: day of month must be between 1 and last day of month
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, '1981-11-17', 5000, NULL, 10);
VALUES(7839, 'King', 'President', NULL, '1981-11-17', 5000, NULL, 10)
*
ERROR at line 2:
ORA-01861: literal does not match format string
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, '17/11/1981', 5000, NULL, 10);
VALUES(7839, 'King', 'President', NULL, '17/11/1981', 5000, NULL, 10)
*
ERROR at line 2:
ORA-01843: not a valid month

You can use the TO_DATE function:
TO_DATE('11/17/1981', 'MM/DD/YYYY')
It lets you specify a date as a string and then the format that its in so Oracle can convert it into a Date.
Or you can use a date literal:
date '1981-11-17'

Dates are format sensitive in SQL. So you need to enclouse them in single quotes like
('21-Dec-2010'). If your date is not coming in the standard format then you need to convert using Single Row Conversion Function TO_DATE using the specific format model. You can find more of these syntax on my portal here
http://www.suhelsayyad.com/p/blog-page_14.html

Related

Temporal Table Oracle Vs SQL Server , How to and history

The following code denotes both Sql Server and Oracle both to create a temporal Solution.
For Oracle the following article is the basis.
---/// Temporal Table SQL Server ///---
CREATE TABLE dbo.Employee(
EMPNO INT
,ENAME VARCHAR(10)
,JOB VARCHAR(9)
,MGR INT
,HIREDATE DATE
,SAL NUMERIC(7,2)
,COMM NUMERIC(7,2)
,DEPTNO INT
CONSTRAINT EMP_PK PRIMARY KEY (EMPNO)
,SysStartTime datetime2 GENERATED ALWAYS AS ROW START NOT NULL
,SysEndTime datetime2 GENERATED ALWAYS AS ROW END NOT NULL
,PERIOD FOR SYSTEM_TIME (SysStartTime,SysEndTime))
WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.EmployeeHistory, DATA_CONSISTENCY_CHECK = ON));
INSERT INTO dbo.Employee (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)values
(7369, 'SMITH', 'CLERK', 7902, '02-MAR-1970', 8000, NULL, 20),
(7499, 'ALLEN', 'SALESMAN', 7698, '20-MAR-1971', 1600, 3000, 30),
(7521, 'WARD', 'SALESMAN', 7698, '07-FEB-1983', 1250, 5000, 30),
(7566, 'JONES', 'MANAGER', 7839, '02-JUN-1961', 2975, 50000, 20),
(7654, 'MARTIN', 'SALESMAN', 7698, '28-FEB-1971', 1250, 14000, 30),
(7698, 'BLAKE', 'MANAGER', 7839, '01-JAN-1988', 2850, 12000, 30),
(7782, 'CLARK', 'MANAGER', 7839, '09-APR-1971', 2450, 13000, 10),
(7788, 'SCOTT', 'ANALYST', 7566, '09-DEC-1982', 3000, 1200, 20),
(7839, 'KING', 'PRESIDENT', NULL, '17-JUL-1971', 5000, 1456, 10),
(7844, 'TURNER', 'SALESMAN', 7698, '08-AUG-1971', 1500, 0, 30),
(7876, 'ADAMS', 'CLERK', 7788, '12-MAR-1973', 1100, 0, 20),
(7900, 'JAMES', 'CLERK', 7698, '03-NOV-1971', 950, 0, 30),
(7902, 'FORD', 'ANALYST', 7566, '04-MAR-1961', 3000, 0, 20),
(7934, 'MILLER', 'CLERK', 7782, '21-JAN-1972', 1300, 0, 10)
SELECT * FROM Employee
SELECT * FROM EmployeeHistory --Would return 0 rows.
UPDATE EMPLOYEE
SET SAL=SAL+2000
WHERE EMPNO=7369
SELECT * FROM Employee
SELECT * FROM EmployeeHistory --Would return 1 rows. prior to change.
UPDATE EMPLOYEE
SET SAL=SAL+2000
WHERE EMPNO=7369
SELECT * FROM Employee
SELECT * FROM EmployeeHistory --Would return 2 rows. prior to change.
--if you want to check the list of changes which have happend you could simply query the history table .
SELECT * FROM EmployeeHistory WHERE EMPNO = 7396
union all
SELECT * FROM Employee WHERE EMPNO = 7396
ORDER BY SysStartTime DESC
---/// Temporal Table Oracle ///---
---/// ORACLE (ASSUME AUTOCOMMIT IS ON). ///---
CREATE TABLESPACE STATIC_DATA DATAFILE 'static_data.dbf' SIZE 10m AUTOEXTEND ON MAXSIZE 1g ;
CREATE TABLESPACE STATIC_ARCHIVE DATAFILE 'static_archive.dbf' SIZE 10m AUTOEXTEND ON MAXSIZE 1g ;
CREATE FLASHBACK ARCHIVE STATIC_ARCHVIE_FB TABLESPACE STATIC_ARCHIVE QUOTA 1 G RETENTION 7 YEAR; --7 YEARS RETENTION.
CREATE TABLE TEST.Employee(
EMPNO INT
,ENAME VARCHAR(10)
,JOB VARCHAR(9)
,MGR INT
,HIREDATE DATE
,SAL NUMERIC(7,2)
,COMM NUMERIC(7,2)
,DEPTNO INT
CONSTRAINT EMP_PK PRIMARY KEY (EMPNO)
)
TABLESPACE STATIC_DATA;
ALTER TABLE TEST.Employee ADD PERIOD FOR History;
ALTER TABLE TEST.Employee FLASHBACK ARCHIVE STATIC_ARCHVIE_FB;
INSERT INTO TEST.Employee (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)values
(7369, 'SMITH', 'CLERK', 7902, '02-MAR-1970', 8000, NULL, 20),
(7499, 'ALLEN', 'SALESMAN', 7698, '20-MAR-1971', 1600, 3000, 30),
(7521, 'WARD', 'SALESMAN', 7698, '07-FEB-1983', 1250, 5000, 30),
(7566, 'JONES', 'MANAGER', 7839, '02-JUN-1961', 2975, 50000, 20),
(7654, 'MARTIN', 'SALESMAN', 7698, '28-FEB-1971', 1250, 14000, 30),
(7698, 'BLAKE', 'MANAGER', 7839, '01-JAN-1988', 2850, 12000, 30),
(7782, 'CLARK', 'MANAGER', 7839, '09-APR-1971', 2450, 13000, 10),
(7788, 'SCOTT', 'ANALYST', 7566, '09-DEC-1982', 3000, 1200, 20),
(7839, 'KING', 'PRESIDENT', NULL, '17-JUL-1971', 5000, 1456, 10),
(7844, 'TURNER', 'SALESMAN', 7698, '08-AUG-1971', 1500, 0, 30),
(7876, 'ADAMS', 'CLERK', 7788, '12-MAR-1973', 1100, 0, 20),
(7900, 'JAMES', 'CLERK', 7698, '03-NOV-1971', 950, 0, 30),
(7902, 'FORD', 'ANALYST', 7566, '04-MAR-1961', 3000, 0, 20),
(7934, 'MILLER', 'CLERK', 7782, '21-JAN-1972', 1300, 0, 10)
SELECT * FROM TEST.Employee
-- ??? any Equivalent in Oracle for this -> SELECT * FROM EmployeeHistory .
UPDATE TEST.Employee
SET SAL=SAL+2000
WHERE EMPNO=7369
UPDATE TEST.Employee
SET SAL=SAL+1000
WHERE EMPNO != 7369
SELECT * FROM TEST.Employee --//Would return the current dataset.
SELECT employee_id FROM TEST.Employee AS OF PERIOD FOR history 'Date_to_be_replaced_with_Scn_in_your_database'
--// The above would retun the data before update all records.
Question 1: I can see the changes only at a point in time. If the record is updated multiple times in a day how do i get the changes to show the history?
Question 2: Is flashback not a temporary table and may get purged when certain events occur in db?
I tried querying between specific date ranges but the query is only point in time and would not give me all the history of the records like sql server.
I will if this does not work need to write trigger on each static table and insert it to my own history table. Trying to avoid triggers.
If you're looking at a complete history, then flashback data archive is probably the solution you want to be exploring, combined with the VERSIONS BETWEEN clause. For example, for a (single) row in the DEPT table you could write
SQL> SELECT deptno, dname,
2 VERSIONS_STARTTIME
3 ,VERSIONS_XID
4 ,VERSIONS_OPERATION
5 FROM dept
6 VERSIONS BETWEEN TIMESTAMP
7 SYSTIMESTAMP - INTERVAL '20:00' MINUTE TO SECOND
8 AND SYSTIMESTAMP
9 WHERE deptno = 10;
DEPTNO DNAME VERSIONS_STARTTIME VERSIONS_XID V
--------- -------------- ---------------------- ---------------- -
10 UNKNOWN 06-MAY-20 11.53.45 PM 0200100060040000 U
10 MONEY GRABBERS 06-MAY-20 11.53.36 PM 0600050065040000 U
10 FINANCE 06-MAY-20 11.53.24 PM 09000D001D050000 U
10 BEAN COUNTERS 06-MAY-20 11.53.12 PM 01001A00EA030000 U
10 ACCOUNTING
Here's a video explaining the "behind the scenes" on FDA
https://youtu.be/qIs2UPIodQg
and if you do end up going down the trigger route, I've got an implementation that lets you auto-generate them for tables here
https://github.com/connormcd/audit_utility

GETTING THIS [Error] Execution (25: 14): ORA-00984: column not allowed here WHEN TRYING TO INSERT

HERE'S THE TABLE I CREATED:
CREATE TABLE personal_info (
Person_name VARCHAR(30) NOT NULL,
Date_of_Birth DATE,
Join_date DATE,
Join_year NUMBER,
Person_address VARCHAR(75),
Person_Post VARCHAR(15),
Person_id VARCHAR(9) NOT NULL UNIQUE,
Email_primary VARCHAR(30),
Phone_primary NUMBER,
Email_secondary VARCHAR(30),
Phone_secondary NUMBER,
Sal_grade CHAR(1) NOT NULL,
Empl_id NUMBER NOT NULL,
CONSTRAINT FK_Salary_Person FOREIGN KEY (Sal_grade) REFERENCES salary(Salary_grade) ON DELETE CASCADE,
CONSTRAINT FK_Employee_Person FOREIGN KEY (Empl_id) REFERENCES employee(Employee_id) ON DELETE CASCADE,
CONSTRAINT UC_Person_ID UNIQUE (Empl_id,Person_name)
);
HERE'S THE EMPLOYEE TABLE:
CREATE TABLE employee (
Employee_id NUMBER NOT NULL PRIMARY KEY,
Employee_job_description VARCHAR(200),
Proj_id NUMBER NOT NULL,
Dep_id NUMBER NOT NULL
);
ALTER TABLE employee
ADD CONSTRAINT FK_project_employee
FOREIGN KEY (Proj_id) REFERENCES PROJECTS(Project_id) ON DELETE CASCADE;
ALTER TABLE employee
ADD CONSTRAINT FK_dept_employee
FOREIGN KEY (Dep_id) REFERENCES dept(Dept_id) ON DELETE CASCADE;
CREATE SEQUENCE EMPID_SEQ1
MINVALUE 1
MAXVALUE 9999999
START WITH 10000
INCREMENT BY 4
CACHE 20;
I ALREADY INSERTED INTO THE EMPLOYEE TABLE, NO ISSUE.
INSERT INTO employee (Employee_id, Employee_job_description, Proj_id, Dep_id) VALUES(EMPID_SEQ1.NEXTVAL,'SENIOR VICE PRESIDENT',501,1);
BUT WHEN I TRY TO INSERT INTO THE PERSONAL_INFO TABLE:
/* Formatted on 19-Oct-22 11:58:19 AM (QP5 v5.256.13226.35538) */
INSERT INTO PERSONAL_INFO (Empl_id,
Person_name,
Date_of_Birth,
Join_date,
Join_year,
Person_address,
Sal_grade,
Actual_salary,
Person_Post,
PERSON_ID,
Email_primary,
Phone_primary,
Email_secondary,
Phone_secondary)
VALUES (EMPID_SEQ1.CURRVAL,
'Mr. FF',
TO_DATE ('1980/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss'),
TO_DATE ('2000/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss'),
TO_CHAR (Join_DATE, 'YYYY'),
'Banani,Dhaka.',
'D',
150000,
'SVP',
TO_CHAR(TO_CHAR(Join_YEAR) || TO_CHAR (EMPID_SEQ1.CURRVAL)),
'FF#bank.com',
01234567891,
'FFF#bank.com',
99998882222);
I GET THE AFORMENTIONED ERROR WHILE INSERTING THE PERSON_ID UNIQUE KEY VALUE. BASICALLY I WANTED THE PERSON_ID TO LOOK SOMETHING LIKE '200710016'. JOINING YEAR FOLLOWED BY EMPLOYEE ID.
BUT IT'S TELLING ME THAT THE JOIN_YEAR COLUMN IS NOT ALLOWED HERE.
You can't reference a column that's just being inserted - you'll have to "repeat" the same data again.
Also, target table should be modified (two datatypes - phone numbers aren't really "numbers" because of possible leading zeros; one column is missing).
As of the sequence, you first have to select nextval and then currval because - initially - currval doesn't exist.
When fixed:
SQL> CREATE TABLE personal_info
2 (
3 Person_name VARCHAR (30) NOT NULL,
4 Date_of_Birth DATE,
5 Join_date DATE,
6 Join_year NUMBER,
7 Person_address VARCHAR (75),
8 Person_Post VARCHAR (15),
9 Person_id VARCHAR (9) NOT NULL UNIQUE,
10 Email_primary VARCHAR (30),
11 Phone_primary VARCHAR (30), --> change datatype
12 Email_secondary VARCHAR (30),
13 Phone_secondary VARCHAR (30), --> change datatype
14 Sal_grade CHAR (1) NOT NULL,
15 Empl_id NUMBER NOT NULL,
16 actual_salary NUMBER --> add this column
17 );
Table created.
Insert:
SQL> INSERT INTO PERSONAL_INFO (Empl_id,
2 Person_name,
3 Date_of_Birth,
4 Join_date,
5 Join_year,
6 Person_address,
7 Sal_grade,
8 Actual_salary,
9 Person_Post,
10 PERSON_ID,
11 Email_primary,
12 Phone_primary,
13 Email_secondary,
14 Phone_secondary)
15 VALUES (
16 EMPID_SEQ1.nextval,
17 'Mr. FF',
18 TO_DATE ('1980/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss'),
19 TO_DATE ('2000/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss'),
20 TO_CHAR (
21 TO_DATE ('2000/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss'),
22 'YYYY'),
23 'Banani,Dhaka.',
24 'D',
25 150000,
26 'SVP',
27 TO_CHAR (
28 TO_CHAR (
29 TO_DATE ('2000/05/03 21:02:44',
30 'yyyy/mm/dd hh24:mi:ss'),
31 'YYYY')
32 || TO_CHAR (EMPID_SEQ1.CURRVAL)),
33 'FF#bank.com',
34 '01234567891',
35 'FFF#bank.com',
36 '99998882222');
1 row created.
You should not have a join_year column in the table as the value can be calculated from the join_date column and if it is independent then the two columns can get out-of-sync. If you did want to have it in the table then it should be a virtual column. Similarly for person_id (although if there were business reasons to persist the person_id so that it could be changed later then you could set it with a BEFORE INSERT trigger rather than using a virtual column, but either way you should not need to supply a default value derived from other columns in the INSERT statement):
CREATE TABLE personal_info (
Person_name VARCHAR(30) NOT NULL,
Date_of_Birth DATE,
Join_date DATE,
Join_year NUMBER
GENERATED ALWAYS AS (EXTRACT(YEAR FROM join_date)),
Person_address VARCHAR(75),
Person_Post VARCHAR(15),
Person_id VARCHAR(9)
GENERATED ALWAYS AS (
CAST(
TO_CHAR(EXTRACT(YEAR FROM join_date), 'FM0000')
|| TO_CHAR(empl_id, 'FM00000')
AS VARCHAR2(9)
)
)
NOT NULL
UNIQUE,
Email_primary VARCHAR(30),
Phone_primary VARCHAR2(12),
Email_secondary VARCHAR(30),
Phone_secondary VARCHAR2(12),
Sal_grade /* CHAR(1) */ NOT NULL,
Actual_Salary NUMBER(10,2),
Empl_id /* NUMBER */ NOT NULL,
CONSTRAINT FK_Salary_Person FOREIGN KEY (Sal_grade)
REFERENCES salary(Salary_grade) ON DELETE CASCADE,
CONSTRAINT FK_Employee_Person FOREIGN KEY (Empl_id)
REFERENCES employee(Employee_id) ON DELETE CASCADE,
CONSTRAINT UC_Person_ID UNIQUE (Empl_id, Person_name)
);
Note: You do not need to include the data type when a column has a foreign key constraint; if you omit it then the column will take the same data type as the primary/unique key it is referencing and you can ensure consistency between the tables.
Note 2: The EMPID_SEQ1 sequence starts with 10000 and increases by 4 to a maximum of 9999999 and the Person_id column can take 9 characters, of which 4 are the year so once there are 22500 people in the table and the sequence gets to 100000 then it will be too large for the Person_id column. You should probably either limit the sequence to 99999 or make the Person_id column larger.
Note 3: While phone numbers are numeric, they often have leading zeroes and these will not be stored in a NUMBER column so you should store phone numbers as a VARCHAR2.
Note 4: You are missing the actual_salary column.
Then you can use:
INSERT INTO PERSONAL_INFO (
Empl_id,
Person_name,
Date_of_Birth,
Join_date,
Person_address,
Sal_grade,
Actual_salary,
Person_Post,
Email_primary,
Phone_primary,
Email_secondary,
Phone_secondary
) VALUES (
EMPID_SEQ1.CURRVAL,
'Mr. FF',
TO_DATE ('1980/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss'),
TO_DATE ('2000/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss'),
'Banani,Dhaka.',
'D',
150000,
'SVP',
'FF#bank.com',
'01234567891',
'FFF#bank.com',
'99998882222'
);
fiddle

Oracle unique constraint or index

If I had the following table in an Oracle database:
CREATE TABLE TESTTABLE
(
"MYUNIQUEIDCOL" NUMBER(9,0) NOT NULL,
"VAN" NUMBER(9,0) NOT NULL,
"STATUS" VARCHAR2(30 BYTE) NOT NULL
)
I would like a van (of which there could be many) to only ever have one row with a status of "Active" - what kind of constraint would I need to ensure the illustration below does not happen (ie. rows 2 & 5 should not have been allowed)
MYUNIQUEIDCOL | VAN | STATUS
1 100 Active
2 100 Active
3 100 Cancelled
4 200 Active
5 200 Active
Thank you.
You can create unique index on mentioned column with the condition as following:
CREATE UNIQUE INDEX UQ_VAN_STATUS
ON TESTTABLE (VAN, CASE WHEN STATUS = 'ACTIVE' THEN STATUS END);
Example:
SQL> INSERT INTO TESTTABLE VALUES (1, 100, 'ACTIVE');
1 row created.
SQL> INSERT INTO TESTTABLE VALUES (1, 100, 'INACTIVE');
1 row created.
SQL> INSERT INTO TESTTABLE VALUES (1, 100, 'ACTIVE');
INSERT INTO TESTTABLE VALUES (1, 100, 'ACTIVE')
*
ERROR at line 1:
ORA-00001: unique constraint (TEJASH.UQ_VAN_STATUS) violated
SQL>
Cheers!!

'invalid identifier' occures when trying "SDO_INSIDE"

I am trying to check if "visitors" are inside a "village building". But everytime i execute my SELECT statement i get the error that "position" in the table "visitors" is an invalid identifier.
DROP TABLE village CASCADE CONSTRAINTS;
CREATE TABLE village (
building_id integer PRIMARY KEY,
name VARCHAR2(30),
visitors integer,
building SDO_GEOMETRY
);
/*****************index********************/
delete from user_sdo_geom_metadata where table_name = 'VILLAGE';
INSERT INTO user_sdo_geom_metadata
( TABLE_NAME,
COLUMN_NAME,
DIMINFO,
SRID
)
VALUES
( 'village',
'building',
SDO_DIM_ARRAY( -- 20X20 grid
SDO_DIM_ELEMENT('X', 0, 1000, 0.5),
SDO_DIM_ELEMENT('Y', 0, 1000, 0.5)
),
NULL -- SRID
);
-- inserts
INSERT INTO village VALUES(1,'Kirche', 4,
SDO_GEOMETRY(
2003,
NULL,
NULL,
SDO_ELEM_INFO_ARRAY(1,1003,1),
SDO_ORDINATE_ARRAY(100,100, 100,120, 80,120, 80,150 ,100,200, 150,200, 150,150, 200,150, 200,120, 150,120, 150,100)
)
);
drop table visitors;
create table visitors(
id integer,
position SDO_GEOMETRY
);
drop sequence visitors_seq;
create sequence visitors_seq;
INSERT INTO visitors VALUES (visitors_seq.nextval,
SDO_GEOMETRY(
2001,
NULL,
SDO_POINT_TYPE(160, 100, NULL),
NULL,
NULL
)
);
commit;
SELECT * FROM village WHERE SDO_INSIDE(village.building, visitors.position) = 'TRUE';
Can it be that i have to use SDO_GEOMETRY data as a parameter instead of "visitors.position"?
Your query FROM clause does not reference the table visitors. You'll need to include it.

How to solve Which month has the highest number of bookings in oracle

This is my table
CREATE TABLE Book (
hotelNo VARCHAR2(2) NOT NULL,
guestNo VARCHAR2(1) DEFAULT NULL,
dateFrom DATE NOT NULL,
dateTo DATE NOT NULL,
roomNo VARCHAR2(2) NOT NULL,
CONSTRAINT pk_Book PRIMARY KEY(hotelNo)
);
This is one of my data (hotelNo, roomNo, guestNo, dateFrom, dateTo) VALUES (13,20,16,to_date('1/21/2008', 'MM/DD/YYYY'),to_date('4/0/2008', 'MM/DD/YYYY'));
My solution is
SELECT dateFrom, COUNT(*) TOTAL from Booking
GROUP BY dateFrom
ORDER BY dateFrom DESC
This could be a simple approach, aggregating by year and month, ordering for the number of occurrences and keeping the row with the maximum number of occurrences.
If you have more than a month with the maximum number of occurrences, this will only give one of them.
Say you have a table like this:
insert into book values (1, null, date '2017-01-01', date '2017-01-01', 99);
insert into book values (2, null, date '2017-01-02', date '2017-01-01', 99);
insert into book values (3, null, date '2017-01-03', date '2017-01-01', 99);
insert into book values (4, null, date '2017-02-01', date '2017-01-01', 99);
insert into book values (5, null, date '2017-02-02', date '2017-01-01', 99);
This query
select *
from (
select count(1),
extract(month from dateFrom) month,
extract(year from dateFrom) year
from Book
group by extract(month from dateFrom),
extract(year from dateFrom)
order by 1 desc
)
where rownum = 1
will give
COUNT(1) MONTH YEAR
---------- ---------- ----------
3 1 2017

Resources