I don't understand what's the difference between table name / schema name? Because I have connected to AdventureWorks database and then I wrote this query. So in this does this HR.Employee
mean a table or something else?
select * from HR.Employee
I don't understand what's the difference between table name / schema name? Because I have connected to AdventureWorks database and then I wrote this query. So in this does this HR.Employee
mean a table or something else?
select * from HR.Employee
I think the question is valid and far from trivial. Here's a big picture perspective.
In general, a database object is identified by a 4 part name. The "servername" is really an instance name. Brackets are optional if the naming follows the rules for naming identifiers. If a named instance, brackets are required because "\" is not legal. A default instance using a FQDN for the machine also needs brackets because of the periods. You don't have to specify all the parts if the defaults are okay; however, it is a best practice to at least specify the schema_name even if it is dbo. I added a links re the 4 part name, identifiers, and ownership chaining.
[server_name].[database_name].[schema_name].[object_name]
To simplify, think of a "servername" as an instance that contains databases. Access is controlled via server logins and login mappings to database users.
The database has objects to store and manipulate data. The security to access any objects in the database is defined in the database. A 4 part name used in a database can be used to access an object in a database on a different instance. A 3 part name can be used to access a object in a different database on the same server. In either case, a distributed transaction is used. (We have code that does this and it's a nightmare to support. Joining tables from different databases on different servers or the same server each has it's support problems.)
Now for schema_name. Suppose you have two related businesses that share a large amount of data and workflow but require guaranteed isolation of some data. A contrived example: a court application that stores data for both the DA and the public defender. They don't trust each other, but lets say they agree to not use separate servers and separate databases because they trust IT to isolate and protect their data. And IT is more than happy to avoid the need to code the application to use 2 databases which might avoid extra work (e.g., replication, distributed transactions, BizTalk, Mulesoft,...). A developer might rather use linked servers and distributed queries to move the complexity from development to support. Instead, we can use one database with a "DA" schema for DA tables and procedures while using a "PD" schema for public defender tables and code. Access to each schema can be controlled.
If everything was in the default "dbo" schema, any "dbo" procedure would have access to all "dbo" tables because of ownership chaining. Ownership chaining does not apply across schemas. The DA coder might reference a PD table or procedure in a DA procedure, but they won't have access to PD unless it's specifically granted. The schema allows isolation of groups of objects from each other and adds a layer of security to control access from one schema to another. If you use schemas thinking it's just a container to organize objects, it becomes quickly painful because of the lack of ownership chaining. As with most things SQL, it depends. This avoids pain if this is what you need to meet security requirements.
If you want a really short answer, the HR schema exists to secure HR data from unauthorized access by code in other schemas.