SQL Server 2008 automatically creates table when creating new database [duplicate] - sql-server

This question already has an answer here:
CREATE DATABASE ends up cloning an old database
(1 answer)
Closed 6 years ago.
I am using SQL Server 2008. When I create a database by right clicking on the database folder and create new database, it creates the database with the name I given.
Additionally, the tables that are generated automatically are from a previously created table with different database name.

When a database is created, it is created based on the model database, as described here:
The model database is used as the template for all databases created
on an instance of SQL Server. Because tempdb is created every time SQL
Server is started, the model database must always exist on a SQL
Server system. The entire contents of the model database, including
database options, are copied to the new database.
So, you probably just need to remove the tables from model.

Related

Cleared SQL Server tables still retain some data

I made a custom application that is running from several years and is full of company data.
Now I need to replicate the application for another customer, so I set up a new server then i cloned the databases and empty all the tables.
Then I made a database and file shrink.
On the SQL Server side, the databases looks empty but if I run a grep search on the database files .mdf and .log I still can find recurrence of the previous company name also in system databases.
How do I really clean a SQL Server database?
Don't use backup/restore to clone a database for distribution to different clients. These commands copy data at the physical page/extent level, which may contain artifacts of deleted data, dropped objects, etc.
The best practice for this need is to create a new database with schema and system data from scratch using T-SQL scripts (ideally source controlled). If you don't already have these scripts, T-SQL scripts for schema/data can be generated from an existing database using the SMO API via .NET code or PowerShell. Here's the first answer I found with a search that uses the Microsoft.SqlServer.Management.SMO.Scripter class. Note you can include scripts data too (insert statements) by specifying the ScriptData scripting option for desired tables.

Copying database from SQL Server 2008 to SQL Server 2016 [duplicate]

This question already has answers here:
Is there any way to generate database scripts from a SQL query in SQL Server?
(2 answers)
Closed 4 years ago.
I am presently using SQL Server 2008. I want to move the entire applications and database to SQL Server 2016 without data. i.e. I don't want to copy the entire data but I need all the tables and everything from the previous server. Can please anyone help me with this?
You'll want to script out each database. To do so:
right click on the database
tasks
generate scripts
using the wizard, choose all the data objects you want (tables, views, etc.) or all of them
save to a location
open the file from that location
boom, there is your code set up all your tables, etc. for your database!
Just repeat for each database you want to 'copy'. Just remember (as requested) each table will have no data in it. Alternatively you can backup and restore each database and then truncate each table, that is probably a lot more work though.

MVC Membership database to SQL Server 2008

When I create a new MVC application with EF, it creates all the views, models, controllers and logic for users to be able to log in, change passwords etc. The data is held in a MDF file in the app_data directory.
I used this for my users, and then had my own SQL Server 2008 database which was created code first using EF for all my other tables. When I wanted to reference a user from a table within this database, I used the ApplicationUser.Id.
Doing it this way, I have not got a foreign key between the table that holds the users and any other table in my custom DB, but thats a different topic.
The question I have, is how I can I stop this happening every time I create a new application, and how do I fix the issue I have now.
I have two different databases, when I just need one. have one database in SQL Server (which is what I want) and one in a file which I cannot convert to SQL Server as it was created in 2012, and I am using 2008 SQL Server.
I am wanting to now deploy this on a server where all data is read from SQL Server.
The way I see it, I need to somehow get all the tables it creates for Membership, and put them into the database I created, then change the connection string. However first off I don't know how I would do this, and second... WHY do I have to do this? How can I just have one database next time. Am I missing something?
After some research and messing around, I have answered my own question and think it may be useful for others.
I am using MVC 5 with the default ASP.NET Identity for my users.
What I found is that if I changed the connection string to point to my SQL server, it will create the database for me (the one that was previously a file in my app directory).
I then added a connection string to the same database for the context I created and it created all the tables for that in the same database.
The result is that I have one database with all my tables in.

On creating new database in sql server 2008, some tables created automatically?

When I create any new database It automatically create some tables in new database.
Details:
I created a new database "TestDatabase" using below command
Create database TestDatabase
When I expand the Tables folder, I found that there are already some tables created automatically.
These are those tables which I was using in some other database.
Table names:
1. Employee
2. Admin
etc
How can I create new fresh database to make sure that no tables created in it?
Presumably at some point you accidentally created these tables in the model database.
This is used as the template for creating new databases. Simply delete them from model to stop them appearing in every freshly created database.
SQL Server uses the model database as the basis for new databases. Check and see if these tables exist in model - and if they do, delete them.
You should probably check for other objects in modeltoo (stored procedures, views etc).

how to take sql database backup without data [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Backup SQL Schema Only?
Anybody tell how to take sql database backup without data. i wanted to take all the tables and structures from sql server 2008. and import to another sql server 2008. i dont need the data.
Use "tasks" -> "Generate scripts" and choose what you want to script. Run, save to a file, open the file against the new database and run the script after changing the database name to match (if it changed)
First time you can use the script database option, as Otavio suggested.
Subsequent times you can use a tool like RedGate SQL Compare or the Compare Schema functionality of Visual Studio Database Edition. These tools allow you to synch schema (ie. 'table structure') from one database to another.
You can do this by making scripts.
The way to go is:
Right click the table you want to script to the other database.
Script table as: -> Create to New query window.

Resources