there is table called event that act as parent the child inherit tables of event are special event and hotel event i have created the types as bellow but I'm contuse about how to create tables to these tables in oracle.I have referred most of the currently available solutions within Stack overflow, git hub etc. However, none of these solutions have worked out successfully.
Table types :
Event_t (
EventID:char(5),
EventType:varchar(20),
VenueName:varchar(50),
NoOfGuest:number(10)
) NOT FINAL
HotelEvent_t (
Date:date,
Price:numbr(8,2)
) UNDER Event_t
SpecialEvent_t (
BookingDate:date,
EndDate:date,
MenuNumber:number(2),
Reservation ref Reservation_t
) UNDER event_t
Thank you very much and any suggestion will be greatly appreciated.
Create your types using the correct syntax:
CREATE TYPE Event_t AS OBJECT(
EventID char(5),
EventType varchar(20),
VenueName varchar(50),
NoOfGuest number(10)
) NOT FINAL;
CREATE TYPE HotelEvent_t UNDER Event_t (
datetime date, -- Date is a keyword, try to use a different name.
Price number(8,2)
);
CREATE TYPE SpecialEvent_t UNDER event_t (
BookingDate date,
EndDate date,
MenuNumbers NUMBER(2),
Reservation ref Reservation_t
);
Then you can create an object table:
CREATE TABLE Events OF Event_T(
eventid CONSTRAINT Events__EventID__PK PRIMARY KEY
);
Then you can insert the different types into it:
INSERT INTO EVENTS VALUES(
HotelEvent_T(
'H1',
'HOTEL',
'Venue1',
42,
DATE '0001-02-03' + INTERVAL '04:05:06' HOUR TO SECOND,
123456.78
)
);
INSERT INTO EVENTS VALUES(
SpecialEvent_T(
'SE1',
'SPECIAL',
'Time Travel Convention',
-1,
SYSDATE,
TRUNC(SYSDATE),
0,
NULL
)
);
and get the data out of it:
SELECT e.*,
TREAT( VALUE(e) AS HotelEvent_T ).datetime AS datetime,
TREAT( VALUE(e) AS HotelEvent_T ).price AS price,
TREAT( VALUE(e) AS SpecialEvent_T ).bookingdate AS bookingdate,
TREAT( VALUE(e) AS SpecialEvent_T ).enddate AS enddate,
TREAT( VALUE(e) AS SpecialEvent_T ).menunumbers AS menunumbers
FROM Events e;
Which outputs:
EVENTID | EVENTTYPE | VENUENAME | NOOFGUEST | DATETIME | PRICE | BOOKINGDATE | ENDDATE | MENUNUMBERS
:------ | :-------- | :--------------------- | --------: | :------------------ | --------: | :------------------ | :------------------ | ----------:
H1 | HOTEL | Venue1 | 42 | 0001-02-03 04:05:06 | 123456.78 | null | null | null
SE1 | SPECIAL | Time Travel Convention | -1 | null | null | 2020-03-30 21:11:22 | 2020-03-30 00:00:00 | 0
db<>fiddle here
The typical way to create these tables in Oracle would be:
create table event_t (
event_id char(5) primary key not null,
event_type varchar2(20),
venue_mame varchar2(50),
no_of_guest number(10)
);
create table hotel_event_t (
event_date date,
price number(8,2),
event_id char(5),
constraint fk1 foreign key (event_id) references event_t (event_id)
);
create table special_event_t (
booking_date date,
end_date date,
menu_number number(2),
reservation_id char(5),
constraint fk2 foreign key (reservation_id)
references reservation_t (reservation_id),
event_id char(5),
constraint fk3 foreign key (event_id) references event_t (event_id)
);
Related
i created table with 4column with
create table conversation(
user_name varchar(200),
employer_name varchar(200),
message text,
date_sent timestamp
)
now i want alter date_sent column without remove it i want to set default value of current_timestamp for this column what i must do
You can use
ALTER TABLE conversation ALTER COLUMN
date_sent SET DEFAULT current_timestamp;
create table conversation(
user_name varchar(200),
employer_name varchar(200),
message text,
date_sent timestamp );
ALTER TABLE conversation ALTER COLUMN
date_sent SET DEFAULT current_timestamp;
insert into conversation
(user_name) values
('me');
select * from conversation;
user_name | employer_name | message | date_sent
:-------- | :------------ | :------ | :-------------------------
me | null | null | 2022-04-01 09:38:13.674547
db<>fiddle here
i have this table salle that has 2 attributes
this is the table that inherits from salle, it's called salleCours and has 3 additional attributes.
when i run the second command in oracle 11g express in sql command line it says under 'under' : missing or invalid option.
i dont know if it's a syntax problem or something else
Create table salle(
Numero varchar(20) primary key,
Videoprojecteur char(1) ) ;
Create table salleCours UNDER salle(
Capacite number(3),
Retroprojecteur char(1),
micro char(1)) ;
You want to define an OBJECT type and then use an object-defined table:
CREATE TYPE salle_type AS OBJECT(
Numero varchar(20),
Videoprojecteur char(1)
) NOT FINAL;
CREATE TYPE salleCours_type UNDER salle_type(
Capacite number(3),
Retroprojecteur char(1),
micro char(1)
);
CREATE TABLE salle OF salle_type (
Numero PRIMARY KEY
);
Then you can insert rows of either type:
INSERT INTO salle VALUES( salle_type( 'abc', 'Y' ) );
INSERT INTO salle VALUES( salleCours_type( 'def', 'Y', 42, 'N', 'X' ) );
And, if you want the values:
SELECT s.*,
TREAT( VALUE(s) AS salleCours_type ).Capacite AS capacite,
TREAT( VALUE(s) AS salleCours_type ).Retroprojecteur AS Retroprojecteur,
TREAT( VALUE(s) AS salleCours_type ).micro AS micro
FROM salle s
Which outputs:
NUMERO | VIDEOPROJECTEUR | CAPACITE | RETROPROJECTEUR | MICRO
:----- | :-------------- | -------: | :-------------- | :----
abc | Y | null | null | null
def | Y | 42 | N | X
db<>fiddle here
I'm practising triggers on Oracle and I'm having troubles with this trigger:
CREATE OR REPLACE TRIGGER MODIFICACIONES_SALARIOS AFTER
UPDATE OF salario ON empleados_pac
FOR EACH ROW
DECLARE
v_username VARCHAR(10);
v_hora VARCHAR2(10);
BEGIN
SELECT user INTO v_username FROM dual;
SELECT to_char(sysdate, 'HH24:MI:ss') INTO v_hora FROM dual;
INSERT INTO audita_salarios (id_emp, salario_antiguo, salario_nuevo, fecha, hora, username) VALUES (id_empleado, :old.salario, :new.salario, sysdate, v_hora, v_username);
END;
I'm doing an historic table where when the salary of the employeer changes the trigger inserts in the historic his ID, the old salary, the new one, the date and the user who changed the salary.
All works perfectly except for the ID insert. Oracle says that the column is not allowed.
I think the problem is because I want to insert an unique ID into a new column where the ID is going to be repeated if the salary changes two or more times. How could I fix this?
I think the problem is because I want to insert an unique ID into a new column where the ID is going to be repeated if the salary changes two or more times. How could I fix this?
Do not have a UNIQUE constraint on audita_salarios.id_emp; instead have a composite UNIQUE constraint on id_emp and fecha (if you need a UNIQUE constraint at all).
Also:
use a virtual column for HORA (or remove that column as it is just duplicating the time component of FECHA);
use :NEW.id_empleado rather than id_empleado in the trigger.
CREATE TABLE empleados_pac (
id_empleado NUMBER(2),
salario NUMBER(10,2)
);
CREATE TABLE audita_salarios (
id_emp NUMBER(2),
salario_antiguo NUMBER(10,2),
salario_nuevo NUMBER(10,2),
fecha DATE,
hora VARCHAR2(8)
GENERATED ALWAYS AS ( CAST( TO_CHAR( fecha, 'HH24:MI:SS' ) AS VARCHAR2(8) ) ),
username VARCHAR2(60),
CONSTRAINT audita_salarios__pk PRIMARY KEY ( id_emp, fecha )
);
CREATE TRIGGER MODIFICACIONES_SALARIOS
AFTER UPDATE OF salario ON empleados_pac
FOR EACH ROW
DECLARE
BEGIN
INSERT INTO audita_salarios (
id_emp, salario_antiguo, salario_nuevo, fecha, username
) VALUES (
:new.id_empleado, :old.salario, :new.salario, sysdate, USER
);
END;
/
INSERT INTO empleados_pac ( id_empleado, salario ) VALUES ( 1, 10 );
UPDATE empleados_pac
SET salario = 20
WHERE id_empleado = 1;
BEGIN
DBMS_SESSION.SLEEP(1);
END;
/
UPDATE empleados_pac
SET salario = 30
WHERE id_empleado = 1;
-- You do not actually need to constraint on the audit table unless you
-- want to enforce that salary updates must be on different times.
ALTER TABLE audita_salarios DROP CONSTRAINT audita_salarios__pk;
UPDATE empleados_pac
SET salario = 40
WHERE id_empleado = 1;
Then:
SELECT * FROM audita_salarios;
Outputs:
ID_EMP | SALARIO_ANTIGUO | SALARIO_NUEVO | FECHA | HORA | USERNAME
-----: | --------------: | ------------: | :------------------ | :------- | :--------------------------
1 | 10 | 20 | 2021-04-01 13:02:42 | 13:02:42 | FIDDLE_XRWUFWRAYDJNIGNCOQFP
1 | 20 | 30 | 2021-04-01 13:02:43 | 13:02:43 | FIDDLE_XRWUFWRAYDJNIGNCOQFP
1 | 30 | 40 | 2021-04-01 13:02:43 | 13:02:43 | FIDDLE_XRWUFWRAYDJNIGNCOQFP
db<>fiddle here
I have the following three tables:
CREATE TABLE group (
id SERIAL PRIMARY KEY,
name VARCHAR NOT NULL,
insert_date TIMESTAMP WITH TIME ZONE NOT NULL
);
CREATE TABLE customer (
id SERIAL PRIMARY KEY,
ext_id VARCHAR NOT NULL,
insert_date TIMESTAMP WITH TIME ZONE NOT NULL
);
CREATE TABLE customer_in_group (
id SERIAL PRIMARY KEY,
customer_id INT NOT NULL,
group_id INT NOT NULL,
insert_date TIMESTAMP WITH TIME ZONE NOT NULL,
CONSTRAINT customer_id_fk
FOREIGN KEY(customer_id)
REFERENCES customer(id),
CONSTRAINT group_id_fk
FOREIGN KEY(group_id)
REFERENCES group(id)
)
I need to find all of the groups which have not had any customer_in_group entities' group_id column reference them within the last two years. I then plan to delete all of the customer_in_groups that reference them, and finally delete that group after finding them.
So basically given the following two groups and the following 3 customer_in_groups
Group
| id | name | insert_date |
|----|--------|--------------------------|
| 1 | group1 | 2011-10-05T14:48:00.000Z |
| 2 | group2 | 2011-10-05T14:48:00.000Z |
Customer In Group
| id | group_id | customer_id | insert_date |
|----|----------|-------------|--------------------------|
| 1 | 1 | 1 | 2011-10-05T14:48:00.000Z |
| 2 | 1 | 1 | 2020-10-05T14:48:00.000Z |
| 3 | 2 | 1 | 2011-10-05T14:48:00.000Z |
I would expect just to get back group2, since group1 has a customer_in_group referencing it inserted in the last two years.
I am not sure how I would write the query that would find all of these groups.
As a starter, I would recommend enabling on delete cascade on foreing keys of customer_in_group.
Then, you can just delete the rows you want from groups, and it will drop the dependent rows in the child table. For this, you can use not exists:
delete from groups g
where not exists (
select 1
from customer_in_group cig
where cig.group_id = g.id and cig.insert_date >= now() - interval '2 year'
)
Sales table:
id | product_id | year | quantity | price
---+------------+------+----------+-------
1 | 100 | 2008 | 10 | 5000
2 | 100 | 2008 | 10 | 5000
3 | 200 | 2011 | 15 | 9000
Products table:
id | product_name | product_id
---+--------------+------------
1 | Nokia | 100
2 | Apple | 200
3 | Samsung | 200
In these two tables, I have references the Sales table and the Products table as shown here:
CREATE TABLE Sales_table
(
id SERIAL PRIMARY KEY,
product_id INTEGER NOT NULL,
year INTEGER NOT NULL,
quantity INTEGER NOT NULL,
price INTEGER NOT NULL
);
CREATE TABLE Products_table
(
id SERIAL PRIMARY KEY,
product_name VARCHAR NOT NULL,
product_id INTEGER REFERENCES Sales_table
);
These two tables are created successfully, but when I insert data into the Products_table, I get an error
ERROR: insert or update on table "products_table" violates foreign key constraint "products_table_product_id_fkey"
DETAIL: Key (product_id)=(200) is not present in table "sales_table".
By the mean of, the referenced table doesn't allow same 'product_id' field name means, then why the Products_table table has been created with the REFERENCES of Sales_table?
Your foreign key references are backwards. You want:
CREATE TABLE Products_table (
id SERIAL PRIMARY KEY,
product_name VARCHAR NOT NULL
);
CREATE TABLE Sales_table (
id SERIAL PRIMARY KEY,
product_id INTEGER NOT NULL,
year INTEGER NOT NULL,
quantity INTEGER NOT NULL,
price INTEGER NOT NULL,
product_id INTEGER REFERENCES product_table (id)
);
I actually advise naming the primary key after the table -- so primary key and foreign key column names match (i.e. JOINs with USING make sense). Also, in more recent versions of Postgres, generated always as identity is preferred over serial. So:
CREATE TABLE Products_table (
product_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
product_name VARCHAR NOT NULL
);
CREATE TABLE Sales_table (
sale_id GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
product_id INTEGER NOT NULL,
year INTEGER NOT NULL,
quantity INTEGER NOT NULL,
price INTEGER NOT NULL,
product_id INTEGER REFERENCES products_table (product_id)
);