database implementation of data having M:N relation - database

I have to create a database having only 2 tables student and course. There is no relation between them or say atmost 1 relation is acceptable.
Query that usually runs on it :-
"Get courses registered by a student".
So it should be quick to respond. Please tell how to implement such database?

CREATE TABLE STUDENT
(student_id INT PRIMARY KEY)
CREATE TABLE COURSE
(course_id INT PRIMARY KEY)
CREATE TABLE COURSE_REGISTRATIONS
(
student_id INT,
course_id INT,
)
In COURSE_REGISTRATIONS, the {student_id, course_id} combination is the primary key, and obviously student_id and course_id are foreign keys to their respective table.
You can query COURSE_REGISTRATIONS for the information you need.

Related

How to use the foreign key in SQL Server

I have following question: let's say we have a Chen-Notation 1:n and m:n.
So 1 has a primary key and n also, where do I type the foreign key ? in the n ?
And the second question is about m:n, both have a primary key, and I need 1 more table because it's m:n, do I type the both primary keys as foreign keys in the 3rd table?
Example of a 1:n relationship : customers and orders
One customer may have several orders. In this situation, you want a column in the orders table with a foreign key that references the primary key of the customers table.
Sample DDL:
create table customers (
id int primary key,
name varchar(50),
email varchar(50)
);
create table orders (
id int primary key
price float,
customer_id int foreign key references customer(id)
);
Example of a n:m relationship : books and authors
A book may be written by more than one author. An author may have written more than one book. You create a bridge table, also called junction table, called books_authors, to represent that relationship, and that contains foreign keys to the two other tables.
Sample:
create table books (
id int primary key,
name varchar(50)
);
create table authors (
id int primary key,
name varchar(50)
);
create table books_authors(
book_id int foreign key references books(id),
author_id int foreign key references authors(id),
constraint pk_books_authors primary key(book_id, author_id)
);
So 1 has a primary key and n also, where do I type the foreign key ? in the n ?
The foreign key lives in the n, because 1 can have many keys, but you can't have a field that holds multiple values, you need to have one value per field, so it's in the n.
And the second question is about m:n, both have a primary key, and I need 1 more table because it's m:n, do I type the both primary keys as foreign keys in the 3rd table?
Yes, because that 3rd table in effect has a many-to-one relationship to both tables.

Object relational database primary keys

I am trying to make a object relational database of a school enrolment system in oracle 11g, my sql code is below. I am trying to write a query that gives back the course name and students enrolled in that course. but when i try to add another chemistry class, with the id 101. i get an error. this is because i have made courseID a primary key, so i cant insert multiple courses with that course id? So can anyone show me how i can create multiple chemistry insert statements with the same id, but different student id?
CREATE TYPE Student_objtyp AS OBJECT (
FName VARCHAR2(20),
LName VARCHAR2(20),
StudentID NUMBER
);
/
CREATE TABLE Student_objtab OF Student_objtyp (StudentID PRIMARY KEY)
OBJECT IDENTIFIER IS PRIMARY KEY;
CREATE TYPE Course_objtyp AS OBJECT (
CourseName VARCHAR(20),
CourseID NUMBER,
StudentID REF Student_objtyp
);
/
CREATE TABLE Course_objtab OF Course_objtyp (
PRIMARY KEY (CourseID),
FOREIGN KEY (StudentID) REFERENCES Student_objtab)
OBJECT IDENTIFIER IS PRIMARY KEY;
INSERT INTO Student_objtab VALUES('bill','smitts',1);
INSERT INTO Student_objtab VALUES('bob','jo',2);
INSERT INTO Course_objtab
SELECT 'Chemistry',101,
REF(S)
FROM Student_objtab S
WHERE S.StudentID = 1;
INSERT INTO Course_objtab
SELECT 'Chemistry',101,
REF(S)
FROM Student_objtab S
WHERE S.StudentID = 2;
INSERT INTO Course_objtab
SELECT 'Physics',201,
REF(S)
FROM Student_objtab S
WHERE S.StudentID = 1;
select c.CourseName, c.StudentID
from Course_objtab c;
This design violates database normalization rules. COURSE should only hold information on the course, NOT on the students enrolled in the course. IMO there should at least be an ENROLLMENT table, with references to COURSE and STUDENT. This is known as a "junction table".
I'd model it as follows:
STAFF - information about staff members (teachers/professors, etc)
STUDENT - information about students
COURSE - information about courses which may be taught
CLASS - information about a specific instance of a course, including the days/times the class meets, the location, and the staff member who is teaching it.
CLASS_ENROLLMENT - relates STUDENTs to a specific CLASS.
There should also probably be something here about ROOM, and ROOM_SCHEDULE, and perhaps an ACADEMIC_CALENDAR.
Best of luck.
You need to spend some time thinking about the structure of your database. From what you described, you need at least three tables. Courses, with PK course_id, students, with PK student_id, and enrollment, with foreign keys course_id and student_id, referencing their respective tables.
Since your course_objtab table contains the students enrolled in a course, the PK should probably be the courseID and StudentID:
CREATE TABLE Course_objtab OF Course_objtyp (
PRIMARY KEY (CourseID, studentID),
FOREIGN KEY (StudentID) REFERENCES Student_objtab)
OBJECT IDENTIFIER IS PRIMARY KEY;

How do I create a relationship between two tables where one already has a relationship with another table?

I am trying to create a Many to Many relationship with SQL Server Management studio between two tables called Courses and Students. This relationship uses a junction table called Enrollment. I started by creating a 1:M relationship between Students and Enrollment so that the studentId column in the Enrollment table points to the studentId column in the Student table. That worked fine. My problem is occurring while I create my 1:M relationship between Courses and Enrollment. The courseId column in the Enrollment table needs to point to the courseId column in the Courses table. The relationship dialogue comes up and the columns under the Primary Key Table(Enrollment are automatically populated with courseId and studentId. The Courses table doesn't have a studentId column, so I remove this from the columns under the primary key table. This is where I get the error message 'The columns in table Enrollment do not match an existing primary key or UNIQUE value. This method worked fine for creating the 1:M between Students and Enrollment. Why am I getting this error all of a sudden?
The reason is your Courses table has 2 field in it's primary key.
On solution is you add a new field named semesterId in Enrollment table and use both semesterId and courseId when creating foreign key.
Your Courses primary key includes a SemesterID which isn't in the Enrollment table. I'd suggest the SemesterID should be in the Enrollments table rather than in the Courses table as a Student would be enrolled in a course in a particular Semester.

Extending SQL Table

I have 4 SQL tables: User, Student, Professor and Publication.
User has the common columns for any kind of user;
Student has columns specific for a student;
Professor has columns specific for a professor;
Publication is only for professors.
So I have:
create table dbo.[User] (
Id int identity not null
constraint PK_User_Id primary key clustered (Id),
-- Other user columns
)
create table dbo.Student (
UserId int not null
constraint PK_Student_UserId primary key clustered (Id),
-- Other student columns
)
create table dbo.Professor (
UserId int not null
constraint PK_Professor_Id primary key clustered (Id),
-- Other student columns
)
create table dbo.Publication (
Id int identity not null
constraint PK_Publication_Id primary key clustered (Id),
UserId int not null
-- Other student columns
)
alter table dbo.Student
add constraint FK_Student_UserId foreign key (UserId) references dbo.[User](Id);
alter table dbo.Professor
add constraint FK_Professor_UserId foreign key (UserId) references dbo.[User](Id);
alter table dbo.Publication
add constraint FK_Publication_UserId foreign key (UserId) references dbo.Professor(Id);
QUESTION
Should I have a column Id as PK in Professor and Student tables?
And make, for example, (Id, UserId) as the PK of Professor (Same for student)
Then Publication would reference Professor.Id and not Professor.UserId.
I am asking this because it sounds strange to have Publication to reference UserId from Professor table which can be confusing when I will have more tables.
Could someone please advice me on this?
In your current schema arrangement and without knowing your use cases (programmatically), one could make the argument that you don't need the Id identity columns for any of the extension tables. I assume this would be a 1 to 1 relationship to the User table anyway, so you'd at least want a unique constraint on the UserID columns, which you'd get by making it a PK anyway.
Things I like to consider are:
Can a professor ever become a different user ?
Is it possible for a professor to exist without an user ?
Is it possible for a single user to be two professors (multiple disciplines?)
If so, why wouldn't you give every professor an unique Id (ProfessorId), and only create a foreign key to the User table (UserId, you could call this UserFk).
In the publication table you can reference the professor by his/her id and call this one ProfessorFk. This way you create very efficient references between tables. The publication table than also gets a single PublicationId as primary key.
Correct me if i'm wrong, i don't know your use case. But it would seem reasonable that a professor can have multiple publications, but a publication can also be written by multiple professors ? This would mean you need an extra table between publication and professor for the n-n relationship.
About creating a professor key that is a combined key (Id, UserId). I personally dislike combined keys, if you want to reference this professor from your publication table you need two columns.
This would also suggest you can have multiple professors for the same user, if so, go for the single Id option.
This means i would create the following setup:
User Table
UserId
Student Table
StudentId
UserFk
Professor Table
ProfessorId
UserFk
ProfessorPublication Table
ProfessorFk
PublicationFk
Publication Table
PublicationId
So, it partly is based on what you want to be able to do with your data, and partly just your preference.

many to many normalization

I'm studying Database normalization and I just can't understand one thing.
Suppose we have a Many-to-Many relation. We have a table Course and we have a table Student. Multiple students may take multiple courses.
Suppose we have a "transition" table Course_Student that stores only primary keys of a student and a course he has chosen.
My task is to make all tables in 3NF and if some table is not in 3NF then to explain why.
My question is: are these tables already in 3NF and I'm especially concerned about the "transition" table.
Thanks so much!
Course
id title
1 Math
2 Programming
Student
id name
1 John Stevens
2 Jack Ryan
Course_Student
course_id student_id
1 1
1 2
2 1
CREATE TABLE Course(
id int IDENTITY(1,1) PRIMARY KEY,
title varchar(100) NOT NULL
)
CREATE TABLE Student(
id int IDENTITY(1,1) PRIMARY KEY,
name varchar(50) NOT NULL
)
CREATE TABLE Course_Student(
course_id int FOREIGN KEY REFERENCES Course,
student_id int FOREIGN KEY REFERENCES Student
)
As per my understanding of Normalization, your tables are already in 3.5 NF (BCNF). The design of your table is great.
Over normalization can be bad sometimes but in your case it seems perfectly sane.
For the Table (Course_Student)
Primary Key should be (course_id, student_id)
with 2 foreign keys, referencing the two table :) !
Assessing normal form is something that is done to a database design that includes, for each table, the applicable functional dependencies.
Absent an explicit statement of the latter, all dependencies can only be assumed to be on (all) the keys, and hence the design can only be assumed to satisfy (at least) BCNF, by definition.

Resources