简体   繁体   中英

SQL Server change dbo inside of database

I have an issue, inside my SQL Server database I have master, msdb, tempdb, bets databases. When I'm running my application, application use bets db and inside bets db the default object is dbo.

The question is, how can I change the default dbo (inside bets) to the new value bets?

The structure now

  • bets
    • db_accessadmin
    • db_datawriter
    • etc
    • dbo (Default)
    • bets

Need to be something like this

  • bets
    • db_accessadmin
    • db_datawriter
    • etc
    • dbo
    • bets (Default)

Update : I forgot to say, that I use it inside docker container and the host machine is Redhat

Do you mean change the default schema of SQL Server? you can try Alter Schema

How do you establish a user's default schema?

You can set the default schema in both SSMS GUI and TSQL.

USE [dbname]
GO
ALTER USER [you] WITH DEFAULT_SCHEMA=guest
GO
EXECUTE AS LOGIN = 'YOU'
GO
create table leks(c1 int)
-- Now your table is guest.leks

Default schema naming will be always used when the three part name is not specified.

I can see how to change the ownership of a schema to some user, but then can't you make the same user the owner of several schemas?

You could definitely have a single user or role in a database be owning all schemas but something that should fit well in your environment (security).

USE dbname
GO
ALTER AUTHORIZATION ON SCHEMA::[db_backupoperator] TO you
GO
USE dbname
GO
ALTER AUTHORIZATION ON SCHEMA::[db_accessadmin] TO you
GO

-- I have changed for system schemas , you can definitely do this for all user schemas as well

Which one is the default?

User owning a schema is different to a default schema of an user. An user can own multiple schemas but an user can have only one default schema

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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