I use these tables to store data.
I would like to delete all data older than 1 month (for example) using one SQL query from all tables.
-- TABLE AGENT_HISTORY
CREATE TABLE AGENT_HISTORY(
EVENT_ID INTEGER NOT NULL,
AGENTID INTEGER NOT NULL,
EVENT_DATE DATE NOT NULL
)
/
CREATE TABLE CPU_HISTORY(
CPU_HISTORY_ID INTEGER NOT NULL,
EVENT_ID INTEGER,
CPU_NAME VARCHAR2(50 ) NOT NULL,
CPU_VALUE NUMBER NOT NULL
)
/
CREATE TABLE CPU_TEMP_HISTORY(
CPU_TEMP_HISTORY_ID INTEGER NOT NULL,
EVENT_ID INTEGER,
CPU_TEMP_NAME VARCHAR2(50 ) NOT NULL,
CPU_TEMP_VALUE NUMBER NOT NULL
)
/
CREATE TABLE MEMORY_HISTORY(
MEMORY_HISTORY_ID INTEGER NOT NULL,
EVENT_ID INTEGER,
MEMORY_NAME VARCHAR2(50 ) NOT NULL,
MEMORY_VALUE NUMBER NOT NULL
)
/
CREATE TABLE DISK_HISTORY(
DISK_HISTORY_ID INTEGER NOT NULL,
EVENT_ID INTEGER,
DISK_NAME VARCHAR2(50 ) NOT NULL,
DISK_VALUE NUMBER NOT NULL
)
/
CREATE TABLE NETWORK_HISTORY(
NETWORK_HISTORY_ID INTEGER NOT NULL,
EVENT_ID INTEGER,
ADAPTER_NAME VARCHAR2(50 ) NOT NULL,
TRANSMITBYTES NUMBER NOT NULL,
TRANSMITSPEED NUMBER,
RECEIVESPEED NUMBER,
RECEIVEBYTES NUMBER
)
/
CREATE TABLE SWAP_HISTORY(
SWAP_HISTORY_ID INTEGER NOT NULL,
EVENT_ID INTEGER,
SWAP_NAME VARCHAR2(50 ) NOT NULL,
SWAP_VALUE NUMBER NOT NULL
)
/
CREATE TABLE CONNECTIONS_HISTORY(
CONNECTIONS_HISTORY_ID INTEGER NOT NULL,
EVENT_ID INTEGER,
CONNECTIONS_NAME VARCHAR2(50 ) NOT NULL,
CONNECTIONS_VALUE NUMBER NOT NULL
)
/
CREATE TABLE PARTITIONS_HISTORY(
PARTITIONS_HISTORY_ID INTEGER NOT NULL,
EVENT_ID INTEGER,
PARTITIONS_NAME VARCHAR2(50 ) NOT NULL,
PARTITIONS_VALUE NUMBER NOT NULL
)
/
Is this possible in Oracle?
I use EVENT_ID as unique key ID in all tables.
As suggested by "a_horse_with_no_name", you can use ON DELETE CASCADE by creating the required relationships.
For example,
ALTER TABLE table_name
ADD CONSTRAINT constraint_name
FOREIGN KEY (column1, column2, ... column_n)
REFERENCES parent_table (column1, column2, ... column_n)
ON DELETE CASCADE;
Bookmark the Database SQL Language Reference and start reading the concepts for better understanding.
Related
Does anyone know why my code is giving this error even though my return types should match?
The only thing I can think of is that the datatype of reserved_seats and cancelled_seats are not of type INTEGER despite NumSeats column being of type INTEGER (where it gets the data).
If anyone can give any advice I would appreciate it.
The error that occurs is:
ERROR: "return type mismatch in function declared to return record"
My create statement:
CREATE OR REPLACE FUNCTION seatInformation(INTEGER) RETURNS TABLE(flightID INTEGER, reserved_seats
INTEGER, cancelled_seats INTEGER, available_Seats INTEGER, MaxCapacity bigint) AS
$$
SELECT f.flightID, (SELECT reserved_seats FROM (SELECT fb.NumSeats WHERE fb.Status = 'r') AS reserved_seats),
(SELECT cancelled_seats FROM(SELECT fb.NumSeats WHERE fb.Status = 'c') AS cancelled_seats), MaxCapacity - SUM(fb.NumSeats) AS Available_Seats, f.MaxCapacity
FROM Flight f INNER JOIN FlightBooking fb
ON f.flightID = fb.flightID
WHERE f.flightID = $1
GROUP BY f.flightID, fb.NumSeats, fb.Status;
$$
LANGUAGE SQL;
CREATE TABLE Flight
(
FlightID INTEGER NOT NULL,
FlightDate TIMESTAMP NOT NULL,
Origin VARCHAR(30) NOT NULL,
Destination VARCHAR(30) NOT NULL,
MaxCapacity INTEGER NOT NULL,
PricePerSeat DECIMAL NOT NULL,
CONSTRAINT Flight_pk PRIMARY KEY(FlightID)
);
CREATE TABLE FlightBooking
(
BookingID INTEGER NOT NULL,
CustomerID INTEGER NOT NULL CONSTRAINT FlightBooking_fkc REFERENCES LeadCustomer(CustomerID) ON DELETE CASCADE,
FlightID INTEGER NOT NULL CONSTRAINT FlightBooking_fkf REFERENCES Flight(FlightID) ON DELETE CASCADE,
NumSeats INTEGER NOT NULL,
Status CHAR(1) NOT NULL,
BookingTime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
TotalCost DECIMAL,
CONSTRAINT FlightBooking_pk PRIMARY KEY(BookingID)
);
Many thanks,
Callum
Consider this:
(SELECT reserved_seats FROM (SELECT fb.NumSeats WHERE fb.Status = 'r') AS reserved_seats)
You may think it is integer, well, it's not. It selects a set.
This is what you may want instead:
(SELECT fb.NumSeats as reserved_seats WHERE fb.Status = 'r' LIMIT 1)
The same for cancelled_seats
These are tables in SQLite: especie_similar
CREATE TABLE especie_similar (
id_especie INTEGER NOT NULL,
id_similar INTEGER NOT NULL,
PRIMARY KEY (
id_especie,
id_similar
),
FOREIGN KEY (
id_especie
)
REFERENCES especie (id_especie) ON DELETE CASCADE,
FOREIGN KEY (
id_similar
)
REFERENCES especie (id_especie) ON DELETE CASCADE
);
And especie table:
CREATE TABLE especie (
id_especie INTEGER PRIMARY KEY AUTOINCREMENT
NOT NULL,
nombre_especie VARCHAR NOT NULL,
cient_especie VARCHAR NOT NULL,
desc_especie TEXT,
nidificacion TEXT NOT NULL,
dimorfismo INTEGER NOT NULL,
genero_id INTEGER NOT NULL,
endemismo_id INTEGER NOT NULL,
abundancia_id INTEGER NOT NULL,
FOREIGN KEY (
genero_id
)
REFERENCES genero (id_genero),
FOREIGN KEY (
endemismo_id
)
REFERENCES endemismo (id_endemismo),
FOREIGN KEY (
abundancia_id
)
REFERENCES abundancia (id_abundancia)
);
My situation: Table especie has multiple datas and I want to insert in especie_similar ids from table especie.
How can i code a trigger or sql statement checking if id_especie and id_similar are not equals?
How can i code to retrieve all especies distinct of target id
(ex: table especie_similar has id_especie=1 and id_similar=2
indicating that species id 1 is similar to another species id 2 but
cannot insert id_especie=2 and id_similar=1 due to previous
duplicate data)?
Thanks and sorry my english is not good.
For question #1 you can use a standard SQL constraint:
create table especie_similar (
...
check (id_especie <> id_similar) -- your condition here
);
For question #2 you can use a standard "Recursive CTE". See SQLite WITH Clause. Example:
with recursive node as (
select id from especie_similar
where id_especie = 123 -- first node criteria here
union all
select s.id_especie
from node n
join especie_similar s on s.id_similar = n.id
)
select * from node;
What i am trying to do is calculate diner_payment_due (DINER TABLE), by multiplying food_serve_cost (FOOD_SERVE TABLE) by food_serve_size (FS_DINER TABLE)
CREATE TABLE diner (
diner_no NUMBER(8) NOT NULL,
diner_payment_due NUMBER(6,2) NOT NULL,
diner_seat_no NUMBER(2) NOT NULL,
diner_seated DATE NOT NULL,
diner_completed DATE,
table_no NUMBER(2) NOT NULL
);
CREATE TABLE food_serve (
food_item_no NUMBER(4) NOT NULL,
food_serve_size CHAR(2) NOT NULL,
food_serve_kilojoules NUMBER(4) NOT NULL,
food_serve_cost NUMBER(5,2) NOT NULL
);
CREATE TABLE FS_DINER (
DINER_NO NUMBER(8) NOT NULL,
FOOD_ITEM_NO NUMBER(4) NOT NULL,
FOOD_SERVE_SIZE CHAR(2 BYTE) NOT NULL,
FS_DINER_NO_SERVES NUMBER(1) NOT NULL,
FS_DINER_ITEM_SERVED CHAR (1) NOT NULL,
CONSTRAINT pk_fs_diner PRIMARY KEY (DINER_NO, FOOD_ITEM_NO,FOOD_SERVE_SIZE)
);
I just need to return a single values and then insert into the diner table, i'm thinking that I need to do a Inner Join, then simply sum (food_serve_cost * NO_SERVES) then group by DINER_NO ?
INSERT INTO DINER ( DINER_NO, DINER_PAYMENT_DUE, DINER_SEAT_NO, DINER_SEATED, DINER_COMPLETED, TABLE_NO) INSERT INTO DINER VALUES (1, SELECT SUM (FOOD_SERVE.FOOD_SERVE_COST * FS_DINER..., , 1, 1/05/2017, 1 /05/2017, 1);
Am updating diner table based on the value from other two tables.
INSERT INTO DINER
(SELECT fs.diner_no,
(fs.food_serve_cost * fd.FOOD_SERVE_SIZE),
11,sysdate,sysdate,11 /*Am assuming, Due to Not null constraint, there will be error*/
FROM food_serve fs,
FS_DINER fd
WHERE fs.food_item_no = fd.food_item_no)
I'm trying to run a file which contains INSERT statements. The statements will either give me an not enough values error or unique constraint error.
Below is my create table statement:
CREATE TABLE PART
(PNum VARCHAR(25) NOT NULL,
PName VARCHAR(75) NOT NULL,
PUnitPrice NUMBER(7,2) NOT NULL,
ComponentOf VARCHAR(25),
CONSTRAINT PART_PKEY PRIMARY KEY(PNum),
CONSTRAINT PART_FKEY FOREIGN KEY(ComponentOf)
REFERENCES PART(PNum)
};
CREATE TABLE MANUFACTURER
(MName VARCHAR(50) NOT NULL,
MAddress VARCHAR(100) NOT NULL,
MPhone VARCHAR(25) NOT NULL,
CONSTRAINT MANUFACTURER_PKEY PRIMARY KEY(MName),
);
CREATE TABLE PART-MANUFACTURED
(MDate DATE,not null
PNum VARCHAR(25) NOT NULL,
MName VARCHAR(50) NOT NULL,
Quantity NUMBER(10) NOT NULL,
CONSTRAINT PART-MANUFACTURED_PKEY PRIMARY KEY(MName,PNum,MDate),
CONSTRAINT PART-MANUFACTURED_FKEY1 FOREIGN KEY(PNum)
REFERENCES PART(PNum),
CONSTRAINT PART-MANUFACTURED_FKEY2 FOREIGN KEY(MName)
REFERENCES MANUFACTURER(MName)
);
CREATE TABLE CUSTOMER
(CNum VARCHAR(25) NOT NULL,
CName VARCHAR(75) NOT NULL,
CType VARCHAR(20) NOT NULL,
CONSTRAINT CUSTOMER_PKEY PRIMARY KEY(CNum),
CONSTRAINT CHECK_CType
CHECK(CType IN (‘INDIVIDUAL,’INSTITUITION’))
);
CREATE TABLE ORDERS
(CNum VARCHAR(25) NOT NULL,
PNum VARCHAR(25) NOT NULL,
OrderDate DATE NOT NULL,
OrderQuantity NUMBER(7,2) NOT NULL,
CONSTRAINT ORDERS_PKEY PRIMARY KEY(CNum,PNum,OrderDate),
CONSTRAINT ORDERS_FKEY1 FOREIGN KEY(CNum)
REFERENCES CUSTOMER(CNum),
CONSTRAINT ORDERS_FKEY2 FOREIGN KEY(PNum)
REFERENCES PART(PNum)
);
Below is the statement that gives me the not enough values error:
INSERT INTO PART
VALUES('S001','System-Economy',1100,null);
INSERT INTO PART
VALUES('M001','Monitor-17 inch',250,'S001');
For the unique constraints error, I believe i have to insert the data in order of their primary and referencing keys right?
So what do I need to change in order for the the insert statements to work?
I dont see any error on Insert. The problem is with your create table. Plenty to correct. Try this
CREATE TABLE PART ( PNUM VARCHAR ( 25 ) NOT NULL,
PNAME VARCHAR ( 75 ) NOT NULL,
PUNITPRICE NUMBER ( 7, 2 ) NOT NULL,
COMPONENTOF VARCHAR ( 25 ),
CONSTRAINT PART_PKEY PRIMARY KEY ( PNUM ),
CONSTRAINT PART_FKEY FOREIGN KEY
( COMPONENTOF )
REFERENCES PART ( PNUM ) );
CREATE TABLE MANUFACTURER ( MNAME VARCHAR ( 50 ) NOT NULL,
MADDRESS VARCHAR ( 100 ) NOT NULL,
MPHONE VARCHAR ( 25 ) NOT NULL,
CONSTRAINT MANUFACTURER_PKEY PRIMARY KEY ( MNAME ) );
CREATE TABLE PART_MANUFACTURED ( MDATE DATE NOT NULL,
PNUM VARCHAR ( 25 ) NOT NULL,
MNAME VARCHAR ( 50 ) NOT NULL,
QUANTITY NUMBER ( 10 ) NOT NULL,
CONSTRAINT PART_MANUFACTURED_PKEY PRIMARY KEY
( MNAME, PNUM, MDATE ),
CONSTRAINT PART_MANUFACTURED_FKEY1 FOREIGN KEY
( PNUM )
REFERENCES PART ( PNUM ),
CONSTRAINT PART_MANUFACTURED_FKEY2 FOREIGN KEY
( MNAME )
REFERENCES MANUFACTURER ( MNAME ) );
CREATE TABLE CUSTOMER ( CNUM VARCHAR ( 25 ) NOT NULL,
CNAME VARCHAR ( 75 ) NOT NULL,
CTYPE VARCHAR ( 20 ) NOT NULL,
CONSTRAINT CUSTOMER_PKEY PRIMARY KEY ( CNUM ),
CONSTRAINT CHECK_CTYPE CHECK
( CTYPE IN ('INDIVIDUAL', 'INSTITUITION') ) );
CREATE TABLE ORDERS ( CNUM VARCHAR ( 25 ) NOT NULL,
PNUM VARCHAR ( 25 ) NOT NULL,
ORDERDATE DATE NOT NULL,
ORDERQUANTITY NUMBER ( 7, 2 ) NOT NULL,
CONSTRAINT ORDERS_PKEY PRIMARY KEY
( CNUM, PNUM, ORDERDATE ),
CONSTRAINT ORDERS_FKEY1 FOREIGN KEY
( CNUM )
REFERENCES CUSTOMER ( CNUM ),
CONSTRAINT ORDERS_FKEY2 FOREIGN KEY
( PNUM )
REFERENCES PART ( PNUM ) );
INSERT INTO
PART
VALUES
( 'S001',
'System-Economy',
1100,
NULL );
INSERT INTO
PART
VALUES
( 'M001',
'Monitor-17 inch',
250,
'S001' );
I want to create Interim table from main table for DBMS_Redefinition Process.
but the requirement is not to write entire create syntax because there are n number of tables and i cant take each value every time.
INTERIM TABLE = table_INTER
MAIN_TABLE = table_MAIN
for eg..
CREATE TABLE table_INTER (
ContactPartKey NUMBER(20) NOT NULL,
PartyKey NUMBER(10) NOT NULL,
ConParticipationRoleKey NUMBER(10) NOT NULL,
ChannelKey NUMBER(10) NOT NULL,
StateKey NUMBER(10) NOT NULL,
StateReasonKey NUMBER(10) NOT NULL,
NextStateKey NUMBER(10) NOT NULL,
PreviousStateKey NUMBER(10) NOT NULL,
QueueKey NUMBER(10) NOT NULL,
RtgPointKey NUMBER(10) NOT NULL,
ContactPartIndKey NUMBER(10) NOT NULL,
DeviceKey NUMBER(10) NOT NULL,
ContactID NUMBER(20) NOT NULL,
Parent1ContactID NUMBER(20) ,
StartDateKey NUMBER(10) NOT NULL,
EndDateKey NUMBER(10) NOT NULL,
StartTimeKey NUMBER(3) NOT NULL,
EndTimeKey NUMBER(3) NOT NULL,
StartDateTime DATE NOT NULL,
EndDateTime DATE NOT NULL,
StateDur NUMBER(10) NOT NULL,
ContactAllocatedCost decimal(14,4) ,
ContactPartSeqNum NUMBER(10) NOT NULL,
ContactInProcessInd NUMBER(3) NOT NULL,
FinalPartInd NUMBER(3) NOT NULL,
Counter NUMBER(10) NOT NULL,
SourceKey NUMBER(10) NOT NULL,
StreamKey NUMBER(10) NOT NULL,
ProcessKey NUMBER(10) ,
SelfServiceInd NUMBER(3) )
--PRIMARY KEY(ContactPartKey)
TABLESPACE AVAYAIQFACT1
STORAGE (INITIAL 2097152 NEXT 2097152)
PCTFREE 0
PARTITION BY RANGE (STARTDATETIME)
INTERVAL(NUMTOYMINTERVAL(1, 'MONTH'))
(PARTITION P1 VALUES LESS THAN (TO_DATE('01/09/2013', 'DD/MM/YYYY')) TABLESPACE T1,
PARTITION P2 VALUES LESS THAN (TO_DATE('01/10/2013', 'DD/MM/YYYY')) TABLESPACE T1);
In above syntax, column name value cant be given every time. So what i did was
create table table_INTER as (select * from table_MAIN);
But can anyone tell me how to introduce partition in this interim table (table_INTER).
Can you try this?
CREATE TABLE TABLE_INTER
PARTITION BY RANGE (STARTDATETIME)
INTERVAL ( NUMTOYMINTERVAL ( 1,
'MONTH' ) )
( PARTITION P1
VALUES LESS THAN
(TO_DATE ( '01/09/2013',
'DD/MM/YYYY' ))
TABLESPACE T1,
PARTITION P2
VALUES LESS THAN
(TO_DATE ( '01/10/2013',
'DD/MM/YYYY' ))
TABLESPACE T1 )
AS
( SELECT * FROM TABLE_MAIN );