SQL Server/T-SQL Tutorial/User Role/USER
Содержание
- 1 Change the default schema for user Fred
- 2 Create a proxy user
- 3 Create a user and a table, and make the user own the table
- 4 Creating Database Users with schema
- 5 Modifying a Database User for name
- 6 Modifying a Database User for the schema
- 7 Removing a Database User from the Database
- 8 select USER
Change the default schema for user Fred
ALTER USER Fred WITH DEFAULT_SCHEMA = Production
Create a proxy user
6> CREATE USER Bob
7> WITHOUT LOGIN
8> GO
1>
2>
Create a user and a table, and make the user own the table
4> CREATE USER Tom
5> WITHOUT LOGIN
6> GO
1> CREATE TABLE TomsData
2> (
3> AColumn INT
4> )
5> GO
1>
2> ALTER AUTHORIZATION ON TomsData TO Tom
3> GO
1>
2>
3> --Impersonate the user
4> EXECUTE AS USER="Tom"
5> GO
1>
2> SELECT USER_NAME()
3> GO
--------------------------------------------------------------------------------------------------------------------------------
Tom
(1 rows affected)
1>
2> drop table TomsData;
3> drop user tom;
4> GO
Msg 15151, Level 16, State 1, Server J\SQLEXPRESS, Line 3
Cannot drop the user "tom", because it does not exist or you do not have permission.
1>
Creating Database Users with schema
USE YourDatabaseName
GO
CREATE USER Joe
FOR LOGIN [YourID\TestUser]
WITH DEFAULT_SCHEMA = HumanResources
Modifying a Database User for name
ALTER USER YourName
WITH NAME = YourNewName
Modifying a Database User for the schema
USE YourDatabaseName
GO
ALTER USER Joe
WITH DEFAULT_SCHEMA = Production
Removing a Database User from the Database
DROP USER UserName
select USER
10> SELECT CURRENT_TIMESTAMP, USER, SYSTEM_USER, CURRENT_USER,
11> SESSION_USER
12> GO
----------------------- -------------------------------------------------------------------------------------------------------------------------------- -----------------------------------------------
--------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------
---------- --------------------------------------------------------------------------------------------------------------------------------
2008-08-17 13:20:57.503 dbo J\Administrator
dbo
dbo
(1 rows affected)