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
Related
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
Table Name:
CUSTOMER_INFORMATION
Customer_ID (PK)
Customer_Name
Mobile_Number
EVENT_INFORMATION
Event_ID (PK)
Event_Name
RELATION_TABLE
Event_ID (FK)
Customer_ID (FK)
First You will have to add records in CUSTOMER_INFORMATION, EVENT_INFORMATION tables
Insert into CUSTOMER_INFORMATION table
INSERT INTO CUSTOMER_INFORMATION VALUES (123,'new user','+923417874563');
Insert into EVENT_INFORMATION table
INSERT INTO EVENT_INFORMATION VALUES (456,'new event');
Now we can add new record in the RELATION_TABLE by using above primary keys
INSERT INTO RELATION_TABLE VALUES (456,123);
But there must be some information in the RELATION_ABLE that escribes the relation. and primary key of the relation 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.
In my database I have table "Announcement" with fields:
id int,
text varchar,
sent_date date.
Also table "Group" with fields:
id int,
name varchar.
Each announcement submitted to any number of groups(from 1 to all). How to store this relation in database?
I'm using MySQL and Hibernate in java web project.
Create a third table (I call it announcement_group) with two columns:
announcement_id REFERENCES Announcement(id)
group_id REFERENCES Group (id)
Read more about Foreign Keys from the documentation here http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html
In your query you will want to use a
SELECT group_id WHERE announcement_id=<your id here> from announcement_group;
I have 2 tables Customers & Accounts in my Oracle Database. I have sequence numbers for generating surrogate key values for both tables.
CREATE SEQUENCE customers_seq NOCACHE;
CREATE SEQUENCE accounts_seq NOCACHE;
CREATE TABLE customers
(
customer_surrogate_id NUMBER(10),
customer_id VARCHAR2(8) UNIQUE NOT NULL,
customer_password VARCHAR2(20),
customer_name VARCHAR2(20),
customer_pan VARCHAR2(10) UNIQUE,
customer_email_id VARCHAR2(20) UNIQUE,
CONSTRAINT customer_pk
PRIMARY KEY (customer_surrogate_id)
);
CREATE TABLE accounts
(
accounts_surrogate_id NUMBER(10),
account_id VARCHAR2(10) UNIQUE NOT NULL,
customer_surrogate_id NUMBER(10),
account_type VARCHAR2(10),
account_currency VARCHAR2(20),
account_balance NUMBER(20, 2),
CONSTRAINT accounts_pk
PRIMARY KEY (accounts_surrogate_id),
CONSTRAINT accounts_fk
FOREIGN KEY (customer_surrogate_id)
REFERENCES customers(customer_surrogate_id)
);
I know how to use sequence_name.NEXTVAL & sequence_name.CURRVAL in insert statements to perform the reference
The problem is with using NEXTVAL & CURRVAL is that it assumes that inserts to both tables occur sequentially like
insert into Customers(// use NEXTVAL here)
insert into Accounts(// use CURRVAL here to reference the above row in Customers)
But in my java application, the multiple inserts for the Customers table can occur before even one insert occurs in Accounts table. CURRVAL will return the value of the last inserted row of the Customers table.
When inserting a row into Accounts table, I can get customers_id values in my application. Should the customer_id be used to query the Customers table to get the customer_surrogate_id as shown below?
insert into Customers(// use NEXTVAL here)
...
insert into Accounts(// use the customer_id to query and find customer_surrogate_id)
Is there better way to reference the Customers table in this situation?
Edit: I am using JDBC to access the database.
Simply select the value, and store it in a variable:
long customer1Id = selectNextValueFromSequence("customers_seq");
long customer2Id = selectNextValueFromSequence("customers_seq");
insertCustomerWithId(customer1Id);
insertCustomerWithId(customer2Id);
insertAccountWithCustomerId(customer1Id);
insertAccountWithCustomerId(customer2Id);