Вы находитесь на странице: 1из 8

DB RELATED

create database db3 on primary (name='db3',filename='E:\SQL_DBs\db3.mdf', size=10mb,filegrowth=5mb,maxsize=100mb) log on (name='db3_log',filename='E:\SQL_DBs\db3.ldf', size=5mb,filegrowth=10%,maxsize=50mb) alter database db3 add log file (name='db3_log', filename='E:\SQL_DBs\db3_log.ldf', size=10mb,filegrowth=5mb,maxsize=100mb) --add secondary DB FILE TO db3 database alter database db3 add file (name='db3_sec', filename='E:\SQL_DBs\db3_sec.ndf', size=10mb,filegrowth=5mb,maxsize=100mb) --to add log file to db3 database alter database db3 add log file (name='db3_log_1', filename='E:\SQL_DBs\db3_log_1.ldf', size=10mb,filegrowth=5mb,maxsize=100mb) --to get information about database db3 sp_helpdb db3 select * from sys.databases where name='db3' --to get file related information use db3 sp_helpfile select * from sys.sysfiles --to add secondary file group db3 database with filename fg alter database db3 add filegroup fg --to see the file groups sp_helpfilegroup select * from sys.sysfilegroups sp_help tb2 create table tb1(id int) create table tb2 (id int) on fg insert into tb2 values( 1 ) select * from tb2 alter database db3 add file(name='db3_sec_1',filename='E:\SQL_DBs\db3_sec_1.ndf', size=10mb) to filegroup fg --to check if any active connections with db3 select * from sys.sysprocesses where dbid=DB_ID('db3')

use master --make a db offilne alter database db3 set offline alter database db3 set online -alter database db3 modify file(name='db3_log_1',filename='E:\SQL logs\db3_log_1.ldf')

BACKUP
/*system tables related to backup and restore (msdb database) backupfile restorefile backupfilegroup restorefilegroup backpmediafamily restorehistory backupmediaset */ create database vimdb alter database vimdb add filegroup fg1 alter database vimdb add file ( name='vim_db_sec', filename='e:\backup\vimdb_sec.ndf') to filegroup fg1 --full backup backup database vimdb to disk='E:\backup\vimdb_full.bak'; --differential backup backup database vimdb to disk ='e:\backup\vimdb_diff.bak' with differential; --T-log backup backup log vimdb to disk ='e:\backup\vimdb_log.trn' --file backup backup database vimdb file='vim_db_sec' to disk='e:\backup\vimdb_file.bak' --filegroup backup backup database vimdb filegroup='fg1' to disk='e:\backup\vimdb_fg.bak' use master --to make filegroup as readonly alter database vimdb modify filegroup fg1 readonly --partial backup backup database vimdb read_write_filegroups to disk = 'e:\backup\vimdb_part.bak' --copy backup backup database vimdb to disk = 'e:\backup\vimdb_rw.bak'

GRANTING PERMISSIONS
use vimdb dbcc checkdb vimdb

-create user mary for login mary --to get list of users of a database sp_helpuser select * from sys.database_principals select * from sys.sysusers create table tb11(id int) --to give permissions grant select on tb11 to mary grant insert,update,delete on tb11 to mary --to see list of permissions on an object sp_helprotect tb11 create table tb12 (id int) --to list the database roles sp_helprole --to make a user as a member of db_datareader role sp_addrolemember db_datareader, mary --to see memebers of a database role sp_helprolemember db_owner --to remove a user from db_datareader role sp_droprolemember db_datareader, mary

RESTORING
--RESTORATION use vimdb create table tb1(c1 int) --------------------------------------------------------------------------------------------------------------------------case 1: only full backup backup database vimdb to disk = 'e:\backup\vimdb_full.bak' insert into tb1 values(1) use master drop database vimdb --restore database with backups restore database vimdb from disk ='e:\backup\vimdb_full.bak' ----------------------------------------------------------------------------------------------------------------------------case2 : full + differential use vimdb create table tb2(c1 int) backup database vimdb to disk = 'e:\backup\vimdb_full.bak'

insert into tb2 values(1) backup database vimdb to disk ='e:\backup\vimdb_diff.bak' with differential insert into tb2 values(2) select * from tb2 use master drop database vimdb restore database vimdb from disk='e:\backup\vimdb_full.bak' with norecovery restore database vimdb from disk='e:\backup\vimdb_diff.bak.' with norecovery restore database vimdb with recovery ----------------------------------------------------------------------------------------------------------------------------case 3: full + differential + T-log create table tb3(c1 int) backup database vimdb to disk = 'e:\backup\vimdb_full.bak' insert into tb3 values(1) backup database vimdb to disk ='e:\backup\vimdb_diff.bak' with differential insert into tb3 values(2) backup log vimdb to disk='e:\backup\vimdb_log_1.trn' insert into tb3 values(3) backup log vimdb to disk='e:\backup\vimdb_log_2.trn' insert into tb3 values(4) select * from tb3 use vimdb drop database vimdb restore database vimdb from disk='e:\backup\vimdb_full.bak' with norecovery restore database vimdb from disk='e:\backup\vimdb_diff.bak.' with norecovery restore log vimdb from disk='e:\backup\vimdb_log_2.trn' with norecovery restore database vimdb with recovery ----------------------------------------------------------------------------------------------------------------------------case 4: point in time recovery create table tb4 (c1 int) backup database vimdb to disk = 'e:\backup\vimdb_full.bak' insert into tb4 values(1) select GETDATE() insert into tb4 values(2) backup log vimdb to disk='e:\backup\vimdb_log.trn' select * from tb4 use master drop database vimdb

restore database vimdb from disk='e:\backup\vimdb_full.bak' with norecovery --2014 march 22 15:14:19.560 restore database vimdb from disk='e:\backup\vimdb_log.trn' with norecovery, stopat='2014 march 22 15:14:19.560' restore database vimdb with recovery use vimdb select * from tb4

ROLES
--create login <windows user name> create login mary with password='vim@123' --to get the login details sp_helplogins --full login details select * from sys.syslogins sp_helplogins mary --to list the serveer roles sp_helpsrvrole --to add a login to server role sp_addsrvrolemember 'mary','sysadmin' --to see memebers of server role sp_helpsrvrolemember sysadmin --to remove a member from server role sp_dropsrvrolemember 'mary', sysadmin --to grant server level permissions grant view server state to mary use vimdb

SCHEMA
use vimdb --to create a schema create schema hr create schema sales --to see the schemas select * from sys.schemas --to see object and its respective schemas select name,SCHEMA_NAME(schema_id) from sys.objects where name like 'tb?' --lets create the table under schemas create table hr.table1(c1 char(20)) create table sales.table1(c1 char(20)) insert into hr.table1 values('from hr schema')

insert into sales.table1 values('from sales schema') --get data from table1 of hr schema select * from hr.table1 select * from sales.table1 --to grant permissions at schema level grant select on schema::hr to mary --to change the default schema of user alter user mary with default_schema=hr sp_helpuser mary --impersonate as mary execute as user='mary' select * from table1 revert

TRANSFERRING
use vimdb create table emp( emp_id int, name char(20), place char(20)) /*there are multiple ways to create from/to sql server 1.import export 2.BCP 3.bulk insert 4.select into insert into 5.open row set and open data source */ --1.IMPORT EXPORT /*data sources can be sql, oracle , any rdbms, flat file, excel, ms access, any odbc supported databases*/ select * from emp truncate table emp /*2.BCP BCP is a command line utility for transferring data to and from sql server BCP can be done in 2 formats/ways 1.Native -n if we BCP out the data in character format it will be in user nonreadable format 2.Character [-c] if we BCP out the data in character format it will be in user readable format *if we are transferring data between sql servers, then go for native format which is fastest way of data transfer

*if we are transferring data between sql server and non-sql server destinations then use character format */ bulk insert emp from 'C:\Users\Raghu\Desktop\Mindtree\MS SQL Quiries\emp.txt' with (fieldterminator=',' , rowterminator='\n') --**SELECT INTO**---if table doesnt exists, a new table is created. if table exists we cannot create select * into master.dbo.emp from vimdb.dbo.emp select * from master.dbo.emp --**INSERT INTO**---if already table exists use this method insert into vimdb.dbo.emp select * from master.dbo.emp

USER DEFINED ROLE


use vimdb --create a role create role dataentry --get list of roles sp_helprole sp_addrolemember dataentry, mary --to verify the members of a particular role sp_helprolemember dataentry --to grand premission to a particular role grant select on tb1 to dataentry --IMPERSONATION-EXECUTE as USER='mary' --to verify the user name select SUSER_NAME() select * from tb1 --to get back to the admin user role revert --to grant the permission WITH GRANT option grant insert on tb1 to mary with grant option sp_helprotect tb1 --DIFFERENCES BETWEEN DENY AND REVOKE create table tb5(id int) --lets create 2 roles create role role1 create role role2 --lets grant permissions to roles grant select on tb5 to role1, role2

--lets add mary to role1,role2 sp_addrolemember role1, mary sp_addrolemember role2, mary EXECUTE as USER='mary' select * from tb5 revert --lets take back the permission from role1 --**USING DENY** deny select on tb5 to role1 EXECUTE as USER='mary' select * from tb5 revert sp_helprotect tb1 ----lets remove the deny revoke select on tb5 from role1