Mô tả bài toán
Quản lí sinh viên đại học gồm các thông tin sau:
Quản lý khoa đóng vai trò quan trọng trong việc quản lý thông tin, bao gồm mã khoa, tên khoa, số điện thoại và địa chỉ Mỗi khoa sẽ được phân biệt rõ ràng thông qua mã khoa, giúp việc tổ chức và tra cứu thông tin trở nên dễ dàng và hiệu quả hơn.
Quản lý lớp học đòi hỏi việc theo dõi các thông tin quan trọng như mã lớp, mã khoa, số khóa và năm bắt đầu Mã lớp thể hiện khóa học, trong khi mã khoa đại diện cho khóa ngoại Việc quản lý chính xác những thông tin này giúp đảm bảo sự tổ chức và hiệu quả trong quá trình giảng dạy.
- Quản lí giảng viên: cần quản lí các thông tin như mã giảng viên, tên, giới tính, địa chỉ, số điện thoại Trong đó mã giảng viên là khóa.
Quản lý sinh viên yêu cầu tổ chức và lưu trữ thông tin quan trọng như mã sinh viên, họ tên, giới tính, địa chỉ, ngày sinh và mã lớp Trong đó, mã sinh viên đóng vai trò là khóa chính, trong khi mã lớp được xem là khóa ngoại, giúp liên kết thông tin một cách hiệu quả.
- Quản lí môn học: cần quản lí các thông tin như mã môn, tên môn, số tín chỉ. Trong đó mã môn là khóa.
- Quản lí giảng viên giảng dạy 1 môn học: cần quản lí mã giảng viên và mã môn học.
Quản lý kết quả học tập là cần thiết, bao gồm các thông tin quan trọng như mã sinh viên, mã môn học, điểm chuyên cần, điểm giữa kỳ và điểm cuối kỳ Trong đó, mã sinh viên và mã môn học đóng vai trò khóa để đảm bảo việc quản lý dữ liệu hiệu quả.
Mô hình CSDL quan hệ
1.Cơ sở dữ liệu của bài toán
CSDL bài toán Quản lý sinh viên đại học: tblKhoa (sMaKhoa, sTenKhoa, sDienThoai, sDiaChi) tblLop (sMaLop, sMaKhoa, sKhoa, iNamBD) tblGV_MH (sMaGV, sMaMon) tbkKetQua (sMaSV, sMaMon, fDiemCC, fDiemGK, fDiemCK)
2.Xác định cấu trúc bảng
*tblKhoa: lưu trữ thông tin của Khoa
TT Tên Thuộc Tính Kiểu Dữ Liệu Ràng Buộc Ghi chú
1 sMaKhoa Varchar(10) PK Mã khoa
*tblLop: lưu trữ thông tin của Lớp
T Tên Thuộc Tính Kiểu Dữ Liệu Ràng Buộc Ghi chú
1 sMaLop Varchar(10) PK Mã lớp
2 sMaKhoa Varchar(10) FK, tham chiếu sang bảng tblKhoa Mã khoa
4 iNamBD int Năm bắt đầu
*tblGV: lưu trữ thông tin của Giảng viên
T Tên Thuộc Tính Kiểu Dữ Liệu Ràng Buộc Ghi chú
1 sMaGV Varchar(10) PK Mã giảng viên
2 sTenGV NVarchar(50) Tên giảng viên
*tblSV: lưu trữ thông tin của Sinh Viên
T Tên Thuộc Tính Kiểu Dữ Liệu Ràng Buộc Ghi chú
1 sMaSV Varchar(10) PK Mã sinh viên
5 dNgaySinh Date dNgaySinh< getDate() Ngày sinh
6 sMaLop Varchar(10) FK, tham chiếu sang bảng tblLop Mã lớp
*tblMonHoc: lưu trữ thông tin của Môn học
T Tên Thuộc Tính Kiểu Dữ Liệu Ràng Buộc Ghi chú
1 sMaMon Varchar(10) PK Mã môn
3 iSoTinChi int iSoTinChi>0 Số lượng tín chỉ
Tạo cơ sở dữ liệu
ây dựng các view cho CSDL
To create a view that counts the number of students in a class based on the class code, use the following SQL command: `CREATE VIEW vvSoSV AS SELECT sMaLop AS [Class Code], COUNT(sMaSV) AS [Number of Students] FROM tblSV GROUP BY sMaLop; SELECT * FROM vvSoSV;` This command generates a view named `vvSoSV`, which displays the class code alongside the total number of students in each class.
View hiển thị MaSV, TenSV, Ngay Sinh, Que Quan của các sinh viên tên là Khánh và có tuổi lớn hơn 18. create view vvshow_svKhanh18 as
Select sMaSV, sTenSV, sGioiTinh, dNgaySinh, sDiaChi
Where (sTenSV like '%Khánh') AND DATEDIFF(day,dNgaySinh, getdate())/365>18
To create a view that displays student results by name, use the following SQL command: `CREATE VIEW vvKetQuaSVTheoTen AS SELECT sTenSV AS [Student Name], sMaMon, fDiemCC, fDiemGK, fDiemCK FROM tblSV, tblKetQua WHERE tblSV.sMaSV = tblKetQua.sMaSV;` After creating the view, you can retrieve the results with the query `SELECT * FROM vvKetQuaSVTheoTen.`
The following SQL query creates a view named "vvDiemCklonHonbanng8cuaMH2" that retrieves the names and student IDs of students who scored 8 or higher in the final exam for the course "MH2." It selects the student name (sTenSV), student ID (sMaSV), and final exam score (fDiemCK) from the tables "tblSV" and "tblKetQua," ensuring that the student ID matches in both tables and that the final exam score is at least 8 To view the results, execute the command "select * from vvDiemCklonHonbanng8cuaMH2."
hiện những khoa có lớp năm 2020 create view vvKhoacolopnam2020 as select sTenKhoa, sMaKhoa from tblKhoa where sMaKhoa in ( select sMaKhoa from tblLop where tblLop.iNamBD 20) select * from vvKhoacolopnam2020
Cho biết điểm thấp nhất của mỗi môn học create view vvshow_diemthapnhat as
Select tblMonHoc.sMaMon,MIN(fDiemCK) as [Min diem]
From tblMonHoc inner join tblKetQua on tblMonHoc.sMaMon=tblKetQua.sMaMon
The query creates a view named "vvSVoHN" that retrieves information about students residing in Hanoi It selects the student ID, name, gender, class ID, department name, and address from the tables tblSV, tblKhoa, and tblLop The conditions ensure that the data is filtered to include only those students whose address is in Hanoi The final output will display all relevant student details from this view.
Tinh Trung Binh Diem chuyên cần , giữa kì, cuối kì của từng khoa create view vvTBDiemCuaTungKhoa as
Chọn mã khoa, tên khoa, điểm chuyên cần, điểm giữa kỳ và điểm cuối kỳ từ các bảng tblSV, tblKetQua, tblLop và tblKhoa.
Where tblSV.sMaLop = tblLop.sMaLop And tblKetQua.sMaSV=tblSV.sMaSV and tblLop.sMaKhoa=tblKhoa.sMaKhoa
Group By tblKhoa.sMaKhoa, tblKhoa.sTenKhoa
Hiển thị tất cả sinh viên phải học lại môn Toán rời rạc
Create view vvshow_svhlToanrr as
Select sTenSV, sTenMon, fDiemCC, fDiemGK, fDiemCK
Where tblSV.sMaSV = tblKetQua.sMaSV AND tblKetQua.sMaMon tblMonHoc.sMaMon and (fDiemCC*0.1 + fDiemGK*0.2 + fDiemCK*0.7) 0) BEGIN INSERT INTO tblMonHoc(sMaMon, sTenMon, iSoTinChi) VALUES (@mamon, @tenmon, @tinchi); PRINT N'Nhập thành công'; END ELSE BEGIN PRINT 'Loi!!!'; PRINT 'Moi nhap lai:'; ROLLBACK TRAN; END END; SELECT * FROM tblMonHoc; INSERT INTO tblMonHoc(sMaMon, sTenMon, iSoTinChi) VALUES ('MH6', N'Thiết Kế Web', 3);` This code ensures that only valid entries with positive credit hours are inserted into the database, providing feedback on the success or failure of the operation.
To create a trigger for the `tblKetQua` table, use the `CktblKetQua` trigger to handle insert and update operations This trigger will declare variables for student ID (`@maSV`), subject code (`@maMon`), and scores for continuous assessment (`@diemcc`), mid-term exam (`@diemgk`), and final exam (`@diemck`) The trigger ensures that all relevant information is correctly processed during data manipulation.
The SQL script checks for the existence of a student and a course before inserting grades into the results table If the student ID or course ID does not exist, or if any of the grades are outside the valid range of 0 to 10, an error message is printed, and the transaction is rolled back If all conditions are met, the grades are successfully inserted into the `tblKetQua` table, confirming the data entry Examples of grade entries are demonstrated, including both successful and invalid submissions.
This article discusses the implementation of an automated trigger for updating the student count in each class within a database It begins by creating a table named `tblLop` with a column for the student count (`iSoSV`) The student count is updated by incrementing it based on the data from another table, `vvSoSV`, where the class identifier matches A trigger named `ThemBotSV` is established to handle operations after inserting, updating, or deleting student records in `tblSV` This trigger adjusts the student count by increasing it for newly added records and decreasing it for deleted ones Additionally, it demonstrates how to delete a specific student record and update the class association for another student, ultimately reflecting these changes in the `tblLop` table.
Phân quyền
Tạo login và user create login vuongquanghuy with password ='huyvuong' create user HuyVuong for login vuongquanghuy
cấp mọi quyền trên bảng tblSV cho user HuyVuong grant all on tblSV to HuyVuong
cấp mọi quyền trên bảng tblKetQua cho user HuyVuong
cấp mọi quyền trên bảng tblGV cho user HuyVuong grant all on tblGV to HuyVuong
cấp quyền select trên bảng vvSVoHN cho user HuyVuong grant select on vvSVoHN to HuyVuong
cấp quyền exec trên prThiLai cho user HuyVuong grant exec on prThiLai to HuyVuong
Hủy bỏ quyền xóa trên bảng tblGV của user HuyVuong revoke delete on tblGV from HuyVuong
từ chối quyền insert,delete trên bảng tblMH của user HuyVuong deny insert, delete on tblMonHoc to HuyVuong
Khi exec prThiLai được cấp quyền cho user HuyVuong
Khi exec prChoBietSoSVCuaTungKhoa không được cấp quyền cho user HuyVuong
Phân tán
Tạo View
The view `vvGV_Bang` displays essential information about all teachers, including their ID, name, gender, phone number, achievements, and year of establishment This is achieved by selecting data from the `tblGV` table and performing a left join with the `GV` table based on the teacher's ID.
2.Thức hiện phân tán ngang bảng tblSV
*Dữ liệu trên máy thật
*Dữ liệu trên máy ảo
Tạo view
-view lay thong tin toan bo sv create view vvThongTinToanBoSV as select * from tblSV union select * from SV
Tạo Procedure
The stored procedure `prphanchiaSV` is designed to manage student information in a database It checks if the student ID already exists in the `tblSV` or `SV` tables, and if so, it returns a message indicating that the student ID is already in use The procedure also verifies the existence of the class ID in the `tblLop` table; if the class ID is invalid, it returns an error message If the graduation year (`namTN`) is zero, it inserts the student's details into the `tblSV` table, otherwise, it inserts the information into the `SV` table, confirming successful data addition.
Khi thêm dữ liệu có trùng mã sinh viên
Khi thêm dữ liệu không có mã lớp như đã nhập
Khi thêm dữ liệu vào máy thật thành công
Khi thêm dữ liệu vào máy ảo thành công
The stored procedure `prXemThongTinhCuaSVcoMaSVNhapVao` retrieves all information for a student based on the provided student ID (`@maSV`) It first checks if the student ID exists in the `tblSV` table; if found, it selects all relevant data from that table If the ID is not found in `tblSV`, it then checks the `SV` table for the same ID If the student ID is not present in either table, it prints a message indicating that the student ID does not exist This procedure ensures accurate retrieval of student information while handling potential errors gracefully.
The procedure for updating graduation years for students is implemented using the stored procedure `prCapNhatNamTN` This procedure accepts two parameters: `@maSVnhap` (a varchar representing the student ID) and `@namTN` (an integer for the graduation year) Within the procedure, variables are declared to store the student ID, name, and gender, allowing for efficient data management and updates in the database.
@maLop varchar(10), @ngaysinh date, @diachi nvarchar(50) update tblSV set iNamTN=@namTN where sMaSV=@maSVnhap if(@namTN!=0) begin select
The SQL code snippet provided demonstrates a series of database operations involving a student table (tblSV) It retrieves student information based on a specific student ID (@maSVnhap), deletes the corresponding record from the table, and then inserts a new record with updated details such as student ID, name, gender, class ID, date of birth, and address This sequence of actions ensures that the student data is accurately maintained and reflects any necessary changes.