Skip to main content

Posts

Create Memory - Optimized tables

To create a table in In-memory, follow below steps: Create a database create database test; You must set to AUTO_CLOSE OFF because 'AUTO_CLOSE' is not supported with databases that have a MEMORY_OPTIMIZED_DATA file group. alter database test set auto_close off; The next step would be creating a required file group for memory-optimized tables. To do that, we need to add a memory-optimized file group to test. alter database test add filegroup test_mem contains MEMORY_OPTIMIZED_DATA; Notice the key word MEMORY_OPTIMIZED_DATA, it tells SQL server this file group is in memory and will store memory-optimized objects. Now let's add a file into this file group. alter database test add file(name = 'fileName', filename = 'fileLocation') to filegroup test_mem; Once the memory - optimized data file is ready, we can go ahead and create our first memory - optimized table. use test; create table test_table(id int not null primary key nonclustered hash with (bucket...

Find/drop foreign key using table and column

Find all the foreign keys by executing below query. SELECT f.name AS ForeignKey, OBJECT_NAME(f.parent_object_id) AS TableName, COL_NAME(fc.parent_object_id, fc.parent_column_id) AS ColumnName, OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName, COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS ReferenceColumnName FROM sys.foreign_keys AS f INNER JOIN sys.foreign_key_columns AS fc ON f.OBJECT_ID = fc.constraint_object_id; To find/drop a foreign key, use below query. declare @table_name nvarchar(256) declare @col_name nvarchar(256) declare @Command nvarchar(1000) set @table_name = N'<tableName>' set @col_name = N'<columnName>' select @command = 'ALTER TABLE ' + OBJECT_NAME(f.parent_object_id) + ' drop constraint ' + f.name from sys.foreign_keys f, sys.foreign_key_columns fc where f.object_id = fc.constraint_object_id and OBJECT_NAME(f.parent_object_id) = @table_name and COL_NAME(fc.parent_object_id, fc.parent_column_id) =...

List users with their rights

To list the users with their rights, use below query. You have to system admin (sysadmin) rights to execute this query. SELECT name ,type_desc,is_disabled, create_date FROM master.sys.server_principals WHERE IS_SRVROLEMEMBER ( 'sysadmin' , name ) = 1 ORDER BY name;

Execute queries from SQL files

To execute queries from a SQL file, use below query. MySQL: There are 2 options: If you are in MySQL command line, execute below query. source 'SQLfilePath'; If you are in command line/shell, execute below query. mysql -u userName -p password database < 'SQLfilePath' MSSQL: Use T-SQL, execute below query. sqlcmd -S databaseName -i "SQLfilePath" -o "outputFilePath"

Recovery models

SQL Server offer 3 recovery models Simple Bulk-logged Full In the case of a simple recovery model, you are essentially agreeing that it is ok to lose all changes made to your database after the last full backup . The upside of this recovery model is of course that it takes very little administration. You don’t have to worry about anything like a log chain or tons of log backups , or even when the log will be truncated . Obviously this will never be good enough for mission critical databases, but it definitely has its place and its uses. The purpose of the bulk logged recovery model is to allow you perform bulk operations without writing each transaction to the transaction log , and as such improve the performance of your bulk operation . The downside to this is of course that not each transaction is recorded in the log, and as such i t does not allow you to do a point in time restore. In order to be able to restore to a specific point in time , you need to have your da...