简体   繁体   English

SQL Server表架构名称

[英]SQL Server Table Schema Name

I have some questions about the schema in a table. 我对表中的架构有一些疑问。

Sometime when you create a table the default schema is dbo.TableName. 有时在创建表时,默认架构为dbo.TableName。 Is the dbo the default schema name? dbo是默认架构名称吗? I believe you can change or specify the schema when creating a table right, because there are tables that have different schema like: Sales.Tablename or Users.Roles, etc. I believe the purpose of a schema is to make a difference between tables or something like that? 我相信您可以在创建表时更改或指定架构,因为有些表具有不同的架构,例如:Sales.Tablename或Users.Roles等。我相信架构的目的是使表或表之间有所不同这样的东西? Something like a namespace within a C# class. 类似于C#类中的名称空间。 Is it possible to have two tables with same name but a different schema, like: Sales.Users, Marketing.Users ? 是否可以有两个具有相同名称但模式不同的表,例如:Sales.Users,Marketing.Users?

  1. dbo is the default schema. dbo是默认架构。 You can change the default schema for each sql-login. 您可以更改每个sql登录的默认架构。 If you accidentally create a table in the wrong schema, you can move it: 如果不小心在错误的架构中创建了表,则可以移动它:

    -- Moving Peter table from Sales schema to Orders schema -将Peter表从Sales模式移至Orders模式

    ALTER SCHEMA Orders TRANSFER Sales.peter ALTER SCHEMA订购TRANSFER Sales.peter

  2. You can specify which schema to create the table in by specifying it before the table name: 您可以通过在表名称之前指定它来指定要在其中创建表的模式:

    CREATE TABLE Sales.Users(id int); 创建表Sales.Users(id int);

  3. One of the purposes of schemas is to create logical groups of tables, just like namespaces in C#. 模式的目的之一是创建表的逻辑组,就像C#中的名称空间一样。 They are also useful for controlling permissions and more. 它们对于控制权限等也很有用。

  4. Yes, table names only need to be unique within each schema.. 是的,表名在每个模式中仅需唯一。

Sometime when you create a table the default schema is dbo.TableName. 有时在创建表时,默认架构为dbo.TableName。 Is the dbo the default schema name? dbo是默认架构名称吗?

Why do you ask? 你为什么要问? It is quite obvious that dbo is the default schame name if you get it as default, or? 很明显,如果您将dbo设为默认名称,则dbo是默认名称。 On top it is the only usable schema a new database has. 最重要的是,它是新数据库唯一可用的模式。

I believe you can change or specify the schema when creating a table right, 我相信您可以在创建表权限时更改或指定架构,

What sense would multiple schemata have if you could not use them? 如果您不使用多个模式,它们会有什么意义? And as the create table syntax clearly states you can specify a schema. 并且正如创建表语法明确指出的那样,您可以指定架构。

I believe the purpose of a schema is to make a difference between tables or something like that? 我相信模式的目的是要在表之间或类似的东西之间有所作为? Something like a namespace within a C# class. 类似于C#类中的名称空间。

That pretty much sums it up. 差不多可以总结了。

Is it possible to have two tables with same name but a different schema, 是否可以有两个名称相同但架构不同的表,

What about you spend 10 seconds to try it out? 您花10秒钟时间尝试一下呢? Are you challenged by he concept of trying something totally simplistic out? 您是否对他尝试完全简化的概念感到质疑? And the answer is yes. 答案是肯定的。 object names have to be unique - within their schema. 对象名称在其模式内必须唯一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM