Uploaded by Mr Hero

Question-Bank-Final-Exam-Introduction-to-Database-Concepts

advertisement
Question Bank
Faculty of Engineering
Department of Computer Engineering
Course: Introduction to Database Concepts
Q: According to the given design answer the followings:
a. Create a database called ‘Cinema’ and create the
3 given tables inside the database (use
appropriate data types for each field).
b. Create appropriate relationships between the
tables.
c. Create appropriate constraints for the fields.
d. Permit the user to select only one value from a
list of selections for the seat_type and seat_status fields according to the followings:
- seat_type: (Normal, VIP)
- seat_status: (Available, Reserved)
Create Database Cinema;
Use Cinema;
Create Table Movie(
Movie_id int primary key auto-increment,
Movie_name varchar(40) not null,
Movie_date date not null,
Movie_time time not null,
Hall_id int not null,
Foreign key (Hall_id) references Hall (Hall_id)
On update cascade
On delete cascade
);
Create Table Hall(
Hall_id int primary key auto-increment,
No_of_seats int not null,
Seat_id int not null,
Question Bank
Faculty of Engineering
Department of Computer Engineering
Course: Introduction to Database Concepts
Foreign key (Seat_id) references Seat (Seat_id)
On update cascade
On delete cascade
);
Create Table Seats(
Seat_id int primary key auto-increment,
Row_no int not null,
Seat_type enum(‘normal’, ‘VIP’) not null,
Seat_status enum(‘Available’, ‘Reserved’) not null,
);
Q: Choose only one correct choice:
A. …………… statement is used to set value to variables after they are initialized.
(SELECT, SELECT INTO, UPDATE)
B. The …………… function adds two or more expressions together.
(Concat(), Trim(), Strcmp())
C. …………… can select a single value from a list of values.
(Enum, Set, Varchar)
D. Constraints are used to:
(Specify rules for data, Ensure the accuracy of data, Both of them)
E. Primary Key fields must contain …………… values and must not contain NULL values.
(Unique, Check, None of them)
F. …………… is just like a real table, but actually it is not.
(Procedure, View, Variable)
G. Columns in a database table are not must to have:
(names, data types, constraints)
Question Bank
Faculty of Engineering
Department of Computer Engineering
Course: Introduction to Database Concepts
Q: True or False
a.
b.
c.
d.
e.
f.
g.
Each column in a database table must have name, data type and constraint. False
Users can select only one value from a drop-down list by using Enum data type. True
SELECT INTO statement is used to show data inside tables. False
Procedures must have name and parameters but SQL statements inside it are optional. False
Case Statement goes through conditions and returns a value when the first condition is met. True
View is a virtual table based on result-set of an SQL statement. True
Cascades can be created using either Create Table statement or Alter Table statement. True
Q: Write purpose of using the following constraints briefly:
a.
b.
c.
d.
e.
f.
g.
h.
Not null
: enforces a column to NOT accept NULL values.
Primary key
: uniquely identifies each record in a table.
Check
: used to limit the value range that can be placed in a column.
Default
: used to provide a default value for a column.
Unique
: ensures that all values in a column are different.
Foreign key
: is a key used to link two tables together.
Not null
: enforces a column to NOT accept NULL values.
Auto-increment
: allows a unique number to be generated automatically when a new
record is inserted into a table.
Q: Create a table called ‘shirts’ with the following requirements:
a. First column called ‘id’.
b. Second column called ‘size’ in which the users can select (small, medium, large) sizes.
c. Third column called ‘quantity’.
Create table Shirts(
Id int,
Size enum(‘small’, ‘medium’, ‘large’),
Quantity int
);
Question Bank
Faculty of Engineering
Department of Computer Engineering
Course: Introduction to Database Concepts
Q: Create a case statement for the quantity column:
quantity (0) → Finished
quantity (1 - 10) → Few
quantity (11 – 50) → Normal
SELECT quantity,
CASE
WHEN quantity = 0 THEN ‘Finished’
WHEN quantity >= 1 AND quantity <= 10 THEN ‘Few’
WHEN quantity >= 11 AND quantity <= 50 THEN ‘Normal’
END
FROM shirts;
Question Bank
Faculty of Engineering
Department of Computer Engineering
Course: Introduction to Database Concepts
Q: Create a procedure for the balance column to output results according to each balance (calling procedure is
not required):
balance (0 – 5) → ‘should be refilled’
balance (6 – 100) → ‘normal'
balance (101 – 500) → ‘can be withdrawn'
DELIMITER &&
CREATE procedure fastpay_result( IN balance int, OUT result varchar(255) )
BEGIN
IF balance >= 50 AND balance <= 5 THEN
SET result = ‘should be refilled’;
ELSEIF balance >= 6 AND balance <= 100 THEN
SET result = ‘normal’;
ELSEIF balance >= 101 AND balance <= 500 THEN
SET result = ‘can be withdrawn';
END IF;
END &&
DELIMITER ;
Question Bank
Faculty of Engineering
Department of Computer Engineering
Course: Introduction to Database Concepts
Q: Create a table called ‘employee’ with the following requirements:
A. Columns called (id, fname, lname, department, salary).
B. Let the user to have ability for selecting only one department from a list of departments
(Purchasing, Marketing, Accounting)
Create table employee(
Id int,
fname varchar(255),
lname varchar(255),
department enum(‘Purchasing’, ‘Marketing’, ‘Accounting’),
salary int
);
Question Bank
Faculty of Engineering
Department of Computer Engineering
Course: Introduction to Database Concepts
Q: Create a view to show only id and full name of the employees without their salary.
CREATE VIEW employee_id_&_name AS
SELECT id, fname, lname
FROM employee;
Q: Create a view to show only students of the Biology department without their id.
CREATE VIEW Biology_dept_students AS
SELECT id, fname, lname, department
FROM students
WHERE department = ‘Biology’;
Question Bank
Faculty of Engineering
Department of Computer Engineering
Course: Introduction to Database Concepts
Q: Write queries to create a trigger to check if the inserted expiration date was before the production date, then
make it null.
delimiter &&
create trigger chk_exp_date_insert
before insert on product_tbl
for each row
begin
if (new.exp_date < new.prod_date) then
set new.exp_date = null;
end if;
end &&
delimiter ;
Q: Write a subquery to find full name of those customers whose total deposit is more than 2000 $.
select fname, mname, lname, sum(amount)
from customer_tbl, deposit_tbl
where customer_tbl.id = deposit_tbl.customer_id
group by customer_id
having sum(amount) > 2000;
Question Bank
Faculty of Engineering
Department of Computer Engineering
Course: Introduction to Database Concepts
Q: Write a query to create a trigger to log data of customer table if values inserted to that table. If the customer
table has only id, fname, lname and dob fields. Query for creating customer log table is not required.
create trigger employee_insert
after insert on employee_tbl
for each row
insert into employee_log_tbl
set
actionn
=
'insert',
employee_id
=
new.id,
fname_new
=
new.fname,
lname_new
=
new.lname,
dob_new
=
new.dob,
log_date_time
=
now();
Question Bank
Faculty of Engineering
Department of Computer Engineering
Course: Introduction to Database Concepts
Q - A: Develop a java application to create a JFrame for adding (first name, last name, address and username)
of customers to the database.
Q - B: Develop a java application to create a JFrame for showing full name of those employees salary is
greater than a value which would be entered by the users.
Q - C: Develop a java application to create a JFrame for showing full name of those customers whose total
deposit is greater than a value which would be entered by the users.
Answer: You can find answer of the above three questions in the below URL.
https://classroom.google.com/c/NTg3NDg5ODU2NTky/p/NjA5Nzk3OTAyMDQw/details
Question Bank
Faculty of Engineering
Department of Computer Engineering
Course: Introduction to Database Concepts
Needed Java Packages, Classes, Methods and Variable types for question in the previous page:
Note: Please, don’t use CTRL+SPACE while using the MySQL Workbench.
java.sql.*;
javax.swing.table.DefaultTableModel;
Class
Connection
DriverManager
PreparedStatement
DefaultTableModel
Integer
Double
ResultSet
Object
forName()
getConnection()
executeQuery()
executeUpdate()
setInt()
setDouble()
setString()
next()
parseInteger()
parseDouble()
getString()
getDouble()
getModel()
addRow()
close()
setRowCount()
Question Bank
Faculty of Engineering
Department of Computer Engineering
Course: Introduction to Database Concepts
Q3:Draw an ER diagram for the following database.
1.
Question Bank
Faculty of Engineering
Department of Computer Engineering
Course: Introduction to Database Concepts
Beverage
Meal
Q: According to the following design if only tea or coffee is served to the visitors with the meals,
so, retrieve the list.
SELECT Meal_name, Beverage_name
FROM Meal, Beverage
WHERE Meal.Beverage_ID = Beverage.BID;
Question Bank
Faculty of Engineering
Department of Computer Engineering
Course: Introduction to Database Concepts
Q: If we have a table called student that includes marks of students for each subject. So, write a
query to calculate the average of all the marks?
DELIMITER $$
CREATE PROCEDURE average_mark()
BEGIN
DECLARE x INT;
SET x = 1;
WHILE x <= (select count(id) from student) DO
update student
set average_mark = (DB_mark+Php_mark)/2
where id = x;
SET x = x + 1;
END WHILE;
END$$
DELIMITER ;
Download