What is the concept of degree and cardinality in the context of SQL?

342    Asked by DaniloGuidi in SQL Server , Asked on Dec 26, 2023

Currently I am designing a database for a university. This database requires storing information about students their enrolled courses and their relationships. Explain to me the concept of degree and cardinality considering database design, and attributes. 

Answered by Charles Parr

In the context of SQL, degree and cardinality in sql for developing a database for your university, the degree refers to the number of attributes or even fields within a table.

For example, a “student” table may include the attributes such as “student I’d”, “name”, “email” “age” etc. Therefore, in this given example the degree of the “students” table is 4.

On the other hand, cardinality refers to the relationship between the tables in terms of how many instances of one entity are related to another entity.

For instance, consider a scenario where two tables are present “Students” and “Courses”:-

CREATE TABLE Students (
    Student_id INT PRIMARY KEY,
    Name VARCHAR(50),
    Email VARCHAR(100),
    Age INT
);
CREATE TABLE Courses (
    Course_id INT PRIMARY KEY,
    Course_name VARCHAR(100),
    Instructor VARCHAR(50)
);
Now, for establishing relationships:-
One-to-many relationship from “student” to “courses”
ALTER TABLE Courses
ADD COLUMN student_id INT,
ADD FOREIGN KEY (student_id) REFERENCES Students(student_id);
Many-to-many relationship
CREATE TABLE Student_Course (
    Student_id INT,
    Course_id INT,
    PRIMARY KEY (student_id, course_id),
    FOREIGN KEY (student_id) REFERENCES Students(student_id),
    FOREIGN KEY (course_id) REFERENCES Courses(course_id)

);



Your Answer

Interviews

Parent Categories