0

I have problem with SQL . I am trying to create tables from other tables with foreign keys. The table is created normally without problem but when I'm trying to see the data inside the table there is none of the data inside! Anyone who knows?

-- Here is my table with the foreign keys

CREATE TABLE StaffXCustomersXMeals(
        --MealID int FOREIGN KEY REFERENCES Meals(MealID),
        --CustomersID int FOREIGN KEY REFERENCES Customers(CustomersID),
        --StaffID int FOREIGN KEY REFERENCES Staff(StaffID)
     )
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Giorgos Neokleous
  • 1,709
  • 1
  • 14
  • 25
  • 1
    Have you inserted any data into the table? – Gordon Linoff Dec 03 '14 at 12:03
  • Which DBMS are you using? Postgres? Oracle? Did you commit your `insert` statements? Btw: your example creates a table without columns because all column definitions are commented. –  Dec 03 '14 at 12:06
  • No I inserted data into the others tables! and no the table was created normally i just comment it to try something else – Giorgos Neokleous Dec 03 '14 at 13:03

2 Answers2

0

You didn't enter any data. You only said that whatever data entered must reference the other tables. So you cannot enter invalid meals for instance.

You need INSERT statments to fill the table with whatever data you need.

Thorsten Kettner
  • 89,309
  • 7
  • 49
  • 73
  • U mean that i have to enter data into it ? I want that table to show the data of the others tables ! I have to do inner joins then ? – Giorgos Neokleous Dec 03 '14 at 13:04
  • If you want all combinations of the other tables's data, then you may not want another table at all, because you can always get the data from the other tables. What is StaffXCustomersXMeals supposed to be good for? What do you want to achieve with it? – Thorsten Kettner Dec 03 '14 at 13:14
  • Pass the course ! that table supposed to show the who from the staff is for what customers and which meals but ok ! i understood what you mean. Thank you – Giorgos Neokleous Dec 03 '14 at 13:16
0

You might want to consider reading up on the concept of a SQL View - think of it as a "virtual table based on the result-set of an SQL statement." When you create a view based on the JOIN between multiple tables, it saves your query and you can then select from it like you would a table.

For example, you might have the following:

CREATE VIEW StaffCustomerMeals 
AS

SELECT 
    m.MealID,
    c.CustomersID,
    s.StaffID 
FROM 
    Meals m
     LEFT JOIN
    Customers c ON 
        m.SomeIDThatMeansThisCustomer = c.CustomersID
     LEFT JOIN 
    Staff s ON 
        m.SomeIDThatMeansAStaffMember = s.StaffID

You need to define these relationships in accordance with your own schema - it's your homework assignment, so do your best to figure it out. A couple more questions to consider:

  1. Does a meal always have both a customer and a staff member, or are they customers who might happen to be staff members?
  2. Should you include other information besides the IDs (e.g., CustomerName, StaffMemberDepartment, MealPrice, PurchaseDate)
AHiggins
  • 7,029
  • 6
  • 36
  • 54
  • Man you are very good ! Thank you you were really a big help! I need to fix my connections between my tables too ! – Giorgos Neokleous Dec 03 '14 at 14:42
  • Happy to have provided assistance. If this or any answer helps you, feel free to upvote it by clicking up arrow next to the post. – AHiggins Dec 03 '14 at 15:19