I have three tables which are customer, stock,invoice.
customer table PK is CNo(Customer No)
stock table PK is PNo(Product NO)
invoice table gets the both PKs(CNo from customer table and PNo from stock table) as Its Associate key .
What I want to do is , update stock table QuntyAvailable column through invoice table.
Ex. when a customer gets 2 Quantities from PNo 2 which is Dettol , it automatically update stock table QuntyAvailable column for above product's own row to 18( QuntyAvailable-Qty).
I tried many times with various queries but I got only errors. like PK cannot be Updated ..
bla bla bla......
Please help me on this Thank you.
customer table
CNo(PK) | Name | Address
1 | Jhon | 23, Hill St, NY.
2 | Sam | 24, Bejin , Chaina.
3 | Nic | 25, London ,England.
stock table
PNo(PK) | Description | Each Price | QntyAvailable
1 | Dettol | $2 | 10
2 | Astra | $5 | 20
invoice table
CNo(PK) | PNo(PK) | Qty | value
1 | 2 | 2 | $10
2 | 1 | 3 | $6
after the update done I want the stock table like this...
stock table
PNo(PK) | Description | Each Price | QntyAvailable
1 | Dettol | $2 | 7
2 | Astra | $5 | 18
Please help me .. I m using mysql server and netbeans IDE
My query -------------
s.executeUpdate("INSERT INTO invoice(CNo,PNo,Qty,Value) VALUES('1','2','10','150')"); s.executeUpdate("UPDATE stock set QuntyAvailable=QuntyAvailable-10 WHERE Pno ='2'");
If you want to update stocks table when you do an insert in invoice, you can either do the update after the insert with the data you have for the insert (like the one you tried, the only thing that looks wrong is the ='2' that looks like it should be only =2) or do it in a trigger, having the newly inserted values feed the update:
CREATE TRIGGER updateStock AFTER INSERT ON invoice
FOR EACH ROW BEGIN
UPDATE stock set QntyAvailable=QntyAvailable- new.Qty WHERE Pno =new.Pno;
END;
//
You can check this fiddle to see it working.
P.S.
You really should have an InvoiceID. The way you are constructing it, you are not allowing a customer to purchase the same product twice.
P.S. 2 - Your error creating the trigger is related to not setting the DELIMITER.
If you don't set it the statement will end at the first ; You need to set it before the trigger and end your trigger definition with it. After that you can set the delimiter back to ;.
DELIMITER //
CREATE TRIGGER updateStock AFTER INSERT ON invoice
FOR EACH ROW BEGIN
UPDATE stock set QntyAvailable=QntyAvailable- new.Qty WHERE Pno =new.Pno;
END;
//
DELIMITER ;
Related
I know this question has been asked before and most of the answers warn about doing so or suggest a solution for MyISAM but I'm using InnoDB.
I'm trying to generate an Invoice or Bill for the user for him to give out to me (Business plan requirement) !
The thing is that in my country the reference of the ID of the bill for the same person should be ordered or he will be audited for the missing bills. Like for example he gave me one bill with 0001 and the second one is 0005. He will be interrogated for the missing 4.
So I need to have a custom auto-increment per UserID.
User 1 - idUser= 1 ,idBill = 1
User 1 - idUser= 1 ,idBill = 2
User 2 - idUsr = 2 , idBill = 1
Some threads suggested using triggers while others warned about table locks. I personally not familiar with triggers so I steer away from them since they require maintenance.
I am using Java and MySQL.
An example:
CREATE TABLE main (id INT AUTO_INCREMENT PRIMARY KEY,
primary_id CHAR(3),
secondary_id INT) ENGINE = InnoDB;
CREATE TABLE auxiliary (primary_id CHAR(3),
secondary_id INT AUTO_INCREMENT,
PRIMARY KEY (primary_id, secondary_id)) ENGINE = MyISAM;
CREATE TRIGGER generate_secondary_id
BEFORE INSERT
ON main
FOR EACH ROW
BEGIN
INSERT INTO auxiliary (primary_id) VALUES (NEW.primary_id);
SET NEW.secondary_id = LAST_INSERT_ID();
END
INSERT INTO main (primary_id) VALUES
('A01'),
('A01'),
('B01'),
('C01'),
('A01'),
('B01'),
('A01'),
('B01');
SELECT * FROM main;
id | primary_id | secondary_id
-: | :--------- | -----------:
1 | A01 | 1
2 | A01 | 2
3 | B01 | 1
4 | C01 | 1
5 | A01 | 3
6 | B01 | 2
7 | A01 | 4
8 | B01 | 3
db<>fiddle here
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I want to insert 5 million data into a 500 million table named t_phone_numbers without duplicated phones, so I select exist phones all and compare them in memory, but I got Out Of Memory ERROR because the MySQL tables have a large data.
How can I solve it?
Restriction: Mysql table t_phone_numbers cannot change, and the column phone_number is not unique. Create a new table is acceptable.
AS an example.
CREATE TABLE existing (phone VARCHAR(255))
SELECT '123456789' phone UNION ALL
SELECT '789456123' UNION ALL
SELECT '456456456' UNION ALL
SELECT '654654645' UNION ALL
SELECT '123321123' ;
SELECT * FROM existing;
| phone |
| :-------- |
| 123456789 |
| 789456123 |
| 456456456 |
| 654654645 |
| 123321123 |
CREATE TABLE new (phone VARCHAR(255) UNIQUE)
SELECT '123456789' phone UNION ALL
SELECT '464646464' UNION ALL
SELECT '123321123' ;
SELECT * FROM new;
| phone |
| :-------- |
| 123321123 |
| 123456789 |
| 464646464 |
INSERT
INTO existing (phone)
SELECT phone
FROM new
WHERE NOT EXISTS ( SELECT NULL
FROM existing
WHERE new.phone = existing.phone );
SELECT * FROM existing;
| phone |
| :-------- |
| 123456789 |
| 789456123 |
| 456456456 |
| 654654645 |
| 123321123 |
| 464646464 |
db<>fiddle here
The most problem is SELECT part in INSERT. If neither existing nor new table's phone column is indexed then the process may be infinite..
So the recommendations - index this column in new table and (if possible) in existing table. Maybe even create a copy of existing table (phone column only), index it and use in SELECT part.
Anycase this SELECT optimization is the most problem (indexing, using LEFT JOIN, etc.), the insertion itself will be relatively fast.
So let's say I have the following table for a student:
+----+------------+-----------+---------------------+----------+------------+
| first_name | last_name | ID | Birthday | Grade | Periods |
+----+------------+-----------+---------------------+----------+------------+
| John | Doe | 123456 | 1/1/2004 | 11 | 7 |
+----+------------+-----------+---------------------+----------+------------+
How could I get the ResultSet for just that one student by just using their ID for example?
Could the query be SELECT ID(123456) FROM Students" or something along those lines? I found some answers online as to how I could loop through all the Students for example and print out that data or do something else with it, but not how to get the data for a singular element in the table based on one attribute.
In short, I just want to be able to get all the data on a student from just their ID number.
I am developing an app in which the user first login,and if he doesn't have an account he will register after finishing that step he will be directed to a page,there he will add users to his profile like.. his own family members anything like that and when they add a user its going to be saved in a table on an external server(SQL) and then get that in the user list view but how can the user only see his own users and nobody else can see his users except him and so for all other users,like their own data?!
i didn't figure out how to do this yet,any one help me with this task?
any help would be appreciated
It feels like your question is about how to design your database rather than the technical details of how to call the database from you code and display results on your pages?
You need to split your data up into multiple tables. Each table should have a unique id (the id for each row should be different). Then you link rows in tables together by referencing the ids.
One way to achieve what you want would be like this.
A user table:
User
----
Id
Username
Password (hashed)
A family members table:
Family_Members
--------------
User
Member
You put pairs of relationships in the family_members table. So you would put the id of one user in the first column user and the id of one of their family members in the second column member. Then you have a row for every relationship. When you load the table, you find all the rows where user is equal to the user id you are loading.
For example:
User
+--+--------+--------+
|Id|Username|Password|
| 1|Bob |abc |
| 2|Dad |def |
| 3|Mum |ghi |
+--+--------+--------+
And..
Family_members
+----+------+
|User|Member|
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
| 2 | 3 |
| 3 | 1 |
| 3 | 2 |
+----+------+
Then when you load family members for Bob, who's id is 1, you load all the rows from the family_members table that have User==1. Then you go back to the User table and load the details you want for each family member, and get an object like:
User:
- username: Bob
- password: abc
- family:
- User:
- username: Dad
- User:
- username: Mum
Hope this answers your question.
I have an SQLite table like:
+---+-------------+----------------+
|_id| lap_time_ms |formatted_elapse|
+---+-------------+----------------+
| 1 | 5600 | 00:05.6 |
| 2 | 4612 | 00:04.6 |
| 3 | 4123 | 00:04.1 |
| 4 | 15033 | 00:15.0 |
| 5 | 4523 | 00:04.5 |
| 6 | 6246 | 00:06.2 |
Where lap_time_ms is an of the type long and represents the amount of time in milliseconds for a lap while formatter_elapse is a String that represents the formatted (displayable) form of the first column (elapse).
My question is that if I want to add (say) 5 seconds (5000) to each lap_time_ms then I use a statement like:
DB.execSQL("update table_name set KEY_ELAPSE=KEY_ELAPSE+5000);
Which works fine however the problem is that formatted_elapse still retains its outdated value!
So, what is the best way to update the values in the formatted_elapse column if I have a function like:
public static String getFormattedTime(long milliseconds) {
// custom code that processes the long
return processedString;
}
It may be a long shot (metaphorically speaking of course ;) but is it possible to have SQLite link the two columns such that if I update a lap_time_ms row, the formatted_elapse will automatically update appropriately.
Thanks in advance!
In theory, it would be possible to create a trigger to update that column, but only if the formatting can be done with some built-in SQLite function (Android does not allow user-defined functions):
CREATE TRIGGER update_formatted_elapse
AFTER UPDATE OF lap_time_ms ON MyTable
FOR EACH ROW
BEGIN
UPDATE MyTable
SET formatted_elapse = strftime('%M:%f', NEW.lap_time_ms, 'unixepoch')
WHERE _id = NEW._id;
END;
However, it would be bad design to store the formatted string in the database; it would be duplicated information that is in danger of becoming inconsistent.
Drop the formatted_elapse column and just call getFormattedTime in your code whenever you need it.