You are on page 1of 100

1 / 2019

Yarmouk University
Hijjawi Faculty for Engineering Technology
Industrial Engineering Department

Final Note IS

Eng. Mohammad Bawaneh


0797658644
‫مكتبة البوابة الجنوبية‬

South Gate Library: 0798610029

2.25
‫‪0797658644‬‬ ‫محمد بواعنه‬
‫‪- 1 -IS‬‬
‫‪0797658644‬‬ ‫محمد بواعنه‬
‫‪- 2 -IS‬‬

‫‪ ‬معالجة البيانات‪:‬‬
‫اضافة البيانات(‪)INSERT‬‬ ‫‪-‬‬
‫حذف البيانات(‪)DELETE‬‬ ‫‪-‬‬
‫تعديل البيانات(‪)UPDATE‬‬ ‫‪-‬‬
‫‪ ‬اضافة البيانات‪:‬‬
‫الصيغه العامه‪:‬‬

‫)‪INSERT INTO TABLE_NAME VALUES(' ',' ',' ',' ',.......‬‬


‫‪OR‬‬
‫'(‪INSERT INTO TABLE_NAME (attribute_name1,……,attribute_name N) VALUES‬‬
‫)' ' ‪',….,‬‬

‫مالحظه ‪ -1 :‬دائما بال ‪ INSERT‬ضع القيم بين ' ' تفاديا الخطأ‬
‫‪-2‬عند تعريف ال ‪ attribute‬من نوع ‪ char‬يجب وضع البيانات بين ’‘ ‪.‬‬

‫مثال‪ :‬عندنا الجدول التالي ‪:‬‬


‫)‪CREATE TABLE STUDENTS (SSN CHAR(10),NAME CHAR(50),N_FAMILY INT‬‬

‫بدنا نضيف بيانات عليه‬


‫‪0797658644‬‬ ‫محمد بواعنه‬
‫‪- 3 -IS‬‬

‫)‪INSERT INTO STUDENTS VALUES('99965656','MOHAMMED',4‬‬


‫او‬
‫)‪INSERT INTO STUDENTS(SSN,NAME,N_FAMILY‬‬
‫'‪VALUES('99965656','MOHAMMED','4‬‬
‫)‬
‫اذا بدنا نضيف فقط على ‪ SSN‬و ‪ N_FAMILY‬نستخدم الطريقه الثانيه‬ ‫‪‬‬
‫)’‪INSERT INTO STUDENTS (SSN,N_FAMILY) VALUES(‘78945’,’5‬‬

‫رح نستخدم هاي البيانات بالحذف والتعديل‬ ‫‪-‬‬


‫شركه فيها جدول موظفين يحتوي على الرقم الوطني واالسم وتاريخ الميالد وموقع السكن والجنس و الراتب‬
‫والرقم الوطني للموظف المسؤول ورقم القسم الذي يعمل فيه الموظف‬

‫‪ ‬حذف البيانات(‪:)DELETE‬‬
‫يتم حذف صف بالكامل حسب الشرط واذا لم يكن شرط يتم حذف كافة الصفوف في الجدول ‪.‬‬
‫الصيغه العامه‪:‬‬

‫‪DELETE‬‬
‫‪FROM TABLE_NAME‬‬
‫‪WHERE condition1 AND condition2 AND condition N‬‬

‫بقدر اكتب ‪ OR‬بالشرط مكان ال ‪AND‬‬


‫مثال ‪ :‬لوبدي احذف كل الموظفين ‪:‬‬

‫‪DELETE FROM EMPLOYEE‬‬

‫مثال ‪ :‬لو بدي احذف الموظف الي اسمه االول ‪ Joyce‬و ويعمل في القسم ‪:5‬‬
0797658644 ‫محمد بواعنه‬
- 4 -IS

DELETE FROM EMPLOYEE WHERE FNAME='Joyce' AND DNO='5'

‫ عشان‬PRIMARY KEY ‫ دائما بالحذف او التعديل اختار الشرط يكون على عامود ال‬: ‫مالحظه‬ -
.‫تضمن انك مش رح تحذف اكثر من صف‬

:)UPDATE( ‫ تعديل البيانات‬


:‫الصيغه العامه‬

UPDATE TABLE_NAME
SET Attribute1, Attribute2, Attribute N
WHERE condition 1 AND condition 2 AND condition N

‫ القيم الي بدك تعدلها‬SET

AND ‫ بالشرط مكان ال‬OR ‫بتقدر تكتب‬


: 123456789 ‫ الذي رقمه الوطني‬MOHAMMED ‫ الى‬John ‫ بدنا نعدل االسم االول للموظف‬:‫مثال‬

UPDATE EMPLOYEE SET FNAME='MOHAMMED' WHERE SSN='123456789'


0797658644 ‫محمد بواعنه‬
- 5 -IS

.‫ ال يتم التعديل وما بعطي خطأ‬123456789 ‫طيب فرضا ما عندنا وال موظف الرقم الوطني اله‬

Q: Write SQL update statements to do the following on the database schema shown in Figure.

a. Insert a new student,<‘Johnson’,25,1,‘Math’>,in the database.

b. Change the class of student ‘Smith’to 2.

c. Insert a new course,<‘Knowledge Engineering’,‘CS4390’,3,‘CS’>.

d. Delete the record for the student whose name is ‘Smith’ and whose student number is 17.
‫‪0797658644‬‬ ‫محمد بواعنه‬
‫‪- 6 -IS‬‬

‫)’‪A- Insert into student values(‘Johnson’,25,1,‘Math‬‬


‫’‪B- Update student set class=2 where name=’smith‬‬
‫)’‪C- Insert into course values(‘KnowledgeEngineering’, ‘CS4390’,3,‘CS‬‬
‫’‪D- Delete from student where name=’smith’ and student_number=’17‬‬
‫‪ ‬جرد البيانات ب استخدام ‪SELECT‬‬
‫بالفيس بوك لما نكون بالصفحه الرئيسيه بعرضلنا كل البوستات االصدقاء المجموعات الصفحات ‪,‬بس‬ ‫‪‬‬
‫مثال لما تروح ع بروفايلك بيعرضلك بوستاتك والبوستات المشاركه معك ‪ ,‬ببساطه شو البصير هي بس‬
‫بتختلف بالشرط اتخيلها جملة ‪ if‬اذا كنت بالصفحه الرئيسيه اعرضلي الكل اذا كنت بصفحتي اعرضلي‬
‫بيناتي بس ‪ ,‬وهاي جملة ال ‪ if‬بنعملها بال ‪ sql‬بالشرط ‪ , where‬طيب ما انت بتحكيلنا انو الداتا بيز‬
‫عباره عن تيبل طيب كيف بتطلع هيك مرتبه وحلوه وبوستات‪,‬بحكيلك ما في اشي صعب بكون عندهم‬
‫ستركشر وبس بجيبو قيمة كل خليه حسب الصف مثال مثال لو فرضنا جدول المنشورات هيك‬
‫‪postid‬‬ ‫‪text‬‬ ‫‪Time‬‬ ‫‪Date‬‬
‫‪1‬‬ ‫‪Hello‬‬ ‫‪11:00AM‬‬ ‫‪23/8/2018‬‬
‫‪2‬‬ ‫‪Hi‬‬ ‫‪9:20PM‬‬ ‫‪24/8/2018‬‬

‫اول صف فيه رقم البوست ‪ 1‬ونص البوست ‪ hello‬والوقت ‪ 11‬والتاريخ ‪ 2018/8/23‬شو يلي بصير‬
‫بجيبوهم خليه خليه حسب رقم البوست وبكون عندهم ستركش عام لالشي هيك مثال‬

‫‪username‬‬
‫‪Time-date‬‬
‫‪text‬‬
‫وبنفذو االستعالم وبيتعبى هاذ الستركشر بعدد الصفوف الي موجوده بالجدول‬
‫‪Mohammad bawaneh‬‬
‫‪11:00AM-23/8/2018‬‬
‫‪Hello‬‬

‫‪Mohammad bawaneh‬‬
‫‪9:20PM-24/8/2018‬‬
‫‪0797658644‬‬ ‫محمد بواعنه‬
‫‪- 7 -IS‬‬

‫‪Hi‬‬
‫الحياه بسيطه ‪^_^ .........‬‬

‫بهاذ الجزء رح نتعلم كيف نرجع البيانات هاي بتيبل وكيف نكتب الشروط ونستعلم عن االشي الي بدنا نعرضه بس‬
‫‪,,,‬‬
‫الصيغه العامه ‪:‬‬

‫‪SELECT Attribute1, Attribute2, AttributeN‬‬


‫‪FROM TABLE 1,TABLE 2,TABLE N‬‬
‫‪WHERE condition1 AND condition2 AND condition N‬‬

‫بقدر اكتب ‪ OR‬بالشرط مكان ال ‪AND‬‬

‫رح نتعامل مع الداتا بيز الي موجوده بشابتر ‪3‬‬


‫‪0797658644‬‬ ‫محمد بواعنه‬
‫‪- 8 -IS‬‬
0797658644 ‫محمد بواعنه‬
- 9 -IS

EX1: Retrieve the birth date and address of the employee(s) whose name is ‘John B.
Smith’.
SOL:
John B. Smith’ ‫رجع تاريخ الميالد و الموقع للموظفين للموظف الي اسمه‬
‫ اسم الجدول الموظفين‬FROM , ADDRESS ‫ و الموقع‬BIRTH DATE ‫ تاريخ الميالد‬SELECT ‫ال‬
John B. Smith ‫ الشرط اسم الموظف‬WHERE ‫ ال‬EMPLOYEE
SELECT Bdate, Emp_Address
FROM EMPLOYEE
‫الناتج بدون الشرط‬

SELECT Bdate, Emp_Address


FROM EMPLOYEE
WHERE Fname='John' AND Minit='B' AND Lname='Smith'

‫الناتج مع الشرط‬

EX2: Retrieve name(s) of dependent(s) to the employee which his ssn =


‘123456789’
123456789 ‫ العوائل للموظف الذي رقمه الوطني‬/‫رجع اسماء العائل‬
SOL:

SELECT Dependent_name
FROM EMP_DEPENDENT
0797658644 ‫محمد بواعنه‬
- 10 -IS

WHERE ESSN='123456789'

EX3: Retrieve ALL EMPLOYEE.


SOL:
‫اعرض كافة الموظفين‬
Select *
From employee

EX4: Retrieve the names of employee named = ‘john’ or there salary is more than
40000.00
SOL:
3000.00 ‫ او راتبه اكبر من‬JOHN ‫رجع اسماء الموظفين الي اسمهم‬
SELECT Fname, Minit,Lname

FROM EMPLOYEE
WHERE Fname='JOHN' OR Salary >40000
‫‪0797658644‬‬ ‫محمد بواعنه‬
‫‪- 11 -IS‬‬

‫’‪EX5: Retrieve the name and address of all employees who work for the ‘Research‬‬
‫‪department.‬‬

‫‪SOL:‬‬

‫رجع اسم وموقع الموظفين الي بشتغلو بقسم ال ’‪Research‬‬

‫اسم الموظف وموقعه من جدول ‪EMPLOYEE ‬‬

‫اسماء االقسام من جدول ‪DEPARTMENT ‬‬

‫هون عندنا جدولين ف رح احتاج الربط بينهم وبما انو عندنا جدولين الزم جمل الربط ‪JOIN‬يكونو على االقل جمله واحده‬
‫‪ ,‬في ال ‪ SCHEMA‬عندنا عالقتين مابين‬

‫‪ MGR_SSN  SSN‬و ‪ DNO DNUMBER‬وبما انو العالقه عمل وما جاب سيرة المدراء ف بوخذ‬
‫‪DNO DNUMBER‬‬

‫‪SELECT Fname, Lname,Emp_Address‬‬

‫‪FROM EMPLOYEE, DEPARTMENT‬‬

‫;‪WHERE Dname=‘Research’ AND Dnumber=Dno‬‬


0797658644 ‫محمد بواعنه‬
- 12 -IS

‫ شو رح يصير‬JOIN ‫لكن الحظ اذا حذفنا جملة ال‬

SELECT Fname, Lname, Emp_Address

FROM EMPLOYEE, DEPARTMENT

WHERE Dname=‘Research’

CARTESIAN PRODUCT‫رح يعطي ناتج خطأ بس ليش النو بعمل بينهم‬

N-1 ‫ على االقل‬JOIN ‫ جمل الربط‬N TABLE ‫ اذا كان عندي‬: ‫مالحظه‬

3 TABLE JOIN>=3-1=2

EX5: For every project located in ‘Stafford’, list the project number, the controlling
department number, and the department manager’s last name, address, and birth date.

SOL:
0797658644 ‫محمد بواعنه‬
- 13 -IS

‫بدو رقم المشروع و القسم المسؤول عن المشروع واالسم االخير‬, Stafford ‫في اي مشروع موجود في‬
.‫للموظف المسؤول عن القسم والموقع وتاريخ الميالد للموظف‬
3 table ‫هون رح نحتاج‬
Plocation,pnumber  project
Dnumberdepartment or project
Lname,address,bdateemployee
‫ جداول فبدنا جملتين ربط‬3 ‫استخدمنا‬
Mangers: ssn=mgr_ssn,controlling dep number : dnumber=dnum
: ‫رح يكون االستعالم‬

SELECT Pnumber, Dnum, Lname,Emp_Address, Bdate


FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE Dnum=Dnumber AND Mgr_ssn=Ssn AND Plocation='Stafford';

:)DISTINCT( ‫ جلب البيانات غير مكرره‬


‫ بحيث لو كان يضم العامود نفس‬,‫ من جلب البيانات من عامود معين مع تفادي التكرار‬DISTINCT ‫تمكننا الدالة‬
.‫ تجلب هذه القيمة مرة واحدة‬,‫القيمة في أكثر من سطر‬
distinct ‫ بدون‬QUERY ‫الحظ تنفيذ هذه ال‬

SELECT Dnum, Lname,Emp_Address, Bdate


FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE Dnum=Dnumber AND Mgr_ssn=Ssn AND Plocation='Stafford'

distinct ‫مع‬

SELECT DISTINCT Dnum, Lname,Emp_Address, Bdate


FROM PROJECT, DEPARTMENT, EMPLOYE
WHERE Dnum=Dnumber AND Mgr_ssn=Ssn AND Plocation='Stafford'
‫‪0797658644‬‬ ‫محمد بواعنه‬
‫‪- 14 -IS‬‬

‫‪ ‬جلب البيانات المشابهة (‪: )LIKE‬‬


‫بنستخدم الرمز ‪ %‬و _‬ ‫‪-‬‬
‫توضع بعد جملة ال ‪WHERE‬‬ ‫‪-‬‬
‫الصيغه العامه‪COLUM_NAME LIKE ' ' :‬‬ ‫‪-‬‬
‫ابسط مثال عليها محرك بحث ‪ Google‬شريط البحث ب ‪facebook‬‬

‫‪ : Like'm%' .1‬بجيب الكلمات الي اولها حرف ‪m‬‬


‫‪ :like'%m' .2‬بجيب الكلمات الي اخرها حرف ‪m‬‬
‫‪ : Like '%m%' .3‬بجيب الكلمات الي فيها حرف ‪m‬‬
‫‪ :Like 'md%' .4‬بجيب الكلمات الي اولها ‪md‬‬
‫‪:Like 'm_d% .5‬بجيب الكلمات الي اولها ‪ m‬و الحرف الثاني اي اشي والحرف الثالث ‪d‬‬

‫‪EX:‬‬
‫‪Let table the information of employee, what output of the following :‬‬

‫* ‪1- Select‬‬
0797658644 ‫محمد بواعنه‬
- 15 -IS

From employee
Where ssn like '12%'
2- select fname
from employee
where ssn like '__3%'
3- select bdate
from employee,DEPARTMENT
where dno=dnumber and dno>2 and fname like 'j__%'

 sol:
1-

2-

3-

: BETWEEN 
:‫الصيغه العامه‬

BETWEEN Attribute_NAME BETWEEN __ AND __

‫اي نوع من المتغيرات عادي‬


CHAR’A’ AND ‘F’

INT1 AND 10

OR (‘1’ AND ‘10’)

DATE ’1/1/2016’ AND ‘1/1/2017’


0797658644 ‫محمد بواعنه‬
- 16 -IS

Salary BETWEEN 100 AND 500  Salary <=500 AND SALARY>=100

‫ شو بصير بكون الناتج حسب الترتيب لل احرف‬CHAR ‫ مع‬BETWEEN ‫طيب لما استخدم‬ -
TEXT{Z,B,C,D,X,I,C,A}
TEXT BETWEEN ‘C’ AND ‘I’
{C,D,I,C} : ‫الناتج بكون‬

Attribute ‫رح اعطي مثال عليها مع عمليات حسابيه على ال‬

EX:

Return names and employee’s salary for these whose work from 10 to 15 hour
,assume that project LOCATION is HOUSTON and give them a percentage above
the salary 20%.

SOL:
‫ واعطيهم‬Houston ‫ ساعه و موقع المشروع‬15 ‫ الى‬10 ‫رجع اسماء والرواتب للموظفين الي بشتغلو من‬
. %20 ‫نسبه فوق الراتب‬

SELECT FNAME,Minit,Lname,SALARY +(SALARY *.20)


FROM WORKS_ON,PROJECT,EMPLOYEE
WHERE Pnumber=Pno AND SSN=ESSN AND (Emp_Hours BETWEEN 10 AND 15)
AND Plocation='Houston'
‫‪0797658644‬‬ ‫محمد بواعنه‬
‫‪- 17 -IS‬‬

‫‪Renaming AND Aliasing‬‬ ‫‪‬‬


‫‪ :Aliasing‬وظيفتها تسمية الجداول ب اسم اخر لو فرضنا عندنا جدولين ‪.‬‬

‫‪MyTable as table_name OR MyTable table_name‬‬ ‫الصيغه العامه ‪:‬‬

‫‪TABLE1‬‬
‫‪A‬‬ ‫‪B‬‬

‫‪TABLE2‬‬

‫‪C‬‬ ‫‪A‬‬
‫ونفذنا هذه ال ‪: QUERY‬‬

‫* ‪SELECT‬‬

‫‪FROM TABLE1,TABLE2‬‬

‫‪WHERE A=A‬‬

‫هون ال ‪ SQL‬مش رح تعرف انو ال ‪ Attribute A‬هو ل ‪ TABLE1‬او ‪ TABLE2‬طيب شو الحل ؟ الحل رح‬
‫نستخدم ال ‪ Aliasing‬توضع بعد جملة ال ‪. FROM‬‬

‫ومرات بنحتاجها النشاء من الجدول الواحد جدولين او اكثر ‪.‬‬ ‫‪-‬‬


0797658644 ‫محمد بواعنه‬
- 18 -IS

‫اذا الحل الصحيح هو‬ -

SELECT * SELECT *
FROM TABLE1 as T1,TABLE2 as T2 FROM TABLE1 T1,TABLE2 T2
WHERE T1.A=T2.A WHERE T1.A=T2.A

Attribute ‫ بنحتاجها عشان نعيد تسمية ال‬Attribute ‫ تسمية ال‬:Renaming

EX:
‫ ساعه و اسم البروجكت‬25 ‫ الى‬5 ‫رجع اسماء والرواتب للموظفين الي بشتغلو من‬

. %20 ‫واعطيهم نسبه فوق الراتب‬ProductX

SELECT FNAME AS ‫ االسم_االول‬,Minit AS ‫الوسط‬,Lname ‫االسم_االخير‬,SALARY+(SALARY


*.20) ‫الراتب_مع_الزياده‬
FROM WORKS_ON,PROJECT,EMPLOYEE
WHERE Pnumber=Pno AND SSN=ESSN AND (Emp_Hours BETWEEN 5 AND 25)
AND Pname='ProductX'

EX: For each employee, retrieve the employee’s first and last name and
the first and last name of his or her immediate supervisor.

SOL:
. ‫ رجع االسم االول واالخير للموظف واالسم االول واالخير للموظف المشرف عليه‬, ‫لكل موظف‬
0797658644 ‫محمد بواعنه‬
- 19 -IS

SELECT E.Fname,E.Lname,S.Fname AS FnameSUPER,S.Lname AS LnameSUPER


FROM EMPLOYEE AS E, EMPLOYEE AS S
WHERE E.Super_ssn=S.Ssn;

 Union(‫ )اتحاد‬AND Intersection(‫ )تقاطع‬AND EXCEPT(‫)طرح‬


‫نصي‬, ‫ نفسه يعني نفس نوع البيانات رقمي رقمي‬DOMIN ‫عشان انفذ اي عمليه منهم الزم يكون ال‬ -
. .......... ‫نصي‬

EX: A={1,5,A,8,D},B={1,A,3,8}
1- A UNION B={1,5,A,8,D,3}
2- A Intersection B={1,A,8}
3- A EXCEPT B={5,D}

: DOMIN ‫كيف يعني نفس ال‬ -


B ‫و‬A ‫افرض عندنا جدولين‬
A
NAME SSN GRADE

B
NAME ID INFORMATION
0797658644 ‫محمد بواعنه‬
- 20 -IS

NAME,SSN CHAR(10)
INFROMATIONCHAR(100)
GRADEFLOAT
IDINT

DATA

EX1:

SELECT *
FROM A
UNION
SELECT *
FROM B
‫بعطي خطأ النو‬
IDINT ‫ وال‬SSN CHAR
INFORMATION CHAR ‫ وال‬GRADEFLOAT

EX2:

SELECT NAME,GRADE
FROM A
0797658644 ‫محمد بواعنه‬
- 21 -IS

UNION
SELECT NAME,ID
FROM B

EX3:

A-

SELECT NAME
FROM A
EXCEPT
SELECT NAME
FROM B
‫شاشه فارغه‬

SELECT NAME
FROM B
EXCEPT
SELECT NAME
FROM A

EX4:
0797658644 ‫محمد بواعنه‬
- 22 -IS

Return employee’s name that have children and work in department 5.

SOL:

5 ‫رجع اسماء الموظفين الي عندهم اطفال و يعملو في القسم رقم‬

SELECT Fname,Lname SELECT DISTINCT Fname,Lname


FROM EMPLOYEE FROM EMPLOYEE ,
WHERE DNO =5 EMP_DEPENDENT
Intersect WHERE SSN=ESSN AND DNO=5
SELECT Fname,Lname
FROM
EMP_DEPENDENT,EMPLOYEE
WHERE SSN=ESSN

: ORDER BY ‫ ترتيب البيانات‬


DESC ‫ او تنازليا‬ASC ‫ اما تصاعديا‬QUERY‫وظيفتها ترتيب ناتج ال‬ -
ASC ‫القيمه االفتراضيه الها تصاعدي‬ -
‫ يعني‬WHERE ‫توضع بعد جملة ال‬ -
SELECT ______
FROM_______
WHERE______
ORDER BY_______
0797658644 ‫محمد بواعنه‬
- 23 -IS

EX: Retrieve a list of employees and the projects they are working on, ordered by
department and, within each department, ordered alphabetically by last name, then first
name.
SOL:

Alphabetically ‫ ابجدي‬ ‫تصاعدي‬ASC

SELECT D.Dname, E.Lname, SELECT D.Dname, E.Lname,


E.Fname, P.Pname E.Fname, P.Pname
FROM DEPARTMENT D, OR FROM DEPARTMENT D,
EMPLOYEE E, WORKS_ON W, EMPLOYEE E, WORKS_ON W,
PROJECT P PROJECT P
WHERE D.Dnumber= E.Dno AND WHERE D.Dnumber= E.Dno AND
E.Ssn= W.Essn AND E.Ssn= W.Essn AND
W.Pno= P.Pnumber W.Pno= P.Pnumber
ORDER BY D.Dname, E.Lname, ORDER BY D.Dname ASC, E.Lname
E.Fname; ASC, E.Fname ASC;
0797658644 ‫محمد بواعنه‬
- 24 -IS

‫ عادي‬Aliasing ‫بدون‬

QUESTION:

Q: Make a list of projects number for employees whose last name is ‘smith’ who works on
project or manager on the department responsible for the project.

SOL:

‫ الذي يعمل بمشروع او مدير على القسم المسؤول‬smith ‫كون قائمه ب ارقام المشاريع للموظفين التي االسم االخير لهم‬
. ‫عن المشروع‬
SELECT DISTINCT Pnumber
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE Dnum=Dnumber AND Mgr_ssn=Ssn
AND Lname='Smith'
UNION
( SELECT DISTINCT Pnumber
FROM PROJECT, WORKS_ON, EMPLOYEE
WHERE Pnumber=Pno AND Essn=Ssn
AND Lname='Smith' );

pnumber=pno ‫ وجملة‬project ‫؟ ال لو شلنا ال جدول‬project ‫ هل بالضروره اوخذ جدول ال‬UNION ‫بعد ال‬
OR

SELECT DISTINCT Pnumber


FROM PROJECT, DEPARTMENT, EMPLOYEE,WORKS_ON
WHERE (Dnum=Dnumber AND Mgr_ssn=Ssn AND Lname='Smith') or (Pnumber=pno
and essn=ssn AND Lname='Smith')
0797658644 ‫محمد بواعنه‬
- 25 -IS

Q:

Return the name of the employee responsible for projects whose working hours greater than
15 hours.

SOL:

.‫ ساعه‬15 ‫رجع اسم الموظفين المسؤولين عن المشاريع التي تتجاوز ساعات العمل فيها عن‬

SELECT DISTINCT FNAME,LNAME


FROM EMPLOYEE, DEPARTMENT,PROJECT,WORKS_ON
WHERE SSN=Mgr_ssn AND Dnum=Dnumber AND Pno=Pnumber AND
Emp_Hours>15

Q: Retrieve the names of all employees in department 5 who work more than 10 hours on
the Project X

SOL:

PROJECT X ‫ الذين يعملون اكثر من عشر ساعات على‬5 ‫رجع اسماء الموظفين في القسم‬

SELECT distinct Fname,Lname


FROM EMPLOYEE,PROJECT,WORKS_ON
0797658644 ‫محمد بواعنه‬
- 26 -IS

WHERE Essn=ssn AND Pno=Pnumber AND Pname='ProductX' and Dno=5 AND


EMP_HOURS>10

Q: List the names of all employees who have a dependent with the same first name as
themselves.

SOL:

. ‫قائمه ب اسماء الموظفين الذي اسم العائل له نفس اسمه االول‬

SELECT Fname,Lname
FROM EMP_DEPENDENT,EMPLOYEE
WHERE SSN=Essn AND Dependent_name=Fname

Q: Find the names of all employees who are directly supervised by ‘Franklin Wong’.

SOL:

Franklin Wong ‫بدو اسماء الموظفين الي مسؤول عنهم الموظف‬

SELECT B.FNAME,B.LNAME
FROM EMPLOYEE A,EMPLOYEE B
WHERE A.SSN=B.Super_ssn AND A.Fname='Franklin' AND A.Lname='Wong'
OR
SELECT A.FNAME,A.LNAME
FROM EMPLOYEE A,EMPLOYEE B
WHERE B.SSN=A.Super_ssn AND B.Fname='Franklin' AND B.Lname='Wong'
0797658644 ‫محمد بواعنه‬
- 27 -IS

Q: Find the names of all employees and project number who are directly supervised by
‘Franklin Wong’

SOL:

SELECT distinct B.FNAME,B.LNAME,Pnumber


FROM EMPLOYEE A,EMPLOYEE B,project,DEPARTMENT
WHERE A.SSN=B.Super_ssn AND A.Fname='Franklin' AND A.Lname='Wong' and
b.Dno=dnumber and dnum=Dnumber

Q: For each employee, retrieve the employee’s first last name , dependent name and the
first and last name of his or her immediate supervisor where project name Contain character
‘x’
Sol:
x ‫رجع اسم الموظف واسم العائل واسم السؤول عنه اذا كان اسم البروجكت يحتوي على حرف‬
SELECT E.Fname,E.Lname,Dependent_name,Pname, S.Fname AS
FnameSUPER,S.Lname AS LnameSUPER
0797658644 ‫محمد بواعنه‬
- 28 -IS

FROM EMPLOYEE AS E, EMPLOYEE AS


S,EMP_DEPENDENT,DEPARTMENT,PROJECT
WHERE E.Super_ssn=S.Ssn and E.ssn=essn and E.Dno=dnumber and dnum=Dnumber
and pname like '%x%'

Q: Retrieve SSN AND FNAME of employee they have no dependent :

SOL:

‫رجع اسماء الموظفين الذين ليس لهم عوائل‬

SELECT SSN,FNAME
FROM EMPLOYEE
EXCEPT
SELECT SSN,FNAME
FROM EMPLOYEE,EMP_DEPENDENT
WHERE SSN=ESSN

SSN,FNAME SSN,FNAME ‫ نفسه‬DOMAIN ‫الحظ ان ال‬ 


0797658644 ‫محمد بواعنه‬
- 29 -IS

‫مهــــــــــــم‬

Q:Consider the following operation , what constraint do they violate and how ,explain:
a-
insert into EMP_DEPENDENT values('123456789','Alice','F','1999-12-5','son')

‫ما بيعمل اضافه النو‬


Essn,depndentnameprimary key
b-
insert into EMPLOYEE (ssn,fname) values('58565565','mohammad')
‫ما بيعمل اضافه النو ال‬
dnoFK
null ‫ما بصير ندخل فيه‬

Q15:
Write SQL statement update department number=2 where department number =5?
- What constraint have been violated by this operation explain?
FK constraint ‫ما بصير هون بتكون المشكله‬
-Assume that the update operation on department table will be cascade, what will be the
result of executing the following sql statement after update you have made ,select dnumber
from department_location?
dnumber
1
4
2
2
2

-Assume that the update operation on department table will be SET NULL, what will be the
0797658644 ‫محمد بواعنه‬
- 30 -IS

result of executing the following sql statement after update you have made ,select dnumber
from department_location:
dnumber
1
4
null
null
null

Q16:
1- retrieve location for department number =5 and manger start date =12/2/2002

select Dlocation
from Department , DEPT_LOCATION
where DEPT_LOCATION .Dnumber = 5 and Mgr_start_date = '12-2-2002' and
DEPT_LOCATION.Dnumber = Department .Dnumber

2- retrieve number of hours and project name where the manger name ='john' and
dep_location ='hoston'

select Emp_Hours , Pname


from WORKS_ON, PROJECT, EMPLOYEE , department,DEPT_LOCATIONS
where Fname = 'John'
and
Dlocation = ' Houston'
and
Mgr_ssn = Ssn
and
DEPT_LOCATIONS.Dnumber = department.Dnumber
and
Pno = Pnumber
and Dnum = department.Dnumber

3- For all department mangers retrieve their addresses and the names of the project that
work on 3 hours at least :

select Emp_Addresses , Pname


0797658644 ‫محمد بواعنه‬
- 31 -IS

from EMPLOYEE , WORKS_ON, PROJECT , DEPARTMENT


where Hours >= 3 and Mgr_ssn = Ssn and Essn = Ssn and Pno = Pnumber

--Query 0
--Retrieve the birthdate and address of the employee(s) whose name is
--John B. Smith
--
select bdate,address
from employee
where fname='John' and minit='B' and lname='Smith';
--
--Query 1
--Retrieve the name and address of all employees who work for the Research
--department.
--
select fname,lname,address
from employee,department
where dnumber=dno and
dname='Research';
--
--Query 2
--For every project located in Stafford, list the project number, the
--controlling department number, and the department manager's last name,
--address, and birthdate.
--
select pnumber,dnum,lname,address,bdate
from project,department,employee
where dnum=dnumber and mgrssn=ssn and plocation='Stafford';
--
0797658644 ‫محمد بواعنه‬
- 32 -IS

--
--Query 3
--Make a list of all project numbers for projects that involve an employee whose
--last name is Smith either as a worker or as a manager of the department that
--controls the project.
--
(select distinct pnumber
from project,department,employee
where dnum=dnumber and mgrssn=ssn and lname='Smith')
union
(select pnumber
from project,works_on,employee
where pnumber=pno and essn=ssn and lname='Smith');
--
--Query 5
--Retrieve the names of all employees who have two or more dependents.
--
--Query 8
--For each employee, retrieve the employee's first and last name and the first
--and last name of his or her immediate supervisor
--
select e.fname,e.lname,s.fname,s.lname
from employee as e, employee as s
where e.superssn = s.ssn;
--
--Query 9
--Retrieve all employee SSNs
--
select ssn
from employee;
--
--Query 10
--Retrieve all combinations of SSN and DEPARTMENT NAME
--
select ssn,dname
from employee,department;
--
--Query 11
--Retrieve the salary of every employee
--
select all salary
from employee;
--
--Query 11a`
--Retrieve the distinct salary values
0797658644 ‫محمد بواعنه‬
- 33 -IS

--
select distinct salary
from employee;
--
--Query 12
--Retrieve all employees whose address is in Houston, Texas
--
select fname,lname
from employee
where address like '%Houston, TX%';
--
--Query 13
--Show the resulting salaries if every employee working on the ProductX project
--is given a 10% raise.
--
select fname,lname,1.1*salary as increased_sal
from employee,works_on,project
where ssn=essn and pno=pnumber and pname='ProductX';
--
--Query 14
--Retrieve all employees in department 5 whose salary is between $30,000 and
--$40,000
--
select *
from employee
where dno=5 and (salary between 30000 and 40000);
--
--Query 15
--Retrieve a list of employees and the projects they are working on, ordered
--by department and, within each department, ordered alphabetically by last
--name, first name
--
select dname,lname,fname,pname
from department,employee,works_on,project
where dnumber=dno and ssn=essn and pno=pnumber
order by dname,lname,fname;
--
--
--
0797658644 ‫محمد بواعنه‬
- 34 -IS

:‫ سنوات‬
Q:classfay the following SQL operation DDL or DML:
Update:DML
Create:DDL
Delete:DML

a- What the result of :


Employee(ssn) Except department(mgr_ssn)
SOL:

b- Write SQL statement to delete all data in dep_location:


SOL:
Delete from dep_location
c- Write SQL statement to delete all project in Stafford or in Sugarland
0797658644 ‫محمد بواعنه‬
- 35 -IS

Sol:
delete from project where Plocation='Stafford' or Plocation='Sugarland'

1. What constraint have been violated by this operation explain:


REFERENCE constraint
Pno in works_on reference pnumber in project

2. Assume that the delete operation on project table will be cascade, what will be the
result of executing the following sql statement after deletion you have made :

d- Write SQL statement to modify the location of the project number 1 (pnumber=1) to
‘Hosuton’:
SOL:
update project set Plocation='Houston' where Pnumber=1

e- Consider the following operation , what constraint do they violate and how ,explain:

1. Insert < 'cecilia','f','kono','943775543','1960-04-05','6357 windy


lane,katy',2800,NULL,2> into Employee
. attribute ‫عدد القيم المدخله ال يساوي عدد ال‬

2. Insert < 'productA',NULL,3>into project


SOL:
attribute ‫عدد القيم المدخله ال يساوي عدد ال‬

f- Write SQL queries to do the following:


1. Retrieve full names(first and last) and salaries of all employees with salary between
30000 and 40000
SOL:
Select fname , lname
From employee
Where salary between 30000 and 400000
0797658644 ‫محمد بواعنه‬
- 36 -IS

2. Retrieve full name of all employees whose first name starts will letter ‘j’
SOL:
Select fname,minit,lname
From employee
Where fname like ‘j%’

3. Retrieve the full names of all department mangers:


SOL:
Select fname,minit,lname
From employee, department
Where ssn=mgr_ssn

4. For all department mangers retrieve their addresses and the project numbers of the
project that work on :
SOL:
select Emp_Address,Pno
from DEPARTMENT,EMPLOYEE,WORKS_ON
where ssn=mgr_ssn and ssn=essn

5. For all department mangers retrieve their addresses and the names of the project that
work on:
SOL:
select Emp_Address,Pname
from DEPARTMENT,EMPLOYEE,WORKS_ON,project
where ssn=mgr_ssn and ssn=essn and pno=Pnumber

Q:choose the correct answer:


0797658644 ‫محمد بواعنه‬
- 37 -IS

7)sol:B

Q:
Use company database :
a- .
0797658644 ‫محمد بواعنه‬
- 38 -IS

Sol:
Not correct ,2 not subset department(dnumber)
Referential integrity constraint.

b-.

Sol:
lname hours
wong 10.0
wollace 20.0
wollace 15.0

C:
1- .

Select ssn
From employee,dependent
Where ssn=essn and relationship=’deughters’

2- .

Select dname
From department d,dept_locations dl
Where d.dnumber=dl.dnumber and dlocation=’sugarland’

3- .

Select dependent_name
From employee e,dependent d
Where ssn=essn,d.sex=’F’ and fname=’Franklin’ and minit=’T’ and
lname=’Wong’

4- .
0797658644 ‫محمد بواعنه‬
- 39 -IS

Select hours
From department,works_on,employee
Where ssn=essn and ssn=mgr_ssn and pno=20 and dname=’administration’

Q:

Sol:
Select Essn
From dependent
Where relationship=’daughters’

b)

Sol:
Select dname
From department d,dep_location dl
Where d.dnumber=dl.dnumber and dlocation=’sugarland’
0797658644 ‫محمد بواعنه‬
- 40 -IS

c)

Sol:
Select fname
From works_on,employee
Where essn=ssn and pno=20 and sex=’F’

d)

Delete from project where plocation=’stafford’

e)

Q:

a)

Error ,referential integrity constraint: pno reference pnumber


b)

Error ,key constraint: dnumber and dlocaiton composite primary key

c)

Error ,referential integrity constraint:


Super_ssn(employee) reference ssn(employee)
0797658644 ‫محمد بواعنه‬
- 41 -IS

 Aggregate Functions in SQL

: ‫االقترانات الي رح نتعامل معاها‬


SUM,MAX,MIN,AVG,COUNT

*_* ‫المثال هاذ بلخص كل اشي‬


EX18: what the output of the following query:

1- SELECT COUNT(*)
FROM EMPLOYEE

2- SELECT COUNT(Fname) AS #EMPLOYEE


FROM EMPLOYEE

3- SELECT SUM(FNAME)
FROM EMPLOYEE

4- SELECT SUM(salary)
FROM EMPLOYEE

5- SELECT max(salary)
FROM EMPLOYEE

6- SELECT max(salary) as maxSalary ,min(dno) minDno,count(*)


FROM EMPLOYEE
where fname like 'j%'
0797658644 ‫محمد بواعنه‬
- 42 -IS

7- SELECT max(salary) as maxSalary ,min(fname)


minDname,count(*)
FROM EMPLOYEE
where fname like 'j%'

SOL:
1- .

2- .

3- .
‫ نفس‬avg ‫ بتعامل مع ارقام ولو كان‬sum ‫ وال‬char ‫ من نوع‬fname ‫ النو ال‬error ‫بيعطي‬
. ‫االشي‬

4- .

5- .

6- .

7- .

. ‫هون حسب الترتيب الهجائي بجيب االسم‬


0797658644 ‫محمد بواعنه‬
- 43 -IS

EX19: Return the number of employees who works at department 5


SOL:

5 ‫رجع عدد الموظفين الذين يعملون في القسم رقم‬


select count(*)
from employee
where dno=5

Ex20: return the number of employees and their total salaries for the
employees who work more than 10 hours
Sol:
‫رجع عدد الموظفين و مجموع رواتبهم للموظفين الذين يعملون اكثر من عشر ساعات‬
select count(*) , sum(salary)
from employee,WORKS_ON
where ssn=essn and Emp_Hours>10

EX21: Return the number of depends for the employee JOHN


SOL:
JOHN ‫رجع عدد العوائل للموظف‬
SELECT COUNT(*)
FROM EMP_DEPENDENT,EMPLOYEE
WHERE SSN =ESSN AND FNAME='JOHN'

EX22: Find the sum of the salaries of all employees of the ‘Research’
department, as well as the maximum salary, the minimum salary, and the
average salary in this department.

SOL:
0797658644 ‫محمد بواعنه‬
- 44 -IS

‫ و اعلى راتب و اقل راتب والوسط الحسابي‬RESEARCH ‫مجموع الرواتب للموظفين الي في‬
‫للرواتب‬

SELECT SUM (Salary), MAX (Salary), MIN (Salary), AVG (Salary)


FROM EMPLOYEE ,DEPARTMENT
WHERE Dname='Research' and Dno=Dnumber

EX23: Count the number of distinct salary values in the database.


SOL:
‫عدد الرواتب الغير متكرره‬
SELECT COUNT (DISTINCT Salary)
FROM EMPLOYEE
0797658644 ‫محمد بواعنه‬
- 45 -IS

 Grouping: The GROUP BY and HAVING Clauses:

SELECT ______________
FROM ______________
WHERE ____________
GROUP BY _____________
HAVING ( Condition )

‫المثال هاذ بلخص كل اشي‬


‫ على سبيل المثال بدنا نعرض القسم وكم موظف يعمل‬, ‫ وضيفتها فلترة النتائج‬: GROUP BY ‫ ال‬
. ‫فيه ومجموع الرواتب بنستخدمها‬
‫ موظفين او‬3 ‫ بنعمل شرط مثال بالمثال الي فوق انا بدي اعرض االقسام الي فيها‬: HAVING ‫ ال‬
.)GROUP BY ‫ الزم يكون‬HAVING ‫اكثر (اذا بدي استخدم ال‬

EX25: what the output of the following query:


1- SELECT SUM(SALARY)
FROM EMPLOYEE
GROUP BY DNO
2- SELECT DNO, SUM(SALARY)
FROM EMPLOYEE
GROUP BY DNO

3- SELECT LNAME
FROM EMPLOYEE
GROUP BY DNO
4- .
SELECT DNO
FROM EMPLOYEE
GROUP BY DNO
5- SELECT AVG(SALARY)
FROM EMPLOYEE
GROUP BY DNO

6- SELECT AVG(SALARY)
FROM EMPLOYEE
GROUP BY SALARY
HAVING SALARY >20000
0797658644 ‫محمد بواعنه‬
- 46 -IS

7- SELECT AVG(DNO)
FROM EMPLOYEE
GROUP BY LNAME
HAVING SALARY >20000

SOL:
1- .

2- .

3-
GROUP BY ‫ الزم يكون ب‬SELECT ‫ النو الي الزم نكتبه بال‬ERORR ‫بيعطي‬

4- .

‫الحظ هون ما بعطي خطأ‬

5- .

6- .
0797658644 ‫محمد بواعنه‬
- 47 -IS

GROUP BY ‫ الزم يكون نفسه ب‬HAVING ‫ النو الي بال‬ERORR ‫ بيعطي‬-7

EX19:show the department number , department name and sum of


salary .
SOL:
‫اعرض ارقام و اسماء االقسام ومجموع الرواتب في كل قسم‬
SELECT SUM(SALARY),DNO,Dname
FROM EMPLOYEE JOIN DEPARTMENT ON DNO=Dnumber
GROUP BY DNO , Dname

EX20: retrieve name of employee have 2 dependent or more.


SOL:
‫ قديش رح تريحنا‬HAVING ‫ شوفو ال‬, ‫رجع اسماء الموظفين الي عندهم ولدين او اكثر‬
SELECT Fname,Lname
FROM EMPLOYEE ,EMP_DEPENDENT
WHERE SSN = ESSN
GROUP BY FNAME,LNAME
HAVING COUNT(*)>=2
0797658644 ‫محمد بواعنه‬
- 48 -IS

EX26: For each department, retrieve the department number, the number of
employees in the department, and their average salary.
SOL:
SELECT Dno, COUNT (*), AVG (Salary)
FROM EMPLOYEE
GROUP BY Dno

EX27: For each project, retrieve the project number, the project name, and the
number of employees who work on that project.
SOL:
. ‫ رجع رقم المشروع واسمه وعدد الموظفين الذين يعملون في هذا المشروع‬, ‫لكل مشروع‬
SELECT Pnumber, Pname, COUNT (*) #EMPLOYEE
FROM PROJECT, WORKS_ON
WHERE Pnumber=Pno
GROUP BY Pnumber, Pname

EX28: For each project on which more than two employees work, retrieve the
project number, the project name, and the number of employees FROM
DEPARTMENT 5 who work on the project.

SOL:
‫رجع رقم واسم المشروع وعدد الموظفين الذين يعملون في‬,‫لكل مشروع الذي يعمل فيه اكثر من موظفين‬
. ‫هذا المشروع‬
SELECT Pnumber, Pname, COUNT (*)
FROM PROJECT, WORKS_ON
WHERE Pnumber=Pno AND DNO = 5
GROUP BY Pnumber, Pname
HAVING COUNT (*) > 2
0797658644 ‫محمد بواعنه‬
- 49 -IS

EX29: List the names of managers who have at least two dependent.

SOL:
: ‫قائمه ب اسماء الموظفين المسؤولين الي الهم عائلين على االقل‬
SELECT Fname, Lname
FROM EMPLOYEE
WHERE SSN IN ( SELECT ESSN
FROM EMP_DEPENDENT)
AND SSN NOT IN ( SELECT MGR_SSN
FROM DEPARTMENT)
GROUP BY FNAME,LNAME
HAVING COUNT(*)>=2

EX30:
List of names employee those who have 3 dependent .
Sol:
. ‫ عوائل‬3 ‫قائمه ب اسماء الموظفين الذين لهم‬

select fname,Lname
from employee ,EMP_DEPENDENT
where ssn =Essn
group by fname,lname
having count(Fname)=3
0797658644 ‫محمد بواعنه‬
- 50 -IS

EX31: For each department whose average employee salary is more than
$30,000, retrieve the department name and the number of employees working
for that department.
SOL:
‫استرجع اسم القسم و عدد الموظفين الذين‬, 30000 ‫لكل قسم فيه الوسط الحسابي للراتب اكبر من‬
‫يعملون في القسم‬

SELECT DNAME,COUNT(*)
FROM EMPLOYEE , DEPARTMENT
WHERE DNO=Dnumber
GROUP BY DNAME
HAVING AVG(SALARY)>30000

EX33: Retrieve the department name and department manager full name, for
each department whose maximum salary is more than $41,000.

SOL:
select Dname,fname,lname
from EMPLOYEE , DEPARTMENT
where ssn=Mgr_ssn
group by Dname,fname,Lname
having max(Salary)>41000
0797658644 ‫محمد بواعنه‬
- 51 -IS

EX34: Retrieve the number of female employees for each department whose
maximum salary is more than $41,000.
SOL:
select Dnumber,COUNT(*)
from EMPLOYEE,DEPARTMENT
where dno=Dnumber and sex='f'
group by Dnumber
having max(Salary)>41000
‫‪0797658644‬‬ ‫محمد بواعنه‬
‫‪- 52 -IS‬‬

‫)المشاهد او الجداول الوهميه( ‪ VIEWS‬‬


‫‪ -‬عباره عن جداول وهميه ‪ ,‬وتؤدي نفس دور الجداول العاديه اال انها اسرع من حيث التنفيذ‬
‫‪,‬وكمان بقدر اجمع بيانات اكثر من ‪ TABLE‬في جدول واحد وبصير اسمه ‪. VIEW‬‬
‫‪ -‬انشاء ‪: VIEW‬‬
‫‪CREATE VIEW VIEW_NAME‬‬
‫‪AS QUERY‬‬

‫‪ -‬حذف ‪: VIEW‬‬
‫‪DROP VIEW_NAME‬‬

‫مثال بدنا ننشئ ‪ VIEW‬تحتوي االسم االول للموظفين والراتب اذا كان اكبر من ‪25000‬‬
‫ونسميها ‪: EM_SALARY‬‬
‫‪CREATE VIEW EM_SALARY‬‬
‫‪AS‬‬
‫‪SELECT FNAME,Salary‬‬
‫‪FROM EMPLOYEE‬‬
‫‪WHERE Salary>25000‬‬
‫‪ -‬اذا بدك تشوف وين انشأت بتروح على ‪ COMPANY‬على المجلد ‪: VIEW‬‬

‫بالنسبه لعرض البيانات بعاملها مثل اي جدول ‪:‬‬


‫مثال لو بدنا نعرض بيانات المشهد ‪: EM_SALARY‬‬
‫* ‪SELECT‬‬
‫‪FROM EM_SALARY‬‬
0797658644 ‫محمد بواعنه‬
- 53 -IS

:‫ الحظ تنفيذ االتي‬-


1- SELECT FNAME
FROM EM_SALARY
WHERE FNAME LIKE ‘J%’

2- SELECT FNAME AS E_NAME,SALARY*10 AS NEW_SA


FROM EM_SALARY
WHERE SALARY BETWEEN 30000 AND 40000

EX35:
A- CREATE VIEW (em_de) contains the employee name , dependent
name , salary and dno where dno > 2 .
B- USE VIEW TO SHOW EMPLOYEE NAME AND DEPARTMENT
NAME.
SOL:
‫ يحتوي على اسم الموظف واسم العائل والراتب ورقم القسم اذا كان رقم‬em_de ‫انشئ مشهد اسمه‬
2 ‫القسم اكبر من‬
A-
create view em_de
as select fname,Dependent_name,Salary,Dno
from EMP_DEPENDENT,EMPLOYEE
where ssn=essn and dno>2
0797658644 ‫محمد بواعنه‬
- 54 -IS

B- .
‫ النو فيه اسماء االقسام‬DEPARTMENT ‫ و جدول ال‬EM_DE ‫هون رح نستخدم ال‬
select FNAME,Dname
from em_de,DEPARTMENT
WHERE Dno=Dnumber

EX36: CREATE VIEW (t_pro) contain (proNumber,proName,sum of works


hours and count of employee )
SOL:
‫ يحتوي على ارقام المشاريع واسماء المشاريع ومجموع ساعات العمل في‬T_PRO ‫انشئ مشهد اسمه‬
‫كل مشروع وعدد الموظفين‬
CREATE VIEW T_PRO
AS SELECT PNAME,PNO,SUM(Emp_Hours)AS EM_H,
COUNT(LNAME) AS #EM
FROM EMPLOYEE,WORKS_ON,PROJECT
WHERE Ssn=Essn AND Pno=Pnumber
GROUP BY Pname,PNO

VIEW ‫ وال بعطي خطأ فقط في حالة ال‬OUTPUT ‫ الزم تسمي ال‬FUNCTION ‫اذا استخدمت‬

view ‫ الحظ ناتج هذا ال‬-


CREATE VIEW DEPT_INFO(Dept_name, No_of_emps, Total_sal)
AS SELECT Dname, COUNT (*), SUM (Salary)
FROM DEPARTMENT, EMPLOYEE
WHERE Dnumber=Dno
GROUP BY Dname
‫‪0797658644‬‬ ‫محمد بواعنه‬
‫‪- 55 -IS‬‬

‫طريقه ثانيه ل اعادة تسمية الحقول بدون استخدام ‪. as‬‬


‫‪ ‬التعديل على ال ‪ view‬مش دايما بيزبط ‪-_-‬‬
‫مثال‪ :‬افرض عندنا هاذ الجدول والبيانات التاليه ‪:‬‬
‫‪create table employee(id int primary key ,ename char(100),phone‬‬
‫)‪char(10),salary float‬‬

‫وانشأنا ال ‪ view‬االتيه ‪:‬‬


‫‪create view em_view‬‬
‫‪as‬‬
‫* ‪select‬‬
‫‪from employee‬‬

‫رح تكون البيانات فيها هيك ‪:‬‬

‫‪update ‬‬
‫مثال بدي اعدل االسم في ال ‪view‬من ‪ null‬ل ‪saif‬‬

‫‪update em_view set ename='saif' where id=5‬‬


‫البيانات في ال ‪ view‬الي ‪ id=5‬رح يتغير االسم فيها ل ‪ saif‬وكمان الجدول االساسي رح يتغير فيه‬
‫االسم‬

‫البيانات في ال ‪view:‬‬
‫‪0797658644‬‬ ‫محمد بواعنه‬
‫‪- 56 -IS‬‬

‫البيانات في الجدول ‪employee‬‬

‫‪delete ‬‬
‫اذا حذفنا من ال ‪view‬رح ينحذف من الجدول ‪ employee‬كمان الحظ المثال االتي ‪:‬‬
‫‪delete from em_view where id =5‬‬

‫البيانت في ‪: em_view‬‬

‫البيانات في ‪employee‬‬

‫‪insert ‬‬
‫اذا اضفنا بيانات ‪em_view‬رح تنضاف على ال ‪employee‬‬
‫)'‪insert into em_view values ('6','khaled','045','1044‬‬
0797658644 ‫محمد بواعنه‬
- 57 -IS

: ‫ االتيه‬query ‫ الحظ تنفيذ ال‬-


insert into em_view values ('1','khaled','045','1044')
)primary key( id ‫ النو احنا معرفين ال‬error ‫رح يعطي‬

. ‫ تعامل معاملة الجدول الي انشأناها منه‬update,insert,delete ‫ من حيث‬view ‫ ال‬: ‫ الخالصه‬

‫الحظ الحاله االتيه‬


create view em_view_part
as
select ename as name,phone as ph
from employee
1- insert into em_view_part values('m','52')
null ‫ وال يجوز اضافة‬primary key ‫ خاصيته‬id ‫ النه ال‬error ‫رح يعطي‬
VIEW ‫ وال ب اي طريقه بقدر اضيف بيانات على ال‬-

‫ الحذف والتعديل ما رح يعطي عليهم مشاكل في هاي الحاله‬-

)delete ‫ او‬update ‫ او‬insert ‫ الحظ الحاله االتيه (بهاي الحاله ما بيزبط نعمل‬
create view No_InUpDe
as
select count(*) as #emp,sum(id) as SumId
0797658644 ‫محمد بواعنه‬
- 58 -IS

from employee
delete from No_InUpDe where #emp=1: ‫افرض اعملنا هيك‬
. employee ‫ النو اصال هاي البيانات مشتقه من ال‬error ‫بيعطي‬

:‫ الحظ الحاالت االتيه‬


A- .
create view view_con
as
select id,ename
from employee
where id>2
CONSTRAINT ‫هون بقدر احذف واعدل واضيف بدون اي مشاكل مع مراعات ال‬

B- .
create view view_con_ADDATT
as
select id,ename,salary*110 AS NEW_SALARY
from employee
where id>2
‫ التعديل بيزبط بكل الحاالت اال حالة اعدل‬, ‫ لكن الحذف بتزبط‬ERROR ‫ ما بتزبط بعطي‬INSERT ‫ال‬
NEW_SALARY ‫على‬
: update ‫ و‬insert ‫ و‬delete ‫ في‬error ‫ حاالت ال‬
Function -1
group by -2
DISTINCT -3
. ‫ من جدولين او اكثر‬view ‫ انشاء ال‬-4

Q: Specify the following views in SQL on the COMPANY database schema


A. A view that has the department name, manager name, and manager
salary for every department.
SOL:
‫مشهد يحتوي اسم القسم والمسؤول عنه وراتب المسؤول‬
0797658644 ‫محمد بواعنه‬
- 59 -IS

CREATE VIEW MGR_SA


AS
SELECT Dname,FNAME,Salary
FROM EMPLOYEE,DEPARTMENT
WHERE SSN=Mgr_ssn

B. A view that has the employee name, supervisor name, and employee
salary for each employee who works in the ‘Research’ department.
SOL:
‫ واسم المسؤول وراتب الموظف للموظفين الذين يعملون في‬, ‫مشهد يحتوي على اسم الموظف‬
RESEARCH ‫قسم ال‬
CREATE VIEW D_N_S(EMP_NAME,SUPER_NAME,EMP_SALARY)
AS
SELECT E2.Fname ,E1.Fname,E2.SALARY
FROM EMPLOYEE E1,EMPLOYEE E2,DEPARTMENT
WHERE E1.SSN=E2.Super_ssn AND E1.Dno=Dnumber AND
Dname='Research'

EX37: Consider the following view, DEPT_SUMMARY, defined on the


COMPANY database in Figure 3.6:

CREATE VIEW DEPT_SUMMARY (D, C, Total_s, Average_s)


AS SELECT Dno, COUNT (*), SUM (Salary), AVG (Salary)
FROM EMPLOYEE
GROUP BY Dno;
What Output of following:
a- SELECT *
FROM DEPT_SUMMARY;

b- SELECT D, C
FROM DEPT_SUMMARY
WHERE TOTAL_S > 100000;
0797658644 ‫محمد بواعنه‬
- 60 -IS

C- SELECT D, AVERAGE_S
FROM DEPT_SUMMARY
WHERE C > ( SELECT C FROM DEPT_SUMMARY WHERE D=4);

D- .
UPDATE DEPT_SUMMARY
SET D=3
WHERE D=4;

E- .
DELETE FROM DEPT_SUMMARY
WHERE C > 4;

Sol:

a-

b-

c-
d- .
group by ‫ و‬function ‫ النو مستخدمين‬error ‫بيعطي‬

e- .
d ‫ مثل الفرع‬error ‫بيعطي‬
0797658644 ‫محمد بواعنه‬
- 61 -IS

‫سنوات‬
1) Specify the results of the following queries applied to COMPANY
database state shown above:

a) SELECT Dno, COUNT(*), b SELECT Fname, salary


MAX (Salary) )
FROM EMPLOYEE
FROM EMPLOYEE
WHERE EXISTS ( SELECT
GROUP BY Dno *
HAVING COUNT(*) > 2 FROM
DEPENDENT
WHERE
Ssn=Essn )

1) Write SQL code to add a new attribute /column age of type integer to
the DEPENDENT relation.
ALTER TABLE DEPENDENT ADD age int;
0797658644 ‫محمد بواعنه‬
- 62 -IS

2) Using SQL Specify a view that has the project name, number of
employees, and total hours worked on the project for each project with
more than two employees working on it.
Create view summery(pname,pnumber,#employee,totalhours)
as
SELECT Pname ,Pnumber, COUNT (*) #EMPLOYEE,sum(hours)
FROM PROJECT, WORKS_ON
WHERE Pnumber=Pno
GROUP BY Pnumber, Pname

Consider the following ER diagram given below and answer the following
questions:

2) Answer with true or false


0797658644 ‫محمد بواعنه‬
- 63 -IS

A. All the items must be contained by ( f )


orders.
B. Some orders might not be bought by ( f )
customers.
C. Contains is a weak relationship ( f )
D. A customer can buy more than one ( T )
order
E. The attribute areaCode is a derived ( f )
attribute.
F. Supplier is a weak entity type ( f )

3) Name one relationship attribute. Unitcost,Quantity


4) What is the cardinality ratio for customer:order in buy relationship.1:M
5) Name two composite attributes.
1- Phone 2- supAdd

You might also like