Sunteți pe pagina 1din 11

Laboratory Activity 3 - Pr imary and Foreign Keys

ENGINEE 7/7/2019
CPE011 - CPE21FA1 Engr. Royce B. Chua

2.Create a table named students with attributes:


•ID(Primary Key)
•First Name
•Last Name
•Program

Observation

- Upon creating a studentdb_AGUINALDO I created a table in called students as


instructed by the activity and declared ID as a primary key.

3.Create another table named Courses with attributes


•Course Code(Primary Key)
•Course Description
•Department(Not Null)

Observation

- I created a new table named courses and declared the column course code as the
primary key.

4.Create a third table named Sections with Attributes


•Student Number(References from students)
•Course Code(References from courses)
•Section (Primary Key)
•Class Code
Observation

- Created a new table named sections declared section as a primary key and
declared two foreign keys for student number linked to the ID in the students table,
and course code linked to the course code in the course table.

5.Create a fourth table named classrooms with attributes


•Room Number (3210, 5204)
•Time (ex. 730, 830, 1230, 1330)
•Duration
•Course Code (Reference from courses)
•Section (Reference from sections)

Observation

- Upon creating the last table named classrooms, I have encountered an error
upon declaring course code and section as a foreign key. The instructions earlier
is followed precisely, but it encounters error.

7. Try inserting a value into classrooms. What is the result?

Observation

- It shows an error saying that there is a constraint on the following foreign keys
present on the table.
8. Try inserting a value into sections. What is the result?

Observation

- Same result with previous task, it prompts an error because no value was
registered to the primary keys.

9. Populate the students and courses table with 5 records each


Observation

- Using the insert syntax, I have successfully added new values to tables with
primary keys.

11. Insert records into sections with a student number based from existing
student IDs and with a course code based from existing course codes records.
What is the result?

Observation

- I added value to sections table since an existing value on the primary key was used
on this particular table.
12. Insert records into sections with a student number that does not exist
student table ID but has a course code that exists in the courses table. What is
the result?

Observation

- An error has occurred since a non-existing value from the primary key was used to
add values in this table.

13. Populate the sections table with at least 5 records.


Observation

- Using insert, I have successfully added new values to the sections table, while
using pre-existing value from the student table.

15. Insert values into classrooms with the same records


(ex. 3210,730,2,’CPE011’,’CPE21FB1’) two times. What was the result?

Observation

- Adding duplicate values on a table is possible.

16. Delete all the values in the classroom.


Observation

- using delete syntax, I deleted duplicate values that were inserted in this table.

17. Implement a primary key in classrooms. Use techniques you’ve learned


previously to add a classroom ID.
Observation

- I added a new column in the classrooms table and declared it as a primary key.

18. Try inserting two identical records into classroom again. What is the result?
Observation

- Duplicate value is not allowed since primary key must have a unique value on its
data.

19.Delete all the values in the classroom.


20. Run this command:
ALTER TABLE classrooms ADD UNIQUE KEY uk_roomtime (room_number,time);

Observation

- I noticed that a binding key has been added to the room number it is linked to the
time as indicated in the command.

21. Try inserting two identical records into classrooms again. What is the
result?

Observation

- Upon inserting two sets of values, it prompted an error saying that the data that was
inserted on the section column is not possible since we declared section as a prim.
key
22. Populate classroom with at least 5 records.

Observation

-Then I inserted data on this.


OUTPUT
Supplementary
Activity:

Questions

1. Explain the results of step 7 and 8 in Procedure. Why did they give the outputs that
they did?
- The user can't insert values in it because the user can't add or update a child
row. Adding of values must be done first before making it into a foreign key. In this
case, the instructions was to make it a foreign key/primary key first before
inserting some values to the table.

2. Explain the results of step 11 and 12 in Procedure. Why did they give the outputs
that they did?

- The user must use data from the table that your foreign key is linked. Inserting data
with unknown value will prompt an error.

3. Why is it necessary to implement a primary key and foreign key for a database?
- To link two different tables and to have organized data sets.

4. Cite your own real-life situation where a primary key and foreign key in a database
is necessary. Explain.
- Accounting firm, and the transactions which is saved throughout their clients time
frame, and each transaction is connected to another table that stores the date and
other aspects of their livelihood.

5. Why did implementing the primary key in classrooms fail to solve the redundancy
problem?
- It prevents the user to input duplicate data.

6. What do you think is the difference between the Primary Key and the Unique Key?
- Primary key is used to determine rows or record on a given table so duplicate
values are restricted, while Unique key only prevents duplicate values in a column.
Summary

Our professor discussed about the use of primary keys and foreign keys and their
function and uses in handling many tables and to generate information according
to the interconnected or branched out data.

Conclusion

Each user with the proper knowledge in setting foreign keys and primary keys, the
user can generate generous amount of information according to the relationship of
each entries given at any table. And with proper knowledge, one can utilize the
use of SQL database.

Recommendation

One must study about different techniques used in this kind of database
management activity so that one can maximize the output within a short time. In
real-life and real time situation, one must be efficient in a way that a task can be
done in a short amount of time, so I recommend a database management system
with just a little bit of click and drag style and clicking checkboxes in order to
shorten working time frame.

Lesson Learned

SQL follows strict rules and protocol in adding new data in new tables and
databases. I also learned that. You cannot insert value to foreign keys, because it
must be the same on the primary key.

S-ar putea să vă placă și