SQL Cocktail database in visual studio - sql

I'm trying to create a cocktail database where I can look up any classic cocktail and check if I have the ingredients required for making it. I use visual studio 2022. My knowledge about SQL is very outdated but would like to try anyway.
I understand database normalization is what I need to do here. Furthermore it seems difficult to store several ingredients by ID into a recipe table. I've been looking into arrays in SQL and understand that is not straight forward. Anyone who have an idea about how I can set up this database correct? Next thing after creating the database the way I want it is to program a web app in C# to use the database.
I've tried many times to create tables in SQL in visual studio and can insert and select from created tables. However, getting what I want by storing several ingredients from one SQL table into a recipe table containing several ingredients seems difficult for me to understand.

Related

Import data into Server Management Studios diagram description fields

Suppose I have a big database, about 800 tables. In this database there is a table that serves as a metatable describing all of the fields of the other tables in the database, for documentation purposes. This table is read by a self written tool of the company I work at.
I'm new at the company and I'm rather inexperienced, so please forgive me for asking this...
In SSMS 2008 R2 I want to make an entity relationship diagram, which seems possible, which is great. I also see that it is possible to show a description for each field in the diagram, without clicking on a table/entity first to show the descriptions. Is there any way to automatically insert the relavant data of the previous mentioned "metatable" into the description/note entry of each field of the corresponding table shapes in the diagram? I could do it manually but I thought I better ask here before I do anything stupid (lot of work). I thought about SQL Procedures, but the thing I am trying to do involves the description fields of the diagram editor itsself, so I don't think that is covered with Transact SQL.
BTW, if somebody knows how to set/show the cardinality of the relationships in the diagramm please tell me.
By the way, we (the company) have Visio 2007 too (not that it would solve any of the problems..), but I'd like to use SSMS 2008 R2 first to set foreign keys (which are not there) and then import that into Visio through the reverse engineering option. Seems more logical for me, instead of using Visio in the first step. Of course I'm using a backup of our database.
The diagram description is not a property of the diagram it is a property of the table itself.
See sp_addextendedproperty in SQL Server books online:
http://msdn.microsoft.com/en-us/library/ms180047.aspx
You need to add an extended property with the name 'MS_Description'.
For a useful extended description, see here:
http://www.simple-talk.com/sql/sql-tools/towards-the-self-documenting-sql-server-database/

Normalization of an existing SQL database

I have a single-table database I inherited and migrated to SQL Server, and then I normalized it by creating, linking, and filling a whole bunch of lookup-type tables that represented items in the main table. I now want to replace those items in the original table with their foreign keys. Am I stuck writing a bunch of queries or UDF's and then a giant INSERT statement to accomplish this, or is there a tool I can use to point at the various fields and have it handle the grunt work for me?
Redgate SQL Refactor comes with a 14 day evaluation period and has a "Split Table" refactoring which sounds like it might do what you need?
The feature is described thus:
Split Table splits a table into two
tables, and automatically rewrites the
referencing stored procedures, views,
and so on. You can also use this
refactoring to introduce referential
integrity tables. You can select this
feature from the context menu in
Management Studio’s Object Explorer.
I have had similar experiences. I once inherited a fairly large database that required serious overhaul to the schema before I would look at it without scorn.
Because the upgrade was fairly significant, I designed an SSIS package to migrate data from the old schema to the new. Lookup activities were helpful to map old text values to the new keys. I kept a script of my old schema and data handy and would repeatedly restore the database in a sandbox and re-migrate until I could satisfy the powers-that-be that the migration was reliable.
I found there was only a moderate learning curve to getting started with SSIS. If the tool is available to you, I recommend giving it a try.

Entity framework model creation

When using the Entity Framework there are basically two ways to create your model. You either create the model in SQL server or in Visual Studio EF designer. Those are outlined below.
Start with Database
You first create the model in your SQL server DB then point EF to create the .edmx file for you. By using this approach you can use SQL server management studio to create all of your models and relationships.
Start with Visual Studio EF Designer
This approach is to create the model first in Visual Studio and from that create your database. By doing this it seems like you don't have to be soo concerned with tables and relationships.
Here is what I do and why I do it that way
I start by creating my model using SQL server management studio. I do this because I think its easier to create and modify tables using that tool, also I know exactly what is being created. I create my EF model by pointing it to my existing database. After that I create a Visual Studio Database Project so that my database is scripted into files which I put into version control. When I need to make changes, I change the database and then update my .edmx file as well as my database project.
I was wondering what are the pros and cons to these different approaches and what should be the criteria to decide which to use? Am I doing it wrong? Should I be creating my model first in Visual Studio?
I don't think that there's a 'right' or 'wrong' way to do this, a lot depends on how you deploy your code, where it goes to etc. There is also a third way, which Scott Guthrie blogged about recently:
http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx
As a side note, even if you start with the model designer, I think you always have to think about your tables/relationships, as getting these wrong in the database can cause you big problems further down the line.
I don't think there is a right or wrong way.
At our company we are developing the database changes directly first, apply them to the edmx model for existing models.
For new models, we create the edmx model first, then generate the database. From that point on we usually update the database directly. After we have tested our code internally and it runs correctly, and we know that our SQL database is correct (and of course prior to checking in), we'll then apply the changes to the database project by doing a SQL compare on the database to the database project.
This has worked very well for us.

What's the steps for SQL optimization and changes without reflect live system?

we have a big portal that build using SharePoint 2007 , asp.net 3.5 , SQL Server 2005 .. many developers work in it since 01/2008 and we are now doing huge analysis for current SQL Databases [not share-point DB ] to optimize and enhance it.
The main db have about 330 table and 1720 stored procedure (SP) created from 01/2008 till now
Many table names / Columns is very long and we want to short it
we found SP names is written in 25 format :( , some of them are very complex and also we want to rename
many SP parameters need to be renamed
one of the biggest table is Registered user table, that will be spitted in more than one table for some optimization, many columns name will be changed
I searched for the way that i can rename table names ,columns and i found SQL refactor tool but i still trying it ..
my questions :
Is SQl Refactor is the best tool for renaming ? or is there any other one ?
if i want to make it manually, is there any references or best practice for that ?
How can i do such changes in fast and stable way .. i search for recommendations and case studies if exist ?
This is why people have written coding standards (with defined naming conventions) and have code reviews!! Make sure you implement those procedures right now, to prevent his from getting any worse in the future.
Also for around $300, SQL Refactor™ is an excellent tool. If you were to use search and replace, you'd have countless errors and spend hours and hours editing code. I wouldn't even consider using anything other than SQL Refactor, and would never even try using a manual search and replace method on something as large as you describe.
You can use Visual Studio 2005 Database Edition, 2008 Database Edition or 2010 ultimate to load up your DB schema. This provides refactor capabilities, as well as database "builds" that check references in stored procedures, views and functions to ensure all tables and columns referenced actually exist.

Database schemas WAY out of sync - need to get up to date without losing data

The problem: we have one application that has a portion which is used by a very small subset of the total users, and that part of the application is running off of a separate database as well. In a perfect world, the schemas of the two databases would be synced up, but such is not the case. Some migrations have been run on the smaller database, most haven't; and furthermore, there is nothing such as revision number to be able to easily identify which have and which haven't. We would like to solve this quandary for future projects. During a discussion we've come up with the following possible plan of action, and I am wondering if anyone knows of any project which has already solved this problem:
What we would like to do is create an empty database from the schema of the large fully-migrated database, and then move all of the data from the smaller non-migrated database into that empty one. If it makes things easier, it can probably be assumed for the sake of this problem specifically that no migrations have ever removed anything, only added.
Else, if there are other known solutions, I'd like to hear them as well.
You could use a schema comparison tool like Red-Gate's SQL Compare. You can synchronize the changes and not lose any data. I wrote about this and many alternative tools ranging widely in price here:
http://bertrandaaron.wordpress.com/2012/04/20/re-blog-the-cost-of-reinventing-the-wheel/
The nice thing is that most tools have trial versions. So, you can try them our for 14 days (fully functional) and only buy it if it meets your expectations. I can't speak for the other tools, but I've been using RG for years and it is a very capable and reliable tool.
(Updated 2012-06-23 to help prevent link-rot.)
Red-Gate's SQL Compare as Aaron Bertrand mentions in his answer is a very good option. However, if you are not permitted to purchase something, an option is to try something like:
1) For each database, script out all the tables, constraints, indexes, views, procedures, etc.
2) run a DIFF, and go through all the differences and make sure that the small DB can accept them. If not implement any changes (including data) necessary onto the small DB so it can accept the changes.
3) create a new empty database from the schema of the large DB
4) import the data from the small DB into the nee DB.
You could also reverse engineer your database into Visual Studio as a database project. Visual Studio Team Suite Database Edition GDR R2 (I know long name) has the capability to do a schema comparison and data comparison, but the beauty of this approach is that you get all of your database into a nice database project where you can manage change and integrate with source control. This would allow you to build from a common source and deploy consistent changes.