0% found this document useful (0 votes)
9 views6 pages

Database

Uploaded by

Ha Phuong Anh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Database

Uploaded by

Ha Phuong Anh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Teams

Câu 1 Select all ingredients which appear in more 3 dishes

Câu 2 Select all main dishes which have more than 5 ingredients

Count (ingredient_id)>=5

Câu 3 Insert new dish to the database


INSERT INTO Dishes (dish_name, price, availability, category_id)
VALUES
('Mango Sticky ', 9.00, true, 1);
Câu 4 Delete an ingredient from the database which has Id =1;
delete from dishingredients where ingredient_id=1;
delete from ingredients where ingredient_id=1;
Câu 5: Update the quantity of an ingredient in the database which has ID =10;

UPDATE ingredients
SET quantity = 100
WHERE ingredient_id = 10;

Individual
Câu 1 what is a view in database? Create a view of all Japanese origin dishes in your database
A database view is a searchable object in a database that is defined by a query.
CREATE VIEW japanese AS
SELECT dish_id, dish_name
FROM dishes
WHERE dish_id IN (
SELECT dish_id
FROM dishorigins
WHERE origin_id IN (
SELECT origin_id
FROM origins
WHERE origin_name = 'japan'
)
);
Cau 2
CREATE TABLE Chefs (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
salary DECIMAL(10, 2) NOT NULL
);

CREATE TABLE ChefDishes (


chef_id INT,
dish_id INT,
PRIMARY KEY (chef_id, dish_id),
FOREIGN KEY (chef_id) REFERENCES Chefs(id),
FOREIGN KEY (dish_id) REFERENCES Dishes(dish_id)
);

Câu 3
INSERT INTO Chefs (chef_name, salary) VALUES
('Chef John', 50000.00),
('Chef Mary', 45000.00);

INSERT INTO ChefDishes (chef_id, dish_id) VALUES


(1, 1), --
(1, 2), --
(2, 2); --
Câu 4 Define a functional dependency , pick a relation and describe all FDs in that table
In relational database theory, a functional dependency is a constraint between two sets of attributes in
a relation from a database

 Câu 5

ingredient_id → ingredient_name, import_date, expiry_date, counting_unit, quantity

Another functional dependency is:

ingredient_name → counting_unit

The table dishes above does not satisfy BCNF

Câu 6
Select all dishes with vietnam and france origin
SELECT *
FROM dishes
WHERE dish_id IN (
SELECT tb1.dish_id
FROM (
SELECT dish_id
FROM dishorigins
WHERE origin_id IN (
SELECT origin_id
FROM origins
WHERE origin_name = 'vietnam'
)
) AS tb1,
(
SELECT dish_id
FROM dishorigins
WHERE origin_id IN (
SELECT origin_id
FROM origins
WHERE origin_name = 'france'
)
) AS tb2
WHERE tb1.dish_id = tb2.dish_id
);
Câu 7
select all ingredients that is expired along with its quantity and unit
SELECT ingredient_name, quantity, counting_unit
FROM Ingredients
WHERE expiry_date < CURDATE();
Câu 8

select all ingredient required to make the dish with smallest ID which has japan origin

Câu 9
Select all desert (category) dishes which are not available right now
SELECT dish_name, price, category_name
FROM Dishes
JOIN DishCategory ON Dishes.category_id = DishCategory.category_id
WHERE availability = false AND category_name = 'Dessert';

câu 10 select sum of quantity for all ingredients with unit in kg


SELECT SUM(quantity)
FROM Ingredients
WHERE
counting_unit = 'kg';
câu 11 select all dishes with more than 5 ingredients
select *from dishes,
(select dish_id, count(ingredient_id)
from dishingredients
group by dish_id
having count(ingredient_id)>=5)as tb1
where tb1.dish_id=dishes.dish_id;

You might also like