SQL Server to allow multiple values in a column - sql-server

I'm using SQL Server as backend and MS Access as a front end, I can create a table in Access with lookup wizard which will allow me to create combo box in the form and select multiple values in the drop down, for example,
Lookup value definition, Drama, adventure, horror etc., in combo box I can select multiple genres for a movie and store them in a single cell on the table.
How to replicate the same with SQL Server as back end, I tried entering multiple value in default value and it won't work. Is this possible with SQL Server?

Entering many values into a single column is a big no no in relational database design.
What you are trying to do here is to realize a many to many relationship. You have multiple items, films or whatever and you have multiple genres.
That is the classical case of a many to many relationship. Usually in such a case you have 3 tables, one representing the items, another representing the genres. In between you've got a third table that has foreign keys to the items and the genres.
In the end you will end up with a table structure like this:
Items ItemToGenre Genres
ID | item ItemID | GenreID ID | genre
And have foreign keys on ItemToGenre.ItemID from Items.ID and on ItemToGenre.GenreID to Genres.ID.
Then you can JOIN your tables to get all genres any specific item has. And do not break atomization.

Related

Database to keep track of inventory/ live stock

I am looking for help with database design for a small project I am working on.
In short what I am trying to achieve is to have say the following tables:
Paddocks
Paddock ID
Paddock Name
Paddock Size
etc.
Cattle
Herd ID
Herd Name
Number of cows
Current Paddock
Cattle_Movements
Herd Name
Current Paddock
New Paddock
Date
etc.
I was hoping to have the 'Cattle_Movements' table be like a summary of all movements of a herd of cattle. And when a herd is moved from one paddock to another it would update the 'Current Paddock' field in the 'Cattle' table.
At this stage I am trying to workout the relationships, queries and high level process that I will need to implement.
Any help will be greatly appreciated.
Start by setting up a normalized table structure. make the tables below and hook them together with the relationships tool on the ribbon under database tools-relationships. To create a relationship drag the primary key from one table to the corresponding and same named foreign key in the table that will be the many side of the relationship. In the pop up make sure to check the enforce referential integrity, cascade update, and cascade delete checkboxes.
I've highlighted the two "Many to Many Relationships" in this normalization. HerdsPaddocks is a more generic name for the CattleMovements Table. There are other possible normalized table structures, but the subtle differences are beyond the scope of this answer. When you are ready, look up table normalization and Many to Many Relationships.
Next, Close the Relationships tool, select a table from the sidebar and on the ribbon under Create hit create form. Do this for all the tables. Now we have a working database but you need to learn how to use it. So play!
Below I gave some play suggestions, but just play with everything until you figure out how to use the forms to (add, search, edit) cows, herds, and paddocks. Also learn why you should delete the primary keys from all the forms and how to replace the foreign keys like CattleType in the Cattle Table with the user friendly CattleTypeDesscription from the CattleTypes Table.
Start with the Herds table and enter some random herds. (pro tip: never add data to the table directly except when playing the error rate is too high). Then Open the herds form where you can browse and edit the herds.
Play tips: In the Table Herds HerdID is both an autonumber and a primary key. It behaves differently from the other columns. Check it out. after that delete the HerdID textbox from the form and see what happens(a good thing). How do you add herds using the Herds Form?
Moving on to playing with the Cattle form, first make sure to add a few CattleTypes to the CattleTypes table. Then at some point, try replacing the CattleTypeID in the Cattle Form with the CattleTypeDescription: https://btabdevelopment.com/how-to-change-a-text-box-to-a-combo-box-wont-work-with-data-access-page/
Also, note the Cattle Form has a subform allowing you to simultaneously assign/edit cattle assignments to herds.
Once you are comfortable adding and editing data, play with the query editor. For instance, to get how many cows are currently in Paddock holds10cattle (my dummy data).
query 1 showing the relevant data
results from query1:
Query 2 getting really close:
Query 3: which gives the number 2:
'Query 3 SQL from SQL pane of query designer
SELECT Count(Cattle.CowName) AS CountOfCowName
FROM Paddocks INNER JOIN ((Herds INNER JOIN (Cattle INNER JOIN CattleHerds ON Cattle.CattleID = CattleHerds.CattleID) ON Herds.HerdID = CattleHerds.HerdID) INNER JOIN HerdsPaddocks ON Herds.HerdID = HerdsPaddocks.HerdID) ON Paddocks.PaddockID = HerdsPaddocks.PaddockID
GROUP BY Herds.HerdName, Paddocks.PaddockName, HerdsPaddocks.HerdPaddockEndDate, HerdsPaddocks.HerdPaddockStartDate
HAVING (((Paddocks.PaddockName)="holds10cattle") AND ((HerdsPaddocks.HerdPaddockEndDate) Is Null) AND ((HerdsPaddocks.HerdPaddockStartDate)<Now()))
ORDER BY HerdsPaddocks.HerdPaddockStartDate;
Next Steps could include the specific paddock with a parameter and using the query in a report.

MS Access - column choice based on the other column

I'm creating a simple database which will allow users to manage cars and contains data like registration number, brand, model, dates of upcoming maintenance controls... (in my country marka = brand)
I have created relations like this:
In t_podwozie I have created link & relation columns fk_marka, fk_model. I need fk_model choices to be limited by my choice in fk_marka.
I have tried to use a query connecting brands and model (INNER JOIN) but then I can save only one column in t_podwozie. Any advice?
Both link & relation columns consist of id column and name column (id's are hidden)

Databases - Do tables in a database have to contain all global instances or can they be specific to a certain ID

I'm trying to model a database for facebook users. For my User table I have {account_name, account_id, ..., password} as variables. I'm trying to create a table for friends of that user, such as a Friendlist or Contacts table, but I'm wondering would the contacts table have to have a list of all the possible friendships (i.e. {user_id, friend_id, friends_since,...} ) or is it possible to have the contact table specific to a certain user_id (so {contact_id, friends_since, no_of_mutual_contacts, etc..}).
This is probably just the object orientated part of my brain coming into action, but say for example if we were using a Cinema database. There were 5 cinemas and each had 5 screens, CINEMA is a table and SCREEN is a table. Would the SCREEN table have 25 instances of data or is there some way that the SCREEN table would only contain the 5 screens corresponding to a specific CINEMA.
This comes back to my original question, would the Contacts table have to be a global table containing all friendships, or does the Contacts table only return the contacts specific to the user_id.
For some reason when I'm looking at the relational chart of the database I see the connections ressembling a somewhat object like structure where the links are just denoted members like user.Contacts or user.Events.
Normally a table in a relational database will record all facts of a given type, e.g. user <user_id> is friends with user <friend_id> since <friends_since>.
You can create views for given users if required, or just specify a user_id in your queries, to get the subset of rows applicable to a specific user.
Creating a physical table specific to certain users would create difficulties if you wanted to get all the rows for a certain friend_id.

What datatype should be used to store an array in the SQL Server

I am trying to create a table in my database using Visual Studio.
I've got a table for my Products (like in online shop) and then I have a table for Orders, which should store all products that user has ordered. The problem is that I am not sure which datatype I should use when designing the database to store an array of products in my Orders table. This is what the Orders table should look like
You should create Products and Orders table with relationship between them.
Your Orders table should have Id column as well (which is PrimaryKey)
Then you should create Products table, that keeps all the information about products and additionaly OrderId which should be used as Foreign Key to Orders table.
Please look at that link:
https://msdn.microsoft.com/en-us/library/ms189049.aspx
It's also worth of checking:
One To One, One To Many, Many To Many relations in SQLServer to have better understanding and design your data store properly.
In your case you need ProductsOrders table, Many To Many relationship.
In Relational database, you can create a relationship between 2 tables.
The relationship can be
1 to 1 (1 Product - 1 Order)
1 to Many (1 Product - 'n' Order)
Many to Many (n product - 'n' Order)
Based on your scenario, You can choose any of the relationship listed above. While querying from the database, you can easily operate over each order/Product.

Merge two tables into a single indexed view in SQL Server 2008

So here is my dilemma, I'm currently storing all my records into a giant flat table that has movies, episodes, series, games etc. 70% of the records are episodes that I do NOT want indexed by the default full-text catalog as they make up over 2 million of the records and 90% of the time people are searching for movies/series, so I want to move these records into a separate table so they can have their own separate full-text catalog and a few additional columns that do not apply to movies/series (season, episode # etc.) as well.
My issue is with stored procedure I use to checkTable if an ID already exists before I go and download data or update data for that ID.
I wanted to create a view of all the ID's across both tables so I could lookup in a single place if that ID exists, however when I try to union the tables it will not allow me to create an index on the view.
ID Type | ID Type
1 movie | 2 episode
3 movie | 4 episode
5 movie | 6 episode
The ID's are unique and will never have duplicates in either table.
I feel that since this view will have over 2 million records an indexed might be important being it is called over 250-500+ a second so optimization is a huge factor.
Is there a better approach than using UNION ALL to get ID's 1,2,3,4,5,6 into a single view?
Probably your best solution is to create a view that does UNION ALL of the two tables, but instead of trying to index the view, make sure you have a good indexing strategy implemented on the tables themselves.
You can use Index Hints in your view to force the view to use those indexes whenever someone selects from the view.

Resources