I have two tables:
Table1: orders
idOrder,
Blockquote
idUser
Table2: ordersinfo
idOrder,
.......,
.......
idOrder is primary a key for two tables. I have to delete from this tables rows by idUser. I tried different ways, but nothing helped me.
My Questions: What query, I should use?
I have this exception
MySQLIntegrityConstraintViolationException
You will need to issue two delete statement
-- Delete OrderInfo table
DELETE FROM ordersinfo
WHERE EXISTS (SELECT 1
FROM orders
WHERE orders.idOrder = ordersinfo.idOrder
AND IdUser = ???)
-- Delete Orders
DELETE FROM Orders
WHERE IdUser = ???
Related
I have a 2 tables let's say (Account and Customer) that uses UUID as the primary key without proper relationship, and I am trying to fix appropriately.
The customer_id(not the UUID (PK)) on account is the same thing as the provider_customer_id on customer table,
when I tried to update the account table by setting customer_id as the UUID of the customer table so as to reference the customer table from there with the script below
SET FOREIGN_KEY_CHECKS=0;
ALTER TABLE accounts
MODIFY COLUMN customer_id BINARY(16) NOT NULL;
UPDATE accounts a
INNER JOIN customers c on a.customer_id = c.provider_customer_id
SET a.customer_id = (c.id)
WHERE c.provider_customer_id is not null ;
ALTER TABLE accounts
ADD CONSTRAINT FK_ACCOUNTS_ON_CUSTOMER FOREIGN KEY (customer_id) REFERENCES customers (id);
SET FOREIGN_KEY_CHECKS=1;
mysql updated as expected, but the UUID is totally different from what was in the customer table this was what it was updated to
43313231-3443-4134-0000-000000000000
43313231-3634-3137-0000-000000000000
43313231-3436-4847-0000-000000000000
43313231-3443-4134-0000-000000000000
I'm a complete beginner in sql and I connect a java app to a mysql xampp server . Now I have 2 tables a table of customers and a table of emails .
This is a visual representation of my tables
In my java app I have a list of customer ids I want to delete and when I run my query I get
Could not delete customer data java.sql.SQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`airline_db`.`email_address`, CONSTRAINT `email_address_customer_fk` FOREIGN KEY (`customer_customer_id`) REFERENCES `customer` (`customer_id`))
My code :
ArrayList<String> deleteCustomers = new ArrayList<>();
//... add values to deleteCustomers and then proceed to delete
try {
for(String id : deleteCustomers) {
String delCustomers = "DELETE FROM customer WHERE customer_id = ?";
state=connection.prepareStatement(delCustomers);
state.setString(1, id);
state.executeUpdate();
}
}catch(SQLException e ) {
System.out.println("Could not delete customer data " +e);
}
It seems that since the email table takes the customer_id from customer as a foreign key I cannot delete a customer entry . I do not know how to deal with this and I would appreciate your help since I am a beginner .
Because the customer_id is used as a foreign key in your EMail-Table, you cannot delete it. Therefore you first need to delete all emails that use the customer_id you want to delete, and then delete the customer_id in the Customer-Table
I have created 2 tables in MySQL [items, orderList], the foreign key id in orderlist references the primary key id in items. Now I want to take all columns{id, name, price (in Items), and quantity (in orderList)} from 2 tables in Java, how can I show id once because when I query data it shows id from both tables?
You can do with join queries, try the below query and select the fields whatever you want from two tables
SELECT items.id, items.name, items.price, orderList.quantity
FROM items INNER JOIN orderList ON items.id = orderList.id
In order to fetch the data only once, you need to mention where it should come from. You can try the following:
SELECT I.ID, I.NAME, I.PRICE, O.QUANTITY FROM ORDERLIST O, ITEMS I WHERE I.ID = O.ID
Here we have given aliases to both the tables and we have mentioned that the ID column will be picked from the ITEMS table.
I am using Java Jdbc connection.
My question is very simple, I am inserted the employee record in a employee table
Ex:
Insert into employee (emp_id,emp_name) values(10,'hello');
How could I fetch the foreign key for child table(address) and insert it?
If I use select max(emp_id) from employee and Insert the id into child table
I'm afraid of if more than one person try to insert the values in employee table and there is a possibilities that I can get a wrong employee id for the child table.
Is there any possible way to insert the employee id in child table.
Note:I require a sql query for how to insert the employee id in child table
If you insert a record into a table that contains an AUTO_INCREMENT column, you can obtain the value stored into that column by calling the mysql_insert_id() function.
More details to refer this link
https://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html
You need to do a couple of things.
Assumed, your primary key in employee is not an AUTO_INCREMENT with the name emp_id and your inserting like Insert into employee (emp_id,emp_name) values(10,'hello'); you're done: the foreign key is 10.
BUT: if it is really an AUTO_INCREMENT, you must not specify any value for it:
Insert into employee (emp_name) values('Veronica');
You may then retrieve the last generated emp_id value by the driver or by a subsequent SQL statement. In JAVA, you need to tell the driver, that you want to have the last generated ID back:
PreparedStatement pst = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
Then you are able to fetch the generated primary key:
ResultSet rset = pst.getGeneratedKeys();
Long emp_id;
if (rset.next()) {
emp_id = rset.getLong(1);
}
Now Long emp_id contains the foreign key to insert in to address table.
I have two tables connected by a foreign key with a one to many relation.
In entity A I have the following:
#org.hibernate.annotations.Cascade( {
org.hibernate.annotations.CascadeType.ALL,
org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
#OneToMany(mappedBy="monitoredFlight", fetch = FetchType.LAZY)
#OnDelete(action=OnDeleteAction.CASCADE)
private List<bTable> BTable = new ArrayList<BTable>();
Now I try to delete from table A with a bulk delete query:
Query query = em.createQuery("delete from A where originDateTime<:date");
and I get the foreign key constraint error. I decided to do the delete with a join just as I would in mysql, so I changed it to:
Query query = em.createQuery("delete from A join BTable where originDateTime<:date");
and I got a syntax error. I have tried several combination with or without join and nothing works; any ideas?
I am using mysql for the database and java for the language.
You can use a native query, the following should work in mysql:
delete a , b from a inner join b on a.id=b.a_id where ...
You can setup a foreign key with the parameter on delete cascade so that when the key it references is deleted all rows that it is a foreign key on are also deleted.