SQL Server创建错误的表名,为什么?

时间:2022-03-27 12:48:04

Question: when I create a table (T_TableName) using SQL Server Management-Studio, it always creates the table as

问题:当我使用SQL Server Management-Studio创建表(T_TableName)时,它始终将表创建为

Domain\UserName.T_TableName

instead of

代替

dbo.T_TableName

What's wrong ?

怎么了 ?

4 个解决方案

#1


9  

If you don't specify a schema explicitly on your table name to be created, it will be created in the user's current default schema.

如果未在要创建的表名上明确指定架构,则将在用户的当前默认架构中创建该架构。

I bet the user you're using has its own personal schema set as its default schema - that's why your tables get created in his own personal schema.

我敢打赌,你正在使用的用户将自己的个人架构设置为其默认架构 - 这就是为什么你的表在他自己的个人架构中创建的原因。

You can check what database users you have and what their default schema is by inspecting sys.database_principals (SQL Server 2005 and up):

您可以通过检查sys.database_principals(SQL Server 2005及更高版本)来检查您拥有的数据库用户及其默认架构:

SELECT name, type_desc, default_schema_name
FROM sys.database_principals

To solve this:

解决这个问题:

  • specify the schema you want to use explicitly (best practice anyway!)

    指定要明确使用的模式(无论如何最佳实践!)

    CREATE TABLE dbo.T_TableName
    
  • change the user's default schema to dbo

    将用户的默认架构更改为dbo

    ALTER USER [Domain\YourUser] WITH DEFAULT_SCHEMA = dbo
    

But as a general rule of thumb, I recommend always using the "dbo." prefix explicitly, if you want to have all your database objects in the dbo schema. Helps with performance, too (ever so slightly) since SQL Server won't have to go hunting in different schemas, if you explicitly tell it where your db objects live.

但作为一般经验法则,我建议始终使用“dbo”。如果要在dbo架构中拥有所有数据库对象,则显式前缀。如果您明确地告诉它数据库对象存在的位置,那么SQL Server也不必在不同的模式中进行搜索,从而有助于提高性能(如此轻微)。

#2


4  

You need to either create your table as "dbo.Whatever", OR you need to change your default schema (or have your SA do it for you) by issuing a command like:

您需要将表创建为“dbo.Whatever”,或者您需要通过发出如下命令来更改默认架构(或让SA为您执行此操作):

ALTER USER [DOMAINNAME\UserName] WITH DEFAULT_SCHEMA = dbo;

#3


3  

Call it dbo.T_TableName in SSMS. If you have the correct permissions, it will work.

在SSMS中将其命名为dbo.T_TableName。如果您拥有正确的权限,它将起作用。

#4


1  

Are you assigned as db_owner for the database you created the table in? If not, this could be the issue. Try adding your user mapping permissions to the database as such.

是否为您创建表的数据库指定了db_owner?如果没有,这可能是问题所在。尝试将用户映射权限添加到数据库中。

USE [yourDatabase]
GO
EXEC sp_addrolemember N'db_owner', N'DOMAIN\UserOrGroup'
GO

#1


9  

If you don't specify a schema explicitly on your table name to be created, it will be created in the user's current default schema.

如果未在要创建的表名上明确指定架构,则将在用户的当前默认架构中创建该架构。

I bet the user you're using has its own personal schema set as its default schema - that's why your tables get created in his own personal schema.

我敢打赌,你正在使用的用户将自己的个人架构设置为其默认架构 - 这就是为什么你的表在他自己的个人架构中创建的原因。

You can check what database users you have and what their default schema is by inspecting sys.database_principals (SQL Server 2005 and up):

您可以通过检查sys.database_principals(SQL Server 2005及更高版本)来检查您拥有的数据库用户及其默认架构:

SELECT name, type_desc, default_schema_name
FROM sys.database_principals

To solve this:

解决这个问题:

  • specify the schema you want to use explicitly (best practice anyway!)

    指定要明确使用的模式(无论如何最佳实践!)

    CREATE TABLE dbo.T_TableName
    
  • change the user's default schema to dbo

    将用户的默认架构更改为dbo

    ALTER USER [Domain\YourUser] WITH DEFAULT_SCHEMA = dbo
    

But as a general rule of thumb, I recommend always using the "dbo." prefix explicitly, if you want to have all your database objects in the dbo schema. Helps with performance, too (ever so slightly) since SQL Server won't have to go hunting in different schemas, if you explicitly tell it where your db objects live.

但作为一般经验法则,我建议始终使用“dbo”。如果要在dbo架构中拥有所有数据库对象,则显式前缀。如果您明确地告诉它数据库对象存在的位置,那么SQL Server也不必在不同的模式中进行搜索,从而有助于提高性能(如此轻微)。

#2


4  

You need to either create your table as "dbo.Whatever", OR you need to change your default schema (or have your SA do it for you) by issuing a command like:

您需要将表创建为“dbo.Whatever”,或者您需要通过发出如下命令来更改默认架构(或让SA为您执行此操作):

ALTER USER [DOMAINNAME\UserName] WITH DEFAULT_SCHEMA = dbo;

#3


3  

Call it dbo.T_TableName in SSMS. If you have the correct permissions, it will work.

在SSMS中将其命名为dbo.T_TableName。如果您拥有正确的权限,它将起作用。

#4


1  

Are you assigned as db_owner for the database you created the table in? If not, this could be the issue. Try adding your user mapping permissions to the database as such.

是否为您创建表的数据库指定了db_owner?如果没有,这可能是问题所在。尝试将用户映射权限添加到数据库中。

USE [yourDatabase]
GO
EXEC sp_addrolemember N'db_owner', N'DOMAIN\UserOrGroup'
GO