Monday, April 29, 2019

Foreign Key Reference Tables

To know about the database it is good for one if he/she looking for all references for a object

  • whats tables are dependent on MAIN table (Child Objects)
  • On which tables MAIN table is dependent (Parent Objects)
Suppose i have 3 tables and in parenthesis show its fields

Dept (dept_id, Dept_name )
Emp (emp_id, Emp_first_name, emp_last_name , dept_id)
Sal(sal_id, year, emp_id)

Here EMP is Main table and Dept is Parent table (for Main table) and Sal is Child table (for Main table).

So one can easily understand the database if he has knowledge of both kind of references

Below is the script to identify the same.


SELECT 
f.name AS ForeignKey,
SCHEMA_NAME(f.SCHEMA_ID) SchemaName,
OBJECT_NAME(f.parent_object_id) AS TableName,
COL_NAME(fc.parent_object_id,fc.parent_column_id) AS ColumnName,
SCHEMA_NAME(o.SCHEMA_ID) ReferenceSchemaName,
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
JOIN sys.foreign_key_columns AS fc ON f.OBJECT_ID = fc.constraint_object_id
JOIN sys.objects AS o ON o.OBJECT_ID = fc.referenced_object_id
where 
OBJECT_NAME(f.parent_object_id)='MAIN_TABLE'
or OBJECT_NAME (f.referenced_object_id) = 'MAIN_TABLE'

SQL Server Database Restore Audit

Do you want to know who restore the database, when restore the database, which database backup has been used.

Yes you can audit the database restore, see my previous post

How to Find Latest Database restored on Database and which backup file used

Thursday, March 14, 2019

How to Find Latest Database restored on Database and which backup file used

SELECT 
   resh.destination_database_name, 
   resh.restore_date, 
   bckst.backup_start_date, 
   bckst.backup_finish_date, 
   bckst.database_name as sourceDBName, 
   bmf.physical_device_name as backupFileUsed,
   bckst.*
FROM msdb..restorehistory resh
INNER JOIN msdb..backupset bckst ON resh.backup_set_id = bckst.backup_set_id
INNER JOIN msdb..backupmediafamily bmf ON bckst.media_set_id = bmf.media_set_id
WHERE resh.destination_database_name = '**Database Name**'
ORDER BY resh.restore_date DESC

Most of time i was asked which database bacup has been restore on particular database, who restore the database, what is machine name, which backup file used, where the backup has been placed.
Donot worry about this sqlserver store all of this information as a metadata so above query is capable of give answers of what/when/where 3W/5W

Execute above query on any of database query window, add filder of database name on which you want to find all these questions. make sure user have the rights of executing query in cross databases.

Wednesday, March 13, 2019

py0016 : ModuleNotFoundError: No module named 'Tkinter'

Traceback (most recent call last):
  File "C:/Users/*****/PycharmProjects/Cal1/_main_.py", line 2, in
    from Tkinter import *
ModuleNotFoundError: No module named 'Tkinter'

While import from tkinter i was getting error as "ModuleNotFoundError: No module named 'Tkinter'". As further investigation i found tkinter is the part of python software that we have installed in actual. So no need to run anyother
pip install command for "tkinter".

i have recheck my code as i written "from Tkinter import *", so the error was here i used "T" capital T instead of "t"

so if you get the error i advise check your spelling before doing anything it should all in lowercase

Friday, February 22, 2019

py0015: NameError: name 'Select' is not defined

Error 
select = Select(driver.find_element_by_id('id****')) 
NameError: name 'Select' is not defined

This error i received when i tried to select value from drop down menu while doing automation in python selenium

How ever i came to know that i was using correct code but some import class is missing from header of file. I need to add "from selenium.webdriver.support.select import Select" in main file or same file so that code will work fine.

Solution:
from selenium.webdriver.support.select import Select

Correct code for click on drop down:
select = Select(driver.find_element_by_id('**id**'))
select.select_by_visible_text('String')

web stats