search
HomeDatabaseMysql TutorialMySQL table design tutorial: Create a simple book borrowing table

MySQL table design tutorial: Create a simple book borrowing table

Jul 02, 2023 pm 05:45 PM
mysqltable designTable name: Borrowing table

MySQL table design tutorial: Create a simple book borrowing table

Designing tables in the database is an important task in database development. This tutorial will take creating a simple book borrowing table as an example to teach you how to use MySQL for table design.

First, we need to create a new database. In MySQL, you can create a new database with the following command:

CREATE DATABASE library;

Next, we need to select the database we just created:

USE library;

Create a new database named books A table used to store book information. We need to record the following fields for each book: id, title, author, publication_date, status. Use the following command to create this table:

CREATE TABLE books (
    id INT AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(100) NOT NULL,
    author VARCHAR(100) NOT NULL,
    publication_date DATE,
    status ENUM('available', 'borrowed') DEFAULT 'available'
);

In the above command, we defined an auto-incrementing primary key id as the unique identifier of the book. The title and author fields are used to store the title and author of the book. The publication_date field stores the publication date of the book, and the status field is used to identify the borrowing status of the book. The default is "available".

Next, we create a table named borrowers to store the borrower’s information. Each borrower needs to have a unique id and name. Use the following command to create this table:

CREATE TABLE borrowers (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL
);

In order to record the borrowing information of books, we also need to create a table named borrowings. Each piece of borrowing information needs to include the borrower's borrower_id, the book_id of the borrowed book, and the borrowing date borrow_date. Use the following command to create this table:

CREATE TABLE borrowings (
    borrowing_id INT AUTO_INCREMENT PRIMARY KEY,
    borrower_id INT,
    book_id INT,
    borrow_date DATE,
    FOREIGN KEY (borrower_id) REFERENCES borrowers(id),
    FOREIGN KEY (book_id) REFERENCES books(id)
);

In the above command, we have used a foreign key association to create the borrowings table with borrowers and booksContact the table to ensure that the borrower and book associated with each borrowing information exist in the corresponding table.

Now, we have successfully created a simple book lending table. You can use the following code to add data to the table:

INSERT INTO books (title, author, publication_date) VALUES 
('Animal Farm', 'George Orwell', '1945-08-17'),
('1984', 'George Orwell', '1949-06-08'),
('To Kill a Mockingbird', 'Harper Lee', '1960-07-11');

INSERT INTO borrowers (name) VALUES 
('John Smith'),
('Jane Doe');

INSERT INTO borrowings (borrower_id, book_id, borrow_date) VALUES 
(1, 1, '2020-01-01'),
(1, 2, '2020-02-01'),
(2, 3, '2020-03-01');

Use the following command to query all data in the book table:

SELECT * FROM books;

Query results:

+----+-----------------------+----------------+-------------------+------------+
| id | title                 | author         | publication_date  | status     |
+----+-----------------------+----------------+-------------------+------------+
|  1 | Animal Farm           | George Orwell  | 1945-08-17        | available  |
|  2 | 1984                  | George Orwell  | 1949-06-08        | available  |
|  3 | To Kill a Mockingbird | Harper Lee     | 1960-07-11        | available  |
+----+-----------------------+----------------+-------------------+------------+

Use the following command to query Query all data in the borrower table:

SELECT * FROM borrowers;

Query results:

+----+-------------+
| id | name        |
+----+-------------+
|  1 | John Smith  |
|  2 | Jane Doe    |
+----+-------------+

Use the following command to query all data in the borrowing information table:

SELECT borrowings.borrowing_id, borrowers.name, books.title, borrowings.borrow_date FROM borrowings 
INNER JOIN borrowers ON borrowers.id = borrowings.borrower_id 
INNER JOIN books ON books.id = borrowings.book_id;

Query results:

+--------------+-------------+-----------------------+-------------+
| borrowing_id | name        | title                 | borrow_date |
+--------------+-------------+-----------------------+-------------+
|            1 | John Smith  | Animal Farm           | 2020-01-01  |
|            2 | John Smith  | 1984                  | 2020-02-01  |
|            3 | Jane Doe    | To Kill a Mockingbird | 2020-03-01  |
+--------------+-------------+-----------------------+-------------+

In this way, we successfully created a simple book borrowing table and performed some basic data queries. Through this example, you can learn how to use MySQL for table design, and master basic table creation and data query skills through code examples. I hope this article will help you master MySQL table design!

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

Statement
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
What are some tools you can use to monitor MySQL performance?What are some tools you can use to monitor MySQL performance?Apr 23, 2025 am 12:21 AM

How to effectively monitor MySQL performance? Use tools such as mysqladmin, SHOWGLOBALSTATUS, PerconaMonitoring and Management (PMM), and MySQL EnterpriseMonitor. 1. Use mysqladmin to view the number of connections. 2. Use SHOWGLOBALSTATUS to view the query number. 3.PMM provides detailed performance data and graphical interface. 4.MySQLEnterpriseMonitor provides rich monitoring functions and alarm mechanisms.

How does MySQL differ from SQL Server?How does MySQL differ from SQL Server?Apr 23, 2025 am 12:20 AM

The difference between MySQL and SQLServer is: 1) MySQL is open source and suitable for web and embedded systems, 2) SQLServer is a commercial product of Microsoft and is suitable for enterprise-level applications. There are significant differences between the two in storage engine, performance optimization and application scenarios. When choosing, you need to consider project size and future scalability.

In what scenarios might you choose SQL Server over MySQL?In what scenarios might you choose SQL Server over MySQL?Apr 23, 2025 am 12:20 AM

In enterprise-level application scenarios that require high availability, advanced security and good integration, SQLServer should be chosen instead of MySQL. 1) SQLServer provides enterprise-level features such as high availability and advanced security. 2) It is closely integrated with Microsoft ecosystems such as VisualStudio and PowerBI. 3) SQLServer performs excellent in performance optimization and supports memory-optimized tables and column storage indexes.

How does MySQL handle character sets and collations?How does MySQL handle character sets and collations?Apr 23, 2025 am 12:19 AM

MySQLmanagescharactersetsandcollationsbyusingUTF-8asthedefault,allowingconfigurationatdatabase,table,andcolumnlevels,andrequiringcarefulalignmenttoavoidmismatches.1)Setdefaultcharactersetandcollationforadatabase.2)Configurecharactersetandcollationfor

What are triggers in MySQL?What are triggers in MySQL?Apr 23, 2025 am 12:11 AM

A MySQL trigger is an automatically executed stored procedure associated with a table that is used to perform a series of operations when a specific data operation is performed. 1) Trigger definition and function: used for data verification, logging, etc. 2) Working principle: It is divided into BEFORE and AFTER, and supports row-level triggering. 3) Example of use: Can be used to record salary changes or update inventory. 4) Debugging skills: Use SHOWTRIGGERS and SHOWCREATETRIGGER commands. 5) Performance optimization: Avoid complex operations, use indexes, and manage transactions.

How do you create and manage user accounts in MySQL?How do you create and manage user accounts in MySQL?Apr 22, 2025 pm 06:05 PM

The steps to create and manage user accounts in MySQL are as follows: 1. Create a user: Use CREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password'; 2. Assign permissions: Use GRANTSELECT, INSERT, UPDATEONmydatabase.TO'newuser'@'localhost'; 3. Fix permission error: Use REVOKEALLPRIVILEGESONmydatabase.FROM'newuser'@'localhost'; then reassign permissions; 4. Optimization permissions: Use SHOWGRA

How does MySQL differ from Oracle?How does MySQL differ from Oracle?Apr 22, 2025 pm 05:57 PM

MySQL is suitable for rapid development and small and medium-sized applications, while Oracle is suitable for large enterprises and high availability needs. 1) MySQL is open source and easy to use, suitable for web applications and small and medium-sized enterprises. 2) Oracle is powerful and suitable for large enterprises and government agencies. 3) MySQL supports a variety of storage engines, and Oracle provides rich enterprise-level functions.

What are the disadvantages of using MySQL compared to other relational databases?What are the disadvantages of using MySQL compared to other relational databases?Apr 22, 2025 pm 05:49 PM

The disadvantages of MySQL compared to other relational databases include: 1. Performance issues: You may encounter bottlenecks when processing large-scale data, and PostgreSQL performs better in complex queries and big data processing. 2. Scalability: The horizontal scaling ability is not as good as Google Spanner and Amazon Aurora. 3. Functional limitations: Not as good as PostgreSQL and Oracle in advanced functions, some functions require more custom code and maintenance.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!