Está en la página 1de 15

Name:Ali Bajawi ID number :201609284

Assignment

Database system 1 Lab

You have the following table Employee write a SQL commands for all question below :

Table Name : Employee

Employee_id First_name Last_name Salary Joining_date Department

1 John Abraham 1000000 01-JAN-13 12.00.00 AM Banking

2 Michael Clarke 800000 01-JAN-13 12.00.00 AM Insurance

3 Roy Thomas 700000 01-FEB-13 12.00.00 AM Banking

4 Tom Jose 600000 01-FEB-13 12.00.00 AM Insurance

5 Jerry Pinto 650000 01-FEB-13 12.00.00 AM Insurance

6 Philip Mathew 750000 01-JAN-13 12.00.00 AM Services

7 TestName1 123 650000 01-JAN-13 12.00.00 AM Services

8 TestName2 Lname% 600000 01-FEB-13 12.00.00 AM Insurance

1. Get all employee details from the employee table


Select * from employee;

2.Get First_Name,Last_Name from employee table

Select * First_Name,Last_Name from employee;


1. Get unique DEPARTMENT from employee table
Name:Ali Bajawi ID number :201609284
Select distinct department from employee;
1. Get all employee details from the employee table order by First_Name Ascending
Select * from employee order by FIRST_NAME asc;

1. Get all employee details from the employee table order by First_Name descending
Select * from employee order by FIRST_NAME desc;
1. Get employee details from employee table whose employee name is “John”
2. Select * from employee order by FIRST_NAME='john';
1. Get employee details from employee table whose employee name are “John” and “Roy”

Select * from employee where FIRST_NAME is('john','roy');

1. Get employee details from employee table whose first name starts with 'J'
Select * from employee order by FIRST_NAME like 'J%';
1. Get employee details from employee table whose first name ends with 'n'
Select * from employee order by FIRST_NAME like 'N%';

1. Get employee details from employee table whose Salary greater than 600000
Select * from employee where salary >600000;

1. Get employee details from employee table whose Salary less than 800000
2. Select * from employee where salary >800000;

1. Get employee details from employee table whose Salary between 500000 and 800000
2. Select * from employee where salary between 500000 and 800000;
1. Get department ,total salary from employee table.
Selct employee ,sum(salary) from employee;

1. Get department ,no of employees from employee table

Select employee ,count(FIRST_NAME )from employee;

1. Get department , average salary from employee table


2. Select employee ,avg(salary) from employee;
1. Get department , maximum salary from employee table
2. Select employee ,max(salary) from employee;
1. Get department , minimum salary from employee table
Select employee ,min(salary) from employee;

18.Delete employee data from employee table


Delete from employee;

19.Write syntax to delete table employee


DROP table employee;

1. Update the department for all employees to become ‘services’

;'Update employee set department ='services


Name:Ali Bajawi ID number :201609284

Assignment (Chapter 3)

Find Candidate keys , Primary Key , Unique Keys for the following tables :

1. Table name : school

teacher_id subject teacher_age

111 Maths 38

111 Physics 38

222 Biology 38

333 Physics 40

333 Chemistry 40

Candidate keys : teacher_id


Primary key : teacher_id
Unique keys :none

2. Table name : Employee

Emp_SSN Emp_Number Emp_Name

123456789 226 Steve

999999321 227 Ajeet

888997212 228 Chaitanya

777778888 229 Robert

Candidate keys :emp_ss and emp_number


Primary key :emp_ss
Unique keys :emp_number
3. Table name : Items
Name:Ali Bajawi ID number :201609284
Candidate keys :supller_id and item_id

Primary key : supller_id and item_id

Unique keys :none

4. Table name :Product_Vendor

Candidate keys :VendorID


Primary key :VendorID
Unique keys :none

5. Table name : Purchase

Candidate keys :customer ID


Primary key :customer ID
Unique keys :none

1. Draw a relational schema diagram specifying the foreign keyfthis schema.

F.K F.K

F.K

F.K F.K
Name:Ali Bajawi ID number :201609284

2. Draw a relational schema diagram specifying the foreign keys for this schema.

F.K

F.K

F.K

3. Draw a relational schema diagram specifying the foreign keys for this schema.

F.K

F.K

F.K

F.K
Name:Ali Bajawi ID number :201609284
4. Draw a relational schema diagram specifying the foreign keys for this schema.

F.K

F.K F.K

5. Draw a relational schema diagram specifying the foreign keys for this schema.

F.K F.K

F.K F.K

6.Draw a relational schema diagram specifying the foreign keys for this schema.
Name:Ali Bajawi ID number :201609284

F.K

F.K F.K

F.K

7. Draw a relational schema diagram specifying the foreign keys for this schema.

F.K

F.K F.K

Assignment (Chapter 4)
Name:Ali Bajawi ID number :201609284
For the following binary relationships, suggest cardinality ratios based on the common- sense meaning of the entity
types.

Entity 1 Cardinality Ratio Entity 2


1. STUDENT ________1:1______ STUDENT CARD
2. CUSTOMER ________M:1______ FLIGHT
3. PATIENT _______M:M______ DOCTOR
4. BOOK ________M:1 ______ PUBLISHER

5. INVOICE _____ M:M _________ ITEM

1. Design an ER schema for this application, and draw an ER diagram for the schema. Specify key
attributes of each entity type, and structural constraints on each relationship type. Note any
unspecified requirements, and make appropriate assumptions to make the specification
complete.

 A DB is designed for a library to manage data of books and borrowing transactions

 The library contains books which are described by an ISBN, Title, Specialization. Each Book is
published by one Publisher and written by many Authors.

 A publisher is described by name, ID, Address, Fax and Phone number

 An author is described by name, ID, Address, Fax and Phone number. Each author writes many
books.

 A borrower is described by Card Number, Name, Address. It is required to keep track of date
out and due date of each borrowing transaction.

 It is required to keep track of date out and due date of each borrowing transaction.
Name:Ali Bajawi ID number :201609284

2.Design an ER schema for this application, and draw an ER diagram for the schema. Specify key
attributes of each entity type, and structural constraints on each relationship type. Note any
unspecified requirements, and make appropriate assumptions to make the specification complete.

 Each Doctor is described by Doc ID, Phone, address, and specialization. Each doctor is allowed
to work in only one department.

 Each department is described by Dept no, Name, No of doctors and is supervised by a doctor.

 Each patient is described by Card number, name, address, and phone. Each patient is assigned
to one or more department. It’s required to keep track each admission date.

 Each lab is described by LabNo, name. It’s required to keep track any lab service that may be
done to the patient.
Name:Ali Bajawi ID number :201609284

3. Design an ER schema for this application, and draw an ER diagram for the schema. Specify key
attributes of each entity type, and structural constraints on each relationship type. Note any
unspecified requirements, and make appropriate assumptions to make the specification complete.

 Each customer has name , driver id, and address .

 Each car has license , model and year .

 Each accident has report number , location , and date .

 customers own one or more cars each.

 Each customer has associated with it zero to any number of recorded accidents.

 Assignment (Chapter 6)

1. Consider the following schema for a library database

Author ( authorname, citizenship, birthyear)

Book(isbn, title, authorname)

Topic(isbn, subject)

Branch(libname, city)

Instock(isbn, libname, quantity)

Write relational algebra expressions for the following queries :

a. Give all authors born after 1940.


σ (birthyear> 1940)Author
Name:Ali Bajawi ID number :201609284

b. Give the names of libraries in Sydney.


Librame σ(city='sydney')Brabch

c. Give the title of each book on the topic of either alcohol or drugs.
σ (subject='alcohol'or subject= 'drugs')Book
d. Give the title and author of each book of which at least two copies are held in a branch located in Melbourne.
Authorname σ(quantity>=2 AND city='Melbourn') BOOK INSTOCK
e. Give the name of each Italian author who wrote an autobiography.
σ (subject=authomame AND citizenship='litaly')Author Book

2. Consider the following relations:


Student(ssn, name, address, major)
Course(code, title)
Registered(ssn,code,mark)

Write relational algebra expressions for the following queries :

a. List the codes of courses

Cod(courses)
b. List the marks of student who has name=’Ali’
Marks(Ali)

c. List the name students who are their address in ‘jazan'

Name(jazan)

3. Consider the following relations:

student(id, name)

enrolledIn(id, code)

subject(code, lecturer)

Write relational algebra expressions for the following queries :

a. What are the names of students enrolled in cs3020?

Name(σ cs3020=code(student enrolledin))


b. Which subjects is Hector taking?

Code(σ name =hector(student enrolldin))


c. Who teaches cs1500?

Ecurer(σ code = cs1500(subject))

d. Who teaches cs1500 or cs3020?

Ecturer(σ code=cs1500 or code=3020(subject))

e. Who teaches at least two different subjects?

Ecturer (σ R hectore=S lecture and R cod>S cncle)


Name:Ali Bajawi ID number :201609284
f. What are the codes of all the subjects taught?

Code(subject)
g. What are the names of all the students?

Name(student)
h. What are the names of all the students in cs1500?

Name(σ code=cs1500(student enrolledin))

Assignment (Chapter 7)

1. Consider the relation Reg is in 1NF. Decompose the given relation into 2NF and 3NF with its
functional dependencies

Table : Reg

Course Semester TeacherID TeacherName Course name

IT101 2009-1 332 Mr Jones Programming

IT101 2009-2 332 Mr Jones Programming

IT102 2009-1 495 Mr Bentley Databases

IT102 2010-1 332 Mr Jones Databases

IT103 2009-2 242 Mrs Smith Web Design

ED1: (Course)(Semester)(Teacher ID).


ED2; (Course)(Course name).
ED3: (Teacher ID)(Teatcher Name).
2. Consider the relation Users is in 1NF. Decompose the given relation into 2NF and 3NF with its
functional dependencies

Table : Users
Name:Ali Bajawi ID number :201609284

ED1: (User ID)(Name).


ED2; (URL)(Company)(Company address).

ED3: (User ID)(URL).

3. Consider the relation STCO is in 1NF. Decompose the given relation into 2NF and 3NF with its
functional dependencies

Table : STCO

Stid St_name Address C_code Cname Hours T_id Tname T_dept Grade

5 Fahad Jazan CS101 AI 3 7 ALI CS A

5 Fahad Jazan CS102 C 4 12 Sami IS B

5 Fahad Jazan CS103 C++ 4 2 Faris CS C+

5 Fahad Jazan CS325 DB1 3 2 Khaled CS B+

5 Fahad Jazan CS426 DB2 3 3 Ahmed IS B

ED1: (Student ID)(Student Name)(Address).

ED2: (Course Code)(Course Name)(Hours)(Teacher ID).

ED3: (Teacher ID)(Tetcher Name)(Techer dept).

ED4: (Student ID)(Course ID)(Grade).


Name:Ali Bajawi ID number :201609284

Exercises of Chapter (6)

1. Consider the following EMPLOYEE table :

Use above relation EMPLOYEE and write the relational algebra query for the following
:

a. Retrieve the records form EMPLOYEE whose department is 4.


b. Select the tuples where salary > 30000.
c. Retrieve the records of all employee who are salary>30000 and work for department number 4.

Use above relation EMPLOYEE and write the relational algebra query.

List First name, Last name and salary of all employees who work in department number 5.
Name:Ali Bajawi ID number :201609284

También podría gustarte