Sei sulla pagina 1di 3

Student Name : Rasika Sampath Agampodi Index Number : 121002110 Database system for Malith Book Shop Matara

DBMS: MySql Version 5.5 Store details about books , authors ,customers , sales , Branches, sales . A separate table is created to store these details within the bookstore data base.

Use of Primary Keys Table author is created with primary key aut_id.
CREATE TABLE author(aut_id varchar(8) NOT NULL ,aut_name varchar(50) NOT NULL, country varchar(25) NOT NULL, home_city varchar(25) NOT NULL , PRIMARY KEY (aut_id));

To check whether this is effective an insert statement is executed to violate the PK constraint.

Use of Not Null constraint Book table has been created with book_price as NOT NULL

To check that constraint is working, following insert statement is executed with home_city value as null value.

Use of UNIQE constraint

Book table has created with isbn as UNIQE So the ISBN cant be duplicated.

Use of Cascade Statement

In the above MySQL statement a new table 'newpurchase' will be created. The PRIMARY KEY for that table 'newpurchase' is 'invoice_no'. The one FOREIGN KEY for the table 'newpurchase' is a combination of 'ord_no' and 'book_id'. Another FOREIGN KEY for the table 'newpurchase' is 'cate_id'. The 'ord_no' and 'book_id' combination is the PRIMARY KEY for the table 'neworder'. The 'cate_id' is the PRIMARY KEY for the table 'category'. The FOREIGN KEY 'ord_no' and 'book_id' combination for the table 'newpurchase', which points to the PRIMARY KEY 'ord_no' and 'book_id' combination of the table 'neworder'. That means the distinct ('ord_no' and 'book_id') combination which are present in the 'neworder' table only those unique 'order number' and 'book id' combination will come in the 'newpurchase' table. The another FOREIGN KEY 'cate_id' for the table 'newpurchase' , which points to the PRIMARY KEY 'cate_id' of the table 'category'. That means the 'cate_id' which are present in the 'category' table only those 'category' will come in the 'newpurchase' table. The ON UPDATE CASCADE ensures that the records inside the child table 'newpurchase' always points to the PRIMARY KEY inside the parent table 'neworder'. If any record gets deleted/updated from the 'neworder' table DBMS handles the deletion/updation of the records from 'newpurchase' table. ON DELETE RESTRICT prevents a record in a parent table 'neworder' being deleted or altered when it is still referenced from a child table 'newpurchase'.

Triggers
CREATE TRIGGER stockTrig BEFORE UPDATE ON books_sales FOR EACH ROW BEGIN IF NEW.quantity != OLD.quantity THEN INSERT INTO stock_log SET book_id = OLD. book_id, old_stock = OLD.quantity, new_stock = NEW.quantity, date = CURDATE() ,invoice_no=NEW.invoice_no;

This will ensure at each invoice the deduction in stock for auditing purposes.

Potrebbero piacerti anche