Duplicate entry with a one to many relation - mysql

In Contact class I have
Long contactId;
#OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
private List<Location> locations;
In Location class, I have
Long locationId;
When I try to init database, I do
INSERT INTO contact(first_name, last_name, contact_sub_category_contactSubCategoryId) values ('Dan Alvarez', 'Bernal', 5);
INSERT INTO contact_locations(contact_contactId, locations_locationId) values (1, 5);
INSERT INTO contact(first_name, last_name, contact_sub_category_contactSubCategoryId) values ('Steph', 'Borduas', 5);
INSERT INTO contact_locations(contact_contactId, locations_locationId) values (2, 4);
INSERT INTO contact(first_name, last_name, contact_sub_category_contactSubCategoryId) values ('Luc', 'La Haye', 5);
INSERT INTO contact_locations(contact_contactId, locations_locationId) values (3, 9);
INSERT INTO contact(first_name, last_name, contact_sub_category_contactSubCategoryId) values ('George', 'Leblond', 5);
INSERT INTO contact_locations(contact_contactId, locations_locationId) values (4, 9);
Duplicate entry '9' for key 'UK_63147bdnwnn3oecl34tlxkkd4' Column
count doesn't match value count at row 1
It's like locations_locationId can't have same value.
A contact can have many location and a location can many contact.
Don't know if it need to put a many to many relation there, would like to avoid that if i can.
Edit
I tried to use
#JoinTable(
uniqueConstraints=#UniqueConstraint(columnNames={"locations_locationId","contact_contactId"})
)
but that create 3 index and don't fixe the problem
Edit 2: with many to many, that work.

Related

Get top x values in mysql table according to column value

I have the following table data:
content,count
'a', '1'
'b', '2'
'c', '2'
'd', '2'
'content_name', '18'
'de', '2'
'dea', '2'
'deaasdfdsaf', '5'
'content_name', '17'
I would like to have the correct query for getting x (for example 4) rows with the biggest values in their content column
I have tried the answers given in:
Getting Top 3 from Mysql table based on Condition/Value
but it didnt work as i was expecting
it keeps giving me just the rows by their table location
any suggestions?
create table Test(content varchar(100),count integer );
insert into Test(content, count) values("a", 1);
insert into Test(content, count) values("b", 2);
insert into Test(content, count) values("c", 2);
insert into Test(content, count) values("d", 2);
insert into Test(content, count) values("content_name", 18);
insert into Test(content, count) values("de", 2);
insert into Test(content, count) values("dea", 2);
insert into Test(content, count) values("deaasdfdsaf", 5);
insert into Test(content, count) values("content_name", 7);
select * from Test
order by count desc
limit 4;
However, I am not quite sure what you mean by "biggest values in their content column", therefore I ranked by their count values.
If you are actually looking for the number of character within the content column, which was my initial impression reading your question:
Using Martin Stagl's post to create the example table .
select *, LENGTH(REPLACE(content, 'N', '')) as char_length from Test;
NOTE: If this is not working on the server, try replacing the LENGTH() function with LEN()
Fiddle: http://sqlfiddle.com/#!9/032d48/9

My code is only returning two of the five entries I need, how can I fix this problem?

I am trying to create a list of employees showing their first name, hire date, and the city where they work. There are five employees listed but when I run my problem it only returns the first two from the location data entry.
Shown first is my employee data entered. When I run a select * statement for this table everything appears as it should. The problem comes when I try to run the second script shown, where it only shows the first two entries from the employee table.
first
INSERT INTO `salon`.`employee`
(`employee_id`,
`first_name`,
`last_name`,
`title`,
`hire_date`,
`termination_date`,
`email`,
`phone`,
`location_id`,
`manager_id`)
VALUES
(1,
'Jenny',
'Jensen',
'Owner',
'2013-06-15',
NULL,
'jensenj#salon.com',
2081112222,
1,
NULL),
(2,
'Haley',
'Lopez',
'Assistant Manager',
'2013-08-23',
NULL,
'lopezh#salon.com',
2083334444,
1,
1),
(3,
'Robert',
'Green',
'Associate',
'2014-01-03',
NULL,
'greenr#salon.com',
2085556666,
1,
2),
(4,
'Olive',
'Adams',
'Manager',
'2015-07-12',
NULL,
'adamso#salon.com',
2087778888,
2,
1),
(5,
'Julie',
'Davis',
'Associate',
'2015-10-20',
NULL,
'davisj#salon.com',
2089990000,
2,
4);
second script
SELECT employee.first_name, hire_date, location.city
FROM employee
JOIN location ON employee.employee_id = location.location_id

Insert multiple rows from select result

For a database fix I have to perform I need to do an insert for every ID this query results;
SELECT Id FROM role WHERE id != 99 AND Id NOT IN (SELECT RoleId FROM permissionaction WHERE PermissionId = 19);
So it results some ID's which have to be added with this query.
INSERT INTO permissionaction (AccessLevel, PermissionId, ControllerActionId, Active, CreatedOn, ModifiedOn, CreatedById, ModifiedById, RoleId) VALUES (1, 19, 1, 1, Now(), Now(), 1, 1, *******ID HERE******);
I know it can be done with using mysql cursors but it's not a possibility here. Is there a way to add all of the resulting ID's in one query?
Thanks
Use INSERT...SELECT:
INSERT INTO permissionaction (AccessLevel, PermissionId, ControllerActionId, Active, CreatedOn, ModifiedOn, CreatedById, ModifiedById, RoleId)
SELECT 1, 19, 1, 1, Now(), Now(), 1, 1, Id
FROM role
WHERE id != 99
AND Id NOT IN (SELECT RoleId FROM permissionaction WHERE PermissionId = 19);
With INSERT...SELECT you can add values to the table permissionaction for each returned row of the SELECT query.

Use of `if( ... = ... )` in mysql?

insert into foo_table (fname, lname, number)
values ('John', 'Doe', if(123 = 456));
For the above MySQL query, can somebody kindly explain what the if(123 = 456) is doing? I currently struggle to see an if statement without a body (i.e. if(condition){ // do something });
The query is syntactically not correct as per mysql version 8, The syntactically correct query is insert into foo_table (fname, lname, number) values ('John', 'Doe', if(123 = 456,1,2)). This will insert 1 if the condition (123 =456) is true, otherwise it will insert 2.

Insert a image in MediumBLOB column

I am trying to insert a record in mysql database using the following query, but getting the #1064-sql syntax error.
INSERT INTO RESOURCES(ID, NAME, RESTYPE, CONTENT) VALUES('4', 'Printer.TicketTotal', 0, LOAD_FILE('/home/anand/Openbravo-POS/main/src-pos/com/openbravo/pos/templates/Printer.TicketTotal.xml));
INSERT INTO RESOURCES(ID, NAME, RESTYPE, CONTENT) VALUES('4', 'Printer.TicketTotal', 0, $FILE{/home/anand/Openbravo-POS/main/src-pos/com/openbravo/pos/templates/Printer.TicketTotal.xml});
You miss ' at the end of file name -
INSERT INTO RESOURCES(ID, NAME, RESTYPE, CONTENT) VALUES
('4',
'Printer.TicketTotal',
0,
LOAD_FILE('/home/anand/Openbravo-POS/main/src-pos/com/openbravo/pos/templates/Printer.TicketTotal.xml'));