Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.11 MB, 22 trang )
<span class="text_page_counter">Trang 1</span><div class="page_container" data-page="1">
<b>Database Systems - DBI202</b>
Teacher: VU THANH PHONG
<b>Table of contents: Page </b>
TOPIC ……….3
I. Introduction to the problem ……….……3
1. Describe the problem ……….3
2. Management goals ……… 4
II. Entity Relationship Model ……….4
1. Identify entity sets, attributes ……….4
2. Make E/R diagram ………5
III. Data Requirements Specification ………. 6
1. Relational model ………6
2. Database Diagram ………15
3. Trigger – Procedure – SQL Query ………16
</div><span class="text_page_counter">Trang 3</span><div class="page_container" data-page="3"><b> TOPIC 3</b>
<b>Consider the following set of requirements for a databasethat is used to keep track information about a university.I. Introduction to the problem (write the user requirement).</b>
<b> 1. Describe the problem.</b>
A university has a need to build a database to manage professors and manage projects done by students:
- Professors have an SSN, a name, an age, a rank, and a research specialty. Projects have a project number, a sponsor name (e.g., NSF), a starting date, an ending date, and a budget. Graduate students have an SSN, a name, an age, and a degree program (e.g., M.S. or Ph.D.).
- Each project is managed by one professor (known as the project’s principal investigator). Each project is worked on by one or more professors (known as the project’s co-investigators). Professors can manage and/or work on multiple projects. Each project is workedon by one or more graduate students (known as the project’s research assistants).
- When graduate students work on a project, a professor must supervise their work on the project. Graduate students can work on multiple projects. - Departments have a department number, a department name, and a main office. Departments have a professor (known as the chairman) who runs the department. Professors work in one or more departments.
- Graduate students have one major department in whichthey are working on their degree.
</div><span class="text_page_counter">Trang 4</span><div class="page_container" data-page="4">- Each graduate student has another, more senior graduate student who advises him or her on what courses to take. Design and draw an ER diagram that captures theinformation about the university. Use only the basic ER model here; that is, entities, relationships, and attributes. Be sure to indicate any key and participation constraints.
<b>Professors: ID, Name, Age, Ranking, ResearchDepartments: ID, Name, Office.</b>
<b>Students: ID, Name, Age, Degree.</b>
<b>Projects: ID, Name, StartingDate, EndingDate, </b>
<b> - Relation between entity: </b>
+ Professors run Department+ Professors work in Department+ Professors manage Project+ Professors work on Project
+ Professors supervise Project, Student+ Students work on Projects
</div><span class="text_page_counter">Trang 5</span><div class="page_container" data-page="5">+ Students major Department
+ Senior Graduate introduce Graduate Students
<b>2. Make E/R diagram.</b>
</div><span class="text_page_counter">Trang 6</span><div class="page_container" data-page="6"><b>III. Data Requirements Specification (Data dictionary).1. Relational model (Converted from E/R model).</b>
<b>1.1 Table definitionsa. TABLE PROFESSORS</b>
<b><small>AttributesData typeDefaultCheck</small><sup>Key/Index/Const</sup><small>rains</small></b>
<small>PSSNVarchar(20)PSSN like ‘P%’Primary keyName</small> <sup>Nvarchar(50</sup>
<small>Research Degree =‘Master’ or</small>
<b>Example:</b>
</div><span class="text_page_counter">Trang 7</span><div class="page_container" data-page="7"><b>Create table statement:</b>
CREATE TABLE Professors(
PSSN varchar(20) CHECK(PSSN like 'P%'),Name nvarchar(50),
Age int CHECK(Age >= 22),Ranking int CHECK(Ranking > 0),
Research nvarchar(50) CHECK(Research = 'Master' or Research = 'Doctor'),
<small>BudgetFloat0Budget >= 0</small>
</div><span class="text_page_counter">Trang 8</span><div class="page_container" data-page="8"><small>PR01P01Mar Messenger2022-10-012022-11-100PR02P02Arrange Traffic Lights2022-10-012022-11-100PR03P02Timetable Scheduler2022-10-122022-11-070</small>
<small>PR05P05Elementary Japanese2022-10-012022-11-010</small>
<b>Create table statement:</b>
CREATE TABLE Projects(
ProjectNumber varchar(20) CHECK(ProjectNumber like 'PR%'),
PSSN varchar(20) CHECK(PSSN like 'P%'), Name nvarchar(50),
StartingDate date, EndingDate date,
Budget float CHECK(Budget >= 0) DEFAULT 0, Primary key (ProjectNumber),
FOREIGN KEY (PSSN) REFERENCES Professors(PSSN) )
</div><span class="text_page_counter">Trang 9</span><div class="page_container" data-page="9"><b>c. TABLE DEPARTMENTS<small>Attribute</small></b>
<small>FOREIGN KEY (PSSN)REFERENCESProfessors(PSSN)DNameNvarchar(50)</small>
<b>Create table statement:</b>
CREATE TABLE Departments (
DNumber int CHECK(DNumber > 0),
PSSN varchar(20) CHECK(PSSN like 'P%'),DName nvarchar(50),
</div><span class="text_page_counter">Trang 10</span><div class="page_container" data-page="10">Office nvarchar(50),Primary key (DNumber),
FOREIGN KEY (PSSN) REFERENCES Professors(PSSN) )
<b>d. TABLE STUDENTS</b>
<b><small>s</small><sup>Data type</sup><sup>Default</sup><sup>Check</sup><sup>Key/Index/Constrains</sup></b>
<small>FOREIGN KEY (DNumber)REFERENCESDepartments(DNumber)NameNvarchar(50)</small>
</div><span class="text_page_counter">Trang 11</span><div class="page_container" data-page="11"><b><small> </small></b>
<b> Create table statement:</b>
CREATE TABLE Students (
SSN varchar(20) CHECK(SSN like 'SE%'), SupSSN varchar(20) CHECK(SupSSN like 'SE%'), DNumber int CHECK(DNumber > 0),
Name nvarchar(50), Age int CHECK(Age >= 18), Degree nvarchar(30), Primary key (SSN),
FOREIGN KEY (DNumber) REFERENCES Departments(DNumber), FOREIGN KEY (SupSSN) REFERENCES Students(SSN)
<small>ProjectNumber like 'PR%'</small>
<small>Primary Key, FOREIGNKEY (ProjectNumber)</small>
<small>)SSN</small> <sup>Varchar(20</sup>
<small>SSN like 'SE%'</small>
<small>Primary Key, FOREIGNKEY (SSN) REFERENCES</small>
</div><span class="text_page_counter">Trang 12</span><div class="page_container" data-page="12"><b>Create table statement:</b>
CREATE TABLE WorkOnPRJS (
ProjectNumber varchar(20) CHECK(ProjectNumber like 'PR%'),
SSN varchar(20) CHECK(SSN like 'SE%'), Primary key (SSN, ProjectNumber), FOREIGN KEY (ProjectNumber) REFERENCES Projects(ProjectNumber),
FOREIGN KEY (SSN) REFERENCES Students(SSN))
<b>f. TABLE WORKONPROS</b>
<b><small>AttributesData type</small><sup>Defaul</sup></b>
<small>PSSN</small> <sup>Varchar(20</sup><small>)</small>
<small>PSSN like 'P%'</small>
<small>Primary Key, FOREIGNKEY (PSSN)REFERENCESProfessors(PSSN)ProjectNumbe</small>
<small>ProjectNumber like 'PR%'</small>
<small>Primary Key, FOREIGNKEY (ProjectNumber)</small>
<b> Example:</b>
</div><span class="text_page_counter">Trang 13</span><div class="page_container" data-page="13"><b>Create table statement:</b>
CREATE TABLE WorkOnPROS (
PSSN varchar(20) CHECK(PSSN like 'P%'),
ProjectNumber varchar(20) CHECK(ProjectNumber like 'PR%'),Primary key (PSSN, ProjectNumber),
FOREIGN KEY (PSSN) REFERENCES Professors(PSSN),FOREIGN KEY (ProjectNumber) REFERENCES
<b>g. TABLE SUPERVISE</b>
<b><small>AttributesData type</small><sup>Defaul</sup></b>
<small>PSSN</small> <sup>Varchar(20</sup><small>)</small>
<small>PSSN like 'P%'</small>
<small>Primary Key, FOREIGNKEY (PSSN) REFERENCES</small>
<small>ProjectNumber</small> <sup>Varchar(20</sup><small>)</small>
<small>ProjectNumber like 'PR%'</small>
<small>Primary Key, FOREIGNKEY (ProjectNumber)</small>
<small>)SSN</small> <sup>Varchar(20</sup>
<small>SSN like 'SE%'</small>
<small>Primary Key, FOREIGNKEY (SSN) REFERENCES</small>
<b>Example:</b>
</div><span class="text_page_counter">Trang 14</span><div class="page_container" data-page="14"><b>Create table statement:</b>
CREATE TABLE Supervise (
PSSN varchar(20) CHECK(PSSN like 'P%'),
ProjectNumber varchar(20) CHECK(ProjectNumber like 'PR%'),SSN varchar(20) CHECK(SSN like 'SE%'),
Primary key (PSSN, SSN, ProjectNumber),
FOREIGN KEY (PSSN) REFERENCES Professors(PSSN),FOREIGN KEY (ProjectNumber) REFERENCES
<small>Primary Key, FOREIGNKEY (DNumber)</small>
<b> Example:</b>
</div><span class="text_page_counter">Trang 15</span><div class="page_container" data-page="15"><b>Create table statement:</b>
CREATE TABLE WorkIn (
PSSN varchar(20) CHECK(PSSN like 'P%'),DNumber int CHECK(DNumber > 0),
Hourss int CHECK(Hourss >= 0) DEFAULT 0,Primary key (PSSN, DNumber),
FOREIGN KEY (PSSN) REFERENCES Professors(PSSN),FOREIGN KEY (DNumber) REFERENCES Departments(DNumber))
<b>2. Database diagram</b>
</div><span class="text_page_counter">Trang 16</span><div class="page_container" data-page="16"><b>3. Trigger – Procedure – SQL Query1. SQL QUERY</b>
<b>a. Display all students that not join in any projectsQuery Statement:</b>
SELECT .SSN, .SupSSN, .DNumber, .Name, .Age, .DegreeFROM STUDENTS S LEFT JOIN WORKONPRJS W ON .SSN = W SSNLEFT JOIN PROJECTS P ON .ProjectNumber = P ProjectNumberWHERE .ProjectNumber IS NULL
<b>b. Display the number of professor that is working in each department.</b>
<b>Query Statement:</b>
SELECT .DNumber, .DName, COUNT( .P PSSN) AS NumOfProfessor
</div><span class="text_page_counter">Trang 17</span><div class="page_container" data-page="17">FROM Departments D INNER JOIN WorkIn WI ON .DNumberWI DNumber.
INNER JOIN Professors P ON WI.PSSN = P PSSNGROUP BY .DNumber, .DName
<b>c. Display all students that doesn’t have supervise studentQuery Statement:</b>
SELECT SSN, DNumber, Name, Age, DegreeFROM Students
WHERE SupSSN is null
<b>2. TRIGGER</b>
<b>a. Trigger for insert on Projects table</b>
<b>Usage: If the row that user insert into Projects table has </b>
Ending Date that not after the Starting Date. The trigger will print the notification and rollback for avoiding this insert. Otherwise, the row that user insert will be inserted normally.
<b>Create Trigger Statement:</b>
<small>CREATE TRIGGERstartDate_endDate_checkonProjects</small>
<small>FOR INSERTASBEGIN</small>
<small>DECLARE@SDATEDATE,@EDATEDATE</small>
</div><span class="text_page_counter">Trang 18</span><div class="page_container" data-page="18"><small>SELECT@SDATE =StartingDate,@EDATE =EndingDate</small>
<small>SELECT@SYEAR =YEAR(@SDATE),@SMONTH =MONTH(@SDATE),@SDAY</small>
<small>SELECT@EYEAR =YEAR(@EDATE),@EMONTH =MONTH(@EDATE),@EDAY</small>
<small>IF (@EYEAR <@SYEAR)BEGIN</small>
<small>PRINT('The ending date need to be after the starting date')ROLLBACK TRAN</small>
<small>IF (@EMONTH <@SMONTH)BEGIN</small>
<small>PRINT('The ending date need to be after the starting date')</small>
<small>ROLLBACK TRANEND</small>
<small>IF (@EDAY <@SDAY)BEGIN</small>
<small>PRINT('The ending date need to be after the starting date')</small>
<small>ROLLBACK TRANEND</small>
<small>SELECT *FROMINSERTED</small>
<b>b. Trigger for insert on Students table</b>
<b>Usage: If the row that user insert into Students table has </b>
Supervise Student that has degree is not Senior Graduate. The trigger will print the notification and rollback for avoiding
</div><span class="text_page_counter">Trang 19</span><div class="page_container" data-page="19">this insert. Otherwise, the row that user insert will be inserted normally.
<b>Create Trigger Statement:</b>
<small>CREATE TRIGGERSUP_STU_TRIGONSTUDENTS</small>
<small>FOR INSERTASBEGIN</small>
<small>DECLARE@SUP_SSNVARCHAR(20)SELECT@SUP_SSN =SupSSN</small>
<small>SELECT *FROMINSERTED</small>
<b>c. Trigger for insert on WorkIn table</b>
<b>Usage: The professor that manage the department need to have </b>
the highest ranking (Rank 1 > Rank 2) in department. If the row that user insert into WorkIn table has professor has higher ranking than department's manager. The trigger will print the notification and rollback for avoiding this insert.Otherwise, the row that user insert will be inserted
<b>Create Trigger Statement:</b>
<small>CREATE TRIGGERcheck_WorkInONWORKIN</small>
<small>FOR INSERTASBEGIN</small>
</div><span class="text_page_counter">Trang 20</span><div class="page_container" data-page="20"><small>DECLARE@DNUMINT,@IPSSNVARCHAR(20)SELECT@DNUM =DNUMBER,@IPSSN =PSSN</small>
<small>DECLARE@ManagerNumVARCHAR(20)SELECT@ManagerNum =PSSN</small>
<small>WHEREDNumber =@DNUM</small>
<small>DECLARE@IRANKINGINTSELECT@IRANKING =Ranking</small>
<small>PRINT('The manager need to have highest ranking in the department!')</small>
<small>ROLLBACK TRANEND</small>
<small>SELECT *FROMINSERTED</small>
<b>2. STORE PROCEDURE</b>
<b>a. Procedure that helping user delete a project by the Project Number</b>
<b>Usage: Normally, if user want to delete a Project they need </b>
to delete in all table that referenced to Projects table. Therefore, we create this procedure that help user can deletea project by the Project Number in one statement.
<b>Create procedure statement:</b>
<small>CREATE PROCEDUREDELETE_PRO_BY_KEY @PRONUMvarchar(20)AS</small>
<small>DELETE FROMWorkOnPROS</small>
</div><span class="text_page_counter">Trang 21</span><div class="page_container" data-page="21"><small>WHEREProjectNumber =@PRONUM</small>
<small>DELETE FROMSupervise</small>
<small>WHEREProjectNumber =@PRONUM</small>
<small>DELETE FROMWorkOnPRJS</small>
<small>WHEREProjectNumber =@PRONUM</small>
<small>DELETE FROMProjects</small>
<small>WHEREProjectNumber =@PRONUM</small>
<b>b. Procedure that helping user Insert a project into the Projects table</b>
<b>Usage: Normally, if user want to insert a Project they need </b>
to insert the professor if PSSN not exist in the Professor table. Therefore, we create this procedure that help user caninsert a project into Projects table in one statement. At thesame time, we put the insert statement in TRANSACTION and in block TRY so if any error happen the insert will not be inserted to table for avoiding wrong data in database.
<b>Create procedure statement:</b>
<small>CREATE PROCEDUREINSERT_PRO</small>
<small> @projNumvarchar(20), @PSSNvarchar(20), @projNamenvarchar(50), @startDateDate, @endDateDate, @budgetint,</small>
<small> @profNamenvarchar(50), @profAgeint,</small>
<small> @rankingint,</small>
<small> @researchnvarchar(50)AS</small>
<small>BEGIN</small>
</div><span class="text_page_counter">Trang 22</span><div class="page_container" data-page="22"><small>GROUP BYPSSN</small>
<small>HAVINGPSSN =@PSSN</small>
<small>IF (@CHECKisnotnull)BEGIN</small>
<small>INSERT INTOProjects</small>
<small>VALUES (@projNum,@PSSN,@projName,@startDate,</small>
<small>@endDate, 0)ENDELSEBEGIN</small>
<small>INSERT INTOProfessors</small>
<small>VALUES (@PSSN,@profName,@profAge,@ranking,</small>
<small>@research)INSERT INTOProjects</small>
<small>VALUES (@projNum,@PSSN,@projName,@startDate,@endDate, 0)END</small>
</div>