简体   繁体   中英

Tables of Tables in Database

I'm new to web development and am taking a class in which we are working on websites using PHP and MySQL.

I am working on a project for which I will have a lot of users, each of which has one or more calendars which each have one or more events.

Logically it makes sense for an event to be a row in a calendar table with a date-time, title, location, etc.

My question is:

How do I essentially store a "pointer" to a table in a row of another table?

For example, say the user has a sports calendar, and a work calendar. I understand how to create the table for each of these calendars and add events to them, but how do I store information on how to retrieve them inside my user row.

The user will have columns for

  • userid(auto-incremented),
  • email,
  • username,
  • password(hash),
  • salt,
  • and calendars.)

You just put a column userId inside the calendar's tables. And whenever you need them you query for it in those tables. That's the only reference you need.

And you shouldn't have references to calendar's in the user table, since the user can have multiple calendars, you should be good with the userId as foreign key in calendar's tables.

You will need basically three tables here one for Users another for Calendar and another for Event .

And the relationship would be a User could have many calendars and a Calendar could have many Events .

It would probably look like this:

TABLE Name : Users 
Fields     : userid(auto-incremented), email, username, password(hash), salt

TABLE Name : Calendars 
Fields     : CalendarID, userid, dateFrom, dateTo, Category

TABLE Name : Events
Fields     : EventID, CalendarID, Description

And your SQL would probably look like this:

To get the calendar of a given user would be:

SELECT CalendarID, dateFrom, dateTo, Category
FROM Calendars
WHERE userid = <user id here>

And to get the events of that calendar of a given user would probably look like this:

SELECT Calendars.CalendarID, dateFrom, dateTo, Category, Description
FROM Calendars
INNER JOIN Events
ON Calendars.CalendarID = Events.CalendarID
WHERE userid = <user id here>

My logic is different than your logic. My idea of a calendar table would have a single row for each date. Other columns would contain information about that date, such as fiscal or school year, plus holidays.

For user appointments, depending on the level of detail required, I would simply store a user id, and event id. The event would have a start and end datetime and category id.

I would be working towards something where each user only has one set of appointments to maintain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM