Skip to main content

Accessing a table in SQL Server 2005 (Schema related)

Recently in a discussion forum I came across this question "I have a small Doubt in SQL server. The database name is 'AdventureWorks' and the table name is 'Production.Culture'. What is the query to fetch records from that table."

The answer to this is :

Select cultureid, [name], modifiedDate from AdventureWorks.Production.Culture

There was another member of the forum who was saying that we need to only use the below query:

Select * from AdventureWorks.dbo.Production.Culture

Please note that he has mentioned two Schema names (dbo and Production) in a single query. This query wouldn't work and I thought I would explain the concept of "Schemas" in SQL Server 2005 to him.

In short,

a) Schema is similar to Namespaces in .NET
b) It helps in logical grouping of tables.
c) To view all existing schema in a database, for example within AdventureWorksDB ::

1. Open ur SQL Mgmt studio
2. Go to AdventureWorks DB and expand it
3. Expand the Tables

4. Now just observe what you see within that. There would be,

i) few tables which starts with "dbo.",
ii) few tables which starts with "Person."
iii) few tables which starts with "Production." etc.,

Those are all called as "Schema Names".

Other easier way to find this is to, navigate to the "Security" tab of the Database. There we will find a folder by name "Schema". Expand that to see all the Schema names related to that DB.

5. Now click on "New query" button and try this

--- This would work
Select * from person.address

6. Now try this

--- This wouldn't work
Select * from dbo.person.address

7. Just for your confirmation, navigate to someother database in ur query window. For example,
Use Master
Go

--- This would work
Select * from adventureworks.person.address

--- This would work
Select * from adventureworks.dbo.AWBuildVersion

--- This would FAIL
Select * from adventureworks.dbo.person.address

8) That said, by DEFAULT when we create any new table it would be assigned to "dbo." schema.

This you can verify by going to ur SQL Mgmt Studio again, navigate to any database of your choice and create a sample table. Now expand the "Tables" folder to see "dbo.YourTableName" (the table which you have created now).

So the bottomline is when accessing a table name in SQL Server 2005, it shld be done like this:

ServerName.DatabaseName.SchemaName.TableName


Please note that if we want we can create our own schema and then assign the tables to it. Also when a new user is being created we can assign the default schema to them. This merits a separate discussion by itself so would write on it sometime soon.

Technorati tags:

Comments

Popular posts from this blog

Script table as - ALTER TO is greyed out - SQL SERVER

One of my office colleague recently asked me why we are not able to generate ALTER Table script from SSMS. If we right click on the table and choose "Script Table As"  ALTER To option would be disabled or Greyed out. Is it a bug? No it isn't a bug. ALTER To is there to be used for generating modified script of Stored Procedure, Functions, Views, Triggers etc., and NOT for Tables. For generating ALTER Table script there is an work around. Right click on the table, choose "Modify" and enter into the design mode. Make what ever changes you want to make and WITHOUT saving it right click anywhere on the top half of the window (above Column properties) and choose "Generate Change Script". Please be advised that SQL Server would drop actually create a new table with modifications, move the data from the old table into it and then drop the old table. Sounds simple but assume you have a very large table for which you want to do this! Then it woul

AWS fatal error: An error occurred (400) when calling the HeadObject operation: Bad Request

While using AWS and trying to copy a file from a S3 bucket to my EC2 instance ended up with this error message. Command Used: aws s3 cp s3://mybucketname/myfilename.html /var/www/html/ Error: fatal error: An error occurred (400) when calling the HeadObject operation: Bad Request The error goes off if we add the region information to the command statement. I am using Asia Pacific (Mumbai) so used ap-south-1 as the region name. Modified Command: aws s3 cp s3://mybucketname/myfilename.html /var/www/html/ --region ap-south-1

[Non Tech] Want to know the recipe for Omelette :)

Fed up with Bread - Jam and Curd Rice, today i wanted to eat Omelette. Interesting part is I wanted to cook it myself :) So in the first picture you see all the items which are needed for preparing an Omelette. When I had a closer look at the eggs I see that almost all the eggs are broken. But believe me when I bought it couple of days back it was in perfect condition! I was wondering whether the eggs have become rotten or pretty old to consume! I tried taking an egg and break it but couldn't break it at all :) Since I have kept in the freezer all the eggs have frozen and looked like a iron ball :) After trying for few minutes of trying i removed the shell of the egg and then kept that iron ball :) into a bowl and placed it within Oven. I heated it for 1 minute and checked. It melted only to a limit. So i just set it for another 2 minutes and checked it later. It has melted but the part of the egg white has become a Omelette :( I didn't leave it there. I took the bowl out of