Compare Tables In SQL server from android studio - java

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.

Related

How to set an a second Auto increment column per user?

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

How to pass datatable as part of scenario outline example

I am using cucumber 4 with java
I have a scenario :-
Scenario: user views the address
Given I login with user1
Then I see all my house addresses
|london|
Given I login with user2
Then I see all my house addresses
|london|
|spain|
|brazil|
I want to convert it to scenario outline . How can I pass the addresses datatable as part of the Examples of scenario outline.
Scenario Outline: user views the address
Given I login with <user>
Then I see all my house addresses
Examples:
|user |
|user1|
|user2|
The implementation of I see all my house addresses is (DataTable dataTable) and I want to keep it as datatable. Don't want to concatenate all addresses to a single string then I would miss the datatable.
Then("^I see all my house addresses$", (DataTable dataTable) -> {
List<String> addresses = dataTable.asList(String.class);
});
Cucumber allows you to pass data table arguments in scenario outlines. The syntax is exactly the same as a normal scenario, except the <foo> tokens used as placeholders for the values in each examples table row.
Scenario Outline: user views the address
Given I login with <user>
Then I see all my house addresses
| <address 1> | # <-- "address 1" column in examples table
| <address 2> | # <-- "address 2" column in examples table
| <address 3> | # <-- "address 3" column in examples table
Examples:
| user | address 1 | address 2 | address 3 |
| user1 | ... | ... | ... |
| user2 | ... | ... | ... |
There is a tradeoff though. You cannot specify a different number of addresses for each user. You must specify the same number of addresses for each user. If user1 shows London, Spain and Brazil, but user2 only shows London and Spain, then a data table will not work for this use case.

How to get element from MySQL table using a singular attribute in Java?

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.

Copy value from one datatable to another in Jbehave

I need to copy data from one data table to another as mentioned below
When I enter company manager details of:
|NAME |VALUE |
|title |$randomList |
|firstName |$random |
|initial |$random |
|surname |$random |
When I enter company employee details of
|NAME |VALUE |
|title |$randomList |
|firstName | |
|initial | |
|surname | |
I am new to jbehave. In the above tables when i execute this scenario random characters will be generated and assigned to firstname,initial,surname in the 1st table I somehow need the same data when I execute the second table. I know we should parametrize by using examples table but in this case I don't want to use it. so how to copy the data from one table to another.

Update stock table through the invoice table in MySQL

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 ;

Categories