bubble_chart StackLab

User Sessions

8 steps · pending Not started · person_outline Guest
arrow_back Back

Session Table

code sql
CREATE TABLE user_sessions(
    session_id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    session_start DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    session_end DATETIME DEFAULT NULL,
    session_duration INT DEFAULT NULL,

    CONSTRAINT fk_user_sessions_user_id 
        FOREIGN KEY (user_id) 
        REFERENCES users(user_id) 
        ON DELETE CASCADE
        ON UPDATE CASCADE
);
user_sessions Table Tracks user login sessions — start time, end time, and duration (seconds).
code text
|       Column       |                      Notes                         |
|--------------------|----------------------------------------------------|
| `session_id`       | Primary key, auto-incremented.                     |
| `user_id`          | FK → `users(user_id)`. `ON DELETE/UPDATE CASCADE`. |
| `session_start`    | Defaults to `CURRENT_TIMESTAMP`.                   |
| `session_end`      | `NULL` while session is active.                    |
| `session_duration` | Seconds; `NULL` until session ends.                |
Notes:
    1. Active sessions have session_end and session_duration = NULL.
    2. Deleting a user removes their session history (cascade).

login Sign in to save your progress permanently. info Progress is saved in your browser session
format_list_numbered Step 2 of 8