Créer une database
create database "db-test";
CREATE LOGIN "user-test" WITH PASSWORD = 'mypassword';
GO
USE "db-test";
CREATE USER "user-test" FOR LOGIN "user-test" WITH DEFAULT_SCHEMA = dbo;
EXEC sp_addrolemember N'db_datawriter', N'user-test'
EXEC sp_addrolemember N'db_datareader', N'user-test'
-- If you want a case sensistive database
ALTER DATABASE "db-test" COLLATE Latin1_General_CS_AS;
Change ownership of a database
ALTER AUTHORIZATION DATABASE:Déplacer les fichiers d'une base de données pour libérer de l'espace
- Récupperer les infos sur l'endroit ou se trouvent les fichier de log et de données en allant sur l'onglet propriété--> File de la base de donnée
- Détacher la db
- Copier les fichiers
- Rattacher la db
@Core TE =
N'F:\data\Core TE_mja1.mdf',
@Core TE_log =
N'F:\data\Core TE_log_mja1.ldf';
Ligne de commande
Faire un backup
Ligne de commande
BACKUP DATABASE "dbname" TO DISK='D:\data\act\dumps\finename.BAK'
Interactive client
Se placer sur la db et lancer un backup ar le client SQL Server Management Studio
Restorer une database
Ligne de commande
USE [master];
GO
CREATE DATABASE dbint;
RESTORE DATABASE dbint FROM DISK = 'd:\youbackup-int.bak' with replace;
ALTER AUTHORIZATION ON DATABASE::dbint TO existingloginfordbint
Parfois vous pouveez être bloqué dans la restauration parce que les chemins du serveurs de destination ne correspondent pas au serveur source. Dans ce cas il faut utiliser l'opérateur move.
RESTORE DATABASE actint FROM DISK = 'f:\share\dbint.bak' WITH REPLACE,
MOVE 'dbint' TO 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER1\MSSQL\DATA\dbint.mdf',
MOVE 'dbint_log' TO 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER1\MSSQL\DATA\dbint.ldf' ;
Pour lister le nom des data et journaux utiliser la commande FILELISTONLY
MOVE 'dbint' TO 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER1\MSSQL\DATA\dbint.mdf',
MOVE 'dbint_log' TO 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER1\MSSQL\DATA\dbint.ldf' ;
RESTORE FILELISTONLY FROM DISK = 'f:\share\act-readonly.bak';
Create a SQL script
Pour créer un script SQL et récupper le contenu d'un table:Etudier les performances
SELECT creation_time
,last_execution_time
,total_physical_reads
,total_logical_reads
,total_logical_writes
, execution_count
, total_worker_time
, total_elapsed_time
, total_elapsed_time / execution_count avg_elapsed_time
,SUBSTRING(st.text, (qs.statement_start_offset/2) + 1,
((CASE statement_end_offset
WHEN -1 THEN DATALENGTH(st.text)
ELSE qs.statement_end_offset END
- qs.statement_start_offset)/2) + 1) AS statement_text
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st
ORDER BY total_elapsed_time / execution_count DESC;
,last_execution_time
,total_physical_reads
,total_logical_reads
,total_logical_writes
, execution_count
, total_worker_time
, total_elapsed_time
, total_elapsed_time / execution_count avg_elapsed_time
,SUBSTRING(st.text, (qs.statement_start_offset/2) + 1,
((CASE statement_end_offset
WHEN -1 THEN DATALENGTH(st.text)
ELSE qs.statement_end_offset END
- qs.statement_start_offset)/2) + 1) AS statement_text
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st
ORDER BY total_elapsed_time / execution_count DESC;
Gérer les fichiers
Reduire un fichier de log
DBCC SHRINKFILE (DBWV1Y6101_log,20)
Déplacer la base de donnée temporaire
ALTER DATABASE tempdb
MODIFY FILE (NAME = tempdev, FILENAME = 'E:\SQLData\tempdb.mdf');
ALTER DATABASE tempdb
MODIFY FILE (NAME = templog, FILENAME = 'F:\SQLLog\templog.ldf');
Restore in amazon
exec msdb.dbo.rds_restore_database@restore_db_name='mydb',
@s3_arn_to_restore_from='arn:aws:s3:::mys3bucket/mydimp.bak';
Load from a script
sqlcmd -S localhost -i "D:\data\Core kpi\dump\script.sql" -o "D:\data\Core kpi\dump\script.txt"
sqlcmd -S server,port -r1 -Uusername -Ppassword -b -ddatabase -iinputfile
Aucun commentaire:
Enregistrer un commentaire