REFUND MySQL query - mysql

I currently have 5 tables in my DB
branch, customers, orderline, orders, products
I want to be able to refund an order through its orderid
refund meaning:
setting the orders total price back to 0.00,
each orderlines total price to 0.00,
adding the quantitys back from the orderline to the products,
aswell as having the orders orderstatus to RFND
heres my database so far
DROP DATABASE IF EXISTS MMWally;
CREATE DATABASE IF NOT EXISTS MMWally;
USE MMWally;
--
-- Definition of table `categories`
--
DROP TABLE IF EXISTS `products`;
CREATE TABLE `products`
(
`ProductID` int NOT NULL,
`ProductName` varchar(50) NOT NULL,
`wPrice` double(13, 2) NOT NULL,
`Stock` int NOT NULL,
`BranchID` int NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `customers`;
CREATE TABLE `customers` (
`CustomerID` int NOT NULL,
`FirstName` tinytext NOT NULL,
`LastName` tinytext NOT NULL,
`Phone` VARCHAR(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `branch`;
CREATE TABLE `branch`
(
`BranchID` int NOT NULL,
`BranchName` tinytext DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `orders`;
CREATE TABLE `orders`
(
`OrderID` int NOT NULL,
`CustomerID` int NOT NULL,
`BranchID` int NOT NULL,
`OrderDate` date NOT NULL,
`sPrice` double(13, 2) DEFAULT NULL,
`Status` tinytext NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `orderline`;
CREATE TABLE `orderline`
(
`OrderID` int NOT NULL,
`OrderLineID` int NOT NULL,
`ProductID` int NOT NULL,
`ItemQuantity` int NOT NULL,
`TotalPrice` double(13, 2)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE orders MODIFY OrderID INT AUTO_INCREMENT PRIMARY KEY;
ALTER TABLE orderline MODIFY OrderLineID INT AUTO_INCREMENT PRIMARY KEY;
ALTER TABLE branch MODIFY BranchID INT AUTO_INCREMENT PRIMARY KEY;
ALTER TABLE customers MODIFY CustomerID INT AUTO_INCREMENT PRIMARY KEY;
ALTER TABLE products MODIFY ProductID INT AUTO_INCREMENT PRIMARY KEY;
/* FOREIGN KEYS */
ALTER TABLE orders ADD CONSTRAINT FOREIGN KEY (CustomerID) REFERENCES customers (CustomerID);
ALTER TABLE orders ADD CONSTRAINT FOREIGN KEY (BranchID) REFERENCES branch (BranchID);
ALTER TABLE products ADD CONSTRAINT FOREIGN KEY (BranchID) REFERENCES branch (BranchID);
ALTER TABLE orderline ADD CONSTRAINT FOREIGN KEY (OrderID) REFERENCES orders (OrderID);
ALTER TABLE orderline ADD CONSTRAINT FOREIGN KEY (ProductID) REFERENCES products (ProductID);
ALTER TABLE orders ADD CONSTRAINT CHECK (`Status` = 'PAID' OR `Status` = 'RFND');
/*!40000 ALTER TABLE `branch` DISABLE KEYS */;
INSERT INTO `branch` (`BranchID`, `BranchName`) VALUES
(1, 'Sports World'),
(2, 'Waterloo'),
(3, 'Cambridge Mall');
/*!40000 ALTER TABLE `branch` ENABLE KEYS */;
/*!40000 ALTER TABLE `customers` DISABLE KEYS */;
INSERT INTO `customers` (`CustomerID`, `FirstName`, `LastName`, `Phone`) VALUES
(1, 'Carlo', 'Sgro', 5195550000),
(2, 'Norbert', 'Mika', 4165551111),
(3, 'Russell', 'Foubert', 5195552222),
(4, 'Sean', 'Clarke', 5195553333),
(5, 'Sean', 'Clarke', 5192395285);
/*!40000 ALTER TABLE `customers` ENABLE KEYS */;
/*!40000 ALTER TABLE `products` DISABLE KEYS */;
INSERT INTO `products` (`ProductID`,`ProductName`, `wPrice`, `Stock`, `BranchID`) VALUES
(1, 'Disco Queen Wallpaper (roll)', 12.95, 56, 1),
(2, 'Countryside Wallpaper (roll)', 11.95, 24, 1),
(3, 'Drywall Tape (roll)', 3.95, 120, 2),
(4, 'Drywall Tape (pkg 10)', 36.95, 30, 2),
(5, 'Drywall Repair Compound (tube)', 6.95, 90, 3),
(6, 'Victorian Lace Wallpaper (roll)', 14.95, 44, 3);
/*!40000 ALTER TABLE `products` ENABLE KEYS */;
/*!40000 ALTER TABLE `orders` DISABLE KEYS */;
INSERT INTO orders(CustomerID, BranchID, OrderDate, sPrice, Status) VALUES
(1, 1, '2019-07-20', 20.93, 'PAID'),
(2, 1, '2019-07-20', 9.73, 'PAID'),
(3, 1, '2019-07-20', 5.53, 'PAID'),
(4, 3, '2019-10-06', 16.73, 'PAID'),
(4, 2, '2019-11-02', 18.13, 'PAID'),
(3, 2, '2019-11-02', 5.53, 'PAID'),
(2, 2, '2019-11-02', 18.13, 'RFND'),
(1, 2, '2019-11-02', 5.53, 'RFND');
/*!40000 ALTER TABLE `orders` ENABLE KEYS */;

Related

how to gather data from all the six tabels in mysql

I tried Inner join, however i m not able to figure out what is wrong i m doing.
How can I query data from all the 6 tables in the blue box.
The studentprofile table is what is the link between the other 5 tables in the blue region.
I ran this query and this showed me all the data.
select * from users, roles_assigned,studentprofile,schoolwithusers;
Problem I am facing is that I need only filted data for user who are students and school name and student profile
Please Help.
here is my sql code.
--
-- Database: `onlinemarksheets`
--
-- --------------------------------------------------------
--
-- Table structure for table `exams`
--
CREATE TABLE `exams` (
`id` int(11) NOT NULL,
`examtye_id` int(11) DEFAULT NULL,
`schooluser_id` int(11) DEFAULT NULL,
`duration_to` date DEFAULT NULL,
`duration_from` date DEFAULT NULL,
`year` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `examtype`
--
CREATE TABLE `examtype` (
`id` int(11) NOT NULL,
`type` enum('Mid-Term','Half-yearly','Yearly') DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `examtype`
--
INSERT INTO `examtype` (`id`, `type`) VALUES
(1, 'Mid-Term'),
(2, 'Half-yearly'),
(3, 'Yearly');
-- --------------------------------------------------------
--
-- Table structure for table `marks`
--
CREATE TABLE `marks` (
`id` int(11) NOT NULL,
`exam_id` int(11) DEFAULT NULL,
`name` varchar(45) DEFAULT NULL,
`marks_obtained` int(11) DEFAULT NULL,
`marks_total` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `roles`
--
CREATE TABLE `roles` (
`id` int(11) NOT NULL,
`type` enum('Admin','Teacher','Student') DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `roles`
--
INSERT INTO `roles` (`id`, `type`) VALUES
(1, 'Admin'),
(2, 'Teacher'),
(3, 'Student');
-- --------------------------------------------------------
--
-- Table structure for table `roles_assigned`
--
CREATE TABLE `roles_assigned` (
`id` int(11) NOT NULL,
`role_id` int(11) DEFAULT NULL,
`user_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `roles_assigned`
--
INSERT INTO `roles_assigned` (`id`, `role_id`, `user_id`) VALUES
(1, 1, 1),
(2, 2, 2),
(3, 3, 3),
(4, 3, 4),
(5, 3, 5),
(6, 3, 6),
(7, 1, 1),
(8, 2, 2),
(9, 3, 3),
(10, 3, 4),
(11, 3, 5),
(12, 3, 6);
-- --------------------------------------------------------
--
-- Table structure for table `schools`
--
CREATE TABLE `schools` (
`id` int(11) NOT NULL,
`schoolname` varchar(45) DEFAULT NULL,
`school_email` varchar(45) DEFAULT NULL,
`school_phone` varchar(45) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `schools`
--
INSERT INTO `schools` (`id`, `schoolname`, `school_email`, `school_phone`) VALUES
(1, 'D.A.V Public school', 'info#davschool.com', '789456123'),
(2, 'saraswati Public school', 'info#saraswatischool.com', '9998887774'),
(3, 'S.G.R.R Public School', 'Info#sgrr.com', '54245645125'),
(4, 'Sun Valley Public', 'info#sunvalley.com', '23423423424'),
(5, 'Marshal Public school', 'info#marshalschool.com', '23482728347');
-- --------------------------------------------------------
--
-- Table structure for table `schoolwithusers`
--
CREATE TABLE `schoolwithusers` (
`id` int(11) NOT NULL,
`school_id` int(11) DEFAULT NULL,
`studentprofile_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `schoolwithusers`
--
INSERT INTO `schoolwithusers` (`id`, `school_id`, `studentprofile_id`) VALUES
(1, 1, 3),
(2, 3, 4);
-- --------------------------------------------------------
--
-- Table structure for table `studentprofile`
--
CREATE TABLE `studentprofile` (
`id` int(11) NOT NULL,
`user_id_fk` int(11) DEFAULT NULL,
`rollno` int(11) DEFAULT NULL,
`dob` date DEFAULT NULL,
`attendence` int(11) DEFAULT NULL,
`class` int(11) DEFAULT NULL,
`section` enum('A','B') DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `studentprofile`
--
INSERT INTO `studentprofile` (`id`, `user_id_fk`, `rollno`, `dob`, `attendence`, `class`, `section`) VALUES
(1, 3, 22, '2022-01-21', 60, 1, 'A'),
(2, 4, 21, '2012-01-04', 45, 1, 'A'),
(3, 4, 1, '2012-01-04', 100, 3, 'B'),
(4, 5, 30, '2007-04-26', 45, 3, 'B'),
(5, 6, 2, '2022-01-19', 50, 6, 'B');
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE `users` (
`id` int(11) NOT NULL,
`fname` varchar(45) DEFAULT NULL,
`lname` varchar(45) DEFAULT NULL,
`email` varchar(45) DEFAULT NULL,
`password` varchar(45) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`id`, `fname`, `lname`, `email`, `password`) VALUES
(1, 'Shashank', 'Naithani', 'shashank8036#gmail.com', 'lol123'),
(2, 'Kuldeep', 'Negi', 'negikuldeep#gmail.com', 'lop123'),
(3, 'Arpit', 'Thakut', 'Aptha#gmail.com', 'arp123'),
(4, 'Ankit', 'Barthwal', 'ankitbarth#gmail.com', 'ankit123'),
(5, 'Mukesh', 'Thakur', 'sasdb#gmail.com', 'sha123'),
(6, 'Arjun', 'Negi', 'sasdb#gmail.com', 'sha123');
--
-- Indexes for dumped tables
--
--
-- Indexes for table `exams`
--
ALTER TABLE `exams`
ADD PRIMARY KEY (`id`),
ADD KEY `examtye_id` (`examtye_id`) USING BTREE,
ADD KEY `schooluser_id` (`schooluser_id`);
--
-- Indexes for table `examtype`
--
ALTER TABLE `examtype`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `marks`
--
ALTER TABLE `marks`
ADD PRIMARY KEY (`id`),
ADD KEY `exam_id` (`exam_id`);
--
-- Indexes for table `roles`
--
ALTER TABLE `roles`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `roles_assigned`
--
ALTER TABLE `roles_assigned`
ADD PRIMARY KEY (`id`),
ADD KEY `role_id` (`role_id`) USING BTREE,
ADD KEY `user_id` (`user_id`) USING BTREE;
--
-- Indexes for table `schools`
--
ALTER TABLE `schools`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `schoolwithusers`
--
ALTER TABLE `schoolwithusers`
ADD PRIMARY KEY (`id`),
ADD KEY `school_id` (`school_id`) USING BTREE,
ADD KEY `studentprofile_id` (`studentprofile_id`);
--
-- Indexes for table `studentprofile`
--
ALTER TABLE `studentprofile`
ADD PRIMARY KEY (`id`),
ADD KEY `user_id_idx` (`user_id_fk`);
--
-- Indexes for table `users`
--
ALTER TABLE `users`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `exams`
--
ALTER TABLE `exams`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `examtype`
--
ALTER TABLE `examtype`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
--
-- AUTO_INCREMENT for table `marks`
--
ALTER TABLE `marks`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `roles`
--
ALTER TABLE `roles`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
--
-- AUTO_INCREMENT for table `roles_assigned`
--
ALTER TABLE `roles_assigned`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=13;
--
-- AUTO_INCREMENT for table `schools`
--
ALTER TABLE `schools`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
--
-- AUTO_INCREMENT for table `schoolwithusers`
--
ALTER TABLE `schoolwithusers`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
--
-- AUTO_INCREMENT for table `studentprofile`
--
ALTER TABLE `studentprofile`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `exams`
--
ALTER TABLE `exams`
ADD CONSTRAINT `examtye_id` FOREIGN KEY (`examtye_id`) REFERENCES `examtype` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `schooluser_id` FOREIGN KEY (`schooluser_id`) REFERENCES `schoolwithusers` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
--
-- Constraints for table `marks`
--
ALTER TABLE `marks`
ADD CONSTRAINT `exam_id` FOREIGN KEY (`exam_id`) REFERENCES `exams` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
--
-- Constraints for table `roles_assigned`
--
ALTER TABLE `roles_assigned`
ADD CONSTRAINT `role_id` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
--
-- Constraints for table `schoolwithusers`
--
ALTER TABLE `schoolwithusers`
ADD CONSTRAINT `school_id` FOREIGN KEY (`school_id`) REFERENCES `schools` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `studentprofile_id` FOREIGN KEY (`studentprofile_id`) REFERENCES `studentprofile` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
--
-- Constraints for table `studentprofile`
--
ALTER TABLE `studentprofile`
ADD CONSTRAINT `user_id_fk` FOREIGN KEY (`user_id_fk`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
COMMIT;
You have to join each table on the primary key/foreign key. Some of the sample data is not quite correct because you have duplicate combinations of student_id and roles_id values in the roles_assigned table. Be sure to list the column names you need instead of *.
select *
from users
INNER JOIN roles_assigned ON users.id = roles_assigned.user_id
INNER JOIN roles ON roles.id = roles_assigned.role_id
INNER JOIN studentprofile on studentprofile.id = users.id
INNER JOIN schoolwithusers on schoolwithusers.studentprofile_id =
studentprofile.id
INNER JOIN schools ON schools.id = schoolwithusers.school_id
WHERE roles.type = 'Student';

Query Data from multiple sql tables

I have an ERD above. I want to get the price of the room based on channel and also the status (isInvisible) of the hotel that owns the room mentioned.
Also a RESTful-API endpoint for that, I tried many times on this assignment and can't get it right as I use Nodejs to write an API GET /api/${roomId}/price but the query doesn't work:
SELECT RoomPrice.price, Status.isInvisible
FROM RoomPrice
INNER JOIN Status
ON (RoomPrice.RoomID = Room.Id AND RoomPrice.ChannelID = ChannelId)
AND (Status.HotelID = Hotels.ID AND Status.ChannelID = ChannelID)
I use query below to create my database in WorkBench:
DROP TABLE IF EXISTS `address`;
CREATE TABLE `address` (
`id` int(9) unsigned NOT NULL AUTO_INCREMENT,
`hotel_id` int(9) unsigned NOT NULL,
`address` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY(hotel_id) REFERENCES hotels(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `address` (`hotel_id`, `address`)
VALUES
(1, '7008 Lynch Centers Apt. 596\nLysannemouth, RI 43355'),
(2, '04795 Stanley Mount Apt. 114\nDorrisborough, DC 38070-3542'),
(1, '24586 Eliseo Haven Suite 045\nKossville, WY 17890-7936'),
(2, '639 Toy Corners\nBashirianfort, CA 08964-7258');
DROP TABLE IF EXISTS `channels`;
CREATE TABLE `channels` (
`id` int(9) unsigned NOT NULL AUTO_INCREMENT,
`url` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
INSERT INTO `channels` (`id`, `url`, `name`)
VALUES
(1, 'http://www.beahan.com/', 'quod'),
(2, 'http://www.douglas.com/', 'sit');
DROP TABLE IF EXISTS `hotels`;
CREATE TABLE `hotels` (
`id` int(9) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
INSERT INTO `hotels` (`id`, `name`)
VALUES
(1, 'illum'),
(2, 'aliquid');
DROP TABLE IF EXISTS `rooms`;
CREATE TABLE `rooms` (
`id` int(9) unsigned NOT NULL AUTO_INCREMENT,
`hotel_id` int(9) unsigned NOT NULL,
`name` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY(hotel_id) REFERENCES hotels(id)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
INSERT INTO `rooms` (`id`, `hotel_id`, `name`)
VALUES
(1, 1, 'vel'),
(2, 2, 'fugit'),
(3, 1, 'doloribus'),
(4, 2, 'ut'),
(5, 1, 'et');
DROP TABLE IF EXISTS `room_prices`;
CREATE TABLE `room_prices` (
`room_id` int(9) unsigned NOT NULL,
`channel_id` int(9) unsigned NOT NULL,
`price` decimal(10,2) NOT NULL,
PRIMARY KEY (room_id, channel_id),
FOREIGN KEY(room_id) REFERENCES rooms(id),
FOREIGN KEY(channel_id) REFERENCES channels(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `room_prices` (`room_id`, `channel_id`, `price`)
VALUES
(1, 1, '50687.86'),
(1, 2, '6687.86'),
(2, 1, '10687.86'),
(2, 2, '274739.20'),
(3, 1, '3828.63'),
(3, 2, '12525.86'),
(4, 1, '2623587.86'),
(4, 2, '125151.00'),
(5, 1, '2358704.85'),
(5, 2, '7347473.86');
DROP TABLE IF EXISTS `status`;
CREATE TABLE `status` (
`hotel_id` int(9) unsigned NOT NULL,
`channel_id` int(9) unsigned NOT NULL,
`isInvisible` BOOLEAN NOT NULL,
PRIMARY KEY (hotel_id, channel_id),
FOREIGN KEY(hotel_id) REFERENCES hotels(id),
FOREIGN KEY(channel_id) REFERENCES channels(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `status` (`hotel_id`, `channel_id`, `isInvisible`)
VALUES
(1, 1, false),
(2, 1, true),
(1, 2, true),
(2, 2, true);
I want that once I query to search for the price of roomId (i.e 1) it returns 2 rows (as the mock data only have 2 rows in channels table) that show:
room_id channel_id price isInvisible
1 1 xxxx.xx 0
1 2 xxxx.xx 1
At this moment, I use the query as DRapp help
SELECT
rp.room_id,
rp.channel_id,
rp.price,
s.isInvisible
FROM
room_prices rp
JOIN status s
ON (rp.channel_id = s.channel_id)
JOIN rooms r
ON (rp.room_id = r.id)
JOIN hotels h
ON (r.hotel_id = h.id)
WHERE rp.room_id = 1
It returns 4 rows (instead of 2 rows as expected)
room_id channel_id price isInvisible
1 1 xxxx.xx 0
1 2 xxxx.xx 1
1 1 xxxx.xx 1
1 2 xxxx.xx 1
You need to identify each of the relations as your ERD shows. Each table is joined to its respective context. Dont jump/skip. The only time you can in this scenario is to skip through the channels since the RoomPrice and Status table EACH have a "ChannelId" to qualify the join
SELECT
rp.price,
s.isInvisible
FROM
Room_Prices rp
JOIN Rooms r
on rp.room_id = r.id
JOIN Status s
ON rp.Channel_ID = s.Channel_Id
AND r.hotel_id = s.hotel_id
WHERE
rp.room_id = 1
I had to revise the query. I looked deeper and noticed your CHANNEL table ALSO had the Hotel, so I had to go from the room prices to the room table. From the room table, I can get the hotel. Now that I have the channel from room prices, AND the hotel ID from the rooms table. So NOW I join to the status table on BOTH columns getting the expected single row per room you are expecting.

Composite foreign key updation failed

I am facing the issue in composite foreign key.
I have tried example, and tried with structure change too but still i am not succeed. I have trid in mysql server.
CREATE TABLE `parenttable` (
`ID` int(11) NOT NULL,
`status` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `parenttable` (`ID`, `status`) VALUES
(1, 1),
(2, 1),
(3, 1);
ALTER TABLE `parenttable`
ADD PRIMARY KEY (`ID`),
ADD KEY `ID` (`ID`,`status`);
ALTER TABLE `parenttable`
MODIFY `ID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
COMMIT;
CREATE TABLE `parenttable2` (
`ID` int(11) NOT NULL,
`status` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `parenttable2` (`ID`, `status`) VALUES
(1, 1),
(2, 1),
(3, 1);
ALTER TABLE `parenttable2`
ADD PRIMARY KEY (`ID`),
ADD KEY `ID` (`ID`,`status`);
ALTER TABLE `parenttable2`
MODIFY `ID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
COMMIT;
CREATE TABLE `childtable` (
`ID` int(11) NOT NULL,
`parent_id` int(11) NOT NULL,
`parent_id2` int(11) NOT NULL,
`status` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `childtable` (`ID`, `parent_id`, `parent_id2`, `status`) VALUES
(1, 1, 1, 1),
(3, 1, 1, 1),
(4, 2, 2, 1),
(5, 2, 3, 1),
(6, 1, 1, 1);
ALTER TABLE `childtable`
ADD PRIMARY KEY (`ID`),
ADD KEY `fk_childTable_parent_id` (`parent_id`,`status`),
ADD KEY `fk_childTable_parent_id2` (`parent_id2`,`status`);
ALTER TABLE `childtable`
MODIFY `ID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7;
ALTER TABLE `childtable`
ADD CONSTRAINT `fk_childTable_parent_id` FOREIGN KEY
(`parent_id`,`status`) REFERENCES `parenttable` (`ID`, `status`) ON DELETE
CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `fk_childTable_parent_id2` FOREIGN KEY
(`parent_id2`,`status`) REFERENCES `parenttable2` (`ID`, `status`) ON DELETE
CASCADE ON UPDATE CASCADE;
COMMIT;
I want to update all child rows "status" when parent is changed and single child "status" row too.
Please give me suggestion. Thank You.

what is the best query for tags and other related table

I want to build a query to be suitable for searching in the book.title and the tag.tag
I also want a suitable for counting rows for search query for pagination
Here is my db schema and data
-- Table structure for table `book`
CREATE TABLE IF NOT EXISTS `book` (
`book_id` int(11) NOT NULL,
`title` varchar(200) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
ALTER TABLE `book`
ADD PRIMARY KEY (`book_id`);
-- Dumping data for table `book`
INSERT INTO `book` (`book_id`, `title`) VALUES
(1, 'Alice Journey'),
(2, 'Rody Journey'),
(3, 'My Journey to Amsterdam'),
(4, 'How to train your ketty'),
(5, 'The red fox');
-- --------------------------------------------------------
-- Table structure for table `tag`
CREATE TABLE IF NOT EXISTS `tag` (
`tag_id` int(11) NOT NULL,
`tag` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
ALTER TABLE `tag`
ADD PRIMARY KEY (`tag_id`);
-- Dumping data for table `tag`
INSERT INTO `tag` (`tag_id`, `tag`) VALUES
(1, 'pets'),
(2, 'tale'),
(3, 'Journey');
-- --------------------------------------------------------
-- Table structure for table `tag_book`
CREATE TABLE IF NOT EXISTS `tag_book` (
`tag_id` int(11) NOT NULL,
`book_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `tag_book`
ADD PRIMARY KEY (`tag_id`,`book_id`);
-- Dumping data for table `tag_book`
INSERT INTO `tag_book` (`tag_id`, `book_id`) VALUES
(1, 4),
(2, 1),
(2, 2),
(2, 5),
(3, 1),
(3, 2),
(3, 3);
search = 'rody' // exists in book table
search = 'pets' // exists in tag tab
search = 'journey' // exists in book and tag tables
thanks

Vb.net error for query and database

Dim datetimepicker1 As String = Format(System.DateTime.Now, "yyyy-MM-dd HH:mm:ss")
Try
Dim cmd As MySqlCommand = New MySqlCommand
With cmd
.CommandText = "INSERT INTO tbl_product (`prod_name`,`prod_desc`, `cat_id`, `uom_id`,`uom_num`, `dept_id`, `brand_id`, `size_id`, `type_id`, `remarks`, `date`) values (#prod_name,#prod_desc,#cat_id,#uom_id,#uom_num,#dept_id,#brand_id,#size_id,#type_id,#remarks,#date)"
.Connection = SQLConnection
.CommandType = CommandType.Text
.Parameters.AddWithValue("#prod_name", TextBox1.Text)
.Parameters.AddWithValue("#prod_desc", TextBox2.Text)
.Parameters.AddWithValue("#cat_id", ComboBox1.Text)
.Parameters.AddWithValue("#uom_id", ComboBox2.Text)
.Parameters.AddWithValue("#uom_num", TextBox3.Text)
.Parameters.AddWithValue("#dept_id", ComboBox3.Text)
.Parameters.AddWithValue("#brand_id", ComboBox4.Text)
.Parameters.AddWithValue("#size_id", ComboBox5.Text)
.Parameters.AddWithValue("#type_id", ComboBox6.Text)
.Parameters.AddWithValue("#remarks", RichTextBox1.Text)
.Parameters.AddWithValue("#date", datetimepicker1)
.ExecuteNonQuery()
End With
MsgBox(" SIze Successfully added")
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
cannot add or update child row foreign key constraint fails this is my error
this is my table structure*************8
--
-- Table structure for table tbl_brand
CREATE TABLE IF NOT EXISTS tbl_brand (
brand_id int(11) NOT NULL AUTO_INCREMENT,
brand_name varchar(200) NOT NULL,
brand_desc varchar(200) DEFAULT NULL,
PRIMARY KEY (brand_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
--
-- Dumping data for table tbl_brand
INSERT INTO tbl_brand (brand_id, brand_name, brand_desc) VALUES
(1, 'Nike ', 'Nike Air '),
(2, 'Crocs ', 'Class A '),
(3, 'SafeGuard ', 'SafeGuard ');
--
-- Table structure for table tbl_category
CREATE TABLE IF NOT EXISTS tbl_category (
cat_id int(11) NOT NULL AUTO_INCREMENT,
cat_name varchar(200) NOT NULL,
cat_desc varchar(200) NOT NULL,
PRIMARY KEY (cat_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Dumping data for table tbl_category
INSERT INTO tbl_category (cat_id, cat_name, cat_desc) VALUES
(1, 'Bath Soap ', 'Bath Soap '),
(2, 'Detergent ', 'Detergent ');
--
-- Table structure for table tbl_dept
CREATE TABLE IF NOT EXISTS tbl_dept (
dept_id int(11) NOT NULL AUTO_INCREMENT,
dept_name varchar(200) NOT NULL,
dept_desc varchar(200) NOT NULL,
PRIMARY KEY (dept_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Dumping data for table tbl_dept
INSERT INTO tbl_dept (dept_id, dept_name, dept_desc) VALUES
(1, 'Shoes ', 'Shoes '),
(2, 'Soap ', 'Soap ');
--
-- Table structure for table tbl_product
CREATE TABLE IF NOT EXISTS tbl_product (
prod_id int(11) NOT NULL AUTO_INCREMENT,
prod_name varchar(200) NOT NULL,
prod_desc varchar(200) DEFAULT NULL,
cat_id int(11) NOT NULL,
dept_id int(11) NOT NULL,
brand_id int(11) NOT NULL,
type_id int(11) NOT NULL,
uom_id int(11) NOT NULL,
size_id int(11) NOT NULL,
date datetime NOT NULL,
remarks varchar(200) DEFAULT NULL,
uom_num int(60) DEFAULT NULL,
PRIMARY KEY (prod_id),
KEY tbl_product_ibfk_9 (type_id),
KEY tbl_product_ibfk_10 (uom_id),
KEY tbl_product_ibfk_11 (size_id),
KEY tbl_product_ibfk_12 (dept_id),
KEY tbl_product_ibfk_13 (cat_id),
KEY tbl_product_ibfk_14 (brand_id)
); ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Table structure for table tbl_size
CREATE TABLE IF NOT EXISTS tbl_size (
size_id int(11) NOT NULL AUTO_INCREMENT,
size_name varchar(100) NOT NULL,
PRIMARY KEY (size_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
--
-- Dumping data for table tbl_size
INSERT INTO tbl_size (size_id, size_name) VALUES
(1, 'Small(S) '),
(2, 'Medium(M) '),
(3, 'Large(L) ');
--
-- Table structure for table tbl_type
CREATE TABLE IF NOT EXISTS tbl_type (
type_id int(11) NOT NULL AUTO_INCREMENT,
type_name varchar(200) NOT NULL,
PRIMARY KEY (type_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;
--
-- Dumping data for table tbl_type
INSERT INTO tbl_type (type_id, type_name) VALUES
(1, 'BaskertBall Shoes '),
(2, 'Jersey Shorts '),
(3, 'running shoes '),
(8, 'Bath Soap ');
--
-- Table structure for table tbl_uom
CREATE TABLE IF NOT EXISTS tbl_uom (
uom_id int(11) NOT NULL AUTO_INCREMENT,
uom_name varchar(200) NOT NULL,
PRIMARY KEY (uom_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;
--
-- Dumping data for table tbl_uom
INSERT INTO tbl_uom (uom_id, uom_name) VALUES
(1, 'kilogram(kg) '),
(2, 'Gram(g) '),
(3, 'Milligram(Mg) '),
(4, 'Liter(L) '),
(5, 'Milliliters(ml) '),
(6, 'Pieces(pcs) '),
(7, 'foot(ft) ');
--
-- Table structure for table tbl_user
CREATE TABLE IF NOT EXISTS tbl_user (
user_id int(11) NOT NULL AUTO_INCREMENT,
user_code varchar(200) DEFAULT NULL,
user_password varchar(200) DEFAULT NULL,
user_name varchar(200) DEFAULT NULL,
user_level int(1) DEFAULT NULL,
datetime datetime NOT NULL,
com_code varchar(11) NOT NULL,
PRIMARY KEY (user_id),
UNIQUE KEY user_code (user_code)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
--
-- Dumping data for table tbl_user
INSERT INTO tbl_user (user_id, user_code, user_password, user_name, user_level, datetime, com_code) VALUES
(1, '1024', '1024', 'Vincent Dematera', 3, '2012-01-10 18:18:33', '001'),
(7, '14 ', '14', 'Megan Bueno', 1, '2012-10-30 21:56:14', '002'),
(8, '13', '13', 'Anonymous', 1, '2012-10-20 21:51:00', '002'),
(9, '9', '9', 'boom', 1, '0000-00-00 00:00:00', '003');
--
-- Constraints for dumped tables
--
-- Constraints for table tbl_product
ALTER TABLE tbl_product
ADD CONSTRAINT tbl_product_ibfk_9 FOREIGN KEY (type_id) REFERENCES tbl_type (type_id),
ADD CONSTRAINT tbl_product_ibfk_10 FOREIGN KEY (uom_id) REFERENCES tbl_uom (uom_id),
ADD CONSTRAINT tbl_product_ibfk_11 FOREIGN KEY (size_id) REFERENCES tbl_size (size_id),
ADD CONSTRAINT tbl_product_ibfk_12 FOREIGN KEY (dept_id) REFERENCES tbl_dept (dept_id),
ADD CONSTRAINT tbl_product_ibfk_13 FOREIGN KEY (cat_id) REFERENCES tbl_category (cat_id),
ADD CONSTRAINT tbl_product_ibfk_14 FOREIGN KEY (brand_id) REFERENCES tbl_brand (brand_id),
ADD CONSTRAINT tbl_product_ibfk_2 FOREIGN KEY (type_id) REFERENCES tbl_type (type_id),
ADD CONSTRAINT tbl_product_ibfk_3 FOREIGN KEY (uom_id) REFERENCES tbl_uom (uom_id),
ADD CONSTRAINT tbl_product_ibfk_4 FOREIGN KEY (size_id) REFERENCES tbl_size (size_id),
ADD CONSTRAINT tbl_product_ibfk_5 FOREIGN KEY (dept_id) REFERENCES tbl_dept (dept_id),
ADD CONSTRAINT tbl_product_ibfk_6 FOREIGN KEY (cat_id) REFERENCES tbl_category (cat_id),
ADD CONSTRAINT tbl_product_ibfk_7 FOREIGN KEY (brand_id) REFERENCES tbl_brand (brand_id);
SET FOREIGN_KEY_CHECKS=1;
You have a significant number of foreign key constraints for your table product. For example, any "size_id" you enter into table products HAS to correspond to a "size_id" in table size.
What your vb.net program is telling you is that one of the id values you are entering into table product does not exist in its corresponding table. My best guess would be that one of your id values (ie. size_id, uom_id, etc) is empty, blank, or zero.
You have a number of options - you can:
turn foreign key off for the table if you wish with SET foreign_key_checks = 0;
you can remove the foreign keys altogether
you can fix your code to ensure you are adhering to the foreign keys before you even attempt to insert a value