Overview
Sometimes when tables have a one-to-many relationship or many-to-many relationship we need to create a report of comma separated strings along with the necessary primary information.
Here I will explain a scenario in which a student can join multiple courses. To do this I am creating three tables, two master and one transaction, tables named Student, Course and trnjCourse_Studnet. The Student table has student information. The Course table has course name and course ID. The transaction table contains the information about a student and courses is a one-to-many relationship.
Now I need to write a query for student joining multiple courses, then the entire course name that is joined by that student should be displayed, separated by a comma with the unique student information. Let's see how to do it.
The Scripts
The following are the scripts to create the table.
- Student
- CREATE TABLE [dbo].[Student](
- [StudentId] [int] IDENTITY(1,1) NOT NULL,
- [StudentName] [varchar](50) NOT NULL,
- [FatherName] [varchar](50) NOT NULL,
- [Address] [varchar](100) NOT NULL,
- [MobileNo] [varchar](11) NOT NULL,
- CONSTRAINT [PK_Student] PRIMARY KEY CLUSTERED
- (
- [StudentId] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- Course
- CREATE TABLE [dbo].[Course](
- [CourseId] [int] IDENTITY(1,1) NOT NULL,
- [courseName] [varchar](50) NOT NULL,
- CONSTRAINT [PK_Course] PRIMARY KEY CLUSTERED
- (
- [CourseId] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- Transecting table
The following is the Transecting table that contains the data about the student course list (trnjCourse_Studnet).
- CREATE TABLE [dbo].[trnjCourse_Studnet](
- [Id] [int] IDENTITY(1,1) NOT NULL,
- [StudentId] [int] NOT NULL,
- [CourseId] [int] NOT NULL,
- CONSTRAINT [PK_trnjCourse_Studnet] PRIMARY KEY CLUSTERED
- (
- [Id] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- GO
- ALTER TABLE [dbo].[trnjCourse_Studnet] WITH CHECK ADD CONSTRAINT [FK_CourseId] FOREIGN KEY([CourseId])
- REFERENCES [dbo].[Course] ([CourseId])
- GO
- ALTER TABLE [dbo].[trnjCourse_Studnet] CHECK CONSTRAINT [FK_CourseId]
- GO
- ALTER TABLE [dbo].[trnjCourse_Studnet] WITH CHECK ADD CONSTRAINT [FK_StudentId] FOREIGN KEY([StudentId])
- REFERENCES [dbo].[Student] ([StudentId])
- GO
- ALTER TABLE [dbo].[trnjCourse_Studnet] CHECK CONSTRAINT [FK_StudentId]
- GO



Jaygovind ChauhanPosted Mar 30, 2016, 9:36 AM
thankz this is... i was looking......
Fadi alfadiPosted Feb 18, 2015, 7:02 AM
thank u
Manish Kumar ChoudharyPosted Sep 18, 2014, 8:23 AM
Thanks @Bhanu Verma
Bhanu VermaPosted Sep 11, 2014, 1:36 AM
Very nice Thank you.
Guest UserPosted Sep 9, 2014, 11:05 PM
good example. do you know in sqlserver what this statement actually does "WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]"