DDL Assignment 2
Library System
Use the following Schema to perform the given set of assignment.
TablesMember – It contains information about the members Column Name Data Type Description
Member_Id Integer Unique Member ID Member_Name Varchar(30) Name of the Library member
Member_address Varchar(50) Address of the member Acc_Open_Date Date Date of membership
Membership_type Varchar(20) Type of the membership such as ‘Lifetime’,’ Annual’, ‘Half Yearly’,’
Quarterly’ Fees_paid Integer Membership fees paid Max_Books_Allowed Integer Total Number of
books that can be issued to the member. Penalty_Amount Decimal(7,2) Penalty amount due Books-
It contains information about the books belongs to the library Column Name Data Type Description
Book_No Integer Book identification number Book_Name VarChar(30) Name of the book
Author_name Varchar(30) Author of the book Cost Decimal(7,2) Cost of the book Category Char(10)
Category like Science , Fiction etc. Issue – It contains the information about issue of the books
Column Name Data Type Description Lib_Issue_Id Integer Library Book Issue No Book_No Integer
Number of the book issued Member_Id Integer Member that issued the book Issue_Date Date Date
of Issue Return_date Date Return date Task / Problems:
1)Create the table Member, Books and Issue without any constraints as mentioned in the schema
description above. mysql> create table Member
mysql> create table member
-> (member_id int,
-> member_name varchar(30),
-> member_address varchar(50),
-> acc_open_date varchar(20),
-> fees_paid int,
-> max_books_allowed int,
-> penalty_amount decimal(7,2));
Query OK, 0 rows affected (0.04 sec)
mysql> create table books
-> (book_no int,
-> book_name varchar(30),
-> author_name varchar(30),
-> cost decimal(7,2),
-> category char (10));
Query OK, 0 rows affected (0.03 sec)
mysql> create table issue
-> (lib_issue_id int,
-> book_no int,
-> member_id int,
-> issue_date date,
-> return_date date);
Query OK, 0 rows affected (0.03 sec)
2) View the structure of the tables.
mysql> desc member;
+-------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+-------+
| member_id | int | YES | | NULL | |
| member_name | varchar(30) | YES | | NULL | |
| member_address | varchar(50) | YES | | NULL | |
| acc_open_date | varchar(20) | YES | | NULL | |
| fees_paid | int | YES | | NULL | |
| max_books_allowed | int | YES | | NULL | |
| penalty_amount | decimal(7,2) | YES | | NULL | |
+-------------------+--------------+------+-----+---------+-------+
7 rows in set (0.00 sec)
mysql> desc books;
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| book_no | int | YES | | NULL | |
| book_name | varchar(30) | YES | | NULL | |
| author_name | varchar(30) | YES | | NULL | |
| cost | decimal(7,2) | YES | | NULL | |
| category | char(10) | YES | | NULL | |
+-------------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> desc issue;
+--------------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------+------+-----+---------+-------+
| lib_issue_id | int | YES | | NULL | |
| book_no | int | YES | | NULL | |
| member_id | int | YES | | NULL | |
| issue_date | date | YES | | NULL | |
| return_date | date | YES | | NULL | |
+--------------+------+------+-----+---------+-------+
5 rows in set (0.00 sec)
3) Drop the Member table
mysql> drop table member;
Query OK, 0 rows affected (0.02 sec)
mysql> desc member;
ERROR 1146 (42S02): Table 'sample.member' doesn't exist
4) Create the table Member again as per the schema description with the following constraints. a.
Member_Id – Primary Key b. Membership_type - ‘Lifetime’,’ Annual’, ‘Half Yearly’,’ Quarterly’
mysql> create table member
-> (Member_id int primary key,
-> Member_Name varchar(30),
-> Member_Address varchar(50),
-> Acc_open_date date,
-> Membership_type varchar(20) check (Membership_type in
('Lifetime','Annual','Half_Yearly','Quarterly')),
-> Fees_paid int,
-> Max_Books_Allowed int,
-> Penalty_Amount decimal(7,2));
Query OK, 0 rows affected (0.75 sec)
mysql>desc member;
+-------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+-------+
| Member_id | int | NO | PRI | NULL | |
| Member_Name | varchar(30) | YES | | NULL | |
| Member_Address | varchar(50) | YES | | NULL | |
| Acc_open_date | date | YES | | NULL | |
| Membership_type | varchar(20) | YES | | NULL | |
| Fees_paid | int | YES | | NULL | |
| Max_Books_Allowed | int | YES | | NULL | |
| Penalty_Amount | decimal(7,2) | YES | | NULL | |
+-------------------+--------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
5) Modify the table Member increase the width of the member name to 30 characters.
mysql> alter table member
-> modify column member_name varchar(30);
Query OK, 0 rows affected (0.78 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc member;
+-------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+-------+
| Member_id | int | NO | PRI | NULL | |
| member_name | varchar(30) | YES | | NULL | |
| Member_Address | varchar(50) | YES | | NULL | |
| Acc_open_date | date | YES | | NULL | |
| Membership_type | varchar(20) | YES | | NULL | |
| Fees_paid | int | YES | | NULL | |
| Max_Books_Allowed | int | YES | | NULL | |
| Penalty_Amount | decimal(7,2) | YES | | NULL | |
+-------------------+--------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
6) Add a column named as Reference of Char(30) to Issue table.
mysql> alter table issue
-> add reference char(30);
Query OK, 0 rows affected (0.43 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc issue;
+--------------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+----------+------+-----+---------+-------+
| Lib_Issue_Id | int | YES | | NULL | |
| Book_No | int | YES | | NULL | |
| Member_id | int | YES | | NULL | |
| Issue_Date | date | YES | | NULL | |
| Return_Date | date | YES | | NULL | |
| reference | char(30) | YES | | NULL | |
+--------------+----------+------+-----+---------+-------+
6 rows in set (0.00 sec)
7) Delete/Drop the column Reference from Issue.
mysql> alter table issue
-> drop column reference;
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc issue;
+--------------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------+------+-----+---------+-------+
| lib_issue_id | int | YES | | NULL | |
| book_no | int | YES | | NULL | |
| member_id | int | YES | | NULL | |
| issue_date | date | YES | | NULL | |
| return_date | date | YES | | NULL | |
+--------------+------+------+-----+---------+-------+
5 rows in set (0.00 sec)
8) Rename the table Issue to Lib_Issue.
mysql> rename table issue to Lib_Issue;
Query OK, 0 rows affected (0.54 sec)
mysql> desc Lib_issue;
+--------------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+----------+------+-----+---------+-------+
| Lib_Issue_Id | int | YES | | NULL | |
| Book_No | int | YES | | NULL | |
| Member_id | int | YES | | NULL | |
| Issue_Date | date | YES | | NULL | |
| Return_Date | date | YES | | NULL | |
| reference | char(30) | YES | | NULL | |
+--------------+----------+------+-----+---------+-------+
6 rows in set (0.00 sec)
9) Insert following data in table Member Member ID Member Name Member Address
Acc_Open_Date Membership_type Fees_Paid Max_Books _Allowed Penalty_ Amount 1 Richa
Sharma Pune 10-12-05 Lifetime 25000 5 50 2 Garima Sen Pune current date Annual 1000 3 Null
mysql> insert into member
->
(member_id,member_name,member_address,acc_open_date,membership_type,fees_paid,max_bo
oks_allowed,penalty_amount)
-> values
-> (1,'Richa Sharma','pune','2005-12-10','Lifetime',25000,5,50.00),
-> (2,'Garima Sen','pune','CURRENT_DATE','Annual',1000,3,NULL);
Query OK, 2 rows affected (0.21 sec)
Records: 2 Duplicates: 0 Warnings: 0
select* from member;
+-----------+--------------+----------------+---------------+-----------------+-----------+-------------------+----------------+
| Member_Id | Member_Name | Member_address | acc_open_date | Membership_type |
Fees_paid | Max_Books_Allowed | Penalty_Amount |
+-----------+--------------+----------------+---------------+-----------------+-----------+-------------------+----------------+
| 1 | Richa Sharma | pune | 2005-12-10 | Lifetime | 25000 | 5|
50.00 |
| 2 | Garima Sen | pune | 2023-12-26 | Annual | 1000 | 3| NULL
|
+-----------+--------------+----------------+---------------+-----------------+-----------+-------------------+----------------+
2 rows in set (0.00 sec)
10) Insert at least 5 records with suitable data.
mysql> INSERT INTO Member (Member_Id, Member_Name, Member_Address, Acc_Open_Date,
Membership_Type, Fees_Paid, Max_Books_Allowed, Penalty_Amount)
-> VALUES
-> (3, 'John Doe', 'New York', '2022-01-15', 'Annual', 1200, 4, 20),
-> (4, 'Alice Smith', 'London', '2022-02-20', 'Half Yearly', 800, 2, 10),
-> (5, 'Bob Johnson', 'Paris', '2022-03-25', 'Quarterly', 500, 3, NULL),
-> (6, 'Emma White', 'Sydney', '2022-04-30', 'Lifetime', 30000, 6, 25),
-> (7, 'Michael Brown', 'Toronto', '2022-05-05', 'Annual', 1500, 5, NULL);
Query OK, 5 rows affected (0.14 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select*from member;
+-----------+---------------+----------------+---------------+-----------------+-----------+-------------------+----------------
+
| Member_id | member_name | Member_Address | acc_open_date | Membership_type |
Fees_paid | Max_Books_Allowed | Penalty_Amount |
+-----------+---------------+----------------+---------------+-----------------+-----------+-------------------+----------------
+
| 1 | Richa Sharma | pune | 2005-12-10 | Lifetime | 25000 | 5|
50.00 |
| 2 | Garima Sen | pune | CURRENT_DATE | Annual | 1000 | 3|
NULL |
| 3 | John Doe | New York | 2022-01-15 | Annual | 1200 | 4|
20.00 |
| 4 | Alice Smith | London | 2022-02-20 | Half Yearly | 800 | 2|
10.00 |
| 5 | Bob Johnson | Paris | 2022-03-25 | Quarterly | 500 | 3| NULL
|
| 6 | Emma White | Sydney | 2022-04-30 | Lifetime | 30000 | 6|
25.00 |
| 7 | Michael Brown | Toronto | 2022-05-05 | Annual | 1500 | 5|
NULL |
+-----------+---------------+----------------+---------------+-----------------+-----------+-------------------+----------------
+
7 rows in set (0.00 sec)
11) Modify the column Member_name. Decrease the width of the member name to 20 characters.
(If it does not allow state the reason for that)
mysql> alter table member
-> modify column member_name varchar(20);
Query OK, 7 rows affected (2.01 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql> desc member;
+-------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+-------+
| Member_Id | int | NO | PRI | NULL | |
| member_name | varchar(20) | YES | | NULL | |
| Member_address | varchar(50) | YES | | NULL | |
| acc_open_date | date | YES | | NULL | |
| Membership_type | varchar(20) | YES | | NULL | |
| Fees_paid | int | YES | | NULL | |
| Max_Books_Allowed | int | YES | | NULL | |
| Penalty_Amount | decimal(7,2) | YES | | NULL | |
+-------------------+--------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
12) Try to insert a record with Max_Books_Allowed = 110, Observe the error that comes.
mysql->INSERT INTO member (member_id, member_name, member_address, acc_open_date,
membership_type, fees_paid, max_books_allowed, penalty_amount)
->VALUES
->(8, 'John Doe', 'Cityville', '2023-01-01', 'Annual', 1200, 110, 0);
Error Code: XXXX
Data too long for column 'max_books_allowed' at row 1
13) Generate another table named Member101 using a Create command along with a simple SQL
query on member table.
mysql> create table member101 as
-> select * from member where 1 = 0;
Query OK, 0 rows affected (0.98 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from member;
+-----------+---------------+----------------+---------------+-----------------+-----------+-------------------+----------------
+
| Member_Id | member_name | Member_address | acc_open_date | Membership_type |
Fees_paid | Max_Books_Allowed | Penalty_Amount |
+-----------+---------------+----------------+---------------+-----------------+-----------+-------------------+----------------
+
| 1 | Richa Sharma | pune | 2005-12-10 | Lifetime | 25000 | 5|
50.00 |
| 2 | Garima Sen | pune | 2023-12-26 | Annual | 1000 | 3| NULL
|
| 3 | John Doe | New York | 2022-01-15 | Annual | 1200 | 4|
20.00 |
| 4 | Alice Smith | London | 2022-02-20 | Half Yearly | 800 | 2|
10.00 |
| 5 | Bob Johnson | Paris | 2022-03-25 | Quarterly | 500 | 3| NULL
|
| 6 | Emma White | Sydney | 2022-04-30 | Lifetime | 30000 | 6|
25.00 |
| 7 | Michael Brown | Toronto | 2022-05-05 | Annual | 1500 | 5|
NULL |
| 8 | John Doe | Cityville | 2023-01-01 | Annual | 1200 | 110 | 0.00
|
+-----------+---------------+----------------+---------------+-----------------+-----------+-------------------+----------------
+
8 rows in set (0.00 sec)
mysql>
14) Add the constraints on columns max_books_allowed and penalty_amt as follows a.
max_books_allowed < 100 b. penalty_amt maximum 1000 Also give names to the constraints.
mysql> alter table member
-> add constraint penalty_amt_constraint check (penalty_amount <= 1000);
Query OK, 8 rows affected (1.71 sec)
Records: 8 Duplicates: 0 Warnings: 0
alter table member
-> add constraint max_books_allowed_constraint check (max_books_allowed < 100);
Query OK, 8 rows affected (1.71 sec)
Records: 8 Duplicates: 0 Warnings: 0
15) Drop the table books.
mysql> drop table books;
Query OK, 0 rows affected (0.38 sec)
16) Create table Books again as per the schema description with the following constraints. a.
Book_No – Primary Key b. Book_Name – Not Null c. Category – System, Fiction, Database, RDBMS,
Others.
mysql> create table books (
-> book_no int primary key,
-> book_name varchar(30) not null,
->
-> author_name varchar(30),
-> cost decimal(7,2),
-> category varchar(10) check (category in ('system', 'fiction', 'database', 'rdbms', 'others'))
-> );
Query OK, 0 rows affected (0.53 sec)
mysql> desc books;
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| book_no | int | NO | PRI | NULL | |
| book_name | varchar(30) | NO | | NULL | |
| author_name | varchar(30) | YES | | NULL | |
| cost | decimal(7,2) | YES | | NULL | |
| category | varchar(10) | YES | | NULL | |
+-------------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql>
17) Insert data in Book table as follows: Book_N o Book Name Author Cost Category 101 Let us C
Denis Ritchie 450 System 102 Oracle – Complete Ref Loni 550 Database 103 Mastering SQL Loni 250
Database 104 PL SQL-Ref Scott Urman 750 Database
mysql> INSERT INTO books (book_no, book_name, author_name, cost, category)
-> VALUES
-> (101, 'Let us C', 'Denis Ritchie', 450, 'system'),
-> (102, 'Oracle - Complete Ref', 'Loni', 550, 'database'),
-> (103, 'Mastering SQL', 'Loni', 250, 'database'),
-> (104, 'PL SQL-Ref', 'Scott Urman', 750, 'database');
Query OK, 4 rows affected (0.18 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> desc books;
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| book_no | int | NO | PRI | NULL | |
| book_name | varchar(30) | NO | | NULL | |
| author_name | varchar(30) | YES | | NULL | |
| cost | decimal(7,2) | YES | | NULL | |
| category | varchar(10) | YES | | NULL | |
+-------------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
18) Insert more records in Book table.
mysql> INSERT INTO Books (Book_NO, Book_Name, Author_name, Cost, Category) VALUES
-> (107, 'Introduction to Databases', 'Eva Miller', 280, 'Database');
Query OK, 1 row affected (0.17 sec)
19) View the data in the tables using simple SQL query.
mysql> select *from books;
+---------+---------------------------+---------------+--------+----------+
| book_no | book_name | author_name | cost | category |
+---------+---------------------------+---------------+--------+----------+
| 101 | Let us C | Denis Ritchie | 450.00 | system |
| 102 | Oracle - Complete Ref | Loni | 550.00 | database |
| 103 | Mastering SQL | Loni | 250.00 | database |
| 104 | PL SQL-Ref | Scott Urman | 750.00 | database |
| 107 | Introduction to Databases | Eva Miller | 280.00 | Database |
+---------+---------------------------+---------------+--------+----------+
5 rows in set (0.10 sec)
20) Insert into Book following data. 105, National Geographic, Adis Scott, 1000, Science
mysql> INSERT INTO Books (Book_NO, Book_Name, Author_name, Cost, Category)
-> VALUES (105, 'National Geographic', 'Adis Scott', 1000, 'Science');
Query OK, 1 row affected (0.17 sec)
21) Rename the table Lib_Issue to Issue.
mysql> rename table lib_issue to Issue;
Query OK, 0 rows affected (0.56 sec)
22) Drop table Issue.
mysql> drop table issue;
Query OK, 0 rows affected (0.57 sec)
23) As per the given structure Create table Issue again with following constraints. Lib_Issue_Id-
Primary key Book_No- foreign key Member_id - foreign key Issue_date Return_date
mysql> create table issue (
-> lib_issue_id int primary key,
-> book_no int,
-> member_id int,
-> issue_date date,
-> return_date date,
-> foreign key (book_no) references books(book_no),
-> foreign key (member_id) references member(member_id)
-> );
Query OK, 0 rows affected (2.37 sec)
mysql> desc issue;
+--------------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------+------+-----+---------+-------+
| lib_issue_id | int | NO | PRI | NULL | |
| book_no | int | YES | MUL | NULL | |
| member_id | int | YES | MUL | NULL | |
| issue_date | date | YES | | NULL | |
| return_date | date | YES | | NULL | |
+--------------+------+------+-----+---------+-------+
5 rows in set (0.00 sec)
24) Insert following data into Issue table. Lib_Issu e_Id Book No Member ID Issue Date 7001 101 1
10-Dec-06 7002 102 2 25-Dec-06 7003 104 1 15-Jan-06 7004 101 1 04-Jul-06 7005 104 2 15-Nov-06
7006 101 3 18-Feb-06
mysql> insert into issue (lib_issue_id, book_no, member_id, issue_date)
-> values (7001, 101, 1, '2006-12-10');
Query OK, 1 row affected (0.24 sec)
mysql>
mysql> insert into issue (lib_issue_id, book_no, member_id, issue_date)
-> values (7002, 102, 2, '2006-12-25');
Query OK, 1 row affected (0.15 sec)
mysql>
mysql> insert into issue (lib_issue_id, book_no, member_id, issue_date)
-> values (7003, 104, 1, '2006-01-15');
Query OK, 1 row affected (0.23 sec)
mysql>
mysql> insert into issue (lib_issue_id, book_no, member_id, issue_date)
-> values (7004, 101, 1, '2006-07-04');
Query OK, 1 row affected (0.05 sec)
mysql>
mysql> insert into issue (lib_issue_id, book_no, member_id, issue_date)
-> values (7005, 104, 2, '2006-11-15');
Query OK, 1 row affected (0.07 sec)
mysql>
mysql> insert into issue (lib_issue_id, book_no, member_id, issue_date)
-> values (7006, 101, 3, '2006-02-18');
Query OK, 1 row affected (0.06 sec)
mysql> select*from issue;
+--------------+---------+-----------+------------+-------------+
| lib_issue_id | book_no | member_id | issue_date | return_date |
+--------------+---------+-----------+------------+-------------+
| 7001 | 101 | 1 | 2006-12-10 | NULL |
| 7002 | 102 | 2 | 2006-12-25 | NULL |
| 7003 | 104 | 1 | 2006-01-15 | NULL |
| 7004 | 101 | 1 | 2006-07-04 | NULL |
| 7005 | 104 | 2 | 2006-11-15 | NULL |
| 7006 | 101 | 3 | 2006-02-18 | NULL |
+--------------+---------+-----------+------------+-------------+
6 rows in set (0.00 sec)
25) Remove the constraints on Issue table
mysql> show create table issue;
+-------+---------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------+
| Table | Create Table
|
+-------+---------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------+
| issue | CREATE TABLE `issue` (
`lib_issue_id` int NOT NULL,
`book_no` int DEFAULT NULL,
`member_id` int DEFAULT NULL,
`issue_date` date DEFAULT NULL,
`return_date` date DEFAULT NULL,
KEY `book_no` (`book_no`),
KEY `member_id` (`member_id`),
CONSTRAINT `issue_ibfk_1` FOREIGN KEY (`book_no`) REFERENCES `books` (`book_no`),
CONSTRAINT `issue_ibfk_2` FOREIGN KEY (`member_id`) REFERENCES `member` (`Member_Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------+---------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------+
1 row in set (0.06 sec)
mysql> ALTER TABLE issue
-> fg ADD CONSTRAINT `issue_ibfk_1` FOREIGN KEY (`book_no`) REFERENCES `books` (`book_no`);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near 'fg ADD CONSTRAINT `issue_ibfk_1`
FOREIGN KEY (`book_no`) REFERENCES `books` (`bo' at line 2
mysql> ALTER TABLE issue
-> DROP FOREIGN KEY book_no,
-> DROP FOREIGN KEY member_id;
ERROR 1091 (42000): Can't DROP 'book_no'; check that column/key exists
mysql> ALTER TABLE issue
-> DROP FOREIGN KEY issue_ibfk_1;;
Query OK, 0 rows affected (0.18 sec)
Records: 0 Duplicates: 0 Warnings: 0
ERROR:
No query specified
mysql> alter table issue
-> DROP FOREIGN KEY issue_ibfk_2;
Query OK, 0 rows affected (0.20 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table issue;
+-------+---------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------+
| Table | Create Table
|
+-------+---------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------+
| issue | CREATE TABLE `issue` (
`lib_issue_id` int NOT NULL,
`book_no` int DEFAULT NULL,
`member_id` int DEFAULT NULL,
`issue_date` date DEFAULT NULL,
`return_date` date DEFAULT NULL,
KEY `book_no` (`book_no`),
KEY `member_id` (`member_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------+---------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
1) Insert a record in Issue table. The member_id should not exist in member table.
member table.
mysql> insert into issue(lib_issue_id,book_no,member_id,issue_date,return_date)
-> values(4,104,1004,'2023-04-11','2023-04-15');
Query OK, 1 row affected (0.17 sec)
mysql> select *from issue
-> ;
+--------------+---------+-----------+------------+-------------+
| lib_issue_id | book_no | member_id | issue_date | return_date |
+--------------+---------+-----------+------------+-------------+
| 7001 | 101 | 1 | 2006-12-10 | NULL |
| 7002 | 102 | 2 | 2006-12-25 | NULL |
| 7003 | 104 | 1 | 2006-01-15 | NULL |
| 7004 | 101 | 1 | 2006-07-04 | NULL |
| 7005 | 104 | 2 | 2006-11-15 | NULL |
| 7006 | 101 | 3 | 2006-02-18 | NULL |
| 4 | 104 | 1004 | 2023-04-11 | 2023-04-15 |
+--------------+---------+-----------+------------+-------------+
7 rows in set (0.00 sec)
mysql>
2) Now enable the constraints of Issue table. Observe the error
mysql> ALTER TABLE issue
-> ADD CONSTRAINT `fk_issue_books` FOREIGN KEY (`book_no`) REFERENCES `books`
(`book_no`);
Query OK, 7 rows affected (1.20 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql> ALTER TABLE issue
-> ADD CONSTRAINT `fk_issue_member` FOREIGN KEY (`member_id`) REFERENCES
`member` (`member_id`);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails
(`sample`.`#sql-700_8`, CONSTRAINT `fk_issue_member` FOREIGN KEY (`member_id`)
REFERENCES `member` (`Member_Id`))
mysql>
3) Delete the record inserted at Q-27) and enable the constraints.
mysql> delete from issue where member_id=1004;
Query OK, 1 row affected (0.15 sec)
mysql> select * from issue;
+--------------+---------+-----------+------------+-------------+
| lib_issue_id | book_no | member_id | issue_date | return_date |
+--------------+---------+-----------+------------+-------------+
| 7001 | 101 | 1 | 2006-12-10 | NULL |
| 7002 | 102 | 2 | 2006-12-25 | NULL |
| 7003 | 104 | 1 | 2006-01-15 | NULL |
| 7004 | 101 | 1 | 2006-07-04 | NULL |
| 7005 | 104 | 2 | 2006-11-15 | NULL |
| 7006 | 101 | 3 | 2006-02-18 | NULL |
+--------------+---------+-----------+------------+-------------+
6 rows in set (0.00 sec)
mysql>
29) Try to delete the record of member id 1 from member table and observe the error .
mysql> select*from member;
+-----------+---------------+----------------+---------------+-----------------+-----------+-------------------+----------------
+
| Member_Id | member_name | Member_address | acc_open_date | Membership_type |
Fees_paid | Max_Books_Allowed | Penalty_Amount |
+-----------+---------------+----------------+---------------+-----------------+-----------+-------------------+----------------
+
| 1 | Richa Sharma | pune | 2005-12-10 | Lifetime | 25000 | 5|
50.00 |
| 2 | Garima Sen | pune | 2023-12-26 | Annual | 1000 | 3| NULL
|
| 3 | John Doe | New York | 2022-01-15 | Annual | 1200 | 4|
20.00 |
| 4 | Alice Smith | London | 2022-02-20 | Half Yearly | 800 | 2|
10.00 |
| 5 | Bob Johnson | Paris | 2022-03-25 | Quarterly | 500 | 3| NULL
|
| 6 | Emma White | Sydney | 2022-04-30 | Lifetime | 30000 | 6|
25.00 |
| 7 | Michael Brown | Toronto | 2022-05-05 | Annual | 1500 | 5|
NULL |
| 8 | John Doe | Cityville | 2023-01-01 | Annual | 1200 | 110 | 0.00
|
+-----------+---------------+----------------+---------------+-----------------+-----------+-------------------+----------------
+
8 rows in set (0.00 sec)
mysql> delete from member where member_id=1;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails
(`sample`.`issue`, CONSTRAINT `issue_ibfk_1` FOREIGN KEY (`member_id`) REFERENCES `member`
(`Member_Id`))
mysql>
30) View the data and structure of all the three tables Member, Issue, Book.
mysql> desc member;
+-------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+-------+
| Member_Id | int | NO | PRI | NULL | |
| member_name | varchar(20) | YES | | NULL | |
| Member_address | varchar(50) | YES | | NULL | |
| acc_open_date | date | YES | | NULL | |
| Membership_type | varchar(20) | YES | | NULL | |
| Fees_paid | int | YES | | NULL | |
| Max_Books_Allowed | int | YES | | NULL | |
| Penalty_Amount | decimal(7,2) | YES | | NULL | |
+-------------------+--------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
mysql> desc issue;
+--------------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------+------+-----+---------+-------+
| lib_issue_id | int | NO | | NULL | |
| book_no | int | YES | MUL | NULL | |
| member_id | int | YES | MUL | NULL | |
| issue_date | date | YES | | NULL | |
| return_date | date | YES | | NULL | |
+--------------+------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> desc books;
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| book_no | int | NO | PRI | NULL | |
| book_name | varchar(30) | NO | | NULL | |
| author_name | varchar(30) | YES | | NULL | |
| cost | decimal(7,2) | YES | | NULL | |
| category | varchar(30) | YES | | NULL | |
+-------------+--------------+------+-----+---------+-------+
5 rows in set (0.10 sec)
mysql> select *from member;
+-----------+---------------+----------------+---------------+-----------------+-----------+-------------------+----------------
+
| Member_Id | member_name | Member_address | acc_open_date | Membership_type |
Fees_paid | Max_Books_Allowed | Penalty_Amount |
+-----------+---------------+----------------+---------------+-----------------+-----------+-------------------+----------------
+
| 1 | Richa Sharma | pune | 2005-12-10 | Lifetime | 25000 | 5|
50.00 |
| 2 | Garima Sen | pune | 2023-12-26 | Annual | 1000 | 3| NULL
|
| 3 | John Doe | New York | 2022-01-15 | Annual | 1200 | 4|
20.00 |
| 4 | Alice Smith | London | 2022-02-20 | Half Yearly | 800 | 2|
10.00 |
| 5 | Bob Johnson | Paris | 2022-03-25 | Quarterly | 500 | 3| NULL
|
| 6 | Emma White | Sydney | 2022-04-30 | Lifetime | 30000 | 6|
25.00 |
| 7 | Michael Brown | Toronto | 2022-05-05 | Annual | 1500 | 5|
NULL |
| 8 | John Doe | Cityville | 2023-01-01 | Annual | 1200 | 110 | 0.00
|
+-----------+---------------+----------------+---------------+-----------------+-----------+-------------------+----------------
+
8 rows in set (0.00 sec)
mysql> select *from issue;
+--------------+---------+-----------+------------+-------------+
| lib_issue_id | book_no | member_id | issue_date | return_date |
+--------------+---------+-----------+------------+-------------+
| 7001 | 101 | 1 | 2006-12-10 | NULL |
| 7002 | 102 | 2 | 2006-12-25 | NULL |
| 7003 | 104 | 1 | 2006-01-15 | NULL |
| 7004 | 101 | 1 | 2006-07-04 | NULL |
| 7005 | 104 | 2 | 2006-11-15 | NULL |
| 7006 | 101 | 3 | 2006-02-18 | NULL |
+--------------+---------+-----------+------------+-------------+
6 rows in set (0.00 sec)
mysql> select*from books
-> ;
+---------+---------------------------+---------------+--------+----------+
| book_no | book_name | author_name | cost | category |
+---------+---------------------------+---------------+--------+----------+
| 101 | Let us C | Denis Ritchie | 450.00 | system |
| 102 | Oracle - Complete Ref | Loni | 550.00 | database |
| 103 | Mastering SQL | Loni | 250.00 | database |
| 104 | PL SQL-Ref | Scott Urman | 750.00 | database |
| 107 | Introduction to Databases | Eva Miller | 280.00 | Database |
+---------+---------------------------+---------------+--------+----------+
5 rows in set (0.00 sec)
31) Modify the Return_Date of 7004,7005 to 15 days after the Issue_date.
mysql> UPDATE issue
-> SET return_date = DATE_ADD(issue_date, INTERVAL 15 DAY)
-> WHERE lib_issue_id = 7004;
Query OK, 1 row affected (0.14 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> UPDATE issue
-> set return_date=date_add(issue_date,interval 15 day)
-> ^C
mysql> UPDATE issue
-> set return_date=date_add(issue_date,interval 15 day)
-> where lib_issue_id=7005;
Query OK, 1 row affected (0.07 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select*from issue;
+--------------+---------+-----------+------------+-------------+
| lib_issue_id | book_no | member_id | issue_date | return_date |
+--------------+---------+-----------+------------+-------------+
| 7001 | 101 | 1 | 2006-12-10 | NULL |
| 7002 | 102 | 2 | 2006-12-25 | NULL |
| 7003 | 104 | 1 | 2006-01-15 | NULL |
| 7004 | 101 | 1 | 2006-07-04 | 2006-07-19 |
| 7005 | 104 | 2 | 2006-11-15 | 2006-11-30 |
| 7006 | 101 | 3 | 2006-02-18 | NULL |
+--------------+---------+-----------+------------+-------------+
6 rows in set (0.00 sec)
mysql>
32) Remove all the records from Issue table where member_ID is 1 and Issue date in before 10-Dec-
06.
mysql> select*from issue;
+--------------+---------+-----------+------------+-------------+
| lib_issue_id | book_no | member_id | issue_date | return_date |
+--------------+---------+-----------+------------+-------------+
| 7001 | 101 | 1 | 2006-12-10 | NULL |
| 7002 | 102 | 2 | 2006-12-25 | NULL |
| 7003 | 104 | 1 | 2006-01-15 | NULL |
| 7004 | 101 | 1 | 2006-07-04 | 2006-07-19 |
| 7005 | 104 | 2 | 2006-11-15 | 2006-11-30 |
| 7006 | 101 | 3 | 2006-02-18 | NULL |
+--------------+---------+-----------+------------+-------------+
6 rows in set (0.00 sec)
mysql> delete from issue
-> where member_id=1 and issue_date<'2006-12-10';
Query OK, 2 rows affected (0.23 sec)
mysql> select*from issue;
+--------------+---------+-----------+------------+-------------+
| lib_issue_id | book_no | member_id | issue_date | return_date |
+--------------+---------+-----------+------------+-------------+
| 7001 | 101 | 1 | 2006-12-10 | NULL |
| 7002 | 102 | 2 | 2006-12-25 | NULL |
| 7005 | 104 | 2 | 2006-11-15 | 2006-11-30 |
| 7006 | 101 | 3 | 2006-02-18 | NULL |
+--------------+---------+-----------+------------+-------------+
4 rows in set (0.00 sec)
mysql>
33) Remove all the records from Book table with category other than RDBMS and Database.
mysql> select * from books;
+---------+---------------------------+---------------+--------+----------+
| book_no | book_name | author_name | cost | category |
+---------+---------------------------+---------------+--------+----------+
| 101 | Let us C | Denis Ritchie | 450.00 | system |
| 102 | Oracle - Complete Ref | Loni | 550.00 | database |
| 103 | Mastering SQL | Loni | 250.00 | database |
| 104 | PL SQL-Ref | Scott Urman | 750.00 | database |
| 107 | Introduction to Databases | Eva Miller | 280.00 | Database |
+---------+---------------------------+---------------+--------+----------+
5 rows in set (0.00 sec)
mysql> delete from books
-> where category not in ('RDBMS','database');
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails
(`sample`.`issue`, CONSTRAINT `fk_issue_books` FOREIGN KEY (`book_no`) REFERENCES `books`
(`book_no`))
mysql> DELETE FROM issue WHERE book_no IN (SELECT book_no FROM books WHERE category NOT
IN ('RDBMS', 'database'));
Query OK, 2 rows affected (0.17 sec)
mysql> select * from books;
+---------+---------------------------+---------------+--------+----------+
| book_no | book_name | author_name | cost | category |
+---------+---------------------------+---------------+--------+----------+
| 101 | Let us C | Denis Ritchie | 450.00 | system |
| 102 | Oracle - Complete Ref | Loni | 550.00 | database |
| 103 | Mastering SQL | Loni | 250.00 | database |
| 104 | PL SQL-Ref | Scott Urman | 750.00 | database |
| 107 | Introduction to Databases | Eva Miller | 280.00 | Database |
+---------+---------------------------+---------------+--------+----------+
5 rows in set (0.00 sec)
mysql>
34) Remove the table Member.
mysql> DROP TABLE member;
Query OK, 0 rows affected (0.59 sec)
mysql> desc member;
ERROR 1146 (42S02): Table 'sample.member' doesn't exist
mysql>
35) Remove the table Book
mysql> drop table books
-> ;
Query OK, 0 rows affected (0.41 sec)
mysql> desc books;
ERROR 1146 (42S02): Table 'sample.books' doesn't exist
mysql>