Question : SQL server database schema, backup/restore


I just started to administrate a web site ( server/c#) and I need to add some columns to a couple of tables and I want to clarify a few things before I do it.

My account name at the web host is something like XY_1234
When I download a backup of the database and restore it in SQL Server Management Studio all tables get a schema called XY_sa, i.e. table customer is named XY_sa.Customers.

If I want to get all columns from customers (example) and make the query
select * from Customers I get "Invalid object name "Customers." so I have to query
select * from XY_sa.Customers.

My questions is why I don't have to add XY_sa into the code on the site?
In my queries in the code behind I can (if I want) query select * from Customers

I'm just afraid I will be doing some basic mistake here with backup/restore etc when I download *.bak, restore it on my local machine, add a column to a couple of tables, create a backup, upload it to the web host, restore it using their restore tool?

Answer : SQL server database schema, backup/restore

The answer to your question.

My questions is why I don't have to add XY_1234 into the code on the site?

Your website login is somehow connected to your MS SQL Login. Whatever changes you do to the database uses that SQL login as your Schema name also. When you run code that has schema [dbo] it is replaced with your default SQL Schema Name. You are probably not seeing an error that says you don't have dbo permission. This usually happens on shared hosting.

Referencing the tables in a SQL Server database happens in this order


The schema assigned to your user XY_1234 in the database you downloaded has a schema named XY_1234.

The normal naming convention of a database table from one server to another.

Additionally: Your last paragraph about editing the database and uploading it... Are you collecting data from the web using your website or are you only displaying data that does not change through your website?

If you're changing a live database which is updated by the website, restoring a database with changes will cause you to lose information that was captured since the last backup. You might want to create change scripts that you can run against that database on your website. I would test those on your local server first.

Random Solutions  
programming4us programming4us