'Structure of T-SQL tables
I am switching from MySQL
to SQL Server
for a new job and I have encountered something I don't quite understand as I haven't encountered something like this before.
I am using the WideWorldImporters Microsoft Sample DB for reference.
Looking at the table structures I can see that it is SCHEMA_NAME.X.TABLE_NAME
Example
[WideWorldImporters].[Application].[Cities]
In MySQL
this would just be schema.tablename
Example
[WideWorldImporters].[Cities]
What is this middle part (the [Application] part in the example) exactly? I can't seem to find anything about it but that's likely because I don't know what it is to look for. It's obviously important as a select query won't work if it's removed. Can anyone explain or even name this so I can research it?
Solution 1:[1]
Microsoft uses a three-part naming pattern of Database_Name.Schema_Name.Table_Name
that differs from the MySQL convention. In general terms, a MySQL "schema" is roughly equivalent to a SQL Server "database."
In practice, one should avoid explicitly referencing the database name unless specifically needed.
Microsoft documentation: https://docs.microsoft.com/en-us/sql/t-sql/language-elements/transact-sql-syntax-conventions-transact-sql?view=sql-server-ver15#multipart-names
Solution 2:[2]
I would like to add some extra details on paneerakbari
Accepted Answer
First: About SQL Server
according to Microsoft Doc Transact-SQL has different formats to define Object name (Table name), Here are supported formats
Object reference format | Description |
---|---|
server.database.schema.object | Four-part name |
server.database..object | Schema name is omitted. |
server..schema.object | Database name is omitted |
server...object | Database and schema name are omitted. |
database.schema.object | Server name is omitted. |
database..object | Server and schema name are omitted. |
schema.object | Server and database name are omitted. |
object | Server, database, and schema name are omitted. |
For case you can use Two Formats, database.schema.object
OR database..object
Examples:
[WideWorldImporters].[Application].[Cities]
OR
[WideWorldImporters]..[Cities]
Second: About MySQL, the Schema name is Synonymous with the Database name So There is two SQL formats database.object
OR object
, Here are useful links describe Whats differents between Schema and Database in MySQL Stackoverflow Question And TutorialsPoint Question
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | paneerakbari |
Solution 2 | ahmed hamdy |