Guruh talabasi Abdig’opporov javoxir Mysql 1-2-amaliy ishlar



Download 353,77 Kb.
bet42/43
Sana18.07.2022
Hajmi353,77 Kb.
#818857
1   ...   35   36   37   38   39   40   41   42   43
Bog'liq
311-18 AJ

Exercises

  1. Customer 'Tan Ah Teck' has rented 'SBA1111A' from today for 10 days. (Hint: You need to insert a rental record. Use a SELECT subquery to get the customer_id. Use CURDATE() (or NOW()) for today; and DATE_ADD(CURDATE(), INTERVAL x unit) to compute a future date.)

  2. INSERT INTO rental_records VALUES

  3. (NULL,

  4. 'SBA1111A',

  5. (SELECT customer_id FROM customers WHERE name='Tan Ah Teck'),

  6. CURDATE(),

  7. DATE_ADD(CURDATE(), INTERVAL 10 DAY),

NULL);

  1. Customer 'Kumar' has rented 'GA5555E' from tomorrow for 3 months.

  2. List all rental records (start date, end date) with vehicle's registration number, brand, and customer name, sorted by vehicle's categories followed by start date.

  3. SELECT

  4. r.start_date AS `Start Date`,

  5. r.end_date AS `End Date`,

  6. r.veh_reg_no AS `Vehicle No`,

  7. v.brand AS `Vehicle Brand`,

  8. c.name AS `Customer Name`

  9. FROM rental_records AS r

  10. INNER JOIN vehicles AS v USING (veh_reg_no)

  11. INNER JOIN customers AS c USING (customer_id)

ORDER BY v.category, start_date;

  1. List all the expired rental records (end_date before CURDATE()).

  2. List the vehicles rented out on '2012-01-10' (not available for rental), in columns of vehicle registration no, customer name, start date and end date. (Hint: the given date is in between the start_date and end_date.)

  3. List all vehicles rented out today, in columns registration number, customer name, start date, end date.

  4. Similarly, list the vehicles rented out (not available for rental) for the period from '2012-01-03' to '2012-01-18'. (Hint: start_date is inside the range; or end_date is inside the range; or start_date is before the range and end_date is beyond the range.)

  5. List the vehicles (registration number, brand and description) available for rental (not rented out) on '2012-01-10' (Hint: You could use a subquery based on a earlier query).

  6. Similarly, list the vehicles available for rental for the period from '2012-01-03' to '2012-01-18'.

  7. Similarly, list the vehicles available for rental from today for 10 days.

  8. Foreign Key Test:

    1. Try deleting a parent row with matching row(s) in child table(s), e.g., delete 'GA6666F' from vehicles table (ON DELETE RESTRICT).

    2. Try updating a parent row with matching row(s) in child table(s), e.g., rename 'GA6666F' to 'GA9999F' in vehicles table. Check the effects on the child table rental_records (ON UPDATE CASCADE).

    3. Remove 'GA6666F' from the database (Hints: Remove it from child table rental_records; then parent table vehicles.)

  1. Payments: A rental could be paid over a number of payments (e.g., deposit, installments, full payment). Each payment is for one rental. Create a new table called payments. Need to create columns to facilitate proper audit check (such as create_date, create_by, last_update_date, last_update_by, etc.)

  2. DROP TABLE IF EXISTS `payments`;

  3. CREATE TABLE payments (

  4. `payment_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,

  5. `rental_id` INT UNSIGNED NOT NULL,

  6. `amount` DECIMAL(8,2) NOT NULL DEFAULT 0,

  7. `mode` ENUM('cash', 'credit card', 'check'),

  8. `type` ENUM('deposit', 'partial', 'full') NOT NULL DEFAULT 'full',

  9. `remark` VARCHAR(255),

  10. `created_date` DATETIME NOT NULL,

  11. `created_by` INT UNSIGNED NOT NULL, -- staff_id

  12. -- Use a trigger to update create_date and create_by automatically

  13. `last_updated_date` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

  14. -- Updated by the system automatically

  15. `last_updated_by` INT UNSIGNED NOT NULL,

  16. -- Use a trigger to update created_by

  17. PRIMARY KEY (`payment_id`),

  18. INDEX (`rental_id`),

  19. FOREIGN KEY (`rental_id`) REFERENCES rental_records (`rental_id`)

  20. ) ENGINE=InnoDB;

  21. DESC `payments`;

  22. SHOW CREATE TABLE `payments` \G

SHOW INDEX FROM `payments` \G

  1. Staff: Keeping track of staff serving the customers. Create a new staff table. Assume that each transaction is handled by one staff, we can add a new column called staff_id in the rental_records table,

  2. DROP TABLE IF EXISTS `staff`;

  3. CREATE TABLE `staff` (

  4. `staff_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,

  5. -- Always use INT for AUTO_INCREMENT column to prvent run-over

  6. `name` VARCHAR(30) NOT NULL DEFAULT '',

  7. `title` VARCHAR(30) NOT NULL DEFAULT '',

  8. `address` VARCHAR(80) NOT NULL DEFAULT '',

  9. `phone` VARCHAR(15) NOT NULL DEFAULT '',

  10. `report_to` INT UNSIGNED NOT NULL,

  11. -- Reports to manager staff_id. Boss reports to himself

  12. PRIMARY KEY (`staff_id`),

  13. UNIQUE INDEX (`phone`), -- Build index on this unique-value column

  14. INDEX (`name`), -- Build index on this column

  15. FOREIGN KEY (`report_to`) REFERENCES `staff` (`staff_id`)

  16. -- Reference itself

  17. ) ENGINE=InnoDB;

  18. DESC `staff`;

  19. SHOW INDEX FROM `staff` \G


  20. INSERT INTO staff VALUE (8001, 'Peter Johns', 'Managing Director', '1 Happy Ave', '12345678', 8001);

  21. SELECT * FROM staff;


  22. -- Add a new column to rental_records table

  23. ALTER TABLE `rental_records` ADD COLUMN `staff_id` INT UNSIGNED NOT NULL;

  24. -- Need to set to a valid value, before adding the foreign key

  25. UPDATE `rental_records` SET `staff_id` = 8001;

  26. ALTER TABLE `rental_records` ADD FOREIGN KEY (`staff_id`) REFERENCES staff (`staff_id`)

  27. ON DELETE RESTRICT ON UPDATE CASCADE;


  28. SHOW CREATE TABLE `rental_records` \G

  29. SHOW INDEX FROM `rental_records` \G


  30. -- Also Add a new column to payments table

  31. ALTER TABLE `payments` ADD COLUMN `staff_id` INT UNSIGNED NOT NULL;

  32. -- Need to set to a valid value, before adding the foreign key

  33. UPDATE `payments` SET `staff_id` = 8001;

  34. ALTER TABLE `payments` ADD FOREIGN KEY (`staff_id`) REFERENCES staff (`staff_id`)

  35. ON DELETE RESTRICT ON UPDATE CASCADE;


  36. SHOW CREATE TABLE `payments` \G

SHOW INDEX FROM `payments` \G

Download 353,77 Kb.

Do'stlaringiz bilan baham:
1   ...   35   36   37   38   39   40   41   42   43




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish