Bài tập lớn số 2 ĐỀ 3 Cho ERD và ánh xạ như sau: useruser_id, email, full_name, profile_pic, dob, gender, town, phone registered_useruser_id, salt, password facebook_useruser_id, faceb
Trang 1Bài tập lớn số 2
ĐỀ 3
Cho ERD và ánh xạ như sau:
user(user_id, email, full_name, profile_pic, dob, gender, town, phone)
registered_user(user_id, salt, password)
facebook_user(user_id, facebook_id)
google_user(user_id, google_id)
country(country_id, country_name)
state(state_id, country_id, state_name)
city(city_id, city_name)
city_in_country(city_id, country_id)
city_in_state(city_id, state_id)
street(street_id, street_name, city_id)
place(place_id, name, lat, lng, address_number, street_id, parent_place_id,
creator_id)
place_comment(comment_id, place_id, user_id, comment)
tagged_place_comment (comment_id, user_id, text, pos)
route(route_id, place_depart, place_dest, vehicle, distance, note)
route_comment(comment_id, route_id, user_id, comment)
tagged_route_comment(comment_id, user_id, text, pos)
place_pic(pic_id, place_id, url, timestamp, caption, description)
tagged_pic(pic_id, user_id, x, y)
place_pic_comment(comment_id, pic_id, user_id, comment)
tagged_place_pic_comment(comment_id, user_id, text, pos)
Trang 3PHẦN 1: TẠO CSDL TRÊN ORACLE (4đ)
I Tạo bảng dữ liệu và ràng buộc thể hiện sơ đồ trên (3đ)
Ch V
ng bu ( UNIQUE NOT NULL) y nghi
1 user(user_id, email, full_name, profile_pic, dob, gender, town, phone)
a user_id có dạng [RE|FB|GG][0-9]10, ó loạ ời dùng RE,
FB, GG lầ ợ ứng với 3 loại tài khoản trên, hệ thống tự
ă ố thứ tự ó
b email UNIQUE, NOT NULL
c full_name NOT NULL
d dob là kiểu DATE có dạng YYYY/MM/DD
e gender là [Male|Female|Other]
f phone có dạng +84([0-9]9[0-9|]
g Yêu cầu KSTN: email có dạng biểu thức chính quy sau ^[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+\.[a-zA-Z]{2,4}$
2 registered_user(user_id, salt, password)
a salt và password NOT NULL
b salt là chuỗi 4 kí tự không bao gồm các kí tự ặc biệt, UNIQUE,
ợc sinh ra ngẫu nhiên khi ời dùng cập nhật password
c password phả ợc nối với salt và ă MD5 ớ V cập nhật w = ‘12345’ = ‘zzz’ ố { ‘12345zzz’ {
ă MD5 { ‘af7af8388c1062dfbf0f66b6b7032035’ ồi mới { f w
3 facebook_user(user_id, facebook_id)
a facebook_id UNIQUE, NOT NULL
4 google_user(user_id, google_id)
a google_id UNIQUE, NOT NULL
5 country(country_id, country_name)
a country_id là kiểu kí tự ộ dài 8
6 state(state_id, country_id, state_name)
a state_id là kiểu kí tự ộ dài 10
7 city(city_id, city_name)
a city_id là kiểu số yê ộ dài 32
8 city_in_country(city_id, country_id)
9 city_in_state(city_id, state_id)
10 street(street_id, street_name, city_id)
a country_name, state_name, city_name, stree_name NOT NULL
11 place(place_id, name, lat, lng, address_number, street_id, parent_place_id, creator_id)
a name, lat, lng, address_number, street_id NOT NULL
Trang 4c address_number có dạng ([0-9]+[A-Z|][/|])+[bis|]
12 place_comment(comment_id, place_id, user_id, comment)
13 tagged_place_comment (comment_id, user_id, text, pos)
14 route(route_id, place_depart, place_dest, vehicle, distance, note)
15 route_comment(comment_id, route_id, user_id, comment)
16 tagged_route_comment(comment_id, user_id, text, pos)
17 place_pic(pic_id, place_id, url, timestamp, caption, description)
18 tagged_pic(pic_id, user_id, x, y)
19 place_pic_comment(comment_id, pic_id, user_id, comment)
20 tagged_place_pic_comment(comment_id, user_id, text, pos)
a trong _comment, cộ NOT NULL { | ‘’
b trong tagged_ _comment, text có giá trị mặ ị { ‘’ ó | ị
mặ ịnh là 0
II Tạo Index (1đ)
Các truy vấn về ị ể (PLACE) ờng tìm kiếm theo
toạ ộ ột vị í } x y ì | ị ểm nằm trong vùng hình vuông có px, py là trung tâm, cạnh a
o Vd: cho px, py, tìm tất cả | ị ểm có lat trong [px – a/2, px + a/2] và lng trong [py – a/2, py + a/2]
mã ờng street_id
D y y x
y x u
PHẦN 2: STORE PROCEDURE, FUNCTION, TRIGGER (3đ)
I Trigger (1.5đ)
Đảm bảo một user_id không thể thuộc nhiề 1 ại tài khoản (registered, facebook, google)
Đảm bảo một CITY không thể vừa thuộc STATE vừa thuộc COUNTRY
Đảm bảo distance của một ROUTE không thể nào chênh lệch quá 5Km khi tính theo khoảng cách lat, lng của 2 PLACE
Đảm bảo ràng buộc khi một PLACE chứa một PLACE khác, street_id của 2 PLACE
ó ải giống nhau (*)
Yêu cầ KSTN (*) Đảm bảo không xuất hiện chu trình trong quan hệ chứa nhau của PLACE
II Store Procedure/Function (1.5đ)
Cho bảng thống kê user_statistic(user_type, user_count) Viết thủ tục thống kê số ợng từng loạ ời dùng vào bảng trên ( _ y = [‘ ’ ‘f ’
‘ ’])
Cho bảng thống kê place_statistic(place_id, popularity) Viết thủ tục thống kê mức phổ biến của ịa ểm (tính theo số comment) (*)
Yêu cầu KSTN: (*) Mức phổ biến tính theo số user bị tag trong comment
Trang 5PHẦN 3: THAO TÁC CƠ SỞ DỮ LIỆU (3đ)
I Insert (1.5đ)
T n vi c nh p u
D 1
II Update (0.25đ)
Cập nhậ t USER
Cập nhật street_id của một PLACE
Cập nhật note và distance của một ROUTE
III Delete (0.25đ)
USER ó
y x y
u
x y ụ cho cả 2 ờng hợp thành công và thất bại
IV Select (1.5đ)
Liệt kê thông tin nhữ ị ểm có trong thành phố ‘ ồ C í M ’
Liệt kê thông tin nhữ ị ể | ị ể ‘Suố T ê ’ ò | í 40Km (*)
Đếm số comment, số ợc tag tại từ ị ểm
Yêu cầu KSTN: (*) phả ợc thực hiện bằng 2 cách (truy vấn theo distance và tự tính theo lat, lng)
Chú ý: S I II III y
y u