Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab No. 04

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

LAB NO.

04 DATE: 12/09/2023
HOW TO CREATE PRIMARY and FOREIGN KEYS IN MS SQL
SERVER

Lab outcomes:
After completing this lab, students will be able to:
 Know about Microsoft SQL server
 Know about SQL server management studio
 Create table in SQL server
 Create primary and foreign keys

Corresponding CLO and PLO:


 CLO-1, PLO-3(Design and Development solutions)

SOFTWARE REQUIRED:
 Microsoft SQL server
 SQL server management studio

Theory:
Primary Key:
A Primary key is a unique column we set in a table to easily identify and locate data in
queries. A table can have only one primary key. The primary key column has a unique value
and doesn’t store repeating values. A Primary key can never take NULL values.
For example, in the case of a student when identification needs to be done in the class, the
roll number of the student plays the role of Primary key. Similarly, when we talk about
employees in a company, the employee ID is functioning as the Primary key for
identification.
Let us now understand the Syntax of creating the table with the Primary key specified.
Syntax:

CREATE TABLE tableName (


col1 int NOT NULL,
col2 varchar(50) NOT NULL,
col3 int,
…………….
PRIMARY KEY (col1) );
What is a Foreign key in SQL?
A Foreign key is beneficial when we connect two or more tables so that data from both can
be put to use parallel. A foreign key is a field or collection of fields in a table that refers to the
Primary key of the other table. It is responsible for managing the relationship between the
tables. The table which contains the foreign key is often called the child table, and the table
whose primary key is being referred by the foreign key is called the Parent Table.
For example: When we talk about students and the courses they have enrolled in, now if we
try to store all the data in a single table, the problem of redundancy arises.
To solve this table, we make two tables, one the student detail table and the other department
table. In the student table, we store the details of students and the courses they have enrolled
in. And in the department table, we store all the details of the department. Here the courseId
acts as the Primary key for the department table whereas it acts as the Foreign key in the
student table.
Syntax:

CREATE TABLE childTable (


col1 int NOT NULL,
col2 int NOT NULL,
col3 int,
………...
PRIMARY KEY (col1),
FOREIGN KEY (col3) REFERENCES parentTable(parent_Primary_key));

Creation of Parent Table DataFlair:


Query:

CREATE TABLE DataFlair(


emp_id varchar(5) NOT NULL,
name varchar(50),
location varchar(50),
experience int,
PRIMARY KEY(emp_id));
Output:

Figure 1: Assigning primary key


Lab Task:

Task 1:
1) Create a Primary Key and Foreign Key for two tables i.e. Student’s Table and
Subject’s Table also add the procedure (query) and output screenshots with the
lab task.
Solution:
Student Table Query:

Student Table Output:

Subject Table Query:

Subject Table Output:


Conclusion:
In this lab, I learned about primary keys and how to reference them using foreign keys.

Rubrics:
Report not Plagiarized Requirements Observations Appropriate Correctly
submitted content are listed and are recorded computation drawn
presented or experimental along with s or conclusion
Laboratory incomplete procedure is detailed numerical with
Reports submission or presented procedure analysis is exact results
late performed and complete
report in all
respects
Category Ungraded Very Poor Poor Fair Good Excellent
Marks 0 1 2 3 4 5

Date Total Marks Instructor’s Signature

Absent Student failed Student Student Student Student


to develop a developed a developed a developed a excelled in
functional partially mostly fully developing
code or functional functional functional fully
complete the code and code and met code and functional
lab objective struggled to the lab met the lab code,
within the accomplish objective objective exceeding
simulation the lab within the within the expected
environment. objective simulation simulation results within
They exhibit within the environment, environment simulation
inadequate simulation achieving , achieving environment.
understanding environment. satisfactory expected They
Demonstrat
in They results. They results. demonstrate
ion
configuring, demonstrate exhibit They mastery in
operating, and minimal moderate demonstrate independently
executing the understanding proficiency in proficiency configuring,
simulation in configuring, in operating and
independently configuring, operating, and configuring, executing the
. operating, and executing the operating, simulation.
executing the simulation and
simulation independently executing
independently . the
. simulation
independent
ly.
Category Ungraded Very Poor Poor Fair Good Excellent

Marks 0 1 2 3 4 5

Date Total Marks Instructor’s Signature

You might also like