Skip to main content

Posts

Identify the duplicates in MySQL

To identify the duplicate entries in MySQL, use below query.                      SELECT COUNT(*) as repetitions, group_concat(id, ' (', startTime, ', ', endTime, ') ' SEPARATOR ' | ') as row_data FROM GROUP BY startTime, endTime HAVING repetitions > 1 FYI, id, startTime and endTime columns should be changed based on your table schema. you can change the output format here like alias and the separator.

Get all Foreign key relationship with a table and its column

To get all the Foreign key referenced table and its column, use below query in MySQL.             select table_name, column_name, constraint_name, referenced_table_name, referenced_column_name from information_schema.key_column_usage where referenced_table_schema = 'dbName' and referenced_table_name = 'tableName' and referenced_column_name = 'columnName';

Rename database name in MSSQL

To rename the database is MSSQL, use below queries. ALTER DATABASE srcDatabaseName SET SINGLE_USER WITH ROLLBACK IMMEDIATE; GO sp_rename ' srcDatabaseName' , ' dest DatabaseName ' ,'DATABASE'; GO ALTER DATABASE dest DatabaseName SET MULTI_USER;

Get all indexes from a SQL Server database

To get all indexes in SQL Server, use below query. SELECT TableName = t.name, IndexName = ind.name, IndexId = ind.index_id, ColumnId = ic.index_column_id, ColumnName = col.name, ind.*, ic.*, col.* FROM sys.indexes ind INNER JOIN sys.index_columns ic ON  ind.object_id = ic.object_id and ind.index_id = ic.index_id INNER JOIN sys.columns col ON ic.object_id = col.object_id and ic.column_id = col.column_id INNER JOIN sys.tables t ON ind.object_id = t.object_id WHERE ind.is_primary_key = 0 AND t.is_ms_shipped = 0 ORDER BY t.name, ind.name, ind.index_id, ic.index_column_id;

Describe the tables in MSSQL

There is no query to get the description of the table like in MySQL. Use  sp_columns stored procedure to describe the table in SQL Server.            exec sp_columns tableName; And also we can use sp_help to describe the table. sp_help is returned depend on whether name is specified, when it is specified, and what database object it is.            exec sp_help tableName;