0% found this document useful (0 votes)
6 views4 pages

Guestbook Project - SQL

Sql

Uploaded by

Jordan Samuelson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Guestbook Project - SQL

Sql

Uploaded by

Jordan Samuelson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 4

mysql> CREATE DATABASE guestbook;

Query OK, 1 row affected (0.03 sec)

mysql>
mysql> USE guestbook;
Database changed
mysql>
mysql> CREATE TABLE Users (
-> user_id INT PRIMARY KEY AUTO_INCREMENT,
-> username VARCHAR(50) NOT NULL UNIQUE,
-> email VARCHAR(100) NOT NULL UNIQUE,
-> created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
-> );
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql> CREATE TABLE Messages (
-> message_id INT PRIMARY KEY AUTO_INCREMENT,
-> user_id INT NOT NULL,
-> message_content TEXT NOT NULL,
-> posted_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
-> FOREIGN KEY (user_id) REFERENCES Users(user_id)
-> );
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> CREATE TABLE Admin (
-> admin_id INT PRIMARY KEY AUTO_INCREMENT,
-> admin_username VARCHAR(50) NOT NULL UNIQUE,
-> admin_password VARCHAR(255) NOT NULL,
-> email VARCHAR(100) NOT NULL UNIQUE,
-> created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
-> );
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql> INSERT INTO Users (username, email) VALUES
-> ('john_doe', '[email protected]'),
-> ('jane_smith', '[email protected]'),
-> ('alice_wonder', '[email protected]'),
-> ('bob_builder', '[email protected]'),
-> ('charlie_brown', '[email protected]');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql>
mysql> INSERT INTO Messages (user_id, message_content) VALUES
-> (1, 'Hello, this is John!'),
-> (2, 'Hi everyone, Jane here!'),
-> (3, 'Alice says hi to all!'),
-> (4, 'Bob wants to join the chat.'),
-> (5, 'Charlie is happy to be here!'),
-> (1, 'Another message from John.'),
-> (2, 'Jane is posting again.'),
-> (3, 'Alice loves this guestbook!'),
-> (4, 'Bob has more to say.'),
-> (5, 'Charlie just can’t stop talking!');
Query OK, 10 rows affected (0.00 sec)
Records: 10 Duplicates: 0 Warnings: 0

mysql>
mysql> INSERT INTO Admin (admin_username, admin_password, email) VALUES
-> ('admin1', 'securepassword1', '[email protected]'),
-> ('admin2', 'securepassword2', '[email protected]');
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql>
mysql> SHOW TABLES;
+---------------------+
| Tables_in_guestbook |
+---------------------+
| Admin |
| Messages |
| Users |
+---------------------+
3 rows in set (0.01 sec)

mysql> DESCRIBE Users;


+------------+--------------+------+-----+-------------------+-------------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+-------------------+-------------------+
| user_id | int | NO | PRI | NULL | auto_increment |
| username | varchar(50) | NO | UNI | NULL | |
| email | varchar(100) | NO | UNI | NULL | |
| created_at | datetime | NO | | CURRENT_TIMESTAMP |
DEFAULT_GENERATED |
+------------+--------------+------+-----+-------------------+-------------------+
4 rows in set (0.01 sec)

mysql> DESCRIBE Messages;


+-----------------+----------+------+-----+-------------------+-------------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+----------+------+-----+-------------------+-------------------+
| message_id | int | NO | PRI | NULL | auto_increment |
| user_id | int | NO | MUL | NULL | |
| message_content | text | NO | | NULL | |
| posted_at | datetime | NO | | CURRENT_TIMESTAMP |
DEFAULT_GENERATED |
+-----------------+----------+------+-----+-------------------+-------------------+
4 rows in set (0.00 sec)

mysql> DESCRIBE Admin;


+----------------+--------------+------+-----+-------------------+-------------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+-------------------+-------------------+
| admin_id | int | NO | PRI | NULL | auto_increment |
| admin_username | varchar(50) | NO | UNI | NULL |
|
| admin_password | varchar(255) | NO | | NULL |
|
| email | varchar(100) | NO | UNI | NULL | |
| created_at | datetime | NO | | CURRENT_TIMESTAMP |
DEFAULT_GENERATED |
+----------------+--------------+------+-----+-------------------+-------------------+
5 rows in set (0.00 sec)

mysql> SELECT * FROM Users;


+---------+---------------+---------------------+---------------------+
| user_id | username | email | created_at |
+---------+---------------+---------------------+---------------------+
| 1 | john_doe | [email protected] | 2024-11-26 15:03:47 |
| 2 | jane_smith | [email protected] | 2024-11-26 15:03:47 |
| 3 | alice_wonder | [email protected] | 2024-11-26 15:03:47 |
| 4 | bob_builder | [email protected] | 2024-11-26 15:03:47 |
| 5 | charlie_brown | [email protected] | 2024-11-26 15:03:47 |
+---------+---------------+---------------------+---------------------+
5 rows in set (0.00 sec)

mysql> SELECT * FROM Messages;


+------------+---------+------------------------------------+---------------------+
| message_id | user_id | message_content | posted_at |
+------------+---------+------------------------------------+---------------------+
| 1| 1 | Hello, this is John! | 2024-11-26 15:03:47 |
| 2| 2 | Hi everyone, Jane here! | 2024-11-26 15:03:47 |
| 3| 3 | Alice says hi to all! | 2024-11-26 15:03:47 |
| 4| 4 | Bob wants to join the chat. | 2024-11-26 15:03:47 |
| 5| 5 | Charlie is happy to be here! | 2024-11-26 15:03:47 |
| 6| 1 | Another message from John. | 2024-11-26 15:03:47 |
| 7| 2 | Jane is posting again. | 2024-11-26 15:03:47 |
| 8| 3 | Alice loves this guestbook! | 2024-11-26 15:03:47 |
| 9| 4 | Bob has more to say. | 2024-11-26 15:03:47 |
| 10 | 5 | Charlie just can’t stop talking! | 2024-11-26 15:03:47 |
+------------+---------+------------------------------------+---------------------+
10 rows in set (0.00 sec)

mysql> SELECT * FROM Admin;


+----------+----------------+-----------------+--------------------+---------------------+
| admin_id | admin_username | admin_password | email | created_at
|
+----------+----------------+-----------------+--------------------+---------------------+
| 1 | admin1 | securepassword1 | [email protected] | 2024-11-
26 15:03:47 |
| 2 | admin2 | securepassword2 | [email protected] | 2024-11-
26 15:03:47 |
+----------+----------------+-----------------+--------------------+---------------------+
2 rows in set (0.00 sec)

mysql> SELECT m.message_id, u.username, m.message_content


-> FROM Messages m
-> JOIN Users u ON m.user_id = u.user_id;
+------------+---------------+------------------------------------+
| message_id | username | message_content |
+------------+---------------+------------------------------------+
| 3 | alice_wonder | Alice says hi to all! |
| 8 | alice_wonder | Alice loves this guestbook! |
| 4 | bob_builder | Bob wants to join the chat. |
| 9 | bob_builder | Bob has more to say. |
| 5 | charlie_brown | Charlie is happy to be here! |
| 10 | charlie_brown | Charlie just can’t stop talking! |
| 2 | jane_smith | Hi everyone, Jane here! |
| 7 | jane_smith | Jane is posting again. |
| 1 | john_doe | Hello, this is John! |
| 6 | john_doe | Another message from John. |
+------------+---------------+------------------------------------+
10 rows in set (0.00 sec)

You might also like