i play whit CREATE VIEW's in MySQL 5, now i have a troble, how can i make sure when i use this code.
SELECT
*
FROM
view_shop_invoicer_list
WHERE
accept_date >= '2009-10-16 00:00:00' AND
accept_date <= '2009-10-31 23:59:59' AND
shopid = [SHOPID];
my VIEW look like this, down here, and it will not take my accept_date into this view, is i copy/paste it from my SELECTE and insert int into my VIEW its will work, but not the order way :(
DROP VIEW IF EXISTS view_shop_invoicer_list;
CREATE VIEW view_shop_invoicer_list AS
SELECT
SUM( it.transamount ) AS beloeb,
SUM( it.cargo_fee ) AS cargo,
SUM(
(
it.transamount -
(
( it.transamount - it.cargo_fee ) / 100 *
( ms.pay_adm_fee + ms.pay_marks_fee + ms.pay_kick_fee ) -
( it.adm_fee + it.marketing_fee + it.kickback_fee )
)
)
) AS shop_payout,
SUM( fc_return_fee( it.transamount , it.cargo_fee , ms.pay_adm_fee , it.adm_fee ) ) AS shop_adm_fee,
SUM( fc_return_fee( it.transamount , it.cargo_fee , ms.pay_marks_fee , it.marketing_fee ) ) AS shop_marks_fee,
SUM( fc_return_fee( it.transamount , it.cargo_fee , ms.pay_kick_fee , it.kickback_fee ) ) AS shop_kick_fee,
it.shopid AS shopid,
it.accept_date AS accept_date
FROM
invoice_trans it INNER JOIN invoice i ON it.orderid = i.ordreid
INNER JOIN shops ms ON ms.id = it.shopid
WHERE
i.status != 0
i hobe sombardy know why its not working :/
Table 1: invoice_trans
CREATE TABLE `invoice_trans` (
`id` int(11) NOT NULL auto_increment,
`orderid` varchar(32) NOT NULL default '0',
`transamount` int(11) NOT NULL default '0',
`transact` varchar(32) NOT NULL default '',
`transnr` int(11) NOT NULL default '0',
`shopid` int(11) NOT NULL default '0',
`status` int(11) NOT NULL default '0',
`accept_date` timestamp NOT NULL default '0000-00-00 00:00:00',
`md5_key_declie` varchar(32) NOT NULL,
`declie_date` datetime NOT NULL default '0000-00-00 00:00:00',
`pay_points` int(1) NOT NULL default '0',
`cargo_fee` int(11) NOT NULL default '4900',
`first_time_discount` int(2) NOT NULL default '0',
`adm_fee` int(11) NOT NULL default '0',
`marketing_fee` int(11) NOT NULL default '0',
`kickback_fee` int(11) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Table 2: invoice
CREATE TABLE `invoice` (
`id` int(11) NOT NULL auto_increment,
`userid` int(11) NOT NULL default '0',
`per_fullname` varchar(64) NOT NULL,
`per_street` varchar(64) NOT NULL,
`per_zipcode` int(4) NOT NULL,
`per_city` varchar(64) NOT NULL,
`per_email` varchar(256) NOT NULL default '',
`fak_fullname` varchar(64) NOT NULL default '',
`fak_street` varchar(64) NOT NULL,
`fak_zipcode` int(4) NOT NULL,
`fak_city` varchar(64) NOT NULL,
`fak_email` varchar(256) NOT NULL default '',
`push_date` datetime NOT NULL default '0000-00-00 00:00:00',
`status` int(1) NOT NULL default '0',
`splits` int(11) NOT NULL default '0',
`dibs_ordreid` varchar(32) NOT NULL default '0',
`reftag` varchar(64) NOT NULL,
`vonchers_prices` int(11) NOT NULL default '0',
`ordre_complate` int(1) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
There's no ordreid (or orderid) column in invoice to join against. The script is still very much incomplete by the way, no function fc_return_fee or test data, so testability is very limited.
What is the error message mysql is giving you?
i found the problem.... i fix the problem this way.
DROP VIEW IF EXISTS view_shop_invoicer_list;
CREATE VIEW view_shop_invoicer_list AS
SELECT
it.transamount AS beloeb,
SUM( it.cargo_fee ) AS cargo,
(
it.transamount -
(
( it.transamount - it.cargo_fee ) / 100 *
( ms.pay_adm_fee + ms.pay_marks_fee + ms.pay_kick_fee ) -
( it.adm_fee + it.marketing_fee + it.kickback_fee )
)
) AS shop_payout,
fc_return_fee( it.transamount , it.cargo_fee , ms.pay_adm_fee , it.adm_fee ) AS shop_adm_fee,
fc_return_fee( it.transamount , it.cargo_fee , ms.pay_marks_fee , it.marketing_fee ) AS shop_marks_fee,
fc_return_fee( it.transamount , it.cargo_fee , ms.pay_kick_fee , it.kickback_fee ) AS shop_kick_fee,
it.shopid AS shopid,
it.accept_date AS accept_date
FROM
invoice_trans it INNER JOIN invoice i ON it.orderid = i.ordreid
WHERE
i.status != 0
GROUP BY
it.id;
and i use this select ;) tanks for help dudes.
SELECT
SUM( beloeb ) AS beloeb,
SUM( cargo ) AS cargo,
SUM( shop_payout ) AS shop_payout,
SUM( shop_adm_fee ) AS shop_adm_fee,
SUM( shop_marks_fee ) AS shop_marks_fee,
SUM( shop_kick_fee ) AS shop_kick_fee,
accept_date,
shopid
FROM
view_shop_invoicer_list
WHERE
accept_date >= '". $this->from_date ."' AND
accept_date <= '". $this->to_date ."'
GROUP BY
shopid
Related
Why is this statement failing despite the "IF NOT EXISTS" clause?
org.h2.jdbc.JdbcSQLSyntaxErrorException: Attempt to define a second primary key; SQL statement:
ALTER TABLE QRTZ_CALENDARS
ADD CONSTRAINT IF NOT EXISTS PK_QRTZ_CALENDARS PRIMARY KEY ( SCHED_NAME, CALENDAR_NAME ) [90017-212]
H2 is run in memory mode jdbc:h2:mem:quartz;INIT=RUNSCRIPT FROM 'classpath:tables_h2.sql'
CREATE TABLE IF NOT EXISTS QRTZ_CALENDARS (
SCHED_NAME VARCHAR(120) NOT NULL,
CALENDAR_NAME VARCHAR (200) NOT NULL ,
CALENDAR IMAGE NOT NULL
);
If I move the PK definition into table definition the error goes away.
The script is
CREATE TABLE IF NOT EXISTS QRTZ_CALENDARS (
SCHED_NAME VARCHAR(120) NOT NULL,
CALENDAR_NAME VARCHAR (200) NOT NULL ,
CALENDAR IMAGE NOT NULL
);
CREATE TABLE IF NOT EXISTS QRTZ_CRON_TRIGGERS (
SCHED_NAME VARCHAR(120) NOT NULL,
TRIGGER_NAME VARCHAR (200) NOT NULL ,
TRIGGER_GROUP VARCHAR (200) NOT NULL ,
CRON_EXPRESSION VARCHAR (120) NOT NULL ,
TIME_ZONE_ID VARCHAR (80)
);
CREATE TABLE IF NOT EXISTS QRTZ_FIRED_TRIGGERS (
SCHED_NAME VARCHAR(120) NOT NULL,
ENTRY_ID VARCHAR (95) NOT NULL ,
TRIGGER_NAME VARCHAR (200) NOT NULL ,
TRIGGER_GROUP VARCHAR (200) NOT NULL ,
INSTANCE_NAME VARCHAR (200) NOT NULL ,
FIRED_TIME BIGINT NOT NULL ,
SCHED_TIME BIGINT NOT NULL ,
PRIORITY INTEGER NOT NULL ,
STATE VARCHAR (16) NOT NULL,
JOB_NAME VARCHAR (200) NULL ,
JOB_GROUP VARCHAR (200) NULL ,
IS_NONCONCURRENT BOOLEAN NULL ,
REQUESTS_RECOVERY BOOLEAN NULL
);
CREATE TABLE IF NOT EXISTS QRTZ_PAUSED_TRIGGER_GRPS (
SCHED_NAME VARCHAR(120) NOT NULL,
TRIGGER_GROUP VARCHAR (200) NOT NULL
);
CREATE TABLE IF NOT EXISTS QRTZ_SCHEDULER_STATE (
SCHED_NAME VARCHAR(120) NOT NULL,
INSTANCE_NAME VARCHAR (200) NOT NULL ,
LAST_CHECKIN_TIME BIGINT NOT NULL ,
CHECKIN_INTERVAL BIGINT NOT NULL
);
CREATE TABLE IF NOT EXISTS QRTZ_LOCKS (
SCHED_NAME VARCHAR(120) NOT NULL,
LOCK_NAME VARCHAR (40) NOT NULL
);
CREATE TABLE IF NOT EXISTS QRTZ_JOB_DETAILS (
SCHED_NAME VARCHAR(120) NOT NULL,
JOB_NAME VARCHAR (200) NOT NULL ,
JOB_GROUP VARCHAR (200) NOT NULL ,
DESCRIPTION VARCHAR (250) NULL ,
JOB_CLASS_NAME VARCHAR (250) NOT NULL ,
IS_DURABLE BOOLEAN NOT NULL ,
IS_NONCONCURRENT BOOLEAN NOT NULL ,
IS_UPDATE_DATA BOOLEAN NOT NULL ,
REQUESTS_RECOVERY BOOLEAN NOT NULL ,
JOB_DATA IMAGE NULL
);
CREATE TABLE IF NOT EXISTS QRTZ_SIMPLE_TRIGGERS (
SCHED_NAME VARCHAR(120) NOT NULL,
TRIGGER_NAME VARCHAR (200) NOT NULL ,
TRIGGER_GROUP VARCHAR (200) NOT NULL ,
REPEAT_COUNT BIGINT NOT NULL ,
REPEAT_INTERVAL BIGINT NOT NULL ,
TIMES_TRIGGERED BIGINT NOT NULL
);
CREATE TABLE IF NOT EXISTS QRTZ_SIMPROP_TRIGGERS (
SCHED_NAME VARCHAR(120) NOT NULL,
TRIGGER_NAME VARCHAR(200) NOT NULL,
TRIGGER_GROUP VARCHAR(200) NOT NULL,
STR_PROP_1 VARCHAR(512) NULL,
STR_PROP_2 VARCHAR(512) NULL,
STR_PROP_3 VARCHAR(512) NULL,
INT_PROP_1 INTEGER NULL,
INT_PROP_2 INTEGER NULL,
LONG_PROP_1 BIGINT NULL,
LONG_PROP_2 BIGINT NULL,
DEC_PROP_1 NUMERIC(13,4) NULL,
DEC_PROP_2 NUMERIC(13,4) NULL,
BOOL_PROP_1 BOOLEAN NULL,
BOOL_PROP_2 BOOLEAN NULL
);
CREATE TABLE IF NOT EXISTS QRTZ_BLOB_TRIGGERS (
SCHED_NAME VARCHAR(120) NOT NULL,
TRIGGER_NAME VARCHAR (200) NOT NULL ,
TRIGGER_GROUP VARCHAR (200) NOT NULL ,
BLOB_DATA IMAGE NULL
);
CREATE TABLE IF NOT EXISTS QRTZ_TRIGGERS (
SCHED_NAME VARCHAR(120) NOT NULL,
TRIGGER_NAME VARCHAR (200) NOT NULL ,
TRIGGER_GROUP VARCHAR (200) NOT NULL ,
JOB_NAME VARCHAR (200) NOT NULL ,
JOB_GROUP VARCHAR (200) NOT NULL ,
DESCRIPTION VARCHAR (250) NULL ,
NEXT_FIRE_TIME BIGINT NULL ,
PREV_FIRE_TIME BIGINT NULL ,
PRIORITY INTEGER NULL ,
TRIGGER_STATE VARCHAR (16) NOT NULL ,
TRIGGER_TYPE VARCHAR (8) NOT NULL ,
START_TIME BIGINT NOT NULL ,
END_TIME BIGINT NULL ,
CALENDAR_NAME VARCHAR (200) NULL ,
MISFIRE_INSTR SMALLINT NULL ,
JOB_DATA IMAGE NULL
);
ALTER TABLE QRTZ_CALENDARS ADD
CONSTRAINT IF NOT EXISTS PK_QRTZ_CALENDARS PRIMARY KEY
(
SCHED_NAME,
CALENDAR_NAME
);
ALTER TABLE QRTZ_CRON_TRIGGERS ADD
CONSTRAINT IF NOT EXISTS PK_QRTZ_CRON_TRIGGERS PRIMARY KEY
(
SCHED_NAME,
TRIGGER_NAME,
TRIGGER_GROUP
);
ALTER TABLE QRTZ_FIRED_TRIGGERS ADD
CONSTRAINT IF NOT EXISTS PK_QRTZ_FIRED_TRIGGERS PRIMARY KEY
(
SCHED_NAME,
ENTRY_ID
);
ALTER TABLE QRTZ_PAUSED_TRIGGER_GRPS ADD
CONSTRAINT IF NOT EXISTS PK_QRTZ_PAUSED_TRIGGER_GRPS PRIMARY KEY
(
SCHED_NAME,
TRIGGER_GROUP
);
ALTER TABLE QRTZ_SCHEDULER_STATE ADD
CONSTRAINT IF NOT EXISTS PK_QRTZ_SCHEDULER_STATE PRIMARY KEY
(
SCHED_NAME,
INSTANCE_NAME
);
ALTER TABLE QRTZ_LOCKS ADD
CONSTRAINT IF NOT EXISTS PK_QRTZ_LOCKS PRIMARY KEY
(
SCHED_NAME,
LOCK_NAME
);
ALTER TABLE QRTZ_JOB_DETAILS ADD
CONSTRAINT IF NOT EXISTS PK_QRTZ_JOB_DETAILS PRIMARY KEY
(
SCHED_NAME,
JOB_NAME,
JOB_GROUP
);
ALTER TABLE QRTZ_SIMPLE_TRIGGERS ADD
CONSTRAINT IF NOT EXISTS PK_QRTZ_SIMPLE_TRIGGERS PRIMARY KEY
(
SCHED_NAME,
TRIGGER_NAME,
TRIGGER_GROUP
);
ALTER TABLE QRTZ_SIMPROP_TRIGGERS ADD
CONSTRAINT IF NOT EXISTS PK_QRTZ_SIMPROP_TRIGGERS PRIMARY KEY
(
SCHED_NAME,
TRIGGER_NAME,
TRIGGER_GROUP
);
ALTER TABLE QRTZ_TRIGGERS ADD
CONSTRAINT IF NOT EXISTS PK_QRTZ_TRIGGERS PRIMARY KEY
(
SCHED_NAME,
TRIGGER_NAME,
TRIGGER_GROUP
);
ALTER TABLE QRTZ_CRON_TRIGGERS ADD
CONSTRAINT IF NOT EXISTS FK_QRTZ_CRON_TRIGGERS_QRTZ_TRIGGERS FOREIGN KEY
(
SCHED_NAME,
TRIGGER_NAME,
TRIGGER_GROUP
) REFERENCES QRTZ_TRIGGERS (
SCHED_NAME,
TRIGGER_NAME,
TRIGGER_GROUP
) ON DELETE CASCADE;
ALTER TABLE QRTZ_SIMPLE_TRIGGERS ADD
CONSTRAINT IF NOT EXISTS FK_QRTZ_SIMPLE_TRIGGERS_QRTZ_TRIGGERS FOREIGN KEY
(
SCHED_NAME,
TRIGGER_NAME,
TRIGGER_GROUP
) REFERENCES QRTZ_TRIGGERS (
SCHED_NAME,
TRIGGER_NAME,
TRIGGER_GROUP
) ON DELETE CASCADE;
ALTER TABLE QRTZ_SIMPROP_TRIGGERS ADD
CONSTRAINT IF NOT EXISTS FK_QRTZ_SIMPROP_TRIGGERS_QRTZ_TRIGGERS FOREIGN KEY
(
SCHED_NAME,
TRIGGER_NAME,
TRIGGER_GROUP
) REFERENCES QRTZ_TRIGGERS (
SCHED_NAME,
TRIGGER_NAME,
TRIGGER_GROUP
) ON DELETE CASCADE;
ALTER TABLE QRTZ_TRIGGERS ADD
CONSTRAINT IF NOT EXISTS FK_QRTZ_TRIGGERS_QRTZ_JOB_DETAILS FOREIGN KEY
(
SCHED_NAME,
JOB_NAME,
JOB_GROUP
) REFERENCES QRTZ_JOB_DETAILS (
SCHED_NAME,
JOB_NAME,
JOB_GROUP
);
COMMIT;
The error only happens when concurrent connections are established.
If I set the pool size to 1 it does not occur.
If I set the pool size to 2 I see it logged once.
If I set the pool size to 3 I see it logged twice.
The error seems to be gone when the connection pool retries.
It all looks like a concurrency bug in H2
IF NOT EXISTS checks only presence of constraint with exactly the same name. This exception indicates that your table already has a primary key constraint with the same or other set of columns, but with different name.
If you're sure that this table either has a primary key constraint on these two columns with unknown name or doesn't have a primary key constraint at all, you can replace your command with the following one:
EXECUTE IMMEDIATE
CASE WHEN NOT EXISTS (
SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE TABLE_NAME = 'QRTZ_CALENDARS' AND CONSTRAINT_TYPE = 'PRIMARY KEY')
THEN 'ALTER TABLE QRTZ_CALENDARS ADD CONSTRAINT PK_QRTZ_CALENDARS PRIMARY KEY (SCHED_NAME, CALENDAR_NAME)'
ELSE ''
END;
I have two tables. products and product_images.
Products: ~ 6 276 445 rows.
ProductImages: ~ 22 888 685 rows.
Table definitions:
products
CREATE TABLE `products` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`unique_id` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL,
`feed_id` bigint(20) unsigned NOT NULL,
`feed_item_id` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
`item_group_id` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
`name` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL,
`slug` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL,
`description` text COLLATE utf8mb4_unicode_ci NOT NULL,
`categorytext` text COLLATE utf8mb4_unicode_ci NOT NULL,
`categorytext_hash` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL,
`manufacturer` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
`product_url` text COLLATE utf8mb4_unicode_ci NOT NULL,
`price_vat` float(12,2) DEFAULT NULL,
`price_vat_old` float(12,2) DEFAULT NULL,
`vat` tinyint(4) DEFAULT NULL,
`discount_percentage` tinyint(4) NOT NULL DEFAULT 0,
`image_source_url` text COLLATE utf8mb4_unicode_ci NOT NULL,
`image_filename` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL,
`gtin` varchar(14) COLLATE utf8mb4_unicode_ci NOT NULL,
`ean` varchar(13) COLLATE utf8mb4_unicode_ci NOT NULL,
`isbn` varchar(13) COLLATE utf8mb4_unicode_ci NOT NULL,
`upc` varchar(12) COLLATE utf8mb4_unicode_ci NOT NULL,
`mpn` varchar(70) COLLATE utf8mb4_unicode_ci NOT NULL,
`missing_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`deleted_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`project_1` tinyint(4) NOT NULL DEFAULT 0,
`project_2` tinyint(4) NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_id_UNIQUE` (`unique_id`),
KEY `feed_id_updated_at` (`feed_id`,`updated_at`),
KEY `feed_id` (`feed_id`),
KEY `feed_id_missing_at` (`feed_id`,`missing_at`),
KEY `feed_id_categorytext_hash_id` (`feed_id`,`categorytext_hash`,`id`),
KEY `categorytext_hash` (`categorytext_hash`),
KEY `missing_at_deleted_at_categorytext_hash_feed_id_id` (`missing_at`,`deleted_at`,`categorytext_hash`,`feed_id`,`id`),
KEY `project_1_id` (`project_1`,`id`),
KEY `project_2_id` (`project_2`,`id`),
KEY `project_1` (`project_1`),
KEY `project_2` (`project_2`),
CONSTRAINT `fk_products_feeds` FOREIGN KEY (`feed_id`) REFERENCES `feeds` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=122268834 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
product_images
CREATE TABLE `product_images` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`product_unique_id` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL,
`unique_id` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL,
`feed_id` bigint(20) unsigned NOT NULL,
`image_source_url` text COLLATE utf8mb4_unicode_ci NOT NULL,
`image_filename` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`deleted_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
UNIQUE KEY `unique_id_UNIQUE` (`unique_id`),
KEY `feed_id_updated_at` (`feed_id`,`updated_at`),
KEY `product_unique_id` (`product_unique_id`),
KEY `product_unique_id_id` (`product_unique_id`,`id`),
CONSTRAINT `fk_product_images_products1` FOREIGN KEY (`product_unique_id`) REFERENCES `products` (`unique_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=333584333 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
My Query without ORDER BY
SELECT IMG.id
FROM products PR
INNER JOIN product_images IMG on IMG.product_unique_id = PR.unique_id
WHERE
PR.project_1 = 1
-- ORDER BY IMG.id
LIMIT 1000 OFFSET 0
Duration: 0.016 sec
EXPLAIN:
id|select_type|table|type|possible_keys |key |key_len|ref |rows |Extra |
--+-----------+-----+----+---------------------------------------+-----------------+-------+-----------------------------+------+-----------+
1|SIMPLE |PR |ref |unique_id_UNIQUE,project_1_id,project_1|project_1 |1 |const |286960| |
1|SIMPLE |IMG |ref |product_unique_id,product_unique_id_id |product_unique_id|130 |products-storage.PR.unique_id|2 |Using index|
My Query with ORDER BY
SELECT IMG.id
FROM products PR
INNER JOIN product_images IMG on IMG.product_unique_id = PR.unique_id
WHERE
PR.project_1 = 1
ORDER BY IMG.id
LIMIT 1000 OFFSET 0
Duration 17.922 sec
EXPLAIN:
id|select_type|table|type|possible_keys |key |key_len|ref |rows |Extra |
--+-----------+-----+----+---------------------------------------+-----------------+-------+-----------------------------+------+-------------------------------+
1|SIMPLE |PR |ref |unique_id_UNIQUE,project_1_id,project_1|project_1 |1 |const |286960|Using temporary; Using filesort|
1|SIMPLE |IMG |ref |product_unique_id,product_unique_id_id |product_unique_id|130 |products-storage.PR.unique_id|2 |Using index |
Problem
Query with order by is too slow. Is there a way to speed up query with some index or server settings? I need order by for pagination.
The goal is to select all images for project and send them through the api to another server.
I'm using mariadb 10.8.2
Server is on SSD with 8GB RAM
I have been trying to get this thing working for a few day now (foreign key) and it just don't work, and feel like every solution I used don't work, so i'm asking here to learn what was the problem and how to fix it
Table creation :
CREATE TABLE CUSTOMER
(
Customer_ID varchar(255) NOT NULL,
Customer_Name varchar(50),
Customer_Gender varchar(10),
Customer_DOB varchar(20) ,
CONSTRAINT CUSTOMER_PK PRIMARY KEY(Customer_ID)
) ;
CREATE TABLE PAYMENT
(
Payment_ID varchar(255) NOT NULL,
Cust_ID varchar(255),
Payment_Method varchar(30),
Payment_Date varchar(20),
Payment_Total NUMBER(10,2) ,
CONSTRAINT PAYMENT_PK PRIMARY KEY(Payment_ID),
CONSTRAINT fk_customer FOREIGN KEY(Cust_ID) REFERENCES CUSTOMER(Customer_ID)
) ;
Inserting values :
INSERT INTO CUSTOMER VALUES ('1277','Jenny','Female', ( TO_Date ( '03/04/1988' , 'DD/MM/yyyy')));
INSERT INTO CUSTOMER VALUES ('3423','Bryan','Male', ( TO_Date ( '15/06/1990' , 'DD/MM/YYYY')));
INSERT INTO CUSTOMER VALUES ('4385','Mohd Shafik','Male',( TO_Date ( '20/08/1993' , 'DD/MM/YYYY')));
INSERT INTO PAYMENT VALUES ('24P','Cash', ( TO_Date ( '11/02/2022' , 'DD/MM/YYYY')),24.50);
INSERT INTO PAYMENT VALUES ('09p','Online Transfer', ( TO_Date ( '08/04/2022' , 'DD/MM/YYYY')),25.00);
INSERT INTO PAYMENT VALUES ('10P','Cash', ( TO_Date ( '08/07/2022' , 'DD/MM/YYYY')),22.50);
The foreign keys are now working , but just for life of me can't figure out why the it spits out ORA-00947: not enough values and
ORA-01400: cannot insert NULL into ("SQL_GUUNNGDQAOXJVYPBKNMILVXJR"."PAYMENT"."PAYMENT_ID") ORA-06512: at "SYS.DBMS_SQL", line 1721
Also, Please explain to me how references works, i read a few places but they use words that just confuse me. Please and thank you!
Use DATE data types to store date values (and VARCHAR2 instead of VARCHAR) in your tables:
CREATE TABLE CUSTOMER(
Customer_ID varchar2(255) NOT NULL,
Customer_Name varchar2(50),
Customer_Gender varchar2(10),
Customer_DOB DATE,
CONSTRAINT CUSTOMER_PK PRIMARY KEY(Customer_ID)
);
CREATE TABLE PAYMENT(
Payment_ID varchar2(255) NOT NULL,
Cust_ID varchar2(255),
Payment_Method varchar2(30),
Payment_Date DATE,
Payment_Total NUMBER(10,2) ,
CONSTRAINT PAYMENT_PK PRIMARY KEY(Payment_ID),
CONSTRAINT fk_customer FOREIGN KEY(Cust_ID) REFERENCES CUSTOMER(Customer_ID)
);
Then name the columns in your INSERT statements:
INSERT INTO CUSTOMER (
customer_id, customer_name, customer_gender, customer_dob
) VALUES (
'1277','Jenny','Female', TO_Date('03/04/1988', 'DD/MM/yyyy')
);
INSERT INTO CUSTOMER (
customer_id, customer_name, customer_gender, customer_dob
) VALUES (
'3423','Bryan','Male', TO_Date('15/06/1990', 'DD/MM/YYYY')
);
INSERT INTO CUSTOMER (
customer_id, customer_name, customer_gender, customer_dob
) VALUES (
'4385','Mohd Shafik','Male', TO_Date('20/08/1993' , 'DD/MM/YYYY')
);
Then for the PAYMENT inserts, you have 5 columns in the table but only 4 pieces of data being inserted:
INSERT INTO PAYMENT (
payment_id, payment_method, payment_date, payment_total
) VALUES (
'24P','Cash', TO_Date('11/02/2022', 'DD/MM/YYYY'),24.50
);
INSERT INTO PAYMENT (
payment_id, payment_method, payment_date, payment_total
) VALUES (
'09p','Online Transfer', TO_Date( '08/04/2022' , 'DD/MM/YYYY'),25.00
);
INSERT INTO PAYMENT (
payment_id, payment_method, payment_date, payment_total
) VALUES (
'10P','Cash', TO_Date('08/07/2022', 'DD/MM/YYYY'),22.50
);
You have not provided a Cust_ID value so it will default to NULL in those rows.
If you want to provide a Cust_ID then add it to the statement:
INSERT INTO PAYMENT (
payment_id, cust_id, payment_method, payment_date, payment_total
) VALUES (
'ABC', '1277', 'Cash', TO_Date('08/07/2022', 'DD/MM/YYYY'),22.50
);
db<>fiddle here
ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY';
CREATE TABLE CUSTOMER
(
Customer_ID varchar(255) NOT NULL,
Customer_Name varchar(50),
Customer_Gender varchar(10),
Customer_DOB DATE ,
CONSTRAINT CUSTOMER_PK PRIMARY KEY(Customer_ID)
) ;
CREATE TABLE PAYMENT
(
Payment_ID varchar(255) NOT NULL,
Customer_ID varchar(255),
Payment_Method varchar(30),
Payment_Date DATE,
Payment_Total NUMBER(10,2) ,
CONSTRAINT PAYMENT_PK PRIMARY KEY(Payment_ID),
CONSTRAINT fk_customer FOREIGN KEY(Customer_ID) REFERENCES CUSTOMER(Customer_ID)
) ;
INSERT INTO CUSTOMER VALUES ('1277','Jenny','Female', ( TO_Date ( '03/04/1988' , 'DD/MM/yyyy')));
INSERT INTO PAYMENT VALUES ('24P','1277','Cash', ( TO_Date ( '11/02/2022' , 'DD/MM/YYYY')),24.50);
SELECT * FROM Customer;
CUSTOMER_ID CUSTOMER_NAME CUSTOMER_GENDER CUSTOMER_DOB
1277 Jenny Female 03-APR-1988
SELECT * FROM payment;
PAYMENT_ID CUSTOMER_ID PAYMENT_METHOD PAYMENT_DATE PAYMENT_TOTAL
24P 1277 Cash 11-FEB-2022 24.5
CREATE TABLE event (
event_id NUMBER(6) NOT NULL check(event_id > 0),
event_title VARCHAR2(100 CHAR) NOT NULL,
event_team VARCHAR2(1 CHAR) default 'N' NOT NULL check(event_team
IN('Y', 'N')),
no_per_team NUMBER(2) default 1 NOT NULL check((event_team = 'N' AND
no_per_team = 1) OR (event_team = 'Y' AND no_per_team > 1)),
event_gender VARCHAR2(1) NOT NULL check(event_gender IN ('M', 'F')),
og_id NUMBER(3) NOT NULL,
sport_id NUMBER(3) NOT NULL
);
Error report:
ORA-02438: Column check constraint cannot reference other columns
*Cause: attempted to define a column check constraint that references
another column.
*Action: define it as a table check constraint.
You cannot reference another column when declaring CHECK constrain on a column. Define it at the table level instead.
CREATE TABLE event (
event_id NUMBER(6) NOT NULL check(event_id > 0),
event_title VARCHAR2(100 CHAR) NOT NULL,
event_team VARCHAR2(1 CHAR) default 'N' NOT NULL check(event_team
IN('Y', 'N')),
no_per_team NUMBER(2) default 1 NOT NULL ,
event_gender VARCHAR2(1) NOT NULL check(event_gender IN ('M', 'F')),
og_id NUMBER(3) NOT NULL,
sport_id NUMBER(3) NOT NULL,
CONSTRAINT CHECK_NO_P check((event_team = 'N' AND
no_per_team = 1) OR (event_team = 'Y' AND no_per_team > 1))
);
Move column check constraint to a table check constraint (as Oracle told you):
CREATE TABLE event
(
event_id NUMBER (6) NOT NULL CHECK (event_id > 0),
event_title VARCHAR2 (100 CHAR) NOT NULL,
event_team VARCHAR2 (1 CHAR)
DEFAULT 'N'
NOT NULL
CHECK (event_team IN ('Y', 'N')),
no_per_team NUMBER (2) DEFAULT 1 NOT NULL,
event_gender VARCHAR2 (1) NOT NULL CHECK (event_gender IN ('M', 'F')),
og_id NUMBER (3) NOT NULL,
sport_id NUMBER (3) NOT NULL,
--
CONSTRAINT ch_npt CHECK
( ( event_team = 'N'
AND no_per_team = 1)
OR ( event_team = 'Y'
AND no_per_team > 1))
);
I am trying to find the minimum priced holiday for each month from each departure airport to a target destination airport from a table with over 1 million records. I have successfully created the required MySQL query :-
SELECT * FROM results r INNER JOIN (SELECT id,MIN(price) FROM results GROUP BY BulkLoaderRef) r2 ON r.id=r2.id ORDER BY r.departureDate, r.depAirportCode, r.resortID LIMIT 10
But I cannot find a valid JPQL syntax for this query?
Help please?
My table format is as follows :-
CREATE TABLE `results` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`HotelReference` varchar(128) NOT NULL DEFAULT '',
`HotelName` varchar(64) NOT NULL DEFAULT '',
`Resort` varchar(128) NOT NULL DEFAULT '',
`Description` longtext NOT NULL,
`Photo` varchar(255) NOT NULL DEFAULT '',
`OfferProcessType` varchar(1) NOT NULL DEFAULT 'A',
`TradingName` varchar(32) NOT NULL DEFAULT 'Heffernans Travel',
`OfferLabelDesc` varchar(16) NOT NULL DEFAULT 'EasyHolidays',
`DestinationName` varchar(128) NOT NULL DEFAULT '',
`DepAirportCode` varchar(3) NOT NULL DEFAULT '',
`TelephoneNumber` varchar(16) NOT NULL DEFAULT '021 230 0700',
`ReturnAirportCode` varchar(3) NOT NULL DEFAULT '',
`ArrivalAirportCode` varchar(3) NOT NULL DEFAULT '',
`ResortID` int(11) NOT NULL,
`ABTA` varchar(16) NOT NULL DEFAULT 'ABTA',
`ATOL` varchar(16) NOT NULL DEFAULT 'ATOL',
`OtherBonding` varchar(64) NOT NULL DEFAULT 'Agent for ATOL protected operator',
`BoardTypesShort` varchar(2) NOT NULL DEFAULT '',
`AccomodationShort` varchar(3) NOT NULL DEFAULT 'Htl',
`AllocationShort` varchar(3) DEFAULT '""',
`OperatorShort` varchar(16) NOT NULL DEFAULT 'EasyHolidays',
`DepartureDate` date NOT NULL,
`DepartureMonth` varchar(2) DEFAULT NULL,
`DepartureYear` varchar(4) DEFAULT NULL,
`Price` int(11) NOT NULL,
`ShareBasisAdult` int(11) DEFAULT '2',
`BulkLoaderRef` varchar(64) DEFAULT NULL,
`Email` varchar(64) DEFAULT '',
`PublicNotes` varchar(64) DEFAULT 'EasyHolidays',
`CCC` varchar(32) DEFAULT ' Credit Card Charges',
`TOD` varchar(16) DEFAULT '',
`DCC` varchar(16) DEFAULT '',
`AMEX` varchar(16) DEFAULT '',
`CDW` varchar(16) DEFAULT '',
`BookingFee` double DEFAULT NULL,
`AccomodationName` varchar(64) NOT NULL DEFAULT '',
`duration` int(11) NOT NULL DEFAULT '0',
`ShareBasisChild` int(11) DEFAULT '2',
`Rating` varchar(16) NOT NULL DEFAULT '',
`OfferLink` varchar(16) NOT NULL DEFAULT '',
`AccomodationRef` varchar(128) NOT NULL DEFAULT '',
`Transfers` varchar(32) NOT NULL DEFAULT '',
`OperatorsRating` varchar(32) NOT NULL DEFAULT '',
`QuoteRef` varchar(64) NOT NULL DEFAULT '',
`Url` varchar(128) NOT NULL DEFAULT '',
`OutDepTime` varchar(16) NOT NULL DEFAULT '',
`OutArrTime` varchar(16) NOT NULL DEFAULT '',
`InDepTime` varchar(16) NOT NULL DEFAULT '',
`InArrTime` varchar(16) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY `HotelReference` (`HotelReference`),
KEY `BulkLoaderRef` (`BulkLoaderRef`),
KEY `Price` (`Price`)
) ENGINE=InnoDB AUTO_INCREMENT=1091073 DEFAULT CHARSET=utf8;
JPQL doesn't support sub-queries for JOIN statements. With that said, I would suggest that you move the subquery of your JOIN into a view. Then it becomes a simple JPQL statement.
Move subquery into a view:
CREATE VIEW min_price_view AS
SELECT id,MIN(price) FROM results GROUP BY BulkLoaderRef;
Then replace in query:
SELECT
*
FROM
results r
INNER JOIN
min_price_view r2 ON r.id=r2.id
ORDER BY
r.departureDate, r.depAirportCode, r.resortID
LIMIT 10
At this point, it becomes very simple to convert it to JPQL.