mysql: For billing purpose, printing user ID on bill . - mysql

I have this idea which I need some help with. Currently I am developing a mysql database with a java interface. It is a hotel management system, and for billing purpose. I have the following tables.
Bill (BillID, ResvID, IssueDate, ToTalBill, ChargedTo)
Employee (EmpID, Name, Age, Nationality, NatId, Mobile, Telephone, Email, Position, Language, Salary, JoinDate)
User(UserID, Password)
as per the requirements, the ID number of the employee (UserID or EmpID) should be printed on the bill.
Any suggestions or ideas about implementing this ?
Thanx
Note : to log in the system the user has to log in (front desk receptionist). The employee table includes list of all the employees, and there is another table User (who are the users of the system, In other words, User has to be an employee and not all the employees are users )
I am considering linking Bill table to User table

Change your table structure as follows
Bill (BillID, ResvID, IssueDate, ToTalBill, ChargedTo, UserID(foreign key User))
Employee (EmpID, Name, Age, Nationality, NatId, Mobile, Telephone, Email, Position, Language, Salary, JoinDate)
User(UserID, EmpID(foreign Key Employee), Password)
Now you have user id in bill itself and if required you can get empID using userID
Assumptions
- every user is employee
- only users can generate bills.

Related

Students records in MySQL are entered again in more than one table after entering the email wrong. How to fix this issue?

There are three tables - student, exams, sports.
STUDENT : sid(primary, auto increment), email, fname, lname, address, standard
EXAMS : eid(primary, auto increment), sid(foreign) ename, date, result
SPORTS : spid(primary, auto increment), sid(foreign) spname, date, score
Data was added into the DB, but after sometime a guy in my project realized that he entered some emails wrong. Then, instead of editing the emails, he tried to add new entries of some of those students but not all data (he missed adding entries to exams/sports table for some students) . He did that for random students.
I tried this query to get a clear understanding
SELECT a.sid, a.fname,
CASE WHEN EXISTS (SELECT * FROM EXAMS e WHERE a.sid = e.sid) THEN 'YES' ELSE 'NO' END,
CASE WHEN EXISTS (SELECT * FROM SPORTS s WHERE a.sid = s.sid) THEN 'YES' ELSE 'NO' END
FROM STUDENT a;
How do I find which records I need to delete and which I need to update.
As existing copy of data has been inserted, instead of updating the emails, you can check using the other attributes in STUDENT table for more than one copies of data. Assuming he has entered data for other attribtes correctly, you can check using this:
SELECT fname, lname, address, standard, MAX(sid)
FROM STUDENT c GROUP BY fname, lname, address, standard HAVING count(*) > 1;
As sid is AUTO_INCREMENT, then for the same student, the later inserted IDs are duplicate. The above query finds those, use that to delete those IDs from your STUDENT table. Subsequently delete all those IDs from the rest tables.
Note: if a foreign key constraint is declared, you might need to delete from EXAM and SPORT first.

SQL Table commands to show certain data?

I have IT homework that is due at midnight tonight. For our assignment, we had to create a table in an SQL database off my school AFS database. I am using MobaXTerm to do this homework assignment.
I created a table name "student". I created the entire table correctly. It is correct, because my professor gave me the exact command to create it. Here are the columns in order: id, firstname, lastname, address, state, gpa, credits. I populated this table with 20 students, however I do not want to post the picture of the result on here, because it has personal info on it.
I answered the other questions correctly, however I am stuck on this question that has multiple questions in it:
Next, write and run (issue) SQL queries that do the following. For each query, provide screenshots for the SQL query and the results within a Word document so I can grade it.
a. Show state and gpa information about students with the first name Peter (I was told to add students with the name "Peter" before I created this table).
This one is correct here is the command i used:
select state, gpa, firstname from student where lastname = 'Peter';
b. Retrieve the last names, state, and credits of all students that are NOT from AZ or FL. Order by the state.
I am struggling on this one, because I do not know how to show the table of students that are both NOT from AZ and FL.
But here is a command that worked to show if they are not from one state.
select, lastname, credits, state from student where state != 'AZ'
How am I supposed to write that student is not equal to both AZ and FL?
c. How many students live on '10 Main Street'?
select id, address from student where address='10 Main Street';
This question is correct.
d. Retrieve all sophomore student ids along with their credits that are NOT C students (see the table for definition for “sophomore” and “C” grades).
So the table shows that a sophomore has 33-64 credits. A C student has a GPA of 1.7-2.69. So what is my line of command to show these range of numbers?
Q: How am I supposed to write that student is not equal to both AZ and FL?
... WHERE state != 'AZ' AND state != 'FL'
Q: How many students ...
SELECT COUNT(*) AS count_students FROM ...
Q: Sophmore not C
... WHERE credits >= 33 AND credits <= 64
AND NOT ( gpa >= 1.7 AND gpa <= 2.69)

how to design: users with different roles see different records

I have a schema design question for my application, hope I can get advices from teachers. This is very alike of Role Based Access Controll, but a bit different in detail.
Spec:
For one company, there are 4 roles: Company (Boss) / Department (Manager) / Team (Leader) / Member (Sales), and there are about 1 million Customers records. Each customer record can be owned by someone, and he could be Boss or Manager or Leader or Sales. If the record's owner is some Sales, then his upper grade (say: his leader / manager / boss) can see this record as well (but others: say the same level of his workmates, cannot see, unless his upper grade manager share the customer to his workmates), but if the record's owner is boss, none except the boss himself can see it.
My Design is like this (I want to improve it to make it more simple and clear):
Table:
departments:
id (P.K. deparment id)
d_name (department name)
p_id (parent department id)
employees
id (P.K. employee id)
e_name (employee name)
employee_roles
id (P.K.)
e_id (employee id)
d_id (department id)
customers
id (P.K. customer id)
c_name (customer name)
c_phone (customer phone)
permissions
id (P.K.)
c_id (customer id)
e_id (owner employee id)
d_id (this customer belongs to which deparment)
share_to (this customer share to other's id)
P.S.: each employee can have multi roles, for example, employee A can be the manager of department_I and meanwhile he can also be one sales of deparment_II >> Team_X.
So, when an employee login to application, by querying from employee_roles table, we can get all of the department ids and sub department ids, and save them into an array.
Then I can use this array to query from permissions table and join it with customers table to get all the customers this employee should see. The SQL might look like this:
SELECT * FROM customers AS a INNER JOIN permissions AS b ON a.id =
b.c_id AND (b.d_id IN ${DEP_ARRAY} OR e_id = ${LOGIN_EMPLOYEE_ID} OR
share_to = ${LOGIN_EMPLOYEE_ID})
I don't really like the above SQL, especially the "IN" clause, since I am afraid it will slow down the query, since there are about 1 million records or even more in the customer table; and, there will be as many records as the customers table in the permissions table, the INNER JOIN might be very slow too. (So what I care about is the performance like everyone :))
To my best knowledge, this is the best design I can work out, could you teachers please help to give me some advice on this question? If you need anything more info, please let me know.
Any advice would be appreciated!
Thanks a million in advance!!
Do not use an array, use a table, ie the value of a select statement. And stop worrying about performance until you know more basics about thinking in terms of tables and queries.
The point of the relational model is that if you structure your data as tables then you can looplessly describe the output table and the DBMS figures out how to calculate it. See this. Do not think about "joining"; think about describing the result. Whatever the DBMS ends up doing is its business not yours. Only after you become knowledgeable about variations in descriptions and options for descriptions will you have basic knowledge to learn about performance.

How to create a view while joining two tables in MySql

I need to Create a view named customer_addresses that shows the shipping and billing addresses for each customer. This view should return these columns from the Customers table:
customer_id
email_address
last_name
first_name.
This view should return these columns from the Addresses table:
bill_line1
bill_line2
bill_city
bill_state
bill_zip
ship_line1
ship_line2
ship_city
ship_state
ship_zip
The rows in this view should be sorted by the last_name and then first_name columns.
Notes
The customers table includes the following columns: customer_id, email_address, password, first_name, last_name, shipping_address_id, and billing_address_id
The Addresses table includes the following columns: address_id, customer_id, line1, line2, city, state, zip_code, phone
I tried to post a picture of both tables but I am brand new here and do not yet have 10 rep. I am primarily having issues within the join statement due to the conversion of billing_id / shipping_id to actual addresses.
Join the 'addresses' table twice, linking one to the billing address id, and the other to shipping address id.
CREATE VIEW CUSTOMER_ADDRESSES AS
SELECT cust.customer_id, cust.last_name, cust.first_name, cust.email_address
bill.line1 as bill_line1, bill.line2 as bill_line2, bill.city as bill_city,
bill.state as bill_state, bill.zip as bill_zip,
ship.line1 as ship_line1, ship.line2 as ship_line2, ship.city as ship_city,
ship.state as ship_state, ship.zip as ship_zip
FROM customers cust, addresses bill, addresses ship
WHERE cust.shipping_address_id = ship.address_id
and cust.billing_address_id = bill.address_id

creating relational tables within a database

I am having a major issue with relating the following tables together. It is a many to many relationship between Patient and Nurse.
Here is the relational table:
Patient (PatientID (PK), Forename, surname, gender, date of birth, address, illness, prioirty)
seen_by (ID(PK) PatientID(FK to Patient.PatientID), NurseID(FK to Nurse.NurseID) )
Nurse (NurseID(PK), Forename, surname)
The issue I am getting is that, I want the PatientId to be assigned to a NurseID so I know what patient is seen by what nurse. Please note that the ID are all auto_increment values.
Any suggesions, thanks in advance!
Why not create a join table that would be the relationship between the patient and the nurse?
Table: Appointment (AppointmentID (PK), PatientID, NurseID)
Then you can assign the patient and the nurse to the appointment record and keep track of the relationship. This also gives you the benefit of keeping track of additional information regarding the appointment such as dates, prescriptions, etc.
I'm not sure what development environment you're using but Rails has some nice patterns for accessing objects through these kinds of relationships.