I'm trying to better understand why one of our database update scripts failed to work properly at a particular customer site, and narrowed it down (I think) to database ownership and roles.
Disclaimer: I'm actually waiting to hear back from the customer's DBA so they can tell us if they upgraded their SQL database recently and so we can look at their database. I'm thinking a SQL 2000 to SQL 2005 conversion might have hosed our scripts if our applications's database login was converted to a schema, because we were referencing
dbo
in a few places in the update script.
Anyway, I've been trying to find a better explanation of database ownership and roles and how it impacts what owner a database object is actually assigned when you don't 开发者_开发技巧explicitly specify the owner in a T-SQL statement. For example, our update scripts typically just do CREATE TABLE foo
instead of CREATE TABLE dbo.foo
or something else, but I found a few that were explicitly using dbo
, and those are the ones causing problems at the moment (only for this one customer).
I found this article (specific to SQL Server 2000), but the table on that page is confusing. It mentions db_owner
and "owns the database" as two distinct possibilities for what role a user can have.
For example, the table states that if a user sam
, who is in the db_owner
role, runs the query CREATE TABLE [test3](abc int)
, it will be owned by sam
.
It then mentions that if a another user sue
, who "owns the database" (sic), runs the same query, it will be owned by dbo
.
Wouldn't db_owner
and "owns the database" be the same thing? The table implies that there is a difference between "being in the db_owner
role" and actually "being the owner of the database." But, if that's, true, what does it mean to "own the database" if it's something other than being a member of the db_owner
role?
No, db_owner and the owner of the database are not the same. dbo is a user and db_owner is a database role. Databases are owned by logins. Whatever login owns the database is aliased as dbo
inside the database. You can change the database owner by using the sp_changedbowner
system stored procedure.
All objects in a database are owned by a user. Users that are members of the db_owner role, among other permissions, are allowed to create objects owned by dbo. If a user is not a member of db_owner, but has some create permissions (e.g. Create Table), then any objects they create will be owned by the user that created them. You can change the ownership of an object using sp_changeobjectowner
system stored procedure.
精彩评论