Home > Database > Mysql Tutorial > MySQL table design tutorial: Create a simple Weibo message table

MySQL table design tutorial: Create a simple Weibo message table

WBOY
Release: 2023-07-01 21:42:16
Original
1306 people have browsed it

MySQL table design tutorial: Create a simple Weibo message table

When creating a Weibo application, an important component is the storage and management of Weibo messages. In this tutorial, we will use MySQL database to design and create a simple Weibo message table.

First, we need to create a database to store Weibo messages. Open the MySQL command line or use a graphical interface tool and execute the following command to create a database:

CREATE DATABASE weibo;
Copy after login

Next, use the following command to select the created database:

USE weibo;
Copy after login

Now, we can start creating our Weibo message table. We will create a table named messages, containing the following fields:

  1. id: The unique identifier of the message, type is integer.
  2. content: The content of the Weibo message, type is text.
  3. user_id: The user ID that published the message, type is integer.
  4. created_at: The timestamp of message creation, type is date and time.

Execute the following command to create the table:

CREATE TABLE messages (
  id INT PRIMARY KEY AUTO_INCREMENT,
  content TEXT NOT NULL,
  user_id INT NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Copy after login

The above command creates a table named messages and defines the types and constraints of each field. The id field is the primary key and uses self-increasing attributes to ensure that each message has a unique identification. The content field stores the text content of the message, using the TEXT type to accommodate longer text. The user_id field stores the user ID of the publisher, which is used to identify which user the message was published by. The created_at field is used to store the creation time of the message, using the TIMESTAMP type, and setting the default value to the current timestamp.

Now we can add some sample data to the table to verify that the table is designed correctly. Execute the following command to insert some sample data:

INSERT INTO messages (content, user_id) VALUES
  ('第一条微博消息', 1),
  ('这是一条有点长的微博消息,用来测试消息内容的长度限制是否有效。', 2),
  ('今天是个好日子!', 3);
Copy after login

By executing the above command, we inserted three Weibo messages into the table. Each message contains the content and the user ID of the publisher.

Now, let’s verify that our table design works. Execute the following command to query all the data in the table:

SELECT * FROM messages;
Copy after login

You will see the following results:

+----+-------------------------------------------------------------+---------+---------------------+
| id | content                                                     | user_id | created_at          |
+----+-------------------------------------------------------------+---------+---------------------+
| 1  | 第一条微博消息                                             | 1       | 2021-06-01 10:00:00 |
| 2  | 这是一条有点长的微博消息,用来测试消息内容的长度限制是否有效。 | 2       | 2021-06-01 10:00:00 |
| 3  | 今天是个好日子!                                           | 3       | 2021-06-01 10:00:00 |
+----+-------------------------------------------------------------+---------+---------------------+
Copy after login

If you see the above results, then you have successfully created a simple micro Blog message table and inserted some sample data into it.

Through this tutorial, we learned how to use MySQL to design and create a simple Weibo message table. This is just a basic table in a Weibo application. The actual application may contain more tables to manage users, follow relationships, comments and other functions. But this simple example can help you get started and understand the basics of table design.

Hope this tutorial is helpful to you! If you have other questions or need further learning, please refer to MySQL's official documentation or other related tutorials.

The above is the detailed content of MySQL table design tutorial: Create a simple Weibo message table. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template