You are on page 1of 9

TEMASEKPOLYTECHNIC SCHOOLOFINFORMATICS&IT DatabaseInformationSystems(CFI1C07) AY2011/2012OctSemester Lab 3 Create Database Objectives - On completion of this practical, the students will be able

e to implement: Primary key constraint Foreign key constraint

Preparation 1. Copy a file named company.sql from OLIVE Logon to OLIVE Choose Database Information Systems Course Document Lab Right click on company.sql, and click Save Target As Save the file as company.sql in your thumb drive. 2. Starting MySQL At the bottom left of the screen, click Start Program MySQL Select MySQL Query Browser 3. Access to MySQL Server Type ServerHost, UserName, Password and Default Schema. Click OK. 4. Open company.sql and populate world database At right of the screen, click your own database. Open script file, click File Open Script. And navigate to the thumb drive and open up the company.sql in your thumb drive. Click Execute to populate database into your own database area.

Relational Schema: Emp (EmpNo, EName, Job, Mgr, Hiredate, Sal, Comm, DeptNo) Dept (DeptNo, DName, Loc)

DBIS(CFI1C07)Lab3CreatingDatabase

Primary Key: 1. Enter the following department record into the Dept table. Select the Dept table. At bottom of the screen, click Edit. Double click each column and enter data. Click Apply Changes to insert new data into a table. DeptNo 50 50 DName HR HR Loc LA LA

2. Are both records inserted successfully? Only the first record is inserted successfully. The second insert operation fail. 3. Which relation property is violated? It violates the primary key constraint. DeptNo 50 exists in Dept table. DeptNo must be unique.

4. Insert the following department record. DeptNo DName PRODUCTION Loc DALLAS

Why cant the record be added?

It violates entity integrity constraint. Value of DeptNo is NULL. 5. Choose Emp table and right click Edit Table. Remove Primary key constraint from EmpNo column. And click Apply Changes Execute

6.

Double click on Emp table to display the query command in the text box. Click Execute to display the employee data contained in the table.

DBIS(CFI1C07)Lab3CreatingDatabase

7. Insert new data with EmpNo = 7844. Modify SQL statement as following: and click Execute.

INSERT INTO t0601111c.emp (EmpNo,Ename,DeptNo) VALUES (7844,'YourName',10);


Display the employee data.

8. Is the insertion successful? Why? Yes the record is inserted successfully. There is no primary key for EMp table and duplicated records got entered.

9. Delete the record from Emp by executing following SQL statement. And click Execute

DELETE FROM t0601111c.emp WHERE Ename= 'YourName';


Display the employee data.

Composite Key: 10. Create a Project table with the following columns. Remember to create a primary key for the table. Column Name Pcode Description Duration Data Type CHAR VARCHAR INT Size 4 30 2 Remarks Project code to identify each project Brief description of the project Project duration in month

11. Enter the following data into the Project table. Pcode P001 P002 P003 Description Billing software Medical claim software Inventory software Duration 6 8 5 3

DBIS(CFI1C07)Lab3CreatingDatabase

12. Create an Attachment table with the following columns. Identify and create the primary key for the table. Note that an employee can be attached to more than one project; and a project can have more than one member. (Refer to the sample data in the next step) Column Name Member Project Status Data Type INT CHAR CHAR Size 4 4 2 Remarks Employee attached to a project Project to which the employee is attached FT for full time attachment, PT for part time

What is the primary key for the Attachment table? Member and Project

What is a composite key? It is a primary key made up of more than one attribute that uniquely identifies each tuple in a relation.

How do you represent the Attachment table as a relation in the relational model? Attachment (Member, Project, Status)

13. Enter the following data into the Attachment table. Member 7782 7782 7788 Project P001 P002 P003 Status PT PT FT

Does any record violate the primary key constraint?

14. Enter the following data into the Attachment table. Member 7654 Project Status PT

Can the record be added? Why or why not? No. Project cannot be NULL.

DBIS(CFI1C07)Lab3CreatingDatabase

DBIS(CFI1C07)Lab3CreatingDatabase

Foreign Key: The Emp table has a foreign key (the DeptNo column) that references the primary key of the Dept table. The existence of the foreign key dictates that each values in the DeptNo column of Emp must exist in the DeptNo column of Dept, or it must be null. That is, every employee must work in an existing department. 15. Enter the following data into the Emp table. EmpNo 9000 Ename E9000 Job Mgr HireDate Sal Comm DeptNo 90

Can the record be added? What is wrong with this record? Yes, Dept table does not have DeptNo 90.

16. Delete the record for department 10 from Dept table. Note that there are employee records in the Emp table still referencing (working in) department 10. Execute following SQL statement. And click Execute

DELETE FROM t0601111c.dept WHERE DeptNo= 10;


Can the record for department 10 be deleted? Yes. How does the deletion cause the database to be inconsistent? DeptNo in Emp table cannot reference to the record in the Dept Table. 17. Enter the following data into the Dept table. DeptNo 10 90 DName ACCOUNTING SURVEY Loc NEW YORK SAN FRANCISCO

DBIS(CFI1C07)Lab3CreatingDatabase

18. Create a foreign key constraint between Emp and Dept as follow. Choose Emp table and right click Edit Table Select Foreign Key tab Click +

Type emp_fk as Key Name

Select Dept as referenced table

Click Apply Changes Execute

DBIS(CFI1C07)Lab3CreatingDatabase

19. Enter the following data into the Emp table. Can the record be added? EmpNo 8500 Yes. 20. Delete the record for department 10 from Dept table. Can the record be deleted? Ename E8500 Job Mgr HireDate Sal Comm DeptNo 85

21. Change the Foreign Key constraint by selecting ON DELETE CASCADE.

22. Delete the record for department 10 from Dept table. Can the record be deleted? Yes. What has happened to the employee records in the Emp table. The employee records in Emp table where DeptNo = 10 are deleted

23. Change the Foreign Key constraint by selecting ON UPDATE CASCADE.

24. Change the record for department 30 to 35 from Dept table. Can the record be updated? Yes. DBIS(CFI1C07)Lab3CreatingDatabase 8

What has happened to the employee records in the Emp table. Employee records in Emp table where DeptNp = 30 are updated to 35

25. Drop all tables. Choose a table and right click Drop Table. Which table should be dropped first? Why? Emp table should be dropped first. Emp table does not have foreign key reference from another table.

~ End ~

DBIS(CFI1C07)Lab3CreatingDatabase

You might also like