I have created a table 'USER' in Oracle SQL Developer.
CREATE TABLE USER(
USER_ID INTEGER NOT NULL,
FIRST_NAME VARCHAR2(50) NOT NULL,
LAST_NAME VARCHAR2(50) NOT NULL,
ID_CODE INTEGER NOT NULL,
DATE_OF_BIRTH TIMESTAMP NOT NULL,
TRAINING_CLUB VARCHAR2(50) NOT NULL,
HAVE_LICENCE NUMBER(1) NOT NULL,
AGE_GROUP VARCHAR2(3) NULL,
SPECIALITY VARCHAR2(100) NOT NULL,
COMMENT VARCHAR2(200) NULL,
CONSTRAINT USER_PK PRIMARY KEY (USER_ID)
);
Now I'd like to insert data in it, but it will only accept date which is 2000 or more and when it's less than 2000, for example '1998-13-07', it shows not a valid month.
INSERT INTO USER(USER_ID, FIRST_NAME, LAST_NAME, ID_CODE, DATE_OF_BIRTH, TRAINING_CLUB, HAVE_LICENCE, AGE_GROUP, SPECIALITY) VALUES (1, 'Mari', 'Mets', 4990713134, '1999-13-07', 'Erki Noole Athletics club', 1, 'U23', 'Sprint, hurdle race' );
INSERT INTO USER(USER_ID, FIRST_NAME, LAST_NAME, ID_CODE, DATE_OF_BIRTH, TRAINING_CLUB, HAVE_LICENCE, AGE_GROUP, SPECIALITY) VALUES (2, 'Meelis', 'Valgepea', 39704230213, '1997-23-04', 'Runningpartner', 1, 'M', 'Middle- ja long distance running');
INSERT INTO USER(USER_ID, FIRST_NAME, LAST_NAME, ID_CODE, DATE_OF_BIRTH, TRAINING_CLUB, HAVE_LICENCE, AGE_GROUP, SPECIALITY) VALUES (3, 'Karina', 'Justinov', 6020330872, '2002-30-03', 'SK Fortis', 1,'U20','Long and high jump');
INSERT INTO USER(USER_ID, FIRST_NAME, LAST_NAME, ID_CODE, DATE_OF_BIRTH, TRAINING_CLUB, HAVE_LICENCE, AGE_GROUP, SPECIALITY) VALUES (4, 'Lewis', 'Kordon', 38711120678, '1987-12-11', 'Niidupargi Athletics club', 1, 'M', 'Shot put');
INSERT INTO USER(USER_ID, FIRST_NAME, LAST_NAME, ID_CODE, DATE_OF_BIRTH, TRAINING_CLUB, HAVE_LICENCE, AGE_GROUP, SPECIALITY) VALUES (5, 'Getter', 'Tihhikov', 49802280417, '1998-28-02', 'Trainingpartner', 1, 'W','Long distance running');
INSERT INTO USER(USER_ID, FIRST_NAME, LAST_NAME, ID_CODE, DATE_OF_BIRTH, TRAINING_CLUB, HAVE_LICENCE, AGE_GROUP, SPECIALITY) VALUES (6, 'Andres', 'Allikvee', 50603212165, '2006-21-03', 'SK Lindon', 1, 'U16', 'Pole vault');
It shows that row 3 and 6 will be added and 1, 2, 4, 5 have not valid month.
create table users(
user_id integer not null,
date_of_birth timestamp not null
);
insert into users values (1, '1999-13-07');
-- ORA-01843: not a valid month
The problem is with the date literal. We are giving a literal string for a timestamp column, so how this is interpreted depends on database settings.
We can either explictly turn the strings to timestamps, providing the proper format specification:
into users values (1, to_timestamp('1999-13-07', 'YYYY-DD-MM'));
-- 1 rows affected
Or we can change the default timestamp format for our current session, so strings are correctly implicitly converted:
alter session set nls_timestamp_format = 'YYYY-DD-MM';
insert into users values (2, '1999-13-07');
-- 1 rows affected
Demo on DB Fiddle
Related
Summary of Post: I tried to full redact the TITLE column, but it's not working even though the script is executing just fine. I just want the data from the TITLE column in the FACULTY table to be redacted and not visible to users who aren't secmgr.
Hello, Below is my code and it executes perfectly (or so I thought). No error messages appear, but when I go to check if the redact policy works, I find that the data is not being redacted and it is is still visible. I check the priviledges of the test account (OO) but it hasn't been granted any privileged other thatn create session and select. Please help.
set echo on
show user;
---- Clean up workspace
drop table STUDENT;
drop table FACULTY;
drop table OFFERING;
drop table ENROLLMENT;
drop user oo;
drop user secmgr cascade;
drop user sysmgr cascade;
drop user u1;
drop user u2;
drop role pr;
---- Create the tables using DDL
create table STUDENT(
STUDENTID number(3) not null,
NAME varchar(10),
MAJOR varchar(20),
STATUS char(2),
ADDRESS varchar(15),
GPA number(3,2)
);
create table FACULTY(
FACULTYID number(4),
NAME varchar(10),
ORGCD varchar (3),
ADDRESS varchar(15),
CC number(16),
SALARY number (7),
RANK varchar(12),
TITLE varchar (4)
);
create table OFFERING(
OFFERINGNUM number(4) not null,
COURSENUM char(5),
FACULTYID number(4),
TERM varchar(6),
YEAR char(4),
TIME varchar(8)
);
create table ENROLLMENT(
OFFERINGNUM number(4) not null,
STUDENTID number(3) not null
);
---- Populate the tables with data and check using DML
insert all
into STUDENT (STUDENTID, NAME, MAJOR, STATUS, ADDRESS, GPA) values (100, 'ABLE', 'HISTORY', 'SR', '1 UTAH', 3.00)
into STUDENT (STUDENTID, NAME, MAJOR, STATUS, ADDRESS, GPA) values (200, 'BAKER', 'ACCOUNTING', 'JR', '2 IOWA', 2.70)
into STUDENT (STUDENTID, NAME, MAJOR, STATUS, ADDRESS, GPA) values (300, 'CHARLES', 'MATH', 'SR', '3 MAINE', 3.50)
into STUDENT (STUDENTID, NAME, MAJOR, STATUS, ADDRESS, GPA) values (400, 'DRAKE', 'COMPUTER SCIENCEY', 'FR', '4 IDAHO', 2.80)
into STUDENT (STUDENTID, NAME, MAJOR, STATUS, ADDRESS, GPA) values (500, 'ELLIOT', 'COMPUTER SCIENCEY', 'SM', '5 NEVADA', 3.25)
SELECT * FROM dual;
SELECT * FROM STUDENT;
insert all
into FACULTY (FACULTYID, NAME, ORGCD, ADDRESS, CC, SALARY, RANK, TITLE) values (0980, 'MARTIN', 'IM', '11 MAIN', 4560123450001234, 250000, 'DEAN', 'CEO')
into FACULTY (FACULTYID, NAME, ORGCD, ADDRESS, CC, SALARY, RANK, TITLE) values (5430, 'SEAVER', 'IS', '12 SOUTH', 4560123450002345, 180000, 'PROFESSOR', 'CIO')
into FACULTY (FACULTYID, NAME, ORGCD, ADDRESS, CC, SALARY, RANK, TITLE) values (7650, 'LOONEY', 'IT', '14 NORTH', 4560123450003456, 160000, 'INSTRUCTOR', 'CISO')
into FACULTY (FACULTYID, NAME, ORGCD, ADDRESS, CC, SALARY, RANK, TITLE) values (9870, 'MILLS', 'SA', '16 EAST', 4560123450004567, 90000, 'LECTURER', 'CFO')
into FACULTY (FACULTYID, NAME, ORGCD, ADDRESS, CC, SALARY, RANK, TITLE) values (9990, 'BOND', 'INT', '007 NE', 4560123450005678, 90000, 'COACH', 'SPY')
SELECT * FROM dual;
SELECT * FROM FACULTY;
insert all
into OFFERING (OFFERINGNUM, COURSENUM, FACULTYID, TERM, YEAR, TIME) values (1111, 'IS320', 5430, 'FALL', '2012', '10 AM')
into OFFERING (OFFERINGNUM, COURSENUM, FACULTYID, TERM, YEAR, TIME) values (1233, 'IS320', 0980, 'FALL', '2012', '11 AM')
into OFFERING (OFFERINGNUM, COURSENUM, FACULTYID, TERM, YEAR, TIME) values (2222, 'IS460', 7650, 'SPRING', '2013', '10 AM')
into OFFERING (OFFERINGNUM, COURSENUM, FACULTYID, TERM, YEAR, TIME) values (3333, 'IT480', 5430, 'SPRING', '2013', '11 AM')
SELECT * FROM dual;
SELECT * FROM OFFERING;
insert all
into ENROLLMENT (OFFERINGNUM, STUDENTID) values (1111, 100)
into ENROLLMENT (OFFERINGNUM, STUDENTID) values (1233, 500)
into ENROLLMENT (OFFERINGNUM, STUDENTID) values (2222, 300)
into ENROLLMENT (OFFERINGNUM, STUDENTID) values (3333, 400)
SELECT * FROM dual;
SELECT * FROM ENROLLMENT;
-- User created for testing redaction
create user oo identified by o;
grant create session to oo
grant select on FACULTY to oo;
-- Create a security manager who will create the redact policy
show user;
create user secmgr identified by s;
grant dba to secmgr;
grant create session to secmgr;
create user sysmgr identified by s2;
grant dba to sysmgr;
grant create session to sysmgr;
connect sysmgr/s2;
connect sys/ as sysdba;
grant execute on dbms_redact to secmgr;
-- Create the policy (full redact)
-- cleanup workspace
begin
dbms_redact.drop_policy (
object_schema => 'DBST_USER',
object_name => 'FACULTY',
policy_name => 'FACULTY_POL'
);
end;
/
connect secmgr/s;
begin
dbms_redact.add_policy
(object_schema => 'DBST_USER',
object_name => 'FACULTY',
policy_name => 'FACULTY_POL',
column_name => 'TITLE',
function_type => DBMS_REDACT.FULL,
expression => '1=1');
end;
/
connect oo/o;
select * from dbst_user.FACULTY;
I also checked to see if the policy was active and found the following; apparently, the FACULTY_POL is active, multiple times despite my dropping the policy. I can't seem to get rid of them. I have a feeling this is causing the problem.
I have a table as such:
CREATE TABLE people
(
id INTEGER NOT NULL PRIMARY KEY,
motherId INTEGER,
fatherId INTEGER,
name VARCHAR(30) NOT NULL,
age INTEGER NOT NULL,
FOREIGN KEY (motherId) REFERENCES people(id),
FOREIGN KEY (fatherId) REFERENCES people(id)
);
INSERT INTO people(id, motherId, fatherId, name, age)
VALUES (1, NULL, NULL, 'Adam', 50);
INSERT INTO people(id, motherId, fatherId, name, age)
VALUES (2, NULL, NULL, 'Eve', 50);
INSERT INTO people(id, motherId, fatherId, name, age)
VALUES (3, 2, 1, 'Cain', 30);
INSERT INTO people(id, motherId, fatherId, name, age)
VALUES (4, 2, 1, 'Seth', 20);
I would like to write a query and get the name of the parents and the age of their youngest children. Not sure how to go about with this problem.
Expected output:
-- Expected output (in any order):
-- name age
-- ----------------------------
-- Adam 20
-- Eve 20
SELECT P1.name, P2.name, T.minAge
FROM (SELECT P.motherId, P.fatherId, MIN(AGE) AS minAge
FROM people P
GROUP BY P.motherId, P.fatherId) AS T JOIN people P1 ON T.motherId=P1.id
JOIN people P2 ON T.fatherId=P2.id
db<>fiddle
Schema:
created table dept
CREATE table dept
(dept_id varchar (20) default 'department' NOT NULL,
locat varchar (25) default 'location' NOT NULL,
nme varchar (20) default 'name' NOT NULL);
inserting values
INSERT INTO dept(dept_id, locat, nme)
VALUES (1, 'dublin', 'payroll');
INSERT INTO dept(dept_id, locat, nme)
VALUES (2, 'galway', 'manufacturing');
INSERT INTO dept(dept_id, locat, nme)
VALUES (3, 'cork', 'sales');
created table emp
create table emp
(emp_id varchar (20) default 'id' NOT NULL,
NME varchar (20) default 'name' NOT NULL,
job_title varchar (25) default 'job' NOT NULL,
HIRE_DATE DATE,
SALARY INT (25) default '0' NOT NULL,
dept_id varchar (20) default 'dept' NOT NULL);
inserting values
INSERT INTO emp
VALUES (123, 'byrne', 'clerical', DATE ('2012-06-12'), 28000, 1);
INSERT INTO emp
VALUES (124, 'barry', 'operater', DATE ('11-07-11'), 33000,2);
INSERT INTO emp
VALUES (125, 'hynes', 'senior_operator', DATE ('26-09-13'), 36500,2);
INSERT INTO emp
VALUES (126, 'WILLIAMS', 'manager', DATE ('30-10-13'), 51000,3);
THE FOLLOWING QUERY IS RETURNING NO RECORDS
SELECT NME FROM emp
where HIRE_DATE between ('01-01-10') AND ('01-01-14');
How to change the code to get the desired output.
You proper date formats. In Oracle, you can introduce constants with the date keyword:
INSERT INTO emp (emp_id, NME, job_title , HIRE_DATE, SALARY, dept_id)
VALUES (123, 'byrne', 'clerical', DATE '2012-06-12', 28000, 1);
INSERT INTO emp (emp_id, NME, job_title , HIRE_DATE, SALARY, dept_id)
VALUES (124, 'barry', 'operater', DATE '2011-07-11', 33000, 2);
INSERT INTO emp (emp_id, NME, job_title , HIRE_DATE, SALARY, dept_id)
VALUES (125, 'hynes', 'senior_operator', DATE '2013-09-26', 36500, 2);
INSERT INTO emp (emp_id, NME, job_title , HIRE_DATE, SALARY, dept_id)
VALUES (126, 'WILLIAMS', 'manager', DATE '2013-10-30', 51000, 3);
select NME
from emp
where HIRE_DATE between date '2010-01-01' and date '2014-01-01';
I thought 2-digit years when out in 1999. Oracle might not be interpreting two-digit years the same way that you do.
Also, include the column list when doing an insert.
I need help on SQL Queries.
TABLE:
Create Table Employees
( employee_id number(3) Primary Key,
first_name varchar2(10),
last_name varchar2(10),
dept_code varchar2(3),
hire_date date,
credit_limit number(4,2),
phone_ext varchar2(4),
manager_id number(3)
);
Entries:
insert into Employees values (201, 'Susan', 'Brown', 'Exe', To_Date('01-Jun-1998','DD-Mon-YYYY'), 30, '3484', null);
insert into Employees values (202, 'Jim', 'Kern', 'Sal', To_Date('16-Aug-1999','DD-Mon-YYYY'), 25, '8722', 201);
insert into Employees values (203, 'Martha', 'Woods', 'Shp', To_Date('02-Feb-2004','DD-Mon-YYYY'), 25, '7591', 201);
insert into Employees values (204, 'Ellen', 'Owens', 'Sal', To_Date('01-Jul-2003','DD-Mon-YYYY'), 15, '6830', 202);
I need a query to List all the employees, their hire dates and the number of days each person will have worked for the company as of January 1, 2007.
You can use YEAR() on a date field:
SELECT * FROM Employees WHERE YEAR(hire_date)='2003'
if it doesn't work, you can use EXTRACT():
SELECT * FROM Employees WHERE EXTRACT(YEAR FROM hire_date)='2003'
and, as Alan Hadsell rightly commented:
SELECT COUNT(*) FROM Employees WHERE EXTRACT(YEAR FROM hire_date)='2003'
Using Table aliases, list the first name, last name and start date of students enrolled on the java fundamentals module:
I am having some trouble when running the query below.
SELECT stu.StudFName, stu.StudLName, enrol.StartDate
From Student stu
INNER JOIN Enrolment enrol
ON stu.StudID = enrol.StudID
INNER JOIN Module mod
ON enrol.ModCode = mod.ModCode
WHERE mod.ModName = 'Java Fundamentals'
Structure:
CREATE TABLE Student
(StudID INTEGER PRIMARY KEY,
StudFName VARCHAR(10) NOT NULL,
StudLName VARCHAR(10) NOT NULL,
DoB DATE NOT NULL,
Sex CHAR(1) NOT NULL CHECK (Sex IN ('M', 'F')),
Email VARCHAR(30) UNIQUE);
CREATE TABLE Staff
(StaffID INTEGER PRIMARY KEY,
Title VARCHAR(4) CHECK (Title IN ('Prof', 'Dr', 'Mr', 'Mrs', 'Miss')),
StaffFName VARCHAR(10) NOT NULL,
StaffLName VARCHAR(10) NOT NULL,
Email VARCHAR(30) UNIQUE,
Department VARCHAR(25) DEFAULT 'Not Assigned',
Extension INTEGER CHECK (Extension BETWEEN 0001 AND 9999));
CREATE TABLE Module
(ModCode CHAR(4) PRIMARY KEY,
ModName VARCHAR(25) NOT NULL,
ModCredits INTEGER NOT NULL CHECK (ModCredits IN (15, 30, 45, 60)),
ModLevel CHAR(3) NOT NULL CHECK (ModLevel IN ('UG1', 'UG2', 'UG3', 'MSc')),
ModLeader INTEGER NOT NULL,
Foreign Key (ModLeader) REFERENCES Staff (StaffID));
CREATE TABLE Enrolment
(ModCode CHAR(4) NOT NULL,
StudID INTEGER NOT NULL,
StartDate DATE NOT NULL,
PRIMARY KEY (ModCode, StudID),
Foreign Key (StudID) REFERENCES Student (StudID),
Foreign Key (ModCode) REFERENCES Module (ModCode));
The answer is... there is no trouble with your query.
It works just fine.
Check here
INSERT INTO Student (StudID, StudFName, StudLName, DoB, Sex, Email) VALUES
(1, 'Jack', 'Black', TO_DATE('2015/01/01', 'yyyy/mm/dd'), 'M', 'jack#email.com');
INSERT INTO Student (StudID, StudFName, StudLName, DoB, Sex, Email) VALUES
(2, 'Andrew', 'Wiggin', TO_DATE('2015/01/01', 'yyyy/mm/dd'), 'M', 'andrew#email.com');
INSERT INTO Student (StudID, StudFName, StudLName, DoB, Sex, Email) VALUES
(3, 'Bob', 'Marley', TO_DATE('2015/01/01', 'yyyy/mm/dd'), 'M', 'bob#email.com');
INSERT INTO Staff (StaffID, Title, StaffFName, StaffLName, Email, Extension) VALUES
(1, 'Prof', 'Joe', 'Smith', 'stuff#emal.com', 0001);
INSERT INTO Module (ModCode, ModName, ModCredits, ModLevel, ModLeader) VALUES
(1, 'Java Fundamentals', 30, 'UG1', 1);
INSERT INTO Module (ModCode, ModName, ModCredits, ModLevel, ModLeader) VALUES
(2, 'C# Fundamentals', 15, 'UG2', 1);
INSERT INTO Enrolment (ModCode, StudID, StartDate) VALUES
(1, 1, TO_DATE('2015/01/01', 'yyyy/mm/dd'));
INSERT INTO Enrolment (ModCode, StudID, StartDate) VALUES
(1, 2, TO_DATE('2015/01/02', 'yyyy/mm/dd'));
INSERT INTO Enrolment (ModCode, StudID, StartDate) VALUES
(2, 3, TO_DATE('2015/01/03', 'yyyy/mm/dd'));
-------------------------------------------------------------
SELECT stu.StudFName, stu.StudLName, enrol.StartDate
From Student stu
INNER JOIN Enrolment enrol ON stu.StudID = enrol.StudID
INNER JOIN Module mod ON enrol.ModCode = mod.ModCode
WHERE mod.ModName = 'Java Fundamentals'
Result
STUDFNAME STUDLNAME STARTDATE
----------------------------------------
Jack Black January, 01 2015
Andrew Wiggin January, 02 2015