MS SQL common SQL statements (7): database login name, server role, database user, authority management and other operations sq

Seven, database login name, server role, permission management and other operations

--1. Use T-SQL to modify the authentication mode of the server
xp_instance_regwrite N'HKEY_LOCAL_MACHINE',N'SOFTWARE\Microsoft\Microsoft SQL Server\MSSQLServer','LoginMode',N'REG_DWORD',2
--1, windows authentication
--2, SQL Server authentication

--2, change the password of the user name
--Syntax structure: exec sp_password 'old password', 'new password', 'login name';
exec sp_password '123456', 'on', 'on';

--3. Modify the default database of the login name (the default is master)
--Syntax structure: exec sp_defaultdb 'login name', 'database name';
exec sp_defaultdb 'wjl','test';

--4. Modify the default language of the login name (the default is simplified chinese)
--Syntax structure: exec sp_defaultdb 'login name', 'language name';
exec sp_defaultlanguage 'wjl','english';

--5, delete the login name
--Syntax structure: exec sp_droplogin 'login name';
exec sp_droplogin 'wjl'

--6, add users to the database
--Syntax structure: exec sp_grantdbaccess 'login name', 'database user name'
use test;
exec sp_grantdbaccess 'wjl', 'wjlUser';

--7. View the information of all users in the current database
use test;
exec sp_helpuser;

--8. View the information of the specified user in the current database
exec sp_helpuser 'wjlUser';

--9, delete the user specified in the current database
exec sp_revokedbaccess 'wjlUser';

--10. Add a server role to the login name
--Server role and database role permissions in SQLServer
--a, bulkadmin: bulk insert operation
--b, dbcreator: create and change databases
--c, diskadmin: manage disk files
--d, processadmin: manage processes running in sql server
--e, public: Provides administrative privileges for users in the database
--f, securityadmin: login to the management server
--g, serveradmin: Manage configuration server-wide settings
--h, setupadmin: stored procedure for library and library management expansion
--i, sysadmin: can perform any operation in the sql server installation
-- Reference link: http://blog.csdn.net/e_online/article/details/4597957
-- Reference link: http://www.cnblogs.com/xwdreamer/archive/2012/06/25/2561391.html

--Operate using T-sql:
--Add sysadmin role to login name wjl
exec sp_addsrvrolemember 'wjl','sysadmin';

--11. Delete the specified server role for the login name
--Remove sysadmin role for login name wjl
exec sp_dropsrvrolemember 'wjl','sysadmin';

--12. Custom database roles
--Graphical interface: test database--->Security--->Roles--->Database roles--->Right click "Add Database Role"--->Fill in "Database Role Name" and owner, tick Select the owned schema (optional), add role members ---> OK.

--Using T-SQL:
exec sp_addrole 'test_dbAdmin','dbo';
--test_dbAdmin: database role name dbo: owner

--13, delete the database role
exec sp_droprole 'test_dbAdmin';

--14. Add database roles to database users
--Add test_dbAdmin database role for database user wjl
exec sp_addrolemember 'test_dbAdmin','wjl';

--15. Delete a database role of a database user
--Delete the test_dbAdmin database role of the database user wjl
exec sp_droprolemember 'test_dbAdmin','wjl';

--16. Authority management:
--Default administrator login number: Guest, Public

--Add permissions to the specified database role
--Grant the current database test_dbAdmin role the permission to update the table employee, and this role can grant the update permission to other roles
grant update on employee to test_dbAdmin with grant option

-- Grant the current database test_dbAdmin role the permission to add data to the table employee, but this role cannot grant the permission to add data to other roles
grant insert on employee to test_dbAdmin

--Deprive a user of the specified permissions, while depriving it of the right to grant permissions to others
revoke update on employee from test_dbAdmin cascade;
--Deprive a user of the specified permissions, without depriving it of the right to grant permissions to others
revoke insert on employee from test_dbAdmin

-- Deny being granted a permission
--Refuse to grant test_dbAdmin update permission for table employee, and deny the permission to "grant update permission to others"
deny update on employee to test_dbAdmin cascade;

--17. Use T-SQL for database backup
--full backup
backup database test to disk=N'F:\DB_bak\test20161115.bak' with noformat,noinit,name=N'multidatabase-full database backup',skip,norewind,nounload,stats=1;

--differential backup
backup database test to disk=N'F:\DB_bak\test20161115_different.bak' with differential,noinit,name=N'multidatabase-differential database backup',skip,norewind,nounload,stats=10;
--Note: Differential backup requires at least one full backup of the database to be backed up, otherwise an error will be reported:
--Cannot perform differential backup of database "test2" because no current database backup exists. Please reissue BACKUP DATABASE with the WITH DIFFERENTIAL option removed to perform a full backup of the database.

--Transaction date backup (the test was unsuccessful, the reason is currently unknown)
backup log database test to disk=N'F:\DB_bak\test20161115_log2.bak' with noformat,noinit,name=N'multidatabase-transaction log backup',skip,norewind,nounload,stats=10;
--Error: There is a syntax error near the keyword 'database'. Syntax error near keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the preceding statement must end with a semicolon.

--18. Use T-SQL to restore the database (the test was unsuccessful, and the reason is currently unknown)
restore database test from disk=N'F:\DB_bak\test20161115.bak' with file=1,nounload,stats=10;
--Error: The log tail of database "test3" has not been backed up. If the log contains work that you do not want to lose, back up the log with BACKUP LOG WITH NORECOVERY. Use the WITH REPLACE or WITH STOPAT clause of the RESTORE statement to overwrite only the contents of this log.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326539659&siteId=291194637