Hey I have 3 tables called STUDENT, COURSE and ENROLLMENT. STUDENT has a PK of S_ID(student ID) and COURSE has PK of C_ID(course ID). In enrollment it only has S_ID and C_ID. I have an object data source to show all the students name (in text, and S_ID as the value) in a drop down menu and it will show which courses he is registered in when clicked, using a datagrid and another object data source. I wont to have the student to have multiple courses to be registered too, but I cant do that because you cannot have the same ID in the COURSE table, so every student is only registered to one course.
Is there some sort of option to have same ID's in a table?
If not, then I must some how manipulate the string in C_ID in the COURSE table because all the courses start with ISDVXXX or ITSXXXX or HGFXXXX. This may be hard to understand but hopefully someone will help.
An example may help
So if a student named Joe with a S_ID of 123 is registered to ISDV, he will be registered to all the courses that start with ISDV. But my problem is that my COURSE table has to have unique ID for each course such as ISDV123, ISDV346, ISDV395 etc... so this also ruins my enrollment table because I cannot simply have ISDV in there, it needs a specific course but he is registered to all of them. Any more clarification will be given :P Thanks...
What you're trying to solve is a multi-valued attribute problem. Basically, you have two tables where one (students) has a primary key which is a foreign key in another table (classes). You don't want to have multiples of the same class in the classes table, but you do want a student to be able to have multiple classes.
So, there is a very simple fix, you create another table which contains at least these two columns: student_id and class_id. This way, you could have a single class that multiple students are linked to, and also multiple classes to which a student can be linked to.
What you are looking for is a many to many relationship - i.e. a single student can have multiple courses, and a single course can have multiple students. So your link table (is this what you intended enrollment for?) should have two columns, one for the course ID and one for the student ID.
So if you had student 123 and student 234, and courses ABC and XYZ, your table would look something like:
S_ID C_ID
123 ABC
123 XYZ
234 ABC
Now, for your PK on enrollments you could either use a composite key, or add a unique integer RowId (Identity or HiLo algorithm).
In that case, your enrollment table would look something like this:
S_ID C_ID RowID
123 ABC 1
123 XYZ 2
234 ABC 3
Then, to see what classes a student was in, you could do something like
Select * from courses c
inner join enrollments e
on c.C_ID = e.C_ID
AND e.S_ID = #StudentId
Your Enrollment table stands for the m:n relationship between the Studend and Course tables.
For instance your student with S_ID = 69 is Enrolled to the Courses with C_ID = ISDVXXX , C_ID = ITSXXXX, C_ID = HGFXXXX
A Student can be Enrolled to more Courses and more Students can be Enrolled to the same Course, that's why you have your Enrollment table. In our example, the Enrollment will have the following rows:
(69, ISDVXXX), (69, ITSXXXX), (69, HGFXXXX).
If later a student with the S_ID = 96 joins to the Course with C_ID = ISDVXXX, the following will be the new rows of the Enrollment table:
(69, ISDVXXX), (69, ITSXXXX), (69, HGFXXXX), (96, ISDVXXX).
The important thing to understand here is that each row in the Enrollment table stands for a Student Enrolled to a Course and there is no need to other fields than the ID of the Student and the ID of the Course, for these two fields together identify an Enrollment.
Related
I have two tables, shops and products. Table shop with columns shopId (PK) and shopName. Another table Products has columns productId (PK) and productName. Because I have a relationship many-many between two tables I create another table shopProduct with two columns shopId and productId.
For example I have shop1 with id=1 and I have shop2 with id=2, product1 with id=1 and product2 with id=2. and the records of the table shopProduct is 1-1 ,2-2.
But now I want to have a product3 with id=3 that apply to all shops.
I consider in the real problem that i have about 250 shops.
How can implement this without a stored procedure?
I don't want to do of course 250
insert into shopProduct (shopId, productId)
values (#shopId,#productId).
Can I do for loop for example to feed the the shopId value every time?The other value for product id is the same every time.
From my understanding of this question, try this... Seems too simple, but...
Insert into ShipProduct (ProductID, ShopID)
Select 3, ShopID
From Shops
Sorry if the title was confusing.
Lets say I have a model called Employee. Employee has a column called Departments. This allows them to view the calendar for that department. But some Employees should be able to view the calendar for multiple departments, and it is different for every employee.
Because there are many different departments, I do not want to have the Employee table filled with a ton of new columns for each department.
I saw something about linking (not sure if that is the right word) two tables. So I made a second model/table called AdditionalDepartments that has the AdditionalDepartmentsId, EmployeeId, and Department.
Say Joe was part of department A but can also view B and C. This is what employee looks like.
EmployeeId | name | Department
-------------------------------
1 | Joe | IT
and the AdditionalDepartments would look like...
AdditionalDepartmentsId | EmployeeId | Department
--------------------------------------------------
1 | 1 | HR
2 | 1 | Sales
How would I go about combining these? Or is there a better way to store additional department information rather than two tables?
I ask because in my view, when they create or edit an employee, they use the employee database, but I want to be able to set additional departments on these views. The problem with that is you can only use one model per view so I am having difficulties.
Note: I cannot drop the Department from Employee because it is used for a lot more than what the AdditionalDepartments would be used for.
Change your first table column to something like OfficialDepartment. In your second table, drop the 'Additional' and just leave it as Departments. Add 'IT' to this table for 'Joe' as well. Then output the SQL using the statement below into a List<Departments> into your model and use as needed.
select A.Departments as [Departments]
from Employees E
join Departments D on E.EmployeeId = D.EmployeeId
where E.EmployeeId = <empId>
You would want the Employee model to have an IEnumerable<Department> that contains all the Departments that employee is a part of.
On a somewhat unrelated note, if you have the ability to change the table schema at all, you could just drop the Department column in the first table, and make the second table just be a list of departments associated with employees based on the employeeID columns (they would have a foreign key relationship).
Sorry if I misunderstood your question, but unless you are taking one database to be a single model, this should do the trick for you.
Recently I'm working on a project, in which I need save multiples subjects in my database, and numbers of subject will be change as per student's class. e.g: class "VII" has 6 subject (included 1 option subject), class "VIII" has 8 subjects (1 included option subject) and so on. Even the option subject can be different according to student's choice.
I'm using CheckedListBox control. Here is a screenshot.
I'm using MS Sql Server 2012. I'm facing difficulty to design database table. I can't understand should i create all the subject columns in my student table or ????(best way). Please help me, thanks in advance.
What you are describing is something that is called a Many to Many relationship. Many Classes have Many students.
You will have a students table, a subjects table and then a table student_subject table which links both the primary keys of the students and subjects table.
Reference: Many to Many
If a Student can have multiple Subjects, and a Subject can have multiple Students, then that's a many-to-many relationship. Which means there would need to be an intermediary table to link them.
Start with the tables for the objects themselves:
Student
----------
ID
Name
etc.
and
Subject
----------
ID
Name
etc.
Since these are independent aggregate root objects in the domain and neither one owns another one, their tables just contain information which describes that object. A Subject, for example, could have a name and a description, maybe a list of pre-requisites, etc.
Then you create the relationship table between them. A common practice is to just combine the names:
StudentSubject
----------
StudentID
SubjectID
If no two pairs of Student and Subject can be repeated, then the two foreign keys together can make the primary key. If they can be repeated (if the same Student can have the same Subject more than once) then you'd probably want to add a separate primary key to the table.
At its simplest, all this table does is create the relationships between the records in the main tables. However, this can potentially grow to be a domain entity in and of itself. If there are any attributes which describe the relationship between a Student and a Subject, those would be added to this table.
Your database schema should look something like this......
Students
Student_ID (PK)
Student_Name
etc .......
Subject
Subject_ID (PK)
Fee
etc......
Student_Subjects
Student_ID FK Students(Student_ID)
Subject_ID FK Subject(Subject_ID)
DateStarted
etc.....
I am sorry if this is a bit broad but I've been working on a structure for a couple of days and I can't seem to figure this out the cleanest and most efficient way to do this. I could share the tables that I've created for now but I really think it's not close to how a proper diagram should be.
Let me describe my problem a bit:
I have Stores, Countries, Districts, Categories.
Each store could belong to different Countries/Districts enabling Store Branch manipulation.
Of course a store could belong to multiple Categories too, for example Store X could be under both Food and Beverages and Night Clubs.
A Country will have multiple Districts and Stores, and a Store could have many Countries and Districts.
I am writing my application using C# and I don't have problems creating data-layer objects and classes. But I need the proper MSSQL structure to manipulate and filter down data based on given criteria.
The most important criteria would be: Going through Countries as a first step, then locating Stores within that Country as a global view, then it's important to sort Stores based on Districts and/or Categories within that Country.
Please let me know if you need me to share what I have for now, but since I'm on Stack Overflow asking this question you can guess that I'm doing this the wrong way.
Anyway, if you could shed some light on this issue and explain how things should be done properly I would highly appreciate it.
Thanks in advance.
Whenever you have many-to-many relationships (e.g. one district may contain many stores; one store may be contained by many districts), you are going to need to use cross-reference tables between those entities.
I'm assuming that a particular district may only be contained by one single country. Here is how I would model out your schenario:
countries(country_id [PK], name, ...)
districts(district_id [PK], country_id [FK], name, ...)
districts_has_stores(district_id [PK], store_id [PK])
stores(store_id [PK], name, ...)
categories_has_stores(category_id [PK], store_id [PK])
categories(category_id [PK], name, ...)
In ER:
districts_has_stores and categories_has_stores are the cross-reference tables representing the many-to-many relationships between your entities.
Based off of this model, you can retrieve all stores within a particular country, and order the stores by district name using the following SQL:
SELECT
c.*
FROM
districts a
INNER JOIN
districts_has_stores b ON a.district_id = b.district_id
INNER JOIN
stores c ON b.store_id = c.store_id
WHERE
a.country_id = <country_id here>
ORDER BY
a.name
Retrieving the count of stores in each country:
SELECT
a.country_id,
COUNT(*) AS store_count
FROM
districts a
INNER JOIN
districts_has_stores b ON a.district_id = b.district_id
GROUP BY
a.country_id
Edit: As per your comment to this answer, here's an example of how you can retrieve all stores that have a category_id of 1:
SELECT
b.*
FROM
categories_has_stores a
INNER JOIN
stores b ON a.store_id = b.store_id
WHERE
a.category_id = 1
Retrieving all stores in a particular category_id (1) and filtering the result to only include those stores within either districts 4 or 5.
SELECT DISTINCT
b.*
FROM
categories_has_stores a
INNER JOIN
stores b ON a.store_id = b.store_id
INNER JOIN
districts_has_stores c ON b.store_id = c.store_id
WHERE
a.store_id = 1 AND
c.district_id IN (4,5)
It's very easy, you have StoryCategory to solve the "many stores can have many categories" problem. The District and Country tables allow you to store the location of your store.
I advise you to look at the following documentation to expand your database design knowledge:
Many To Many
10 useful articles
I think below should work for you.
Countries
=========
CountryId, CountryName
Districts
=========
DistrictId, CountryId, DistrictName
Stores
=========
StoreId, DistrictId, Storename
Categories
=========
CategoryId, Categoryname
StoreCategories
=========
Storecategoryid, StoreId, CategoryId
You would have a country table with an identity column. Each store can "be-a" Category, you'd have to have an association table which is a child table of the store table which would have a store ID and the ID from the Category table. Each store would belong to a country so your store table would store the ID of the country from the Country table. Similar to the Category, your District table would have a child table which stores the ID of the District and of the Country (since each district can have multiple countries)
Store Table
ID INT IDENTITY PK
Category FK
Country FK (where it actually lives, a 1 to 1)
District FK
Category Table
ID INT IDENTITY PK
StoreCategory Table
StoreID FK
CategoryID FK
Country Table
ID INT IDENTITY PK
District Table
ID INT IDENTITY PK
DistrictCountry Table
DistrictID FK
CountryID FK
I have two classes(Book,Software) that inherit from the Product class. these two classes have some fields that are different from each other. if i am to save the data for both these classes to a single table it would cause some columns to have null values.
if i save the data into different tables(Books table and Software Table) and then wanted to view this data in a single gridview, would i have to join the data and then load it to the gridview?
I would like to add 2 more classes(IndividualCustomer, WholesaleCustomer) later on which will inherit the Customer class in the same manner as mentioned above.
please guide me towards the best approach.thank you
I'm no C# programmer, but I'd go with something like
`products` table
- product_id (pk)
- title
- price
- etc
`products_software` table
- software_id (pk)
- product_id (foreign key, relates to a product_id)
- serial_number
- storage_media
`products_books` table
- book_id (pk)
- product_id (foreign key, relates to a product_id)
- pages
- author
In situations like this I create three tables: Product, Book and Software.
In Product I'll put the PK and all common fields; in Book the same PK and book specific fields; the same as Book for Software.
The associations between these kind of tables should be 1:1.
The Books and Softwares are not different Products they are just different Categories.
I would create several tables:
Products
Categories
Properties
PropertiesToCategories
PropertiesToProducts
In this form you can attach some property to each category, example: Books has Author, Title, Publisher
When you want to add a new Product you should choose first it's Category and then you can attach the available (for Books: Author, Title, Publisher) properties to your new product.