D
D
Dmitry2014-08-14 18:56:38
MySQL
Dmitry, 2014-08-14 18:56:38

How to create a complex SQL query to create tables with relationships and join queries?

The actual task:


For the city library of Prostokvashino, it is necessary to design a database structure for a reader accounting system. The system must store information about readers and the books they borrowed, as well as information about all books in general.
Describe the structure of the database in the form of an SQL script for creating the necessary tables and relationships.
Information about the book
* Title - text
* Author - text
* Status = [free, taken]
* Taken by whom, if taken
* When taken, if taken - date
* Until what date, if taken - date
If the book is available in more than one copy , this must be reflected in the database.
Reader information
* First name - text
* Last name - text
* Service information = [regular reader, rare reader, reader of the reading room only]
Information about the books taken
What document was presented when issuing the book = [Moustache, Paws, Tail]
Document information
Mustache:
* document series - 4 digits, required
* document number - 6 digits, mandatory
* by whom issued - text
* when issued - date
Paws:
* document series - a set of numbers and Latin letters from 2 to 5 characters long, optional
* document number - a set of numbers and Latin letters from 6 to 8 characters long, mandatory
* issued by - text
Tail:
* Document number - a set of numbers and Latin letters from 6 to 8 characters long, mandatory
* Validity start date - date
* Validity expiration date - date

I tried to write something, but I strongly doubt the correctness. The last part with different types of data and conditions for documents is especially not clear. Thank you.
UPD.
What has worked out so far:
CREATE TABLE books ( book_id int (10) AUTO_INCREMENT NOT NULL, book_name VARCHAR(255) NOT NULL, book_author VARCHAR (255) NOT NULL, book_status ENUM (‘Y’,’N’) NOT NULL default(Y), book_owner INT(11), book_take_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, book_return_date DATETIME NOT NULL, FOREIGN KEY (book_owner) REFERENCES users (user_id)))
UNION
CREATE TABLE users ( user_id int (10) AUTO_INCREMENT NOT NULL, user_firstname VARCHAR(255) NOT NULL, user_lastname VARCHAR (255) NOT NULL, user_info ENUM(‘regular_reader’, ’rare_reader’ , ’reading_room’) NOT NULL))
UNION
CREATE TABLE busy_book ( id int (10) AUTO_INCREMENT NOT NULL, bbook_id INT(11) NOT NULL, bbook_document VARCHAR (255) NOT NULL, user_info ENUM(‘regular_reader’, ’rare_reader’ , ’reading_room’) NOT NULL, FOREIGN KEY (bbook_id) REFERENCES books(book_id))

Then I realized that everything is not so :(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2014-08-14
@another_dream

www.w3schools.com/sql/sql_create_table.asp
www.w3schools.com/sql/sql_foreignkey.asp

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question