17
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_na me Last_na me Salar y Joining_date Departme nt 1 John Abraham 10000 00 01-JAN-13 12.00.00 AM Banking 2 Michael Clarke 80000 0 01-JAN-13 12.00.00 AM Insuranc e 3 Roy Thomas 70000 0 01-FEB-13 12.00.00 AM Banking 4 Tom Jose 60000 0 01-FEB-13 12.00.00 AM Insuranc e 5 Jerry Pinto 65000 0 01-FEB-13 12.00.00 AM Insuranc e 6 Philip Mathew 75000 0 01-JAN-13 12.00.00 AM Services 7 TestName 1 123 65000 0 01-JAN-13 12.00.00 AM Services

cscbank.info€¦  · Web viewGet employee details from employee table whose employee name is “John”

Embed Size (px)

Citation preview

Page 1: cscbank.info€¦  · Web viewGet employee details from employee table whose employee name is “John”

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 Departm

ent

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 65000

001-JAN-13 12.00.00 AM Services

8 TestName2 Lname% 60000

001-FEB-13 12.00.00 AM Insurance

Page 2: cscbank.info€¦  · Web viewGet employee details from employee table whose employee name is “John”

Name:Ali Bajawi ID number :201609284

1. Get all employee details from the employee tableSelect * from employee;

2.Get First_Name,Last_Name from employee table

Select * First_Name,Last_Name from employee;1. Get unique DEPARTMENT from employee tableSelect distinct department from employee;1. Get all employee details from the employee table order by First_Name AscendingSelect * from employee order by FIRST_NAME asc;

1. Get all employee details from the employee table order by First_Name descendingSelect * 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 600000Select * from employee where salary >600000;

1. Get employee details from employee table whose Salary less than 8000002. 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 tableDelete from employee;

Page 3: cscbank.info€¦  · Web viewGet employee details from employee table whose employee name is “John”

Name:Ali Bajawi ID number :20160928419.Write syntax to delete table employeeDROP table employee;

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

Update employee set department ='services;'

Assignment (Chapter 3)

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

1. Table name : school

 Candidate keys : teacher_idPrimary key : teacher_idUnique keys :none

2. Table name : Employee

Emp_SSN

Emp_Number

Emp_Name

123456789 226 Steve

999999321 227 Ajeet

888997212 228 Chaitanya

777778888 229 Robert

teacher_id subject teacher_a

ge

111 Maths 38111 Physics 38222 Biology 38333 Physics 40

333 Chemistry 40

Page 4: cscbank.info€¦  · Web viewGet employee details from employee table whose employee name is “John”

Name:Ali Bajawi ID number :201609284 

Candidate keys :emp_ss and emp_numberPrimary key :emp_ssUnique keys :emp_number3. Table name : Items

Candidate keys :supller_id and item_id

Primary key : supller_id and item_id

Unique keys :none

4. Table name :Product_Vendor

Candidate keys :VendorIDPrimary key :VendorIDUnique keys :none

5. Table name : Purchase

Candidate keys :customer IDPrimary key :customer IDUnique keys :none

Page 5: cscbank.info€¦  · Web viewGet employee details from employee table whose employee name is “John”

Name:Ali Bajawi ID number :2016092841. Draw a relational schema diagram specifying the foreign keyfthis schema.

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

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

F.K F.K

F.K F.K

F.K

F.K

F.K

F.K

F.K

Page 6: cscbank.info€¦  · Web viewGet employee details from employee table whose employee name is “John”

Name:Ali Bajawi ID number :201609284

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

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

F.K

F.K F.K

Page 7: cscbank.info€¦  · Web viewGet employee details from employee table whose employee name is “John”

Name:Ali Bajawi ID number :201609284

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

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

F.K

F.K F.K

F.K

F.K

F.K F.K

Page 8: cscbank.info€¦  · Web viewGet employee details from employee table whose employee name is “John”

Name:Ali Bajawi ID number :201609284

Assignment (Chapter 4)

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 CARD2. CUSTOMER ________M:1______ FLIGHT 3. PATIENT _______M:M______ DOCTOR4. 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.

F.K

F.K F.K

Page 9: cscbank.info€¦  · Web viewGet employee details from employee table whose employee name is “John”

Name:Ali Bajawi ID number :201609284 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.

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.

Page 10: cscbank.info€¦  · Web viewGet employee details from employee table whose employee name is “John”

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)

Page 11: cscbank.info€¦  · Web viewGet employee details from employee table whose employee name is “John”

Name:Ali Bajawi ID number :2016092841. 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

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?

Page 12: cscbank.info€¦  · Web viewGet employee details from employee table whose employee name is “John”

Name:Ali Bajawi ID number :201609284Name(σ 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)

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

Page 13: cscbank.info€¦  · Web viewGet employee details from employee table whose employee name is “John”

Name:Ali Bajawi ID number :201609284

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

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 Ahme IS B

Page 14: cscbank.info€¦  · Web viewGet employee details from employee table whose employee name is “John”

Name:Ali Bajawi ID number :201609284

d

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).

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.

Page 15: cscbank.info€¦  · Web viewGet employee details from employee table whose employee name is “John”

Name:Ali Bajawi ID number :201609284

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.